Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

execution_fee.gno

5.16 Kb · 144 lines
  1package impl
  2
  3import (
  4	"gno.land/p/gnoswap/gnsmath/v1"
  5	pdca "gno.land/p/gnoswap/order/dca/v0"
  6	prbac "gno.land/p/gnoswap/rbac/v1"
  7	bptree "gno.land/p/nt/bptree/v0"
  8	ufmt "gno.land/p/nt/ufmt/v0"
  9
 10	"gno.land/r/gnoswap/access/v1"
 11	"gno.land/r/gnoswap/common"
 12	"gno.land/r/gnoswap/order/halt"
 13
 14	pf "gno.land/r/gnoswap/protocol_fee"
 15	_ "gno.land/r/gnoswap/protocol_fee/v1"
 16)
 17
 18// SetExecutionFeeRate sets the execution fee rate, in basis points,
 19// snapshotted by orders created afterwards.
 20// Existing orders keep the rate they were created with.
 21// Admin or governance only.
 22func (d *dcaOrderImpl) SetExecutionFeeRate(_ int, rlm realm, executionFeeRate int64) {
 23	access.AssertIsRlmCurrent(0, rlm)
 24	common.AssertIsNotHandleNativeCoin()
 25	d.assertIsNotExecuting()
 26
 27	prev := rlm.Previous()
 28	access.AssertIsAdminOrGovernance(prev.Address())
 29
 30	config := d.store.GetConfig()
 31	previousExecutionFeeRate := config.ExecutionFeeRate()
 32
 33	if err := config.SetExecutionFeeRate(executionFeeRate); err != nil {
 34		panic(makeErrorWithDetails(
 35			errInvalidExecutionFeeRate,
 36			ufmt.Sprintf("executionFeeRate(%d) must be in [0, %d]", executionFeeRate, pdca.MaxExecutionFeeRate),
 37		).Error())
 38	}
 39
 40	emitExecutionFeeRateChanged(prev.Address(), prev.PkgPath(), previousExecutionFeeRate, executionFeeRate)
 41}
 42
 43// CollectPendingExecutionFee forwards the execution fee pending for
 44// tokenPath to the protocol fee realm. Payouts keep their execution fee as
 45// pending and rounds forward the pending fee of their output token; this
 46// forwards it when no round of that output token runs.
 47// Anyone may call it: the fee only moves to the protocol fee realm.
 48// It returns the amount forwarded and the amount left pending.
 49func (d *dcaOrderImpl) CollectPendingExecutionFee(_ int, rlm realm, tokenPath string) (int64, int64) {
 50	access.AssertIsRlmCurrent(0, rlm)
 51	common.AssertIsNotHandleNativeCoin()
 52	assertIsNotHalted(halt.OperationWithdraw)
 53	d.assertIsNotExecuting()
 54
 55	prev := rlm.Previous()
 56
 57	// The execution lock blocks re-entry while the protocol fee realm pulls
 58	// the fee, as it does during a round.
 59	if err := d.store.SetExecuting(0, rlm, true); err != nil {
 60		panic(err.Error())
 61	}
 62
 63	forwardedAmount, pendingAmount := d.collectExecutionFee(0, rlm, tokenPath, 0)
 64
 65	if err := d.store.SetExecuting(0, rlm, false); err != nil {
 66		panic(err.Error())
 67	}
 68
 69	if forwardedAmount > 0 || pendingAmount > 0 {
 70		emitExecutionFeeCollected(prev.Address(), prev.PkgPath(), "", tokenPath, 0, 0, forwardedAmount, pendingAmount)
 71	}
 72
 73	return forwardedAmount, pendingAmount
 74}
 75
 76// addPendingExecutionFee holds a payout's execution fee for tokenPath
 77// until a round or CollectPendingExecutionFee forwards it.
 78// A payout never calls the protocol fee realm,
 79// so that realm cannot block withdrawals.
 80func (d *dcaOrderImpl) addPendingExecutionFee(tokenPath string, executionFeeAmount int64) {
 81	if executionFeeAmount <= 0 {
 82		return
 83	}
 84
 85	pendingExecutionFees := d.store.GetPendingExecutionFees()
 86	pendingAmount := getPendingExecutionFeeAmount(pendingExecutionFees, tokenPath)
 87
 88	pendingExecutionFees.Set(tokenPath, gnsmath.SafeAddInt64(pendingAmount, executionFeeAmount))
 89}
 90
 91// pendingExecutionFeeAmount returns the execution fee of tokenPath
 92// held by this realm, 0 if none.
 93func (d *dcaOrderImpl) pendingExecutionFeeAmount(tokenPath string) int64 {
 94	return getPendingExecutionFeeAmount(d.store.GetPendingExecutionFees(), tokenPath)
 95}
 96
 97// collectExecutionFee forwards executionFeeAmount, plus anything already
 98// pending for tokenPath, to the protocol fee realm.
 99// When collection is halted or the protocol fee realm rejects it,
100// the total is held as pending and forwarded by a later collection
101// of the same token, so a halt never blocks execution.
102// It returns the amount forwarded and the amount left pending.
103func (d *dcaOrderImpl) collectExecutionFee(_ int, rlm realm, tokenPath string, executionFeeAmount int64) (int64, int64) {
104	pendingExecutionFees := d.store.GetPendingExecutionFees()
105
106	pendingAmount := getPendingExecutionFeeAmount(pendingExecutionFees, tokenPath)
107
108	totalAmount := gnsmath.SafeAddInt64(pendingAmount, executionFeeAmount)
109	if totalAmount <= 0 {
110		return 0, 0
111	}
112
113	protocolFeeAddress := access.MustGetAddress(prbac.ROLE_PROTOCOL_FEE.String())
114	common.SafeGRC20Approve(0, rlm, tokenPath, protocolFeeAddress, totalAmount)
115
116	// AddToProtocolFee pulls the approved amount from this realm. It returns
117	// an error while its collection is halted; nothing was pulled then:
118	// withdraw the approval and keep it pending.
119	if err := pf.AddToProtocolFee(cross(rlm), tokenPath, totalAmount); err != nil {
120		common.SafeGRC20Approve(0, rlm, tokenPath, protocolFeeAddress, 0)
121		pendingExecutionFees.Set(tokenPath, totalAmount)
122
123		return 0, totalAmount
124	}
125
126	// Keep the token's entry at zero rather than removing it: payouts add to
127	// it again, and recreating it would charge the next payout its storage.
128	if pendingAmount > 0 {
129		pendingExecutionFees.Set(tokenPath, int64(0))
130	}
131
132	return totalAmount, 0
133}
134
135// getPendingExecutionFeeAmount returns the execution fee of tokenPath
136// held by this realm, 0 if none.
137func getPendingExecutionFeeAmount(pendingExecutionFees *bptree.BPTree, tokenPath string) int64 {
138	value := pendingExecutionFees.Get(tokenPath)
139	if value == nil {
140		return 0
141	}
142
143	return value.(int64)
144}