-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool.gno
677 lines (554 loc) · 20.5 KB
/
pool.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package pool
import (
"std"
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"
g "gno.land/r/gov"
bar "gno.land/r/bar"
foo "gno.land/r/foo"
)
// only position contract can call this function
func Mint(
pToken0 string,
pToken1 string,
pFee uint16,
recipient std.Address,
tickLower int32,
tickUpper int32,
liquidityAmount bigint,
) (bigint, bigint) {
require(PrevRealmPath() == "gno.land/r/position", ufmt.Sprintf("[POOL] pool.gno__Mint() || PrevRealmPath(%s) == \"gno.land/r/position\"", PrevRealmPath()))
require(liquidityAmount > 0, ufmt.Sprintf("[POOL] pool.gno__Mint() || liquidityAmount(%s) > 0", liquidityAmount))
pool := GetPool(pToken0, pToken1, pFee)
_, amount0Int, amount1Int := pool.modifyPosition(
ModifyPositionParams{
recipient, // owner
tickLower, // tickLower
tickUpper, // tickUpper
liquidityAmount, // liquidityDelta
},
)
amount0 := bigint(amount0Int)
requireUnsigned(amount0, ufmt.Sprintf("[POOL] pool.gno__Mint() || amount0(%s) >= 0", amount0))
amount1 := bigint(amount1Int)
requireUnsigned(amount1, ufmt.Sprintf("[POOL] pool.gno__Mint() || amount1(%s) >= 0", amount1))
var balance0Before, balance1Before bigint
if amount0 > 0 {
// r3v4_xxx
// when dynamic import is supported, we can use transfer()
// ~~ FROM HERE
balance0Before = balanceOf(pool.token0, GetOrigPkgAddr())
from := a2u(GetOrigCaller()) // token should be transferred from actual user(GetOrigCaller), not from the realm(PrevRealm)
to := a2u(GetOrigPkgAddr())
foo.TransferFrom(from, to, uint64(amount0))
require(
balance0Before+amount0 <= balanceOf(pool.token0, GetOrigPkgAddr()),
ufmt.Sprintf(
"[POOL] pool.gno__Mint() || balance0Before(%s) + amount0(%s) <= balanceOf(pool.token0, GetOrigPkgAddr())(%s)",
balance0Before, amount0, balanceOf(pool.token0, GetOrigPkgAddr()),
),
)
} // ~~ TO HERE
if amount1 > 0 {
// r3v4_xxx
// when dynamic import is supported, we can use transfer()
// ~~ FROM HERE
balance1Before = balanceOf(pool.token1, GetOrigPkgAddr())
from := a2u(GetOrigCaller()) // token should be transferred from actual user(GetOrigCaller), not from the realm(PrevRealm)
to := a2u(GetOrigPkgAddr())
bar.TransferFrom(from, to, uint64(amount1))
require(
balance1Before+amount1 <= balanceOf(pool.token1, GetOrigPkgAddr()),
ufmt.Sprintf(
"[POOL] pool.gno__Mint() || balance1Before(%s) + amount1(%s) <= balanceOf(pool.token1, GetOrigPkgAddr())(%s)",
balance1Before, amount1, balanceOf(pool.token1, GetOrigPkgAddr()),
),
)
} // ~~ TO HERE
pool.balances.token0 += amount0
pool.balances.token1 += amount1
return amount0, amount1
}
// only position contract can call this function
func Burn(
pToken0 string,
pToken1 string,
pFee uint16,
tickLower int32,
tickUpper int32,
amount bigint,
) (bigint, bigint) {
require(PrevRealmPath() == "gno.land/r/position", ufmt.Sprintf("[POOL] pool.gno__Burn() || caller(%s) must be position contract", PrevRealmPath()))
requireUnsigned(amount, ufmt.Sprintf("[POOL] pool.gno__Burn() || amount(%s) >= 0", amount))
pool := GetPool(pToken0, pToken1, pFee)
position, amount0Int, amount1Int := pool.modifyPosition(
ModifyPositionParams{
PrevRealmAddr(), // msg.sender
tickLower,
tickUpper,
-amount,
},
)
amount0, amount1 := -amount0Int, -amount1Int
requireUnsigned(amount0, ufmt.Sprintf("pool.gno__Burn() || amount0(%s) >= 0", amount0))
requireUnsigned(amount1, ufmt.Sprintf("pool.gno__Burn() || amount1(%s) >= 0", amount1))
if amount0 > 0 || amount1 > 0 {
position.tokensOwed0 += amount0
position.tokensOwed1 += amount1
}
key := positionGetKey(PrevRealmAddr(), tickLower, tickUpper)
pool.positions[key] = position
return amount0, amount1
}
// only position contract can call this function
func Collect(
pToken0 string,
pToken1 string,
pFee uint16,
recipient std.Address,
tickLower int32,
tickUpper int32,
amount0Requested bigint,
amount1Requested bigint,
) (bigint, bigint) {
require(PrevRealmPath() == "gno.land/r/position", ufmt.Sprintf("[POOL] pool.gno__Collect() || caller(%s) must be position contract(gno.land/r/position)", PrevRealmPath()))
requireUnsigned(amount0Requested, ufmt.Sprintf("pool.gno__Collect() || amount0Requested(%s) >= 0", amount0Requested))
requireUnsigned(amount1Requested, ufmt.Sprintf("pool.gno__Collect() || amount1Requested(%s) >= 0", amount1Requested))
pool := GetPool(pToken0, pToken1, pFee)
key := positionGetKey(PrevRealmAddr(), tickLower, tickUpper)
position, exist := pool.positions[key]
require(exist, ufmt.Sprintf("[POOL] pool.gno__Collect() || position(%s) does not exist", key))
amount0 := min(amount0Requested, position.tokensOwed0)
requireUnsigned(amount0, ufmt.Sprintf("[POOL] pool.gno__Collect() || amount0(%s) >= 0", amount0))
amount1 := min(amount1Requested, position.tokensOwed1)
requireUnsigned(amount1, ufmt.Sprintf("[POOL] pool.gno__Collect() || amount1(%s) >= 0", amount1))
// collect(pool.token0, pool.balances.token0, amount0, recipient)
require(pool.balances.token0 >= amount0, ufmt.Sprintf("[POOL] pool.gno__Collect() || pool.balances.token0(%s) >= amount0(%s)", pool.balances.token0, amount0))
foo.Transfer(a2u(recipient), uint64(amount0))
// collect(pool.token1, pool.balances.token1, amount1, recipient)
require(pool.balances.token1 >= amount1, ufmt.Sprintf("[POOL] pool.gno__Collect() || pool.balances.token1(%s) >= amount1(%s)", pool.balances.token1, amount1))
bar.Transfer(a2u(recipient), uint64(amount1))
// adjust position
position.tokensOwed0 -= amount0
position.tokensOwed1 -= amount1
pool.positions[key] = position
// adjust pool
pool.balances.token0 -= amount0
pool.balances.token1 -= amount1
requireUnsigned(pool.balances.token0, ufmt.Sprintf("[POOL] pool.gno__Burn() || pool.balances.token0(%s) >= 0", pool.balances.token0))
requireUnsigned(pool.balances.token1, ufmt.Sprintf("[POOL] pool.gno__Burn() || pool.balances.token1(%s) >= 0", pool.balances.token1))
return amount0, amount1
}
func Swap(
pToken0 string,
pToken1 string,
pFee uint16,
recipient std.Address,
zeroForOne bool,
amountSpecified bigint,
sqrtPriceLimitX96 bigint,
) (bigint, bigint) {
require(amountSpecified != 0, "[POOL] pool.gno__Swap() || amountSpecified can't be zero")
pool := GetPool(pToken0, pToken1, pFee)
require(pool.liquidity > 0, ufmt.Sprintf("[POOL] math_logic.gno__swapAmount() || pool.liquidity(%d) must be > 0", pool.liquidity))
slot0Start := pool.slot0
require(slot0Start.unlocked, "[POOL] pool.gno__Swap() || slot0 must be unlocked")
var feeProtocol uint8
var feeGrowthGlobalX128 bigint
if zeroForOne {
require(
sqrtPriceLimitX96 < slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 > MIN_SQRT_RATIO,
ufmt.Sprintf("[POOL] pool.gno__Swap() || SPL-zeroForOne(T)__sqrtPriceLimitX96(%s) < slot0Start.sqrtPriceX96(%s) && sqrtPriceLimitX96(%s) > MIN_SQRT_RATIO(%s)",
sqrtPriceLimitX96, slot0Start.sqrtPriceX96, sqrtPriceLimitX96, MIN_SQRT_RATIO),
)
feeProtocol = slot0Start.feeProtocol % 16
feeGrowthGlobalX128 = pool.feeGrowthGlobal0X128
} else {
require(
sqrtPriceLimitX96 > slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 < MAX_SQRT_RATIO,
ufmt.Sprintf("[POOL] pool.gno__Swap() || SPL-zeroForOne(F)__sqrtPriceLimitX96(%s) > slot0Start.sqrtPriceX96(%s) && sqrtPriceLimitX96(%s) < MAX_SQRT_RATIO(%s)",
sqrtPriceLimitX96, slot0Start.sqrtPriceX96, sqrtPriceLimitX96, MAX_SQRT_RATIO),
)
feeProtocol = slot0Start.feeProtocol / 16
feeGrowthGlobalX128 = pool.feeGrowthGlobal1X128
}
pool.slot0.unlocked = false
cache := SwapCache{
liquidityStart: pool.liquidity,
feeProtocol: feeProtocol,
}
exactInput := amountSpecified > 0
var state SwapState
if zeroForOne {
state = SwapState{
amountSpecifiedRemaining: amountSpecified,
amountCalculated: 0,
sqrtPriceX96: slot0Start.sqrtPriceX96,
tick: slot0Start.tick,
feeGrowthGlobalX128: pool.feeGrowthGlobal0X128,
protocolFee: 0,
liquidity: cache.liquidityStart,
}
} else {
state = SwapState{
amountSpecifiedRemaining: amountSpecified,
amountCalculated: 0,
sqrtPriceX96: slot0Start.sqrtPriceX96,
tick: slot0Start.tick,
feeGrowthGlobalX128: pool.feeGrowthGlobal1X128,
protocolFee: 0,
liquidity: cache.liquidityStart,
}
}
for state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96 {
var step StepComputations
step.sqrtPriceStartX96 = state.sqrtPriceX96
step.tickNext, step.initialized = pool.tickBitmapNextInitializedTickWithInOneWord(
state.tick,
pool.tickSpacing,
zeroForOne,
)
if step.tickNext < MIN_TICK {
step.tickNext = MIN_TICK
} else if step.tickNext > MAX_TICK {
step.tickNext = MAX_TICK
}
step.sqrtPriceNextX96 = TickMathGetSqrtRatioAtTick(step.tickNext)
var _sqrtRatioTarget bigint
if zeroForOne {
if step.sqrtPriceNextX96 < sqrtPriceLimitX96 {
_sqrtRatioTarget = sqrtPriceLimitX96
} else {
_sqrtRatioTarget = step.sqrtPriceNextX96
}
} else {
if step.sqrtPriceNextX96 > sqrtPriceLimitX96 {
_sqrtRatioTarget = sqrtPriceLimitX96
} else {
_sqrtRatioTarget = step.sqrtPriceNextX96
}
}
state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount = swapMathComputeSwapStep(
state.sqrtPriceX96,
_sqrtRatioTarget,
state.liquidity,
state.amountSpecifiedRemaining,
uint32(pool.fee),
)
if exactInput {
state.amountSpecifiedRemaining -= step.amountIn + step.feeAmount
state.amountCalculated -= step.amountOut
} else {
state.amountSpecifiedRemaining += step.amountOut
state.amountCalculated += (step.amountIn + step.feeAmount)
}
require(step.amountIn != 0 && step.amountOut != 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || step.amountIn(%d) != 0 && step.amountOut(%d) != 0", step.amountIn, step.amountOut))
if cache.feeProtocol > 0 {
delta := step.feeAmount / bigint(uint64(cache.feeProtocol))
requireUnsigned(delta, ufmt.Sprintf("[POOL] pool.gno__Swap() || delta(%s) >= 0", delta))
step.feeAmount -= delta
state.protocolFee += delta
}
if state.liquidity > 0 {
// save fee
state.feeGrowthGlobalX128 += (step.feeAmount * Q128 / state.liquidity)
}
if state.sqrtPriceX96 == step.sqrtPriceNextX96 {
if step.initialized {
var fee0, fee1 bigint
if zeroForOne {
fee0 = state.feeGrowthGlobalX128
fee1 = pool.feeGrowthGlobal1X128
} else {
fee0 = pool.feeGrowthGlobal0X128
fee1 = state.feeGrowthGlobalX128
}
liquidityNet := pool.tickCross(
step.tickNext,
fee0,
fee1,
)
if zeroForOne {
liquidityNet = -liquidityNet
}
state.liquidity = liquidityMathAddDelta(state.liquidity, liquidityNet)
}
if zeroForOne {
state.tick = step.tickNext - 1
} else {
state.tick = step.tickNext
}
} else if state.sqrtPriceX96 != step.sqrtPriceStartX96 {
state.tick = TickMathGetTickAtSqrtRatio(state.sqrtPriceX96)
}
}
// END LOOP
pool.slot0.sqrtPriceX96 = state.sqrtPriceX96
if state.tick != slot0Start.tick {
pool.slot0.tick = state.tick
}
if cache.liquidityStart != state.liquidity {
pool.liquidity = state.liquidity
}
if zeroForOne {
pool.feeGrowthGlobal0X128 = state.feeGrowthGlobalX128
if state.protocolFee > 0 {
pool.protocolFees.token0 += state.protocolFee
}
} else {
pool.feeGrowthGlobal1X128 = state.feeGrowthGlobalX128
if state.protocolFee > 0 {
pool.protocolFees.token1 += state.protocolFee
}
}
var amount0, amount1 bigint
if zeroForOne == exactInput {
amount0 = amountSpecified - state.amountSpecifiedRemaining
amount1 = state.amountCalculated
} else {
amount0 = state.amountCalculated
amount1 = amountSpecified - state.amountSpecifiedRemaining
}
if zeroForOne {
if amount1 < 0 {
require(pool.balances.token1 > (-1*amount1), ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token1(%s) > (-1 * amount1)(%s)", pool.balances.token1, (-1*amount1)))
// pool.token1.Transfer(recipient, uint64(-1*amount1))
bar.Transfer(a2u(recipient), uint64(-1*amount1))
pool.balances.token1 += amount1
}
// r3v4_xxx
// transfer(pool.token0, amount0)
// ~~ FROM HERE
balance0Before := balanceOf(pool.token0, GetOrigPkgAddr())
from := a2u(GetOrigCaller()) // token should be transferred from actual user(GetOrigCaller), not from the realm(PrevRealm)
to := a2u(GetOrigPkgAddr())
foo.TransferFrom(from, to, uint64(amount0))
require(
balance0Before+bigint(amount0) <= balanceOf(pool.token0, GetOrigPkgAddr()),
ufmt.Sprintf(
"[POOL] pool.gno__Swap() || balance0Before(%s) + amount0(%s) <= balanceOf(pool.token0, GetOrigPkgAddr())(%s)",
balance0Before, amount0, balanceOf(pool.token0, GetOrigPkgAddr()),
),
)
// TO HERE ~~
pool.balances.token0 += amount0
require(pool.balances.token0 >= 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token0(%s) >= 0__#1", pool.balances.token0))
require(pool.balances.token1 >= 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token1(%s) >= 0__#1", pool.balances.token1))
} else {
if amount0 < 0 {
require(pool.balances.token0 > (-1*amount0), ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token0(%s) > (-1 * amount0)(%s)", pool.balances.token0, (-1*amount0)))
// pool.token0.Transfer(a2u(recipient), uint64(-amount0))
foo.Transfer(a2u(recipient), uint64(-amount0))
pool.balances.token0 += amount0
}
// r3v4_xxx
// transfer(pool.token1, amount1)
// ~~ FROM HERE
balance1Before := balanceOf(pool.token1, GetOrigPkgAddr())
from := a2u(GetOrigCaller())
to := a2u(GetOrigPkgAddr())
bar.TransferFrom(from, to, uint64(amount1)) // token should be transferred from actual user(GetOrigCaller), not from the realm(PrevRealm)
require(
balance1Before+bigint(amount1) <= balanceOf(pool.token1, GetOrigPkgAddr()),
ufmt.Sprintf(
"[POOL] pool.gno__Swap() || balance1Before(%s) + amount1(%s) <= balanceOf(pool.token1, GetOrigPkgAddr())(%s)",
balance1Before, amount1, balanceOf(pool.token1, GetOrigPkgAddr()),
),
)
// TO HERE ~~
pool.balances.token1 += amount1
require(pool.balances.token0 >= 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token0(%s) >= 0__#2", pool.balances.token0))
require(pool.balances.token1 >= 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token1(%s) >= 0__#2", pool.balances.token1))
}
pool.slot0.unlocked = true
return amount0, amount1
}
// ADMIN
func SetFeeProtocol(
feeProtocol0 uint8,
feeProtocol1 uint8,
) {
require(isAdmin(PrevRealmAddr()), ufmt.Sprintf("[POOL] pool.gno__SetFeeProtocol() || caller(%s) must be admin", PrevRealmAddr()))
require(
(feeProtocol0 == 0 || (feeProtocol0 >= 4 && feeProtocol0 <= 10)) && (feeProtocol1 == 0 || (feeProtocol1 >= 4 && feeProtocol1 <= 10)),
"Invalid fee protocol",
)
// iterate all pool
for _, pool := range pools {
pool.slot0.feeProtocol = feeProtocol0 + (feeProtocol1 << 4) // ( << 4 ) = ( * 16 )
}
// update governace value
g.SetGovParameter("protocoL_fees", feeProtocol0+(feeProtocol1<<4))
}
// ADMIN
func CollectProtocol(
pToken0 string,
pToken1 string,
pFee uint16,
recipient std.Address,
amount0Requested bigint,
amount1Requested bigint,
) (bigint, bigint) {
requireUnsigned(amount0Requested, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount0Requested(%s) >= 0", amount0Requested))
requireUnsigned(amount1Requested, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount1Requested(%s) >= 0", amount1Requested))
require(isAdmin(PrevRealmAddr()), ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || caller(%s) must be admin", PrevRealmAddr()))
pool := GetPool(pToken0, pToken1, pFee)
amount0 := min(amount0Requested, pool.protocolFees.token0)
requireUnsigned(amount0, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount0(%s) >= 0", amount0))
amount1 := min(amount1Requested, pool.protocolFees.token1)
requireUnsigned(amount1, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount1(%s) >= 0", amount1))
// without procotol fee
amount0, amount1 = pool.saveProtocolFees(amount0, amount1)
// pool.token0.Transfer(a2u(recipient), uint64(amount0))
foo.Transfer(a2u(recipient), uint64(amount0))
// pool.token1.Transfer(a2u(recipient), uint64(amount1))
bar.Transfer(a2u(recipient), uint64(amount1))
return amount0, amount1
}
func (pool *Pool) modifyPosition(params ModifyPositionParams) (PositionInfo, bigint, bigint) {
position := pool.updatePosition(
params.owner,
params.tickLower,
params.tickUpper,
params.liquidityDelta,
pool.slot0.tick,
)
var amount0, amount1 bigint
if params.liquidityDelta != 0 {
if pool.slot0.tick < params.tickLower {
amount0 = sqrtPriceMathGetAmount0Delta(
TickMathGetSqrtRatioAtTick(params.tickLower),
TickMathGetSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta,
)
} else if pool.slot0.tick < params.tickUpper {
liquidityBefore := pool.liquidity
amount0 = sqrtPriceMathGetAmount0Delta(
pool.slot0.sqrtPriceX96,
TickMathGetSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta,
)
amount1 = sqrtPriceMathGetAmount1Delta(
TickMathGetSqrtRatioAtTick(params.tickLower),
pool.slot0.sqrtPriceX96,
params.liquidityDelta,
)
pool.liquidity = liquidityMathAddDelta(liquidityBefore, params.liquidityDelta)
} else {
amount1 = sqrtPriceMathGetAmount1Delta(
TickMathGetSqrtRatioAtTick(params.tickLower),
TickMathGetSqrtRatioAtTick(params.tickUpper),
params.liquidityDelta,
)
}
}
return position, amount0, amount1
}
func (pool *Pool) updatePosition(
owner std.Address,
tickLower int32,
tickUpper int32,
liquidityDelta bigint,
tick int32,
) PositionInfo {
var _feeGrowthGlobal0X128 bigint = pool.feeGrowthGlobal0X128
var _feeGrowthGlobal1X128 bigint = pool.feeGrowthGlobal1X128
var flippedLower, flippedUpper bool
if liquidityDelta != 0 {
flippedLower = pool.tickUpdate(
tickLower,
tick,
liquidityDelta,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128,
false,
pool.maxLiquidityPerTick,
)
flippedUpper = pool.tickUpdate(
tickUpper,
tick,
liquidityDelta,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128,
true,
pool.maxLiquidityPerTick,
)
if flippedLower {
pool.tickBitmapFlipTick(tickLower, pool.tickSpacing)
}
if flippedUpper {
pool.tickBitmapFlipTick(tickUpper, pool.tickSpacing)
}
}
feeGrowthInside0X128, feeGrowthInside1X128 := pool.tickGetFeeGrowthInside(
tickLower,
tickUpper,
tick,
_feeGrowthGlobal0X128,
_feeGrowthGlobal1X128,
)
key := positionGetKey(owner, tickLower, tickUpper)
position := pool.positionUpdateWithKey(
key,
liquidityDelta,
feeGrowthInside0X128,
feeGrowthInside1X128,
)
if liquidityDelta < 0 {
if flippedLower {
pool.tickClear(tickLower)
}
if flippedUpper {
pool.tickClear(tickUpper)
}
}
return position
}
// r3v4_xxx
// unused for now
func transfer(token *grc20.AdminToken, amount bigint) {
balanceBefore, err := token.BalanceOf(GetOrigPkgAddr())
if err != nil {
balanceBefore = 0
}
from := GetOrigCaller() // token should be transferred from actual user(GetOrigCaller), not from the realm(PrevRealm)
to := GetOrigPkgAddr()
token.TransferFrom(GetOrigPkgAddr(), from, to, uint64(amount))
balancAfter, err := token.BalanceOf(GetOrigPkgAddr())
if err != nil {
balancAfter = 0
}
require(
balanceBefore+uint64(amount) <= balancAfter,
ufmt.Sprintf(
"[POOL] pool.gno__mint() || balanceBefore(%s) + amount(%s) <= balancAfter(%s)",
),
)
}
// r3v4_xxx
// unused for now
func (pool *Pool) collect(token *grc20.AdminToken, poolBalance, requestAmount bigint, recipient std.Address) {
require(poolBalance >= requestAmount, ufmt.Sprintf("[POOL] pool.gno__collect() || poolBalance(%s) >= requestAmount(%s)", poolBalance, requestAmount))
token.Transfer(GetOrigPkgAddr(), recipient, uint64(requestAmount))
}
func (pool *Pool) saveProtocolFees(amount0, amount1 bigint) (bigint, bigint) {
if amount0 > 0 && amount0 == pool.protocolFees.token0 {
amount0--
}
if amount1 > 0 && amount1 == pool.protocolFees.token1 {
amount1--
}
pool.protocolFees.token0 -= amount0
pool.protocolFees.token1 -= amount1
// return rest fee
return amount0, amount1
}
func checkTicks(tickLower, tickUpper bigint) {
require(tickLower < tickUpper, ufmt.Sprintf("[POOL] pool.gno__checkTicks() || tickLower(%s) < tickUpper(%s)", tickLower, tickUpper))
require(tickLower >= MIN_TICK, ufmt.Sprintf("[POOL] pool.gno__checkTicks() || tickLower(%s) >= MIN_TICK(%s)", tickLower, MIN_TICK))
require(tickUpper <= MAX_TICK, ufmt.Sprintf("[POOL] pool.gno__checkTicks() || tickUpper(%s) <= MAX_TICK(%s)", tickUpper, MAX_TICK))
}
func a2u(addr std.Address) users.AddressOrName {
return users.AddressOrName(addr)
}