Skip to content

Commit ff1d5b5

Browse files
committed
withdraw example
1 parent 28e7e35 commit ff1d5b5

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

examples/withdraw_normal.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import asyncio
2+
from utils import default_example_setup
3+
4+
AMOUNT = 5.0
5+
6+
async def main():
7+
client, api_client, _ = default_example_setup()
8+
9+
# Note: There is no limit or fee for normal withdrawal
10+
withdraw_tx, response, err = await client.withdraw(
11+
asset_id=client.ASSET_ID_USDC, # change this to `client.ASSET_ID_ETH` to withdraw ETH. Also, change route_type to spot
12+
route_type=client.ROUTE_PERP, # change this to `client.ROUTE_SPOT` to withdraw from spot balance
13+
amount=AMOUNT,
14+
)
15+
if err is not None:
16+
raise Exception(f"error withdrawing {err}")
17+
18+
print(withdraw_tx, response)
19+
20+
await client.close()
21+
await api_client.close()
22+
23+
24+
if __name__ == "__main__":
25+
asyncio.run(main())

lighter/signer_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ def sign_create_grouped_orders(
451451
def sign_cancel_order(self, market_index: int, order_index: int, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
452452
return self.__decode_tx_info(self.signer.SignCancelOrder(market_index, order_index, nonce, api_key_index, self.account_index))
453453

454-
def sign_withdraw(self, usdc_amount: int, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
455-
return self.__decode_tx_info(self.signer.SignWithdraw(usdc_amount, nonce, api_key_index, self.account_index))
454+
def sign_withdraw(self, asset_index: int, route_type: int, amount: int, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
455+
return self.__decode_tx_info(self.signer.SignWithdraw(asset_index, route_type, amount, nonce, api_key_index, self.account_index))
456456

457457
def sign_create_sub_account(self, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
458458
return self.__decode_tx_info(self.signer.SignCreateSubAccount(nonce, api_key_index, self.account_index))
@@ -742,10 +742,15 @@ async def create_sl_limit_order(self, market_index, client_order_index, base_amo
742742
)
743743

744744
@process_api_key_and_nonce
745-
async def withdraw(self, usdc_amount, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[Withdraw, RespSendTx, None], Tuple[None, None, str]]:
746-
usdc_amount = int(usdc_amount * self.USDC_TICKER_SCALE)
745+
async def withdraw(self, asset_id: int, route_type: int, amount: float, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[Withdraw, RespSendTx, None], Tuple[None, None, str]]:
746+
if asset_id == self.ASSET_ID_USDC:
747+
amount = int(amount * self.USDC_TICKER_SCALE)
748+
elif asset_id == self.ASSET_ID_ETH:
749+
amount = int(amount * self.ETH_TICKER_SCALE)
750+
else:
751+
raise ValueError(f"Unsupported asset id: {asset_id}")
747752

748-
tx_type, tx_info, tx_hash, error = self.sign_withdraw(usdc_amount, nonce, api_key_index)
753+
tx_type, tx_info, tx_hash, error = self.sign_withdraw(asset_id, route_type, amount, nonce, api_key_index)
749754
if error is not None:
750755
return None, None, error
751756

0 commit comments

Comments
 (0)