-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.vy
213 lines (200 loc) · 7.9 KB
/
api.vy
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# yay
import math as Math
import params as Params
import pools as Pools
import fees as Fees
import positions as Positions
########################################################################
# This is the entry-point contract.
#
# The callgraph is:
# api --> oracle --> RedstoneExtractor.sol
# |
# |
# core --> ERC20Plus
# / | \
# / | \
# pools <-fees<-positions
# | |
# | |
# params
#
# math is a library called by core, pools, and positions
import core as Core
import oracle as Oracle
ORACLE: public(Oracle)
CORE : public(Core)
DEPLOYER : address
INITIALIZED: bool
@external
def __init__():
self.DEPLOYER = msg.sender
self.INITIALIZED = False
@external
def __init__2(
oracle : address,
core : address):
assert msg.sender == self.DEPLOYER, ERR_INVARIANTS
assert not self.INITIALIZED , ERR_INVARIANTS
self.INITIALIZED = True
self.ORACLE = Oracle(oracle)
self.CORE = Core(core)
########################################################################
# We take the oracle payload and UI price and if everything checks out
# create a context and proxy the call to core.
@internal
def CONTEXT(
base_token : address,
quote_token: address,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> Ctx:
base_decimals : uint256 = convert(ERC20Plus(base_token).decimals(), uint256)
quote_decimals: uint256 = convert(ERC20Plus(quote_token).decimals(), uint256)
# this will revert on error
price : uint256 = self.ORACLE.price(quote_decimals,
desired,
slippage,
payload)
return Ctx({
price : price,
base_decimals : base_decimals,
quote_decimals: quote_decimals,
})
########################################################################
@external
def mint(
base_token : address, #ERC20
quote_token : address, #ERC20
lp_token : address, #ERC20Plus
base_amt : uint256,
quote_amt : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> uint256:
"""
@notice Provide liquidity to the pool
@param base_token Token representing the base coin of the pool (e.g. BTC)
@param quote_token Token representing the quote coin of the pool (e.g. USDT)
@param lp_token Token representing shares of the pool's liquidity
@param base_amt Number of base tokens to provide
@param quote_amt Number of quote tokens to provide
@param desired Price to provide liquidity at (unit price using onchain
representation for quote_token, e.g. 1.50$ would be
1500000 for USDT with 6 decimals)
@param slippage Acceptable deviaton of oracle price from desired price
(same units as desired e.g. to allow 5 cents of slippage,
send 50000).
@param payload Signed Redstone oracle payload
"""
ctx: Ctx = self.CONTEXT(base_token, quote_token, desired, slippage, payload)
return self.CORE.mint(1, base_token, quote_token, lp_token, base_amt, quote_amt, ctx)
@external
def burn(
base_token : address,
quote_token : address,
lp_token : address,
lp_amt : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> Tokens:
"""
@notice Withdraw liquidity from the pool
@param base_token Token representing the base coin of the pool (e.g. BTC)
@param quote_token Token representing the quote coin of the pool (e.g. USDT)
@param lp_token Token representing shares of the pool's liquidity
@param lp_amt Number of LP tokens to burn
@param desired Price to provide liquidity at (unit price using onchain
representation for quote_token, e.g. 1.50$ would be
1500000 for USDT with 6 decimals)
@param slippage Acceptable deviaton of oracle price from desired price
(same units as desired e.g. to allow 5 cents of slippage,
send 50000).
@param payload Signed Redstone oracle payload
"""
ctx: Ctx = self.CONTEXT(base_token, quote_token, desired, slippage, payload)
return self.CORE.burn(1, base_token, quote_token, lp_token, lp_amt, ctx)
@external
def open(
base_token : address,
quote_token : address,
long : bool,
collateral0 : uint256,
leverage : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> PositionState:
"""
@notice Open a position
@param base_token Token representing the base coin of the pool (e.g. BTC)
@param quote_token Token representing the quote coin of the pool (e.g. USDT)
@param long Flag indicating whether to go long or short
@param collateral0 Collateral tokens to send (long positions are collateralized
in quote_token, short positions are collateralized in base token).
@param leverage How much leverage to use
@param desired Price to provide liquidity at (unit price using onchain
representation for quote_token, e.g. 1.50$ would be
1500000 for USDT with 6 decimals)
@param slippage Acceptable deviaton of oracle price from desired price
(same units as desired e.g. to allow 5 cents of slippage,
send 50000).
@param payload Signed Redstone oracle payload
"""
ctx: Ctx = self.CONTEXT(base_token, quote_token, desired, slippage, payload)
return self.CORE.open(1, base_token, quote_token, long, collateral0, leverage, ctx)
@external
def close(
base_token : address,
quote_token : address,
position_id : uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> PositionValue:
"""
@notice Close a position
@param base_token Token representing the base coin of the pool (e.g. BTC)
@param quote_token Token representing the quote coin of the pool (e.g. USDT)
@param position_id The ID of the position to close
@param desired Price to provide liquidity at (unit price using onchain
representation for quote_token, e.g. 1.50$ would be
1500000 for USDT with 6 decimals)
@param slippage Acceptable deviaton of oracle price from desired price
(same units as desired e.g. to allow 5 cents of slippage,
send 50000).
@param payload Signed Redstone oracle payload
"""
ctx: Ctx = self.CONTEXT(base_token, quote_token, desired, slippage, payload)
return self.CORE.close(1, base_token, quote_token, position_id, ctx)
@external
def liquidate(
base_token : address,
quote_token: address,
position_id: uint256,
desired : uint256,
slippage : uint256,
payload : Bytes[224]
) -> PositionValue:
"""
@notice Liquidate a position
@dev This is exactly like close but only the user who opened
a position may close a position, whereas anyone may
liquidate a position iff it is liquidatable
@param base_token Token representing the base coin of the pool (e.g. BTC)
@param quote_token Token representing the quote coin of the pool (e.g. USDT)
@param position_id The ID of the position to close
@param desired Price to provide liquidity at (unit price using onchain
representation for quote_token, e.g. 1.50$ would be
1500000 for USDT with 6 decimals)
@param slippage Acceptable deviaton of oracle price from desired price
(same units as desired e.g. to allow 5 cents of slippage,
send 50000).
@param payload Signed Redstone oracle payload
"""
ctx: Ctx = self.CONTEXT(base_token, quote_token, desired, slippage, payload)
return self.CORE.liquidate(1, base_token, quote_token, position_id, ctx)
# eof