Skip to content

Commit 6a40345

Browse files
committed
Public pools
1 parent d7bd39b commit 6a40345

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

examples/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ Examples on how to do this can be found here:
103103
- `spot_get_account_assets_http.py`
104104
- `spot_get_account_assets_ws.py`
105105

106+
## Public Pools
107+
Public pools behave just like subaccounts, except that anyone can join them.
108+
You can create / modify a public pool using the SDK. Check out the following example:
109+
- `public_pool_create_modify.py`
110+
111+
To create API keys for a public pool, you need to run the setup script but specify the `ACCOUNT_INDEX` to be the one of the public pool.
112+
After that, you can trade from the public as from any other account.
113+
114+
If you want to deposit / withdraw from a public pool, check the following example:
115+
- `public_pool_deposit.py`
116+
- `public_pool_withdraw.py`
117+
118+
To get information about pools, check:
119+
- `public_pool_info.py`
120+
106121
## Setup steps for mainnet
107122
- deposit money on Lighter to create an account first
108123
- change the URL to `mainnet.zklighter.elliot.ai`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import time
2+
import json
3+
import asyncio
4+
import lighter
5+
from utils import default_example_setup
6+
7+
8+
async def main():
9+
client, api_client, _ = default_example_setup()
10+
tx_api = lighter.TransactionApi(api_client)
11+
12+
err = client.check_client()
13+
if err is not None:
14+
print(f"CheckClient error: {err}")
15+
return
16+
17+
auth, _ = client.create_auth_token_with_expiry()
18+
19+
# create a public pool
20+
tx_info, response, err = await client.create_public_pool(
21+
operator_fee=100000, # 10%
22+
initial_total_shares=1_000_000, # 1000 USDC
23+
min_operator_share_rate=100, # 1%
24+
)
25+
if err is not None:
26+
raise Exception(f'failed to create public pool {err}')
27+
tx_hash = response.tx_hash
28+
print(f"✅ send create public pool tx. hash: {tx_hash}")
29+
30+
# fetch pool account index from tx hash
31+
pool_account_index = -1
32+
for i in range(10):
33+
time.sleep(1)
34+
try:
35+
response = await tx_api.tx(by="hash", value=tx_hash)
36+
event_info_j = json.loads(response.event_info)
37+
pool_account_index = event_info_j['a']
38+
except Exception as e:
39+
pass
40+
if pool_account_index != -1:
41+
break
42+
if pool_account_index == -1:
43+
raise Exception(f"failed to find pool account index for tx {tx_hash}")
44+
print(f"✅ pool account index: {pool_account_index}")
45+
46+
# Note: ❗️operator_fee can only decrease
47+
# modify pool metadata
48+
tx_info, response, err = await client.update_public_pool(
49+
public_pool_index=pool_account_index,
50+
status=0, # 0 is active | 1 is frozen
51+
operator_fee=50000, # 5%
52+
min_operator_share_rate=1000, # 10%
53+
)
54+
if err is not None:
55+
raise Exception(f'failed to create update pool {err}')
56+
57+
58+
if __name__ == "__main__":
59+
asyncio.run(main())

examples/public_pool_deposit.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import asyncio
2+
3+
from utils import default_example_setup
4+
5+
POOL_ACCOUNT_INDEX = 281474976710651
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+
tx_info, response, err = await client.mint_shares(public_pool_index=POOL_ACCOUNT_INDEX, share_amount=10_000)
17+
if err is not None:
18+
raise Exception(f'failed to mint shares {err}')
19+
20+
await client.close()
21+
await api_client.close()
22+
23+
24+
if __name__ == "__main__":
25+
asyncio.run(main())

examples/public_pool_info.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import lighter
3+
from utils import default_example_setup
4+
5+
POOL_ACCOUNT_INDEX = 281474976710651
6+
7+
8+
async def main():
9+
client, api_client, _ = default_example_setup()
10+
account_api = lighter.AccountApi(api_client)
11+
12+
err = client.check_client()
13+
if err is not None:
14+
print(f"CheckClient error: {err}")
15+
return
16+
17+
account = await account_api.account(by="index", value=str(client.account_index))
18+
19+
# Note: ❗️shares field does not return the shared you have in pools that you're the operator
20+
for pool in account.accounts[0].shares:
21+
pool_resp = await account_api.account(by="index", value=str(pool.public_pool_index))
22+
pool_account = pool_resp.accounts[0]
23+
24+
share_price = float(pool_account.total_asset_value) / float(pool_account.pool_info.total_shares)
25+
print(
26+
f"poolAccountId: {pool.public_pool_index} numShared: {pool.shares_amount} sharePrice: {share_price:.6f} value: {share_price * pool.shares_amount:.2f} pnl: {share_price * pool.shares_amount - float(pool.entry_usdc):.2f}")
27+
28+
await client.close()
29+
await api_client.close()
30+
31+
32+
if __name__ == "__main__":
33+
asyncio.run(main())

examples/public_pool_withdraw.py

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

0 commit comments

Comments
 (0)