Basic Usage
In this page, we will cover the basic usage of the SDK. The example snippets are all self-contained and can be run independently.
Interact with L1
To interact with Term Structure Layer 1, you should instantiate the ContractBroker
and invoke the desired member functions.
For the first step of interaction, you should call the register
function on Layer 1, which crypto asset deposit is required.
from term_structure.models.contract_broker import ContractBroker
def main():
broker = ContractBroker(
rpc_url="https://xxx.infura.io/...",
layer_1_wallet_private_key="0x78...89",
)
tx_info = broker.register_with_allowance_check(
deposit_asset_symbol="USDT",
deposit_amount=10000000 # 10 USDT (decimals=6)
)
main()
By calling the deposit
function on Layer 1, more crypto asset can be deposited after register
.
from term_structure.models.contract_broker import ContractBroker
def main():
broker = ContractBroker(
rpc_url="https://xxx.infura.io/...",
layer_1_wallet_private_key="0x78...89",
)
tx_info = broker.deposit_with_allowance_check(
layer_1_wallet_address="0x9cd...9A3",
deposit_asset_symbol="USDT",
deposit_amount=10000000 # 10 USDT (decimals=6)
)
main()
Testnet Support
For testnet support, you can use the following scripts:
def main():
broker = ContractBroker(
rpc_url="https://xxx.infura.io/...",
layer_1_wallet_private_key="0x78...89",
layer_1_chain_id="5",
layer_1_zk_true_up_contract_address="0xCB..6C",
)
main()
Interact with L2
For use cases of single wallet, you can instantiate the AsyncSingleWalletExchangeBroker
and invoke the desired functions or coroutines.
import asyncio
from term_structure.models.async_single_wallet_exchange_broker import AsyncSingleWalletExchangeBroker
async def main():
broker = AsyncSingleWalletExchangeBroker(
layer_1_wallet_address="0x9cd...9A3",
layer_1_wallet_private_key="0x78...89",
)
exchange_config = await broker.get_exchange_config()
asyncio.run(main())
For use cases of multiple wallets, you can instantiate the AsyncMultipleWalletExchangeBroker
and invoke the desired functions or coroutines.
import asyncio
from term_structure.models.async_multiple_wallet_exchange_broker import AsyncMultipleWalletExchangeBroker
async def main():
broker = AsyncMultipleWalletExchangeBroker({
"wallet_1": {
"layer_1_wallet_address": "0x9cd...9A3",
"layer_1_wallet_private_key": "0x78...89",
},
"wallet_2": {
"layer_1_wallet_address": "0x6a5...0cF",
"layer_1_wallet_private_key": "0x16...G2",
},
# ...
})
order_1 = await broker.place_order("wallet_1")
order_2 = await broker.place_order("wallet_2")
asyncio.run(main())
Testnet Support
broker = AsyncMultipleWalletExchangeBroker(host="...testnet.") # TODO
Long-lived Process
If you are going to run a long-lived application that utilizes the SDK,
please be informed that broker.exchange_config
and broker.exchange_info
may become stale.
As time passed, markets could be matured or disabled, epoch could be increased, and operational configurations could be updated.
To mitigate errors caused by the aforementioned situations, you can invoke coroutines, broker.sync_exchange_config()
and broker.sync_exchange_info()
, to keep the states fresh.
import asyncio
from term_structure.models.async_single_wallet_exchange_broker import AsyncSingleWalletExchangeBroker
async def main():
broker = AsyncSingleWalletExchangeBroker(
layer_1_wallet_address="0x9cd...9A3",
layer_1_wallet_private_key="0x78...89",
)
await broker.sync_exchange_config()
await broker.sync_exchange_info()
asyncio.run(main())
Full Examples
For more examples, please refer the scripts in this directory.