lighter-v2-python


Namelighter-v2-python JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/elliottech/lighter-v2-python
Summarylighter Python rest api and blockchain interactions for Limit Orders
upload_time2023-10-17 12:04:46
maintainer
docs_urlNone
authorElliot
requires_python
licenseApache 2.0
keywords lighter exchange rest api defi ethereum optimism l2 eth
VCS
bugtrack_url
requirements eth-account pytest pytest-mock requests-mock requests setuptools tox web3 dateparser nest-asyncio pytest-asyncio
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Lighter

Python client for Lighter (v2). The sdk is tested against Python versions 3.9, 3.10 and 3.11.

## Installation

```bash
pip install lighter-v2-python
```

## Getting Started

The `Client` object has four main modules;

- `Api`: allows interaction with the lighter api
- `AsyncApi`: allows for async interaction with the lighter api
- `Blockchain`: allows interaction with lighter contracts
- `AsyncBlockchain`: allows async interaction with lighter contracts

`Client` can be created with private key or not depending on whether you are going to use the api or interact with the contracts. For more complete examples, see the [examples](./examples/) directory.

### API

Following parameters are required to use `Api` module:

- `api_auth`: You should get the key from our servers.
- `web3_provider_url`: You need a node to interact with the contract. We suggest alchemy which provides 300M free compute units monthly, You can register and get your keys [here](https://www.alchemy.com/)

```python
from lighter.lighter_client import Client
import os
from lighter.modules.blockchain import OrderSide
from lighter.constants import ORDERBOOK_WETH_USDC

api_auth = os.environ.get("API_AUTH")

# You don't need to provide private key if you only want to use the api module.

client = Client(api_auth=api_auth, web3_provider_url="ALCHEMY_URL")

# Let's get available blockchains and their details from the api module.
blockchains = client.api.get_blockchains().data
```

### Blockchain

Following parameters are required to use `Blockchain` module:

- `api_auth`: You should get the key from our servers.
- `private_key`: You need to provide your wallet private key to sign your transactions.
- `web3_provider_url`: You need a node to interact with the contract. We suggest alchemy which provides 300M free compute units monthly, You can register and get your keys [here](https://www.alchemy.com/)

Blockchain module enables you to do multiple operations in one transaction. The number of operations is limited to 4 million gas. So the limits are roughly;

- 25 order creations in one transaction
- 100 order cancellations in one transaction
- 25 order updates in one transaction

```python
from lighter.lighter_client import Client
import os
from lighter.modules.blockchain import OrderSide
from lighter.constants import ORDERBOOK_WETH_USDC


private_key = os.environ.get("SOURCE_PRIVATE_KEY")
api_auth = os.environ.get("API_AUTH")


client = Client(
    private_key=private_key, api_auth=api_auth, web3_provider_url="ALCHEMY_URL"
)


x = client.blockchain  # initialize the blockchain module before using it


# You need to give allowance for the trading tokens
client.blockchain.set_token_max_allowance(
    spender=client.blockchain.router_contract.address, token="WETH"
)

client.blockchain.set_token_max_allowance(
    spender=client.blockchain.router_contract.address, token="USDC"
)


# Let's create a batch of limit orders
sizes = ["0.001", "0.001", "0.001"]
prices = ["1050", "1200", "1000"]
sides = [OrderSide.BUY, OrderSide.SELL, OrderSide.BUY]


tx_hash = client.blockchain.create_limit_order_batch("WETH_USDC", sizes, prices, sides)

# if you want to wait for the transaction to be mined and get order id and other details,
# you can use the following method.
# alternatively you can wait the data from websocket
result = client.blockchain.get_create_order_transaction_result(tx_hash, "WETH_USDC")
```

### Async Examples

```python
from lighter.lighter_client import Client
import os
from lighter.modules.blockchain import OrderSide
from lighter.constants import ORDERBOOK_WETH_USDC


private_key = os.environ.get("SOURCE_PRIVATE_KEY")
api_auth = os.environ.get("API_AUTH")


client = Client(
    private_key=private_key, api_auth=api_auth, web3_provider_url="ALCHEMY_URL"
)


async def main():
    client.async_blockchain # to initialize the module
    sizes = ["0.0001"]
    prices = ["1000"]
    sides = [OrderSide.SELL]

    # Test async api
    print(await client.async_api.get_blockchains())
    print(await client.async_api.get_orderbook("WETH_USDC"))

    print(
        client.api.get_candles(
            orderbook_symbol="WETH_USDC",
            resolution=60,
            timestamp_start=int(time.time()) - 60 * 60 * 60 * 24,
            timestamp_end=int(time.time()),
        )
    )

    tx_hash = await client.async_blockchain.create_limit_order_batch(
        "WETH_USDC", sizes, prices, sides
    )
    # tx_hash = await client.async_blockchain.cancel_limit_order_batch(
    #     "WETH_USDC", [24989]
    # )
    # tx_hash = await client.async_blockchain.update_limit_order_batch(
    #     "WETH_USDC", [24991], ["0.0001"], ["100"], [OrderSide.BUY]
    # )
    # tx_hash = await client.async_blockchain.create_market_order(
    #     "WETH_USDC", "0.0001", "100", OrderSide.SELL
    # )

    result = await client.async_blockchain.get_create_order_transaction_result(
        tx_hash, "WETH_USDC"
    )
    # result = await client.async_blockchain.get_limit_order_canceled_transaction_result(
    #     tx_hash, "WETH_USDC"
    # )
    # result = await client.async_blockchain.get_update_limit_order_transaction_result(
    #     tx_hash, "WETH_USDC"
    # )
    print(result)

    # close the aiohtpp session
    await client.async_api.close_connection()


if __name__ == "__main__":
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/elliottech/lighter-v2-python",
    "name": "lighter-v2-python",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "lighter exchange rest api defi ethereum optimism l2 eth",
    "author": "Elliot",
    "author_email": "ahmet@elliot.ai",
    "download_url": "https://files.pythonhosted.org/packages/c0/c2/93defc5cd7d6f3ef2dc3f3deea285a5418189585ae3772e7d73b6747836d/lighter-v2-python-1.0.0.tar.gz",
    "platform": null,
    "description": "# Lighter\n\nPython client for Lighter (v2). The sdk is tested against Python versions 3.9, 3.10 and 3.11.\n\n## Installation\n\n```bash\npip install lighter-v2-python\n```\n\n## Getting Started\n\nThe `Client` object has four main modules;\n\n- `Api`: allows interaction with the lighter api\n- `AsyncApi`: allows for async interaction with the lighter api\n- `Blockchain`: allows interaction with lighter contracts\n- `AsyncBlockchain`: allows async interaction with lighter contracts\n\n`Client` can be created with private key or not depending on whether you are going to use the api or interact with the contracts. For more complete examples, see the [examples](./examples/) directory.\n\n### API\n\nFollowing parameters are required to use `Api` module:\n\n- `api_auth`: You should get the key from our servers.\n- `web3_provider_url`: You need a node to interact with the contract. We suggest alchemy which provides 300M free compute units monthly, You can register and get your keys [here](https://www.alchemy.com/)\n\n```python\nfrom lighter.lighter_client import Client\nimport os\nfrom lighter.modules.blockchain import OrderSide\nfrom lighter.constants import ORDERBOOK_WETH_USDC\n\napi_auth = os.environ.get(\"API_AUTH\")\n\n# You don't need to provide private key if you only want to use the api module.\n\nclient = Client(api_auth=api_auth, web3_provider_url=\"ALCHEMY_URL\")\n\n# Let's get available blockchains and their details from the api module.\nblockchains = client.api.get_blockchains().data\n```\n\n### Blockchain\n\nFollowing parameters are required to use `Blockchain` module:\n\n- `api_auth`: You should get the key from our servers.\n- `private_key`: You need to provide your wallet private key to sign your transactions.\n- `web3_provider_url`: You need a node to interact with the contract. We suggest alchemy which provides 300M free compute units monthly, You can register and get your keys [here](https://www.alchemy.com/)\n\nBlockchain module enables you to do multiple operations in one transaction. The number of operations is limited to 4 million gas. So the limits are roughly;\n\n- 25 order creations in one transaction\n- 100 order cancellations in one transaction\n- 25 order updates in one transaction\n\n```python\nfrom lighter.lighter_client import Client\nimport os\nfrom lighter.modules.blockchain import OrderSide\nfrom lighter.constants import ORDERBOOK_WETH_USDC\n\n\nprivate_key = os.environ.get(\"SOURCE_PRIVATE_KEY\")\napi_auth = os.environ.get(\"API_AUTH\")\n\n\nclient = Client(\n    private_key=private_key, api_auth=api_auth, web3_provider_url=\"ALCHEMY_URL\"\n)\n\n\nx = client.blockchain  # initialize the blockchain module before using it\n\n\n# You need to give allowance for the trading tokens\nclient.blockchain.set_token_max_allowance(\n    spender=client.blockchain.router_contract.address, token=\"WETH\"\n)\n\nclient.blockchain.set_token_max_allowance(\n    spender=client.blockchain.router_contract.address, token=\"USDC\"\n)\n\n\n# Let's create a batch of limit orders\nsizes = [\"0.001\", \"0.001\", \"0.001\"]\nprices = [\"1050\", \"1200\", \"1000\"]\nsides = [OrderSide.BUY, OrderSide.SELL, OrderSide.BUY]\n\n\ntx_hash = client.blockchain.create_limit_order_batch(\"WETH_USDC\", sizes, prices, sides)\n\n# if you want to wait for the transaction to be mined and get order id and other details,\n# you can use the following method.\n# alternatively you can wait the data from websocket\nresult = client.blockchain.get_create_order_transaction_result(tx_hash, \"WETH_USDC\")\n```\n\n### Async Examples\n\n```python\nfrom lighter.lighter_client import Client\nimport os\nfrom lighter.modules.blockchain import OrderSide\nfrom lighter.constants import ORDERBOOK_WETH_USDC\n\n\nprivate_key = os.environ.get(\"SOURCE_PRIVATE_KEY\")\napi_auth = os.environ.get(\"API_AUTH\")\n\n\nclient = Client(\n    private_key=private_key, api_auth=api_auth, web3_provider_url=\"ALCHEMY_URL\"\n)\n\n\nasync def main():\n    client.async_blockchain # to initialize the module\n    sizes = [\"0.0001\"]\n    prices = [\"1000\"]\n    sides = [OrderSide.SELL]\n\n    # Test async api\n    print(await client.async_api.get_blockchains())\n    print(await client.async_api.get_orderbook(\"WETH_USDC\"))\n\n    print(\n        client.api.get_candles(\n            orderbook_symbol=\"WETH_USDC\",\n            resolution=60,\n            timestamp_start=int(time.time()) - 60 * 60 * 60 * 24,\n            timestamp_end=int(time.time()),\n        )\n    )\n\n    tx_hash = await client.async_blockchain.create_limit_order_batch(\n        \"WETH_USDC\", sizes, prices, sides\n    )\n    # tx_hash = await client.async_blockchain.cancel_limit_order_batch(\n    #     \"WETH_USDC\", [24989]\n    # )\n    # tx_hash = await client.async_blockchain.update_limit_order_batch(\n    #     \"WETH_USDC\", [24991], [\"0.0001\"], [\"100\"], [OrderSide.BUY]\n    # )\n    # tx_hash = await client.async_blockchain.create_market_order(\n    #     \"WETH_USDC\", \"0.0001\", \"100\", OrderSide.SELL\n    # )\n\n    result = await client.async_blockchain.get_create_order_transaction_result(\n        tx_hash, \"WETH_USDC\"\n    )\n    # result = await client.async_blockchain.get_limit_order_canceled_transaction_result(\n    #     tx_hash, \"WETH_USDC\"\n    # )\n    # result = await client.async_blockchain.get_update_limit_order_transaction_result(\n    #     tx_hash, \"WETH_USDC\"\n    # )\n    print(result)\n\n    # close the aiohtpp session\n    await client.async_api.close_connection()\n\n\nif __name__ == \"__main__\":\n    import asyncio\n\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(main())\n```\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "lighter Python rest api and blockchain interactions for Limit Orders",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/elliottech/lighter-v2-python"
    },
    "split_keywords": [
        "lighter",
        "exchange",
        "rest",
        "api",
        "defi",
        "ethereum",
        "optimism",
        "l2",
        "eth"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79966b8dc44c1841cae024671fccad643c8a533b5f51bfa5d753dfacafbe7b49",
                "md5": "51f64c53b0a31557475af4ce4d42bd1b",
                "sha256": "7dc4408a8b102396d14a459467da60ea29948f885c90d6e923037ae0dd89114c"
            },
            "downloads": -1,
            "filename": "lighter_v2_python-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "51f64c53b0a31557475af4ce4d42bd1b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 23935,
            "upload_time": "2023-10-17T12:04:44",
            "upload_time_iso_8601": "2023-10-17T12:04:44.963309Z",
            "url": "https://files.pythonhosted.org/packages/79/96/6b8dc44c1841cae024671fccad643c8a533b5f51bfa5d753dfacafbe7b49/lighter_v2_python-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0c293defc5cd7d6f3ef2dc3f3deea285a5418189585ae3772e7d73b6747836d",
                "md5": "6abb670886dc5871238b4e8b956b5449",
                "sha256": "9bd2fa69495a72a4ef165b31af248b50636f2e1e26c266f0e41214e67ead7feb"
            },
            "downloads": -1,
            "filename": "lighter-v2-python-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6abb670886dc5871238b4e8b956b5449",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 22773,
            "upload_time": "2023-10-17T12:04:46",
            "upload_time_iso_8601": "2023-10-17T12:04:46.506730Z",
            "url": "https://files.pythonhosted.org/packages/c0/c2/93defc5cd7d6f3ef2dc3f3deea285a5418189585ae3772e7d73b6747836d/lighter-v2-python-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-17 12:04:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "elliottech",
    "github_project": "lighter-v2-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "circle": true,
    "requirements": [
        {
            "name": "eth-account",
            "specs": [
                [
                    ">=",
                    "0.4.0"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "7.2.2"
                ]
            ]
        },
        {
            "name": "pytest-mock",
            "specs": [
                [
                    ">=",
                    "3.0.0"
                ]
            ]
        },
        {
            "name": "requests-mock",
            "specs": [
                [
                    ">=",
                    "1.6.0"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    ">=",
                    "2.22.0"
                ]
            ]
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    ">=",
                    "50.3.2"
                ]
            ]
        },
        {
            "name": "tox",
            "specs": [
                [
                    "==",
                    "3.25.0"
                ]
            ]
        },
        {
            "name": "web3",
            "specs": [
                [
                    ">=",
                    "6.0.0"
                ]
            ]
        },
        {
            "name": "dateparser",
            "specs": [
                [
                    ">=",
                    "1.0.0"
                ]
            ]
        },
        {
            "name": "nest-asyncio",
            "specs": [
                [
                    ">=",
                    "1.5.6"
                ]
            ]
        },
        {
            "name": "pytest-asyncio",
            "specs": [
                [
                    ">=",
                    "0.21.0"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "lighter-v2-python"
}
        
Elapsed time: 0.37043s