-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
153 lines (131 loc) · 5.42 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from time import time, sleep
from algosdk import account, encoding
from algosdk.logic import get_application_address
from amm.operations import (
createAmmApp,
setupAmmApp,
supply,
withdraw,
swap,
closeAmm,
optInToPoolToken,
)
from amm.util import (
getBalances,
getAppGlobalState,
getLastBlockTimestamp,
)
from amm.testing.setup import getAlgodClient
from amm.testing.resources import (
getTemporaryAccount,
optInToAsset,
createDummyAsset,
)
def simple_amm():
client = getAlgodClient()
print("Alice is generating temporary accounts...")
creator = getTemporaryAccount(client)
supplier = getTemporaryAccount(client)
print("Alice is generating example tokens...")
tokenAAmount = 10 ** 13
tokenBAmount = 10 ** 13
tokenA = createDummyAsset(client, tokenBAmount, creator)
tokenB = createDummyAsset(client, tokenBAmount, creator)
print("TokenA id is:", tokenA)
print("TokenB id is:", tokenB)
print("Alice is creating AMM that swaps between token A and token B...")
appID = createAmmApp(
client=client,
creator=creator,
tokenA=tokenA,
tokenB=tokenB,
feeBps=30,
minIncrement=1000,
)
creatorBalancesBefore = getBalances(client, creator.getAddress())
ammBalancesBefore = getBalances(client, get_application_address(appID))
print("Alice's balances: ", creatorBalancesBefore)
print("AMM's balances: ", ammBalancesBefore)
print("Alice is setting up and funding amm...")
poolToken = setupAmmApp(
client=client,
appID=appID,
funder=creator,
tokenA=tokenA,
tokenB=tokenB,
)
creatorBalancesBefore = getBalances(client, creator.getAddress())
ammBalancesBefore = getBalances(client, get_application_address(appID))
print("Alice's balances: ", creatorBalancesBefore)
print("AMM's balances: ", ammBalancesBefore)
print("Opting Alice in to receive pool token...")
optInToPoolToken(client, appID, creator)
print("Supplying AMM with initial token A and token B")
supply(client=client, appID=appID, qA=500_000, qB=100_000_000, supplier=creator)
ammBalancesSupplied = getBalances(client, get_application_address(appID))
creatorBalancesSupplied = getBalances(client, creator.getAddress())
poolTokenFirstAmount = creatorBalancesSupplied[poolToken]
print("AMM's balances: ", ammBalancesSupplied)
print("Alice's balances: ", creatorBalancesSupplied)
print("Supplying AMM with same token A and token B")
supply(client=client, appID=appID, qA=100_000, qB=20_000_000, supplier=creator)
ammBalancesSupplied = getBalances(client, get_application_address(appID))
creatorBalancesSupplied = getBalances(client, creator.getAddress())
print("AMM's balances: ", ammBalancesSupplied)
print("Alice's balances: ", creatorBalancesSupplied)
print("Supplying AMM with too large ratio of token A and token B")
supply(client=client, appID=appID, qA=100_000, qB=100_000, supplier=creator)
ammBalancesSupplied = getBalances(client, get_application_address(appID))
creatorBalancesSupplied = getBalances(client, creator.getAddress())
print("AMM's balances: ", ammBalancesSupplied)
print("Alice's balances: ", creatorBalancesSupplied)
print("Supplying AMM with too small ratio of token A and token B")
supply(client=client, appID=appID, qA=100_000, qB=100_000_000, supplier=creator)
ammBalancesSupplied = getBalances(client, get_application_address(appID))
creatorBalancesSupplied = getBalances(client, creator.getAddress())
print("AMM's balances: ", ammBalancesSupplied)
print("Alice's balances: ", creatorBalancesSupplied)
poolTokenTotalAmount = creatorBalancesSupplied[poolToken]
print(" ")
print("Alice is exchanging her Token A for Token B")
swap(client=client, appID=appID, tokenId=tokenA, amount=1_000, trader=creator)
ammBalancesTraded = getBalances(client, get_application_address(appID))
creatorBalancesTraded = getBalances(client, creator.getAddress())
print("AMM's balances: ", ammBalancesTraded)
print("Alice's balances: ", creatorBalancesTraded)
print("Alice is exchanging her Token B for Token A")
swap(
client=client,
appID=appID,
tokenId=tokenB,
amount=int(1_000_000 * 1.003),
trader=creator,
)
ammBalancesTraded = getBalances(client, get_application_address(appID))
creatorBalancesTraded = getBalances(client, creator.getAddress())
print("AMM's balances: ", ammBalancesTraded)
print("Alice's balances: ", creatorBalancesTraded)
print(" ")
print("Withdrawing first supplied liquidity from AMM")
print("Withdrawing: ", poolTokenFirstAmount)
withdraw(
client=client,
appID=appID,
poolTokenAmount=poolTokenFirstAmount,
withdrawAccount=creator,
)
ammBalancesWithdrawn = getBalances(client, get_application_address(appID))
print("AMM's balances: ", ammBalancesWithdrawn)
print("Withdrawing remainder of the supplied liquidity from AMM")
poolTokenTotalAmount -= poolTokenFirstAmount
withdraw(
client=client,
appID=appID,
poolTokenAmount=poolTokenTotalAmount,
withdrawAccount=creator,
)
ammBalancesWithdrawn = getBalances(client, get_application_address(appID))
print("AMM's balances: ", ammBalancesWithdrawn)
print("Closing AMM")
closeAmm(client=client, appID=appID, closer=creator)
simple_amm()