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

emits.gno

11.81 Kb · 346 lines
  1package impl
  2
  3import (
  4	"chain"
  5
  6	pdca "gno.land/p/gnoswap/order/dca/v0"
  7	"gno.land/p/gnoswap/utils/v1"
  8)
  9
 10// Events carry the accumulator state
 11// (accumulatedOutputPerAmountX128, round, orderAmount) and each order's
 12// payout state, so indexers can compute claimable amounts without querying
 13// state. The accumulator is gross of execution fees:
 14//
 15//	gross = amountPerExecution * (acc(min(round, endRound)) - debtX128) / 2^128
 16//	paid  = totalClaimedAmount + totalExecutionFeeAmount
 17//	fee   = floor((paid + gross) * executionFeeRate / 10000) - floor(paid * executionFeeRate / 10000)
 18//	claimable = gross - fee
 19
 20func emitGroupCreated(prevAddr address, prevRealm string, group *pdca.Group) {
 21	chain.Emit(
 22		"DCAGroupCreated",
 23		"prevAddr", prevAddr.String(),
 24		"prevRealm", prevRealm,
 25		"groupId", group.ID(),
 26		"inputToken", group.InputToken(),
 27		"outputToken", group.OutputToken(),
 28		"interval", utils.FormatInt(group.Interval()),
 29		"slotOffset", utils.FormatInt(group.SlotOffset()),
 30		"nextExecutionTimestamp", utils.FormatInt(group.NextExecutionTimestamp()),
 31	)
 32}
 33
 34// emitOrderCreated reports a new order. executionFeeRate is the rate the
 35// order snapshotted. referenceAmountOut is the output of selling the whole
 36// deposit at once when the order was placed, net of the order's execution
 37// fee (see quoteReferenceAmountOut), 0 when no pool could absorb it.
 38func emitOrderCreated(
 39	prevAddr address,
 40	prevRealm string,
 41	order *pdca.Order,
 42	group *pdca.Group,
 43	depositAmount int64,
 44	referenceAmountOut int64,
 45) {
 46	chain.Emit(
 47		"DCAOrderCreated",
 48		"prevAddr", prevAddr.String(),
 49		"prevRealm", prevRealm,
 50		"orderId", utils.FormatUint(order.ID()),
 51		"owner", order.Owner().String(),
 52		"groupId", order.GroupID(),
 53		"inputToken", group.InputToken(),
 54		"outputToken", group.OutputToken(),
 55		"interval", utils.FormatInt(group.Interval()),
 56		"slotOffset", utils.FormatInt(group.SlotOffset()),
 57		"amountPerExecution", utils.FormatInt(order.AmountPerExecution()),
 58		"executionCount", utils.FormatInt(order.ExecutionCount()),
 59		"executionFeeRate", utils.FormatInt(order.ExecutionFeeRate()),
 60		"depositAmount", utils.FormatInt(depositAmount),
 61		"startRound", utils.FormatUint(order.StartRound()),
 62		"endRound", utils.FormatUint(order.EndRound()),
 63		"debtX128", order.DebtX128().ToString(),
 64		"executionLogCount", utils.FormatInt(order.ExecutionLogCount()),
 65		"groupOrderAmount", utils.FormatInt(group.OrderAmount()),
 66		"groupNextExecutionTimestamp", utils.FormatInt(group.NextExecutionTimestamp()),
 67		"referenceAmountOut", utils.FormatInt(referenceAmountOut),
 68	)
 69}
 70
 71// emitGroupExecuted reports a round. executedAmountOut, the output credited
 72// to the orders, equals swapAmountOut: orders pay their execution fees when
 73// paid out.
 74func emitGroupExecuted(prevAddr address, prevRealm string, group *pdca.Group, result groupRoundResult) {
 75	chain.Emit(
 76		"DCAGroupExecuted",
 77		"prevAddr", prevAddr.String(),
 78		"prevRealm", prevRealm,
 79		"groupId", group.ID(),
 80		"poolPath", result.poolPath,
 81		"inputToken", group.InputToken(),
 82		"outputToken", group.OutputToken(),
 83		"round", utils.FormatUint(group.Round()),
 84		"executedAmountIn", utils.FormatInt(result.executedAmountIn),
 85		"swapAmountOut", utils.FormatInt(result.swapAmountOut),
 86		"executedAmountOut", utils.FormatInt(result.executedAmountOut),
 87		"accumulatedOutputPerAmountX128", group.AccumulatedOutputPerAmountX128().ToString(),
 88		"endedOrderAmount", utils.FormatInt(result.endedOrderAmount),
 89		"nextOrderAmount", utils.FormatInt(group.OrderAmount()),
 90		"scheduledTimestamp", utils.FormatInt(result.scheduledTimestamp),
 91		"executionTimestamp", utils.FormatInt(result.executionTimestamp),
 92		"nextExecutionTimestamp", utils.FormatInt(group.NextExecutionTimestamp()),
 93	)
 94}
 95
 96func emitGroupOrdersClaimed(
 97	prevAddr address,
 98	prevRealm string,
 99	groupID string,
100	examinedOrderCount int,
101	claimedOrderCount int,
102	releasedExecutionLogCount int64,
103	nextCursor uint64,
104) {
105	chain.Emit(
106		"DCAGroupOrdersClaimed",
107		"prevAddr", prevAddr.String(),
108		"prevRealm", prevRealm,
109		"groupId", groupID,
110		"examinedOrderCount", utils.FormatInt(int64(examinedOrderCount)),
111		"claimedOrderCount", utils.FormatInt(int64(claimedOrderCount)),
112		"releasedExecutionLogCount", utils.FormatInt(releasedExecutionLogCount),
113		"nextCursor", utils.FormatUint(nextCursor),
114	)
115}
116
117// emitOrderClaimed reports a payout. grossAmount is the accrued output paid
118// out, executionFeeAmount the order's fee on it, and claimedAmount the rest,
119// sent to the owner; the totals are the order's lifetime values.
120func emitOrderClaimed(
121	prevAddr address,
122	prevRealm string,
123	order *pdca.Order,
124	group *pdca.Group,
125	payout orderPayout,
126	status pdca.OrderStatus,
127) {
128	chain.Emit(
129		"DCAOrderClaimed",
130		"prevAddr", prevAddr.String(),
131		"prevRealm", prevRealm,
132		"orderId", utils.FormatUint(order.ID()),
133		"owner", order.Owner().String(),
134		"groupId", order.GroupID(),
135		"outputToken", group.OutputToken(),
136		"grossAmount", utils.FormatInt(payout.grossAmount),
137		"executionFeeRate", utils.FormatInt(order.ExecutionFeeRate()),
138		"executionFeeAmount", utils.FormatInt(payout.executionFeeAmount),
139		"claimedAmount", utils.FormatInt(payout.claimedAmount),
140		"totalClaimedAmount", order.ClaimedAmount().ToString(),
141		"totalExecutionFeeAmount", order.ExecutionFeeAmount().ToString(),
142		"debtX128", order.DebtX128().ToString(),
143		"status", status.String(),
144	)
145}
146
147func emitOrderCancelled(
148	prevAddr address,
149	prevRealm string,
150	order *pdca.Order,
151	group *pdca.Group,
152	payout orderPayout,
153	refundedAmount int64,
154) {
155	chain.Emit(
156		"DCAOrderCancelled",
157		"prevAddr", prevAddr.String(),
158		"prevRealm", prevRealm,
159		"orderId", utils.FormatUint(order.ID()),
160		"owner", order.Owner().String(),
161		"groupId", order.GroupID(),
162		"inputToken", group.InputToken(),
163		"outputToken", group.OutputToken(),
164		"refundedAmount", utils.FormatInt(refundedAmount),
165		"grossAmount", utils.FormatInt(payout.grossAmount),
166		"executionFeeRate", utils.FormatInt(order.ExecutionFeeRate()),
167		"executionFeeAmount", utils.FormatInt(payout.executionFeeAmount),
168		"claimedAmount", utils.FormatInt(payout.claimedAmount),
169		"totalClaimedAmount", order.ClaimedAmount().ToString(),
170		"totalExecutionFeeAmount", order.ExecutionFeeAmount().ToString(),
171		"debtX128", order.DebtX128().ToString(),
172		"status", pdca.OrderStatusCancelled.String(),
173		"cancelRound", utils.FormatUint(group.Round()),
174		"groupOrderAmount", utils.FormatInt(group.OrderAmount()),
175	)
176}
177
178// emitPayoutExecutionFee reports the execution fee a payout of the group's
179// orders charged and kept as pending, if any.
180func emitPayoutExecutionFee(prevAddr address, prevRealm string, group *pdca.Group, executionFeeAmount int64, pendingAmount int64) {
181	if executionFeeAmount <= 0 {
182		return
183	}
184
185	emitExecutionFeeCollected(prevAddr, prevRealm, group.ID(), group.OutputToken(), group.Round(), executionFeeAmount, 0, pendingAmount)
186}
187
188// emitExecutionFeeCollected reports execution fees of tokenPath.
189// executionFeeAmount is the fee payouts charged (0 for a round or
190// CollectPendingExecutionFee), forwardedAmount what was sent to the protocol
191// fee realm, and pendingAmount what this realm still holds.
192// groupId is empty and round 0 for CollectPendingExecutionFee.
193func emitExecutionFeeCollected(
194	prevAddr address,
195	prevRealm string,
196	groupID string,
197	tokenPath string,
198	round uint64,
199	executionFeeAmount int64,
200	forwardedAmount int64,
201	pendingAmount int64,
202) {
203	chain.Emit(
204		"DCAExecutionFeeCollected",
205		"prevAddr", prevAddr.String(),
206		"prevRealm", prevRealm,
207		"groupId", groupID,
208		"tokenPath", tokenPath,
209		"round", utils.FormatUint(round),
210		"executionFeeAmount", utils.FormatInt(executionFeeAmount),
211		"forwardedAmount", utils.FormatInt(forwardedAmount),
212		"pendingAmount", utils.FormatInt(pendingAmount),
213	)
214}
215
216func emitExecutionFeeRateChanged(prevAddr address, prevRealm string, previousExecutionFeeRate int64, executionFeeRate int64) {
217	chain.Emit(
218		"DCAExecutionFeeRateChanged",
219		"prevAddr", prevAddr.String(),
220		"prevRealm", prevRealm,
221		"previousExecutionFeeRate", utils.FormatInt(previousExecutionFeeRate),
222		"executionFeeRate", utils.FormatInt(executionFeeRate),
223	)
224}
225
226func emitSlotStepChanged(prevAddr address, prevRealm string, interval int64, previousSlotStep int64, slotStep int64) {
227	chain.Emit(
228		"DCASlotStepChanged",
229		"prevAddr", prevAddr.String(),
230		"prevRealm", prevRealm,
231		"interval", utils.FormatInt(interval),
232		"previousSlotStep", utils.FormatInt(previousSlotStep),
233		"slotStep", utils.FormatInt(slotStep),
234	)
235}
236
237func emitExecutionLogsReleased(prevAddr address, prevRealm string, order *pdca.Order, group *pdca.Group, releasedExecutionLogCount int64) {
238	chain.Emit(
239		"DCAExecutionLogsReleased",
240		"prevAddr", prevAddr.String(),
241		"prevRealm", prevRealm,
242		"orderId", utils.FormatUint(order.ID()),
243		"groupId", group.ID(),
244		"round", utils.FormatUint(group.Round()),
245		"releasedExecutionLogCount", utils.FormatInt(releasedExecutionLogCount),
246		"remainingExecutionLogCount", utils.FormatInt(order.ExecutionLogCount()),
247	)
248}
249
250func emitConfigUpdated(prevAddr address, prevRealm string, key string, previousValue int64, value int64) {
251	chain.Emit(
252		"DCAConfigUpdated",
253		"prevAddr", prevAddr.String(),
254		"prevRealm", prevRealm,
255		"key", key,
256		"previousValue", utils.FormatInt(previousValue),
257		"value", utils.FormatInt(value),
258	)
259}
260
261func emitAllowedIntervalUpdated(prevAddr address, prevRealm string, interval int64, allowed bool) {
262	chain.Emit(
263		"DCAAllowedIntervalUpdated",
264		"prevAddr", prevAddr.String(),
265		"prevRealm", prevRealm,
266		"interval", utils.FormatInt(interval),
267		"allowed", utils.FormatBool(allowed),
268	)
269}
270
271// emitOrderForceCancelled reports an order closed by a keeper with ForceCancelOrder.
272// It carries the fields of DCAOrderCancelled and the reason the order blocked
273// its group (see ForceCancelOrder); the order's status is
274// cancelled, and its output and refund went to the owner.
275func emitOrderForceCancelled(
276	prevAddr address,
277	prevRealm string,
278	order *pdca.Order,
279	group *pdca.Group,
280	payout orderPayout,
281	refundedAmount int64,
282	reason string,
283) {
284	chain.Emit(
285		"DCAOrderForceCancelled",
286		"prevAddr", prevAddr.String(),
287		"prevRealm", prevRealm,
288		"orderId", utils.FormatUint(order.ID()),
289		"owner", order.Owner().String(),
290		"groupId", order.GroupID(),
291		"inputToken", group.InputToken(),
292		"outputToken", group.OutputToken(),
293		"refundedAmount", utils.FormatInt(refundedAmount),
294		"grossAmount", utils.FormatInt(payout.grossAmount),
295		"executionFeeRate", utils.FormatInt(order.ExecutionFeeRate()),
296		"executionFeeAmount", utils.FormatInt(payout.executionFeeAmount),
297		"claimedAmount", utils.FormatInt(payout.claimedAmount),
298		"totalClaimedAmount", order.ClaimedAmount().ToString(),
299		"totalExecutionFeeAmount", order.ExecutionFeeAmount().ToString(),
300		"debtX128", order.DebtX128().ToString(),
301		"status", pdca.OrderStatusCancelled.String(),
302		"cancelRound", utils.FormatUint(group.Round()),
303		"groupOrderAmount", utils.FormatInt(group.OrderAmount()),
304		"reason", reason,
305	)
306}
307
308func emitTokenMinAmountPerExecutionUpdated(
309	prevAddr address,
310	prevRealm string,
311	tokenPath string,
312	previousMinAmountPerExecution int64,
313	minAmountPerExecution int64,
314) {
315	chain.Emit(
316		"DCATokenMinAmountPerExecutionUpdated",
317		"prevAddr", prevAddr.String(),
318		"prevRealm", prevRealm,
319		"tokenPath", tokenPath,
320		"prevMinAmountPerExecution", utils.FormatInt(previousMinAmountPerExecution),
321		"minAmountPerExecution", utils.FormatInt(minAmountPerExecution),
322	)
323}
324
325// emitPairPriceLimitUpdated reports a pair price limit change;
326// minOutputPerInputX128 "0" and updatedAt 0 mean the limit was removed.
327func emitPairPriceLimitUpdated(
328	prevAddr address,
329	prevRealm string,
330	inputToken string,
331	outputToken string,
332	previousMinOutputPerInputX128 string,
333	minOutputPerInputX128 string,
334	updatedAt int64,
335) {
336	chain.Emit(
337		"DCAPairPriceLimitUpdated",
338		"prevAddr", prevAddr.String(),
339		"prevRealm", prevRealm,
340		"inputToken", inputToken,
341		"outputToken", outputToken,
342		"prevMinOutputPerInputX128", previousMinOutputPerInputX128,
343		"minOutputPerInputX128", minOutputPerInputX128,
344		"updatedAt", utils.FormatInt(updatedAt),
345	)
346}