Skip to content

Commit 9d15c2d

Browse files
committed
integrate staking tx
1 parent 80b0e74 commit 9d15c2d

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

examples/stake_and_unstake.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
3+
from utils import default_example_setup
4+
5+
STAKING_POOL_INDEX = 281474976624800
6+
7+
8+
async def main():
9+
client, api_client, _ = default_example_setup()
10+
11+
err = client.check_client()
12+
if err is not None:
13+
print(f"CheckClient error: {err}")
14+
return
15+
16+
try:
17+
tx_info, response, err = await client.stake_assets(public_pool_index=STAKING_POOL_INDEX, share_amount=10_000)
18+
if err is not None:
19+
raise Exception(f'failed to stake assets {err}')
20+
21+
tx_info, response, err = await client.unstake_assets(public_pool_index=STAKING_POOL_INDEX, share_amount=10_000)
22+
if err is not None:
23+
raise Exception(f'failed to unstake assets {err}')
24+
finally:
25+
await client.close()
26+
await api_client.close()
27+
28+
29+
if __name__ == "__main__":
30+
asyncio.run(main())

lighter/signer_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ def __populate_shared_library_functions(signer):
153153
signer.SignBurnShares.argtypes = [ctypes.c_longlong, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
154154
signer.SignBurnShares.restype = SignedTxResponse
155155

156+
signer.SignStakeAssets.argtypes = [ctypes.c_longlong, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
157+
signer.SignStakeAssets.restype = SignedTxResponse
158+
159+
signer.SignUnstakeAssets.argtypes = [ctypes.c_longlong, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
160+
signer.SignUnstakeAssets.restype = SignedTxResponse
161+
156162
signer.SignUpdateLeverage.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
157163
signer.SignUpdateLeverage.restype = SignedTxResponse
158164

@@ -562,6 +568,12 @@ def sign_mint_shares(self, public_pool_index: int, share_amount: int, nonce: int
562568
def sign_burn_shares(self, public_pool_index: int, share_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]]:
563569
return self.__decode_tx_info(self.signer.SignBurnShares(public_pool_index, share_amount, nonce, api_key_index, self.account_index))
564570

571+
def sign_stake_assets(self, staking_pool_index: int, share_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]]:
572+
return self.__decode_tx_info(self.signer.SignStakeAssets(staking_pool_index, share_amount, nonce, api_key_index, self.account_index))
573+
574+
def sign_unstake_assets(self, staking_pool_index: int, share_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]]:
575+
return self.__decode_tx_info(self.signer.SignUnstakeAssets(staking_pool_index, share_amount, nonce, api_key_index, self.account_index))
576+
565577
def sign_update_leverage(self, market_index: int, fraction: int, margin_mode: int, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
566578
return self.__decode_tx_info(self.signer.SignUpdateLeverage(market_index, fraction, margin_mode, nonce, api_key_index, self.account_index))
567579

@@ -1180,6 +1192,28 @@ async def burn_shares(self, public_pool_index, share_amount, nonce: int = DEFAUL
11801192
logging.debug(f"Burn Shares Send. TxResponse: {api_response}")
11811193
return tx_info, api_response, None
11821194

1195+
@process_api_key_and_nonce
1196+
async def stake_assets(self, staking_pool_index, share_amount, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX):
1197+
tx_type, tx_info, tx_hash, error = self.sign_stake_assets(staking_pool_index, share_amount, nonce, api_key_index)
1198+
if error is not None:
1199+
return None, None, error
1200+
1201+
logging.debug(f"Stake Assets TxHash: {tx_hash} TxInfo: {tx_info}")
1202+
api_response = await self.send_tx(tx_type=tx_type, tx_info=tx_info)
1203+
logging.debug(f"Stake Assets Send. TxResponse: {api_response}")
1204+
return tx_info, api_response, None
1205+
1206+
@process_api_key_and_nonce
1207+
async def unstake_assets(self, staking_pool_index, share_amount, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX):
1208+
tx_type, tx_info, tx_hash, error = self.sign_unstake_assets(staking_pool_index, share_amount, nonce, api_key_index)
1209+
if error is not None:
1210+
return None, None, error
1211+
1212+
logging.debug(f"Unstake Assets TxHash: {tx_hash} TxInfo: {tx_info}")
1213+
api_response = await self.send_tx(tx_type=tx_type, tx_info=tx_info)
1214+
logging.debug(f"Unstake Assets Send. TxResponse: {api_response}")
1215+
return tx_info, api_response, None
1216+
11831217
@process_api_key_and_nonce
11841218
async def update_leverage(self, market_index, margin_mode, leverage, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX):
11851219
imf = int(10_000 / leverage)

0 commit comments

Comments
 (0)