proxy.gno
13.26 Kb · 267 lines
1package dca
2
3// CreateOrder creates an order that sells amountPerExecution of inputToken
4// for outputToken every interval seconds, executionCount times,
5// and returns its ID.
6//
7// The caller must first approve Address()
8// for amountPerExecution*executionCount.
9// amountPerExecution must be at least GetMinAmountPerExecution(inputToken),
10// and the pair direction needs a price limit (see SetPairPriceLimit).
11// The order snapshots the execution fee rate in effect (see
12// GetExecutionFeeRate) for its lifetime: the fee is that share of the order's
13// output, deducted when the output is paid out. Later rate changes apply only
14// to orders created afterwards.
15// Orders with the same inputToken, outputToken, interval, and slotOffset
16// share one group and execute together. Each round trades on the pair's
17// best whitelisted pool, chosen at execution time (see GetExecutionQuote).
18//
19// The caller also pays the storage deposit of the order's execution logs,
20// one per three rounds; each is refunded to whoever releases it after all of
21// its rounds execute (see ClaimGroupOrders).
22//
23// Parameters:
24// - inputToken, outputToken: at least one whitelisted direct pool must
25// connect them (see gno.land/r/gnoswap/order/whitelist.AddWhitelistedPool).
26// - interval: one of GetConfig().AllowedIntervals().
27// - slotOffset: seconds after each interval boundary of the unix epoch at
28// which rounds run, a multiple of GetConfig().SlotStep(interval)
29// in [0, interval). Boundaries are UTC midnights for daily intervals;
30// weekly boundaries fall on Thursdays 00:00 UTC (1970-01-01 was one),
31// so a Monday slot is 4 days.
32// The first round waits for the next slot, or runs now if it is due.
33//
34// Reverts while the execute operation is halted for DCA
35// in gno.land/r/gnoswap/order/halt.
36func CreateOrder(
37 cur realm,
38 inputToken string,
39 outputToken string,
40 interval int64,
41 slotOffset int64,
42 amountPerExecution int64,
43 executionCount int64,
44) uint64 {
45 return getImplementation().CreateOrder(0, cur, inputToken, outputToken, interval, slotOffset, amountPerExecution, executionCount)
46}
47
48// CancelOrder pays accrued output, refunds unsold input, and closes the order.
49// It returns the refunded input and the output paid by this call.
50//
51// Owner only. Reverts while the withdraw operation is halted.
52func CancelOrder(cur realm, orderID uint64) (int64, int64) {
53 return getImplementation().CancelOrder(0, cur, orderID)
54}
55
56// Claim pays accrued output to the order owner and returns the amount.
57// Anyone may call it, e.g. a keeper delivering output;
58// proceeds always go to the owner.
59// The order's execution logs whose rounds all executed are released,
60// and their storage deposit is refunded to the caller.
61//
62// Reverts while the withdraw operation is halted.
63func Claim(cur realm, orderID uint64) int64 {
64 return getImplementation().Claim(0, cur, orderID)
65}
66
67// ExecuteGroup runs the next round of a group once it is due.
68// Only accounts holding the DCA keeper role in gno.land/r/gnoswap/order/keeper
69// may call it: the ExecuteGroup selector is assigned to DCAKeeperRole at
70// deployment, and admin or governance grants the role or reassigns it.
71// It returns the round input and the output credited to the orders,
72// before their execution fees, which each order pays when paid out.
73//
74// Parameters:
75// - groupID: see GetExecutableGroups.
76// - amountOutMin: minimum pool output, before the execution fee;
77// 0 disables it.
78// - expectedAmountIn: the group orderAmount amountOutMin was quoted for.
79// The round reverts if the orderAmount differs;
80// 0 is accepted only with amountOutMin 0.
81// - minOrderAmountOut: the least output every order of the round must be
82// credited after its own execution fee rate
83// (see GetExecutionQuote), in the output token's smallest unit; 0 means 1.
84// The round reverts when its smallest order would receive less.
85//
86// Pool selection: among the pair's whitelisted pools (one per fee tier),
87// the pool with the best quote for the full round is used.
88// The round swaps on that pool directly through gno.land/r/gnoswap/pool,
89// without the router, so no router fee is charged.
90// The swap's price limit comes from the pair price limit
91// (see SetPairPriceLimit), so no unit of input is sold below it,
92// whatever amountOutMin is.
93// The round reverts when no whitelisted pool can fill it within the limit.
94//
95// Reverts while the execute or withdraw operation is halted.
96func ExecuteGroup(cur realm, groupID string, expectedAmountIn int64, amountOutMin int64, minOrderAmountOut int64) (int64, int64) {
97 return getImplementation().ExecuteGroup(0, cur, groupID, expectedAmountIn, amountOutMin, minOrderAmountOut)
98}
99
100// ClaimGroupOrders pays accrued output to the owners of a group's active
101// orders, as Claim does, so owners receive output without claiming.
102// Anyone may call it; output always goes to the order owner.
103//
104// One call examines at most min(maxOrderCount, maxClaimBatchSize) orders
105// after startAfterOrderID, in ID order. It returns the number of orders paid
106// or completed and the cursor for the next call; repeat until the cursor is 0.
107//
108// Each order's execution logs whose rounds all executed are released and finished
109// orders are deleted; the freed storage deposit is refunded to the caller,
110// so the reward grows with the orders and rounds claimed.
111//
112// Reverts while the withdraw operation is halted.
113func ClaimGroupOrders(cur realm, groupID string, startAfterOrderID uint64, maxOrderCount int) (int, uint64) {
114 return getImplementation().ClaimGroupOrders(0, cur, groupID, startAfterOrderID, maxOrderCount)
115}
116
117// ForceCancelOrder closes an order that blocks its group's rounds. As with
118// CancelOrder, accrued output and unsold input go to the order owner; the
119// caller receives nothing but the freed storage deposit of the order's
120// execution logs. It returns the refunded input and the output paid.
121//
122// The order must have rounds left and be, as checked on-chain:
123// - below the minimum per execution a new order of its token needs now
124// (reason below_min_amount), or
125// - in a due round that, as quoted now, credits it less than one output
126// unit after its execution fee (no_order_output), or
127// - the largest order (by amountPerExecution, ties included) of a due round
128// that no pool can fill as quoted now while a pool fills part of it,
129// with some output, within the price limit (exceeds_pool_capacity). A round that needs
130// several orders gone is shrunk one largest order per call.
131//
132// Any other order reverts with "order does not block its group".
133// DCAOrderForceCancelled reports the reason.
134//
135// Only accounts holding the role assigned to the ForceCancelOrder selector in
136// gno.land/r/gnoswap/order/keeper (DCAKeeperRole at deployment).
137// Reverts while the withdraw operation is halted.
138func ForceCancelOrder(cur realm, orderID uint64) (int64, int64) {
139 return getImplementation().ForceCancelOrder(0, cur, orderID)
140}
141
142// CollectPendingExecutionFee forwards the execution fee pending for
143// tokenPath (see GetPendingExecutionFeeAmount) to the protocol fee realm and
144// returns the amount forwarded and the amount left pending.
145// Payouts keep each order's execution fee as pending, and every round
146// forwards the pending fee of its output token; this forwards it when no
147// round of that output token runs. If the protocol fee realm rejects it,
148// for example while its collection is halted, the fee stays pending.
149// Anyone may call it: the fee only moves to the protocol fee realm.
150//
151// Reverts while the withdraw operation is halted.
152func CollectPendingExecutionFee(cur realm, tokenPath string) (int64, int64) {
153 return getImplementation().CollectPendingExecutionFee(0, cur, tokenPath)
154}
155
156// SetExecutionFeeRate sets the share of an order's output,
157// in basis points (0 to 1000, at most 10%),
158// sent to the protocol fee realm.
159// Orders created afterwards snapshot it; existing orders keep their rate.
160//
161// Admin or governance only.
162func SetExecutionFeeRate(cur realm, executionFeeRate int64) {
163 getImplementation().SetExecutionFeeRate(0, cur, executionFeeRate)
164}
165
166// SetOrderLimits sets the minimum input per execution
167// and the maximum executions per order. Admin or governance only.
168func SetOrderLimits(cur realm, minAmountPerExecution int64, maxExecutionCount int64) {
169 getImplementation().SetOrderLimits(0, cur, minAmountPerExecution, maxExecutionCount)
170}
171
172// AddAllowedInterval allows new orders to use interval, in seconds
173// (one minute to one year). The interval starts with a single slot,
174// offset 0 (see SetSlotStep). Admin or governance only.
175func AddAllowedInterval(cur realm, interval int64) {
176 getImplementation().AddAllowedInterval(0, cur, interval)
177}
178
179// RemoveAllowedInterval stops new orders from using interval.
180// Existing orders and groups of that interval keep running until they finish;
181// its slot step is dropped, and re-adding it starts from a single slot.
182// At least one interval must stay allowed. Admin or governance only.
183func RemoveAllowedInterval(cur realm, interval int64) {
184 getImplementation().RemoveAllowedInterval(0, cur, interval)
185}
186
187// SetMinExecutionGapRate sets the minimum gap between two executions of a
188// group, in basis points of its interval (0 to 10000), so a late execution is
189// not followed right away by the next one. Admin or governance only.
190func SetMinExecutionGapRate(cur realm, minExecutionGapRate int64) {
191 getImplementation().SetMinExecutionGapRate(0, cur, minExecutionGapRate)
192}
193
194// SetMaxClaimBatchSize caps the orders one ClaimGroupOrders call examines,
195// bounding its gas. Admin or governance only.
196func SetMaxClaimBatchSize(cur realm, maxClaimBatchSize int64) {
197 getImplementation().SetMaxClaimBatchSize(0, cur, maxClaimBatchSize)
198}
199
200// SetMaxPoolPriceDeviationRate sets how far, in basis points of price, a
201// pool's price may sit on the favorable side of its swap price limit (see
202// SetPairPriceLimit) and still be quoted, 0 to 10000; 0 disables the check.
203// A pool further away is ineligible without a quote: its quote or swap would
204// walk every tick between its price and the limit, which an empty or
205// manipulated pool far from the oracle price makes unbounded.
206// v0 starts at 2000 (20%). Admin or governance only.
207func SetMaxPoolPriceDeviationRate(cur realm, maxPoolPriceDeviationRate int64) {
208 getImplementation().SetMaxPoolPriceDeviationRate(0, cur, maxPoolPriceDeviationRate)
209}
210
211// SetMaxPairPriceLimitAge sets how long, in seconds, a pair price limit stays
212// usable after the pool manager set it (see SetPairPriceLimit), 60 to 604800.
213// Under an older limit every pool is ineligible (code price_limit_stale), so
214// rounds stop until the limit is refreshed instead of trading at a stale price.
215// v0 starts at 3600 (one hour). Admin or governance only.
216func SetMaxPairPriceLimitAge(cur realm, maxPairPriceLimitAge int64) {
217 getImplementation().SetMaxPairPriceLimitAge(0, cur, maxPairPriceLimitAge)
218}
219
220// SetSlotStep sets the spacing, in seconds, of the slot offsets new orders
221// may use at interval: slotStep must divide the interval, and the interval
222// then has interval/slotStep slots. slotStep == interval allows offset 0 only.
223//
224// Existing groups keep their slot, even one the new step no longer allows;
225// only new orders are checked. Finer steps split each pair's orders into
226// more groups, so each round sells less in more swaps.
227//
228// Admin or governance only.
229func SetSlotStep(cur realm, interval int64, slotStep int64) {
230 getImplementation().SetSlotStep(0, cur, interval, slotStep)
231}
232
233// SetTokenMinAmountPerExecution sets the minimum input per execution of new
234// orders selling tokenPath, in raw token units, applied on top of the global
235// minimum (see SetOrderLimits); 0 removes it.
236// The pool manager derives it from an oracle price, so every order is worth a
237// set value per round whatever the token's decimals.
238// Existing orders are unaffected.
239//
240// Admin, governance, or accounts holding the role assigned to the
241// SetTokenMinAmountPerExecution selector in gno.land/r/gnoswap/order/keeper
242// (PoolManagerRole at deployment).
243func SetTokenMinAmountPerExecution(cur realm, tokenPath string, minAmountPerExecution int64) {
244 getImplementation().SetTokenMinAmountPerExecution(0, cur, tokenPath, minAmountPerExecution)
245}
246
247// SetPairPriceLimit sets the least pool output, before the execution fee,
248// that rounds selling inputToken for outputToken accept per unit of input:
249// minOutputPerInputX128 is raw outputToken units per raw inputToken unit,
250// in Q128 fixed point, as a decimal string. "0" removes the limit.
251//
252// Every quote and round of the pair direction passes the limit to the pool
253// as its swap price limit, adjusted for the pool's fee tier, so each unit of
254// input fills at a rate of at least the limit and a swap crosses only the
255// ticks up to it. A pool whose price is past the limit is skipped, and a
256// round that cannot be filled within it reverts.
257// A pair direction without a limit cannot be quoted, executed,
258// or receive new orders.
259// The limit applies from the next quote or round, to existing orders too.
260// Its update time is recorded but not checked on-chain.
261//
262// Admin, governance, or accounts holding the role assigned to the
263// SetPairPriceLimit selector in gno.land/r/gnoswap/order/keeper
264// (PoolManagerRole at deployment).
265func SetPairPriceLimit(cur realm, inputToken string, outputToken string, minOutputPerInputX128 string) {
266 getImplementation().SetPairPriceLimit(0, cur, inputToken, outputToken, minOutputPerInputX128)
267}