execute.gno
5.09 Kb · 151 lines
1package impl
2
3import (
4 "strconv"
5 "time"
6
7 plimit "gno.land/p/gnoswap/order/limit/v0"
8 "gno.land/r/gnoswap/common"
9 "gno.land/r/gnoswap/order/halt"
10 lo "gno.land/r/gnoswap/order/limit"
11 "gno.land/r/gnoswap/order/whitelist"
12 "gno.land/r/gnoswap/pool"
13)
14
15// ExecuteOrder returns actual input and pool output before the order fee,
16// allowing the pool to stop before maxAmountIn at a nonzero price limit.
17func (l *limitOrderImpl) ExecuteOrder(_ int, rlm realm, orderID uint64, maxAmountIn int64, poolFee uint32, sqrtPriceLimitX96 string) (int64, int64) {
18 prev := l.assertIsCall(0, rlm)
19
20 assertIsNotHalted(halt.OperationExecute, halt.OperationWithdraw)
21 assertIsAuthorizedKeeper(rlm.PkgPath(), prev.Address())
22
23 order, err := l.getOrder(orderID)
24 if err != nil {
25 panic(err.Error())
26 }
27
28 result, err := l.executeOrder(0, rlm, order, maxAmountIn, poolFee, sqrtPriceLimitX96)
29 if err != nil {
30 panic(realmError(err).Error())
31 }
32
33 emitOrderExecution(prev.Address(), prev.PkgPath(), order, poolFee, result)
34
35 return result.actualInput, result.poolOutput
36}
37
38func emitOrderExecution(prevAddr address, prevRealm string, order *plimit.Order, poolFee uint32, result orderExecutionResult) {
39 route := order.TokenIn() + ":" + order.TokenOut() + ":" + strconv.FormatUint(uint64(poolFee), 10)
40 emitOrderFilled(prevAddr, prevRealm, order, route, result.actualInput, result.poolOutput, result.executionFeeAmount)
41
42 if result.executionFeeAmount > 0 || result.forwardedExecutionFeeAmount > 0 || result.pendingExecutionFeeAmount > 0 {
43 emitExecutionFeeCollected(
44 prevAddr,
45 prevRealm,
46 order.ID(),
47 order.TokenOut(),
48 result.executionFeeAmount,
49 result.forwardedExecutionFeeAmount,
50 result.pendingExecutionFeeAmount,
51 )
52 }
53
54 if order.RemainingInput() == 0 {
55 emitOrderCompleted(prevAddr, prevRealm, order)
56 }
57}
58
59type orderExecutionResult struct {
60 actualInput int64
61 poolOutput int64
62 executionFeeAmount int64
63 forwardedExecutionFeeAmount int64
64 pendingExecutionFeeAmount int64
65}
66
67func (l *limitOrderImpl) executeOrder(_ int, rlm realm, order *plimit.Order, maxAmountIn int64, poolFee uint32, sqrtPriceLimitX96 string) (orderExecutionResult, error) {
68 now := time.Now().Unix()
69 if err := validateExecution(order, maxAmountIn, now); err != nil {
70 return orderExecutionResult{}, err
71 }
72
73 poolPath := pool.GetPoolPath(order.TokenIn(), order.TokenOut(), poolFee)
74 if !whitelist.IsWhitelistedPool(whitelist.TargetLimit, poolPath) {
75 return orderExecutionResult{}, makeErrorWithDetails(errPoolNotWhitelisted, poolPath)
76 }
77
78 zeroForOne := order.TokenIn() < order.TokenOut()
79
80 limit, fullInput, err := orderPriceLimit(sqrtPriceLimitX96, zeroForOne)
81 if err != nil {
82 return orderExecutionResult{}, err
83 }
84
85 token0, token1 := order.TokenIn(), order.TokenOut()
86 if !zeroForOne {
87 token0, token1 = token1, token0
88 }
89
90 custody := lo.Address()
91 inputBefore := common.BalanceOf(order.TokenIn(), custody)
92 outputBefore := common.BalanceOf(order.TokenOut(), custody)
93 callback := lo.NewSwapCallback(0, rlm, order.TokenIn(), zeroForOne, maxAmountIn)
94 amount0, amount1 := pool.Swap(
95 cross(rlm), token0, token1, poolFee, custody, zeroForOne,
96 strconv.FormatInt(maxAmountIn, 10), limit, callback,
97 )
98
99 actualInput, poolOutput, err := poolSwapAmounts(amount0, amount1, zeroForOne)
100 if err != nil {
101 return orderExecutionResult{}, err
102 }
103
104 if actualInput > maxAmountIn || (fullInput && actualInput != maxAmountIn) {
105 return orderExecutionResult{}, plimit.ErrInvalidFillAmount
106 }
107
108 return l.settleOrderFill(0, rlm, order, actualInput, poolOutput, inputBefore, outputBefore, now)
109}
110
111func (l *limitOrderImpl) settleOrderFill(_ int, rlm realm, order *plimit.Order, actualInput, poolOutput, inputBefore, outputBefore, now int64) (orderExecutionResult, error) {
112 custody := lo.Address()
113
114 inputAfter := common.BalanceOf(order.TokenIn(), custody)
115 outputAfter := common.BalanceOf(order.TokenOut(), custody)
116
117 if inputBefore < 0 || inputAfter < 0 || inputAfter > inputBefore || inputBefore-inputAfter != actualInput {
118 return orderExecutionResult{}, plimit.ErrInvalidFillAmount
119 }
120
121 if outputBefore < 0 || outputAfter < outputBefore || outputAfter-outputBefore != poolOutput {
122 return orderExecutionResult{}, plimit.ErrInsufficientOutput
123 }
124
125 executionFeeBefore := order.ExecutionFee()
126 if err := order.ApplyFill(actualInput, poolOutput, now); err != nil {
127 return orderExecutionResult{}, err
128 }
129
130 executionFeeAmount := order.ExecutionFee() - executionFeeBefore
131 userOutput := poolOutput - executionFeeAmount
132
133 if order.RemainingInput() == 0 {
134 l.removeOrder(order)
135 }
136
137 // Finalize order state before paying the owner or collecting the execution fee.
138 if userOutput > 0 {
139 common.SafeGRC20Transfer(0, rlm, order.TokenOut(), order.Owner(), userOutput)
140 }
141
142 forwardedExecutionFeeAmount, pendingExecutionFeeAmount := l.collectExecutionFee(0, rlm, order.TokenOut(), executionFeeAmount)
143
144 return orderExecutionResult{
145 actualInput: actualInput,
146 poolOutput: poolOutput,
147 executionFeeAmount: executionFeeAmount,
148 forwardedExecutionFeeAmount: forwardedExecutionFeeAmount,
149 pendingExecutionFeeAmount: pendingExecutionFeeAmount,
150 }, nil
151}