claim_group_orders.gno
4.44 Kb · 143 lines
1package impl
2
3import (
4 "gno.land/p/gnoswap/gnsmath/v1"
5 pdca "gno.land/p/gnoswap/order/dca/v0"
6 ufmt "gno.land/p/nt/ufmt/v0"
7
8 "gno.land/r/gnoswap/access/v1"
9 "gno.land/r/gnoswap/common"
10 "gno.land/r/gnoswap/order/halt"
11)
12
13// ClaimGroupOrders pays accrued output to the owners of a group's active
14// orders, like Claim, so owners receive output without claiming.
15// Anyone may call it; output always goes to the order owner.
16//
17// The call releases each examined order's execution logs whose rounds all executed
18// and deletes finished orders. The freed storage deposit is refunded to the
19// caller, a fixed reward per log of three executed rounds.
20//
21// One call examines at most min(maxOrderCount, maxClaimBatchSize) orders
22// after startAfterOrderID, in ID order, so its gas stays bounded.
23// The returned cursor is the last examined order ID; pass it back until it
24// is 0. New orders get higher IDs and finished orders leave the index,
25// so changes between calls neither skip nor repeat an order.
26func (d *dcaOrderImpl) ClaimGroupOrders(_ int, rlm realm, groupID string, startAfterOrderID uint64, maxOrderCount int) (int, uint64) {
27 access.AssertIsRlmCurrent(0, rlm)
28 common.AssertIsNotHandleNativeCoin()
29 assertIsNotHalted(halt.OperationWithdraw)
30 d.assertIsNotExecuting()
31
32 if maxOrderCount <= 0 {
33 panic(makeErrorWithDetails(errInvalidAmount, ufmt.Sprintf("maxOrderCount(%d) must be positive", maxOrderCount)).Error())
34 }
35
36 group, err := d.getGroup(groupID)
37 if err != nil {
38 panic(err.Error())
39 }
40
41 prev := rlm.Previous()
42
43 result, err := d.claimGroupOrders(0, rlm, prev.Address(), prev.PkgPath(), group, startAfterOrderID, maxOrderCount)
44 if err != nil {
45 panic(err.Error())
46 }
47
48 emitGroupOrdersClaimed(
49 prev.Address(),
50 prev.PkgPath(),
51 groupID,
52 result.examinedOrderCount,
53 result.claimedOrderCount,
54 result.releasedExecutionLogCount,
55 result.nextCursor,
56 )
57
58 emitPayoutExecutionFee(prev.Address(), prev.PkgPath(), group, result.executionFeeAmount, d.pendingExecutionFeeAmount(group.OutputToken()))
59
60 return result.claimedOrderCount, result.nextCursor
61}
62
63// groupOrdersClaimResult describes one page of claimGroupOrders.
64type groupOrdersClaimResult struct {
65 examinedOrderCount int
66 claimedOrderCount int // orders paid or completed
67 releasedExecutionLogCount int64 // order logs freed for the caller
68 executionFeeAmount int64 // execution fee the payouts kept as pending
69 nextCursor uint64 // last examined order ID, 0 when the group is done
70}
71
72// claimGroupOrders claims one page of the group's orders.
73func (d *dcaOrderImpl) claimGroupOrders(
74 _ int,
75 rlm realm,
76 prevAddr address,
77 prevRealm string,
78 group *pdca.Group,
79 startAfterOrderID uint64,
80 maxOrderCount int,
81) (groupOrdersClaimResult, error) {
82 limit := int64(maxOrderCount)
83 if maxClaimBatchSize := d.store.GetConfig().MaxClaimBatchSize(); limit > maxClaimBatchSize {
84 limit = maxClaimBatchSize
85 }
86
87 orderIDs, hasMore := d.getGroupOrderIDs(group.ID(), startAfterOrderID, int(limit))
88
89 result := groupOrdersClaimResult{examinedOrderCount: len(orderIDs)}
90 for _, orderID := range orderIDs {
91 order, err := d.getOrder(orderID)
92 if err != nil {
93 return groupOrdersClaimResult{}, err
94 }
95
96 payout, releasedCount, isUpdated, err := d.claimOrder(0, rlm, prevAddr, prevRealm, order, group)
97 if err != nil {
98 return groupOrdersClaimResult{}, err
99 }
100
101 result.executionFeeAmount = gnsmath.SafeAddInt64(result.executionFeeAmount, payout.executionFeeAmount)
102
103 result.releasedExecutionLogCount = gnsmath.SafeAddInt64(result.releasedExecutionLogCount, releasedCount)
104
105 if isUpdated {
106 result.claimedOrderCount++
107 }
108 }
109
110 if hasMore {
111 result.nextCursor = orderIDs[len(orderIDs)-1]
112 }
113
114 return result, nil
115}
116
117// getGroupOrderIDs returns up to count active order IDs of a group after
118// startAfterID, and whether more remain. IDs are collected before any
119// order is claimed, since a claim may remove entries from the index.
120func (d *dcaOrderImpl) getGroupOrderIDs(groupID string, startAfterID uint64, count int) ([]uint64, bool) {
121 if startAfterID == ^uint64(0) {
122 return nil, false
123 }
124
125 prefix := pdca.GroupOrderPrefix(groupID)
126 start := pdca.GroupOrderKey(groupID, gnsmath.SafeAddUint64(startAfterID, 1))
127
128 orderIDs := []uint64{}
129 hasMore := false
130
131 d.store.GetOrdersByGroup().Iterate(start, pdca.PrefixEnd(prefix), func(_ string, value any) bool {
132 if len(orderIDs) == count {
133 hasMore = true
134 return true
135 }
136
137 orderIDs = append(orderIDs, value.(uint64))
138
139 return false
140 })
141
142 return orderIDs, hasMore
143}