Skip to content

Commit 1d7d604

Browse files
UpdateAccountAssetConfig (#150)
1 parent d45cc97 commit 1d7d604

12 files changed

Lines changed: 128 additions & 0 deletions

examples/add_eth_margin.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import asyncio
2+
import lighter
3+
from utils import default_example_setup
4+
5+
6+
async def fetch(api_client, idx):
7+
return (await lighter.AccountApi(api_client).account(by="index", value=str(idx))).accounts[0]
8+
9+
def eth_spot(a):
10+
for x in a.assets:
11+
if x.symbol == "ETH":
12+
return float(x.balance)
13+
return 0.0
14+
15+
def show(label, a):
16+
print(f"\n[{label}] mode={a.account_trading_mode} tav={a.total_asset_value}")
17+
for x in a.assets:
18+
extra = getattr(x, "additional_properties", {}) or {}
19+
mb = getattr(x, "margin_balance", None) or extra.get("margin_balance", "-")
20+
mm = getattr(x, "margin_mode", None) or extra.get("margin_mode", "-")
21+
print(f" {x.symbol:<6} spot={x.balance} margin={mb} mode={mm}")
22+
23+
24+
async def wait_for_change(api_client, idx, prev, timeout=60):
25+
for i in range(timeout * 2):
26+
await asyncio.sleep(0.5)
27+
a = await fetch(api_client, idx)
28+
if abs(eth_spot(a) - prev) > 1e-9:
29+
print(f"state updated after {(i+1)*0.5:.1f}s")
30+
return a
31+
print(f"timeout {timeout}s — state didn't change")
32+
return await fetch(api_client, idx)
33+
34+
async def main():
35+
client, api_client, _ = default_example_setup()
36+
before = await fetch(api_client, client.account_index)
37+
show("BEFORE", before)
38+
39+
await client.update_account_config(account_trading_mode=1)
40+
await client.update_account_asset_config(
41+
asset_index=client.ASSET_ID_ETH,
42+
asset_margin_mode=client.ASSET_MARGIN_MODE_ENABLED,
43+
)
44+
await client.transfer_same_master_account(
45+
to_account_index=client.account_index,
46+
asset_id=client.ASSET_ID_ETH,
47+
amount=1.0,
48+
route_from=client.ROUTE_SPOT,
49+
route_to=client.ROUTE_PERP,
50+
fee=0,
51+
memo="0x" + "00" * 32,
52+
)
53+
54+
after = await wait_for_change(api_client, client.account_index, eth_spot(before))
55+
show("AFTER", after)
56+
57+
await client.close()
58+
await api_client.close()
59+
60+
61+
if __name__ == "__main__":
62+
asyncio.run(main())

examples/disable_eth_as_margin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
from utils import default_example_setup
3+
4+
5+
async def main():
6+
client, api_client, _ = default_example_setup()
7+
8+
tx, resp, err = await client.update_account_asset_config(
9+
asset_index=client.ASSET_ID_ETH,
10+
asset_margin_mode=client.ASSET_MARGIN_MODE_DISABLED,
11+
)
12+
print(f"Enable ETH as margin: {tx=} {resp=} {err=}")
13+
if err is not None:
14+
raise Exception(err)
15+
16+
await client.close()
17+
await api_client.close()
18+
19+
20+
if __name__ == "__main__":
21+
asyncio.run(main())

examples/enable_eth_as_margin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
from utils import default_example_setup
3+
4+
5+
async def main():
6+
client, api_client, _ = default_example_setup()
7+
8+
tx, resp, err = await client.update_account_asset_config(
9+
asset_index=client.ASSET_ID_ETH,
10+
asset_margin_mode=client.ASSET_MARGIN_MODE_ENABLED,
11+
)
12+
print(f"Enable ETH as margin: {tx=} {resp=} {err=}")
13+
if err is not None:
14+
raise Exception(err)
15+
16+
await client.close()
17+
await api_client.close()
18+
19+
20+
if __name__ == "__main__":
21+
asyncio.run(main())

lighter/signer_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def __populate_shared_library_functions(signer):
175175
signer.SignUpdateAccountConfig.argtypes = [ctypes.c_uint8, ctypes.c_uint8, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
176176
signer.SignUpdateAccountConfig.restype = SignedTxResponse
177177

178+
signer.SignUpdateAccountAssetConfig.argtypes = [ctypes.c_int16, ctypes.c_uint8, ctypes.c_uint8, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
179+
signer.SignUpdateAccountAssetConfig.restype = SignedTxResponse
180+
178181
signer.Free.argtypes = [ctypes.c_void_p]
179182
signer.Free.restype = None
180183

@@ -279,6 +282,9 @@ class SignerClient:
279282
CROSS_MARGIN_MODE = 0
280283
ISOLATED_MARGIN_MODE = 1
281284

285+
ASSET_MARGIN_MODE_DISABLED = 0
286+
ASSET_MARGIN_MODE_ENABLED = 1
287+
282288
ISOLATED_MARGIN_REMOVE_COLLATERAL = 0
283289
ISOLATED_MARGIN_ADD_COLLATERAL = 1
284290

@@ -628,6 +634,9 @@ def sign_update_account_config(self, account_trading_mode: int, skip_nonce: int
628634
return self.__decode_tx_info(self.signer.SignUpdateAccountConfig(account_trading_mode, skip_nonce, nonce, api_key_index,
629635
self.account_index))
630636

637+
def sign_update_account_asset_config(self, asset_index: int, asset_margin_mode: int, skip_nonce: int = SKIP_NONCE_OFF, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX) -> Union[Tuple[str, str, str, None], Tuple[None, None, None, str]]:
638+
return self.__decode_tx_info(self.signer.SignUpdateAccountAssetConfig(asset_index, asset_margin_mode, skip_nonce, nonce, api_key_index, self.account_index))
639+
631640
@process_api_key_and_nonce
632641
async def create_order(
633642
self,
@@ -1360,6 +1369,17 @@ async def update_account_config(self, account_trading_mode: int, skip_nonce: int
13601369
api_response = await self.send_tx(tx_type=tx_type, tx_info=tx_info)
13611370
return tx_info, api_response, None
13621371

1372+
@process_api_key_and_nonce
1373+
async def update_account_asset_config(self, asset_index: int, asset_margin_mode: int, skip_nonce: int = SKIP_NONCE_OFF, nonce: int = DEFAULT_NONCE, api_key_index: int = DEFAULT_API_KEY_INDEX):
1374+
tx_type, tx_info, tx_hash, error = self.sign_update_account_asset_config(
1375+
asset_index, asset_margin_mode, skip_nonce, nonce, api_key_index
1376+
)
1377+
if error is not None:
1378+
return None, None, error
1379+
api_response = await self.send_tx(tx_type=tx_type, tx_info=tx_info)
1380+
return tx_info, api_response, None
1381+
1382+
13631383
async def send_tx(self, tx_type: StrictInt, tx_info: str) -> RespSendTx:
13641384
if tx_info[0] != "{":
13651385
raise Exception(tx_info)
1.42 KB
Binary file not shown.

lighter/signers/lighter-signer-darwin-arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ extern SignedTxResponse SignStakeAssets(long long cStakingPoolIndex, long long c
142142
extern SignedTxResponse SignUnstakeAssets(long long cStakingPoolIndex, long long cShareAmount, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
143143
extern SignedTxResponse SignApproveIntegrator(long long cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long cApprovalExpiry, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
144144
extern SignedTxResponse SignUpdateAccountConfig(uint8_t cAccountTradingMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
145+
extern SignedTxResponse SignUpdateAccountAssetConfig(int16_t cAssetIndex, uint8_t cAssetMarginMode, uint8_t cSkipNonce, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
145146
extern void Free(void* ptr);
146147

147148
#ifdef __cplusplus

lighter/signers/lighter-signer-linux-amd64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern SignedTxResponse SignStakeAssets(long long int cStakingPoolIndex, long lo
134134
extern SignedTxResponse SignUnstakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
135135
extern SignedTxResponse SignApproveIntegrator(long long int cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long int cApprovalExpiry, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
136136
extern SignedTxResponse SignUpdateAccountConfig(uint8_t cAccountTradingMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137+
extern SignedTxResponse SignUpdateAccountAssetConfig(int16_t cAssetIndex, uint8_t cAssetMarginMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137138
extern void Free(void* ptr);
138139

139140
#ifdef __cplusplus
3 KB
Binary file not shown.

lighter/signers/lighter-signer-linux-arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern SignedTxResponse SignStakeAssets(long long int cStakingPoolIndex, long lo
134134
extern SignedTxResponse SignUnstakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
135135
extern SignedTxResponse SignApproveIntegrator(long long int cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long int cApprovalExpiry, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
136136
extern SignedTxResponse SignUpdateAccountConfig(uint8_t cAccountTradingMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137+
extern SignedTxResponse SignUpdateAccountAssetConfig(int16_t cAssetIndex, uint8_t cAssetMarginMode, uint8_t cSkipNonce, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137138
extern void Free(void* ptr);
138139

139140
#ifdef __cplusplus
11.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)