execution_fee.gno
3.07 Kb · 90 lines
1package impl
2
3import (
4 "gno.land/p/gnoswap/gnsmath/v1"
5 prbac "gno.land/p/gnoswap/rbac/v1"
6 bptree "gno.land/p/nt/bptree/v0"
7
8 "gno.land/r/gnoswap/access/v1"
9 "gno.land/r/gnoswap/common"
10 "gno.land/r/gnoswap/halt/v1"
11 orderhalt "gno.land/r/gnoswap/order/halt"
12 pf "gno.land/r/gnoswap/protocol_fee"
13 _ "gno.land/r/gnoswap/protocol_fee/v1"
14)
15
16// SetExecutionFeeRate changes the rate snapshotted by future orders only.
17// Admin or governance only.
18func (l *limitOrderImpl) SetExecutionFeeRate(_ int, rlm realm, executionFeeRate int64) {
19 prev := l.assertIsAdminCall(0, rlm)
20
21 config := l.store.GetConfig()
22
23 previousExecutionFeeRate := config.ExecutionFeeRate()
24 if err := config.SetExecutionFeeRate(executionFeeRate); err != nil {
25 panic(makeErrorWithDetails(errInvalidExecutionFeeRate, err.Error()).Error())
26 }
27
28 emitExecutionFeeRateChanged(prev.Address(), prev.PkgPath(), previousExecutionFeeRate, executionFeeRate)
29}
30
31// CollectPendingExecutionFee forwards pending fees without executing an order.
32// Anyone may call it; funds only move to the protocol fee realm.
33func (l *limitOrderImpl) CollectPendingExecutionFee(_ int, rlm realm, tokenPath string) (int64, int64) {
34 prev := l.assertIsCall(0, rlm)
35
36 assertIsNotHalted(orderhalt.OperationWithdraw)
37
38 forwardedAmount, pendingAmount := l.collectExecutionFee(0, rlm, tokenPath, 0)
39 if forwardedAmount > 0 {
40 emitExecutionFeeCollected(prev.Address(), prev.PkgPath(), 0, tokenPath, 0, forwardedAmount, pendingAmount)
41 }
42
43 return forwardedAmount, pendingAmount
44}
45
46// collectExecutionFee keeps execution fees pending while collection is halted
47// or rejected, and retries on the next fill or explicit collection.
48// It also runs for zero execution-fee fills so they can forward earlier pending fees.
49// It returns the amount forwarded and the amount left pending.
50func (l *limitOrderImpl) collectExecutionFee(_ int, rlm realm, tokenPath string, executionFeeAmount int64) (int64, int64) {
51 pendingExecutionFees := l.store.GetPendingExecutionFees()
52 pendingAmount := getPendingExecutionFeeAmount(pendingExecutionFees, tokenPath)
53
54 totalAmount := gnsmath.SafeAddInt64(pendingAmount, executionFeeAmount)
55 if totalAmount <= 0 {
56 return 0, 0
57 }
58
59 if halt.IsHaltedProtocolFee() {
60 pendingExecutionFees.Set(tokenPath, totalAmount)
61 return 0, totalAmount
62 }
63
64 // Remove the pending debit before external calls so re-entry cannot collect
65 // it twice. A rejected collection restores it; an abort rolls back the call.
66 if pendingAmount > 0 {
67 pendingExecutionFees.Remove(tokenPath)
68 }
69
70 protocolFeeAddress := access.MustGetAddress(prbac.ROLE_PROTOCOL_FEE.String())
71 common.SafeGRC20Approve(0, rlm, tokenPath, protocolFeeAddress, totalAmount)
72 err := pf.AddToProtocolFee(cross(rlm), tokenPath, totalAmount)
73 common.SafeGRC20Approve(0, rlm, tokenPath, protocolFeeAddress, 0)
74
75 if err != nil {
76 pendingExecutionFees.Set(tokenPath, totalAmount)
77 return 0, totalAmount
78 }
79
80 return totalAmount, 0
81}
82
83func getPendingExecutionFeeAmount(pendingExecutionFees *bptree.BPTree, tokenPath string) int64 {
84 value := pendingExecutionFees.Get(tokenPath)
85 if value == nil {
86 return 0
87 }
88
89 return value.(int64)
90}