diff --git a/gov/const.gno b/gov/const.gno new file mode 100644 index 00000000..0a65b29a --- /dev/null +++ b/gov/const.gno @@ -0,0 +1,7 @@ +package gov + +import "std" + +const ( + GNS_TOKEN_ADDRESS = std.DerivePkgAddr("gno.land/r/gns") +) diff --git a/gov/helper.gno b/gov/gns_helper.gno similarity index 53% rename from gov/helper.gno rename to gov/gns_helper.gno index 63855eb8..5ed34a75 100644 --- a/gov/helper.gno +++ b/gov/gns_helper.gno @@ -3,19 +3,19 @@ package gov import ( "std" - gnos "gno.land/r/gnos" + gns "gno.land/r/gns" "gno.land/r/demo/users" ) func balanceOf(address std.Address) uint64 { - return gnos.BalanceOf(users.AddressOrName(address)) + return gns.BalanceOf(users.AddressOrName(address)) } func transfer(to std.Address, amount uint64) { - gnos.Transfer(users.AddressOrName(to), amount) + gns.Transfer(users.AddressOrName(to), amount) } func mint(recipient std.Address, amount uint64) { - gnos.Mint(users.AddressOrName(recipient), amount) -} \ No newline at end of file + gns.Mint(users.AddressOrName(recipient), amount) +} diff --git a/gov/proposal_community_pool_spend.gno b/gov/proposal_community_pool_spend.gno index 1972162d..2053fb26 100644 --- a/gov/proposal_community_pool_spend.gno +++ b/gov/proposal_community_pool_spend.gno @@ -1,9 +1,8 @@ -package gov +package gov import ( + "errors" "std" - - gnos "gno.land/r/gnos" // GNOS, Gnoswap Share ) ////////////////////////// @@ -24,25 +23,20 @@ func (p ProposalCommunityPoolSpend) ProposalType() ProposalType { func (p ProposalCommunityPoolSpend) isProposalData() {} func (p ProposalCommunityPoolSpend) isValid() bool { - /* - if p.Token != GNOS_TOKEN_ADDRESS { + if p.Token != GNS_TOKEN_ADDRESS { return false } - */ + } func (p ProposalCommunityPoolSpend) execute() error { - // XXXXXXXX - // commented out, replace `GNOS_TOKEN_ADDRESS` with the address of the GNOS token contract - /* - if p.Token != GNOS_TOKEN_ADDRESS { - return errors.New("unimplemented: only GNOS token is supported") + if p.Token != GNS_TOKEN_ADDRESS { + return errors.New("unimplemented: only GNS token is supported") } - */ // TODO: if transfer failes, return error // TODO: we might need to whitelist set of tokens that can be transferred from the community pool, as some might panic on transfer transfer(p.Recipient, p.Amount) return nil -} \ No newline at end of file +} diff --git a/gov/proposal_mint.gno b/gov/proposal_mint.gno index 539a6ffe..662ff4c6 100644 --- a/gov/proposal_mint.gno +++ b/gov/proposal_mint.gno @@ -1,11 +1,9 @@ -package gov +package gov import ( "std" "errors" - - gnos "gno.land/r/gnos" // GNOS, Gnoswap Share ) ////////////////////////// @@ -15,8 +13,8 @@ var _ ProposalData = (*ProposalMint)(nil) type ProposalMint struct { Recipient std.Address - Amount uint64 - Token std.Address + Amount uint64 + Token std.Address } func (p ProposalMint) ProposalType() ProposalType { @@ -26,12 +24,11 @@ func (p ProposalMint) ProposalType() ProposalType { func (p ProposalMint) isProposalData() {} func (p ProposalMint) isValid() bool { - // XXXXXX - /* - if p.Token != GNOS_TOKEN_ADDRESS { + + if p.Token != GNS_TOKEN_ADDRESS { return false } - */ + return true } @@ -43,4 +40,4 @@ func (p ProposalMint) execute() error { mint(p.Recipient, p.Amount) return nil -} \ No newline at end of file +} diff --git a/pool/_TEST_math_logic_test.gnoa b/pool/_TEST_math_logic_test.gnoa index 4b1230b3..6718d26b 100644 --- a/pool/_TEST_math_logic_test.gnoa +++ b/pool/_TEST_math_logic_test.gnoa @@ -12,6 +12,7 @@ import ( var ( gsa = testutils.TestAddress("gsa") lp01 = testutils.TestAddress("lp01") + pc01 = testutils.TestAddress("pc01") poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") @@ -33,6 +34,8 @@ var ( func init() { std.TestSetOrigCaller(gsa) InitManual() + + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, sqrtPrice) } @@ -51,17 +54,19 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) { std.TestSetOrigCaller(lp01) // no mint == no liquidity => swap will fail - shouldPanic(t, func() { DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) }) + _, _, ok := DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) + shouldEQ(t, ok, false) // not enough mint == swap will fail Mint(fooPath, barPath, pFee, posAddr, tickLower, tickUpper, 1000) - shouldPanic(t, func() { DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) }) + _, _, ok = DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) + shouldEQ(t, ok, false) Mint(fooPath, barPath, pFee, posAddr, tickLower, tickUpper, liquidityExpect) // zeroForOne true // amountSpecified 16000 - input, output := DrySwap( + input, output, _ := DrySwap( fooPath, // fooPath barPath, // barPath pFee, // pFee @@ -78,7 +83,7 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) { // zeroForOne true // amountSpecified -16000 - input, output := DrySwap( + input, output, _ := DrySwap( fooPath, // fooPath barPath, // barPath pFee, // pFee @@ -89,14 +94,14 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) { ) shouldEQ(t, input, bigint(5888)) - shouldEQ(t, output, bigint(-15999)) + shouldEQ(t, output, bigint(-16000)) } func TestDrySwap_ZeroForOneFalse_AmountSpecified_Positive_16000(t *testing.T) { // zeroForOne false // amountSpecified 16000 - input, output := DrySwap( + input, output, _ := DrySwap( fooPath, // fooPath barPath, // barPath pFee, // pFee @@ -113,7 +118,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) { // zeroForOne false // amountSpecified -16000 - input, output := DrySwap( + input, output, _ := DrySwap( fooPath, // fooPath barPath, // barPath pFee, // pFee @@ -122,7 +127,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) { -16000, // amountSpecified MAX_SQRT_RATIO-1, // sqrtPriceLimitX96 ) - shouldEQ(t, input, bigint(-15999)) + shouldEQ(t, input, bigint(-16000)) shouldEQ(t, output, bigint(5888)) } diff --git a/pool/_TEST_pool_creation_fee_test.gno b/pool/_TEST_pool_creation_fee_test.gno new file mode 100644 index 00000000..6b44d18a --- /dev/null +++ b/pool/_TEST_pool_creation_fee_test.gno @@ -0,0 +1,82 @@ +package pool + +import ( + "std" + "testing" + + "gno.land/p/demo/testutils" + + _ "gno.land/r/grc20_wrapper" + + gns "gno.land/r/gns" +) + +var ( + pc01 = testutils.TestAddress("pc01") // pool creator + gsa = testutils.TestAddress("gsa") + + poolAddr = std.DerivePkgAddr("gno.land/r/pool") + posAddr = std.DerivePkgAddr("gno.land/r/position") +) + +var ( + fooPath = "gno.land/r/foo" // token1 + barPath = "gno.land/r/bar" // token2 + bazPath = "gno.land/r/baz" + sqrtPrice bigint = 130621891405341611593710811006 + + tickLower = int32(9000) + tickUpper = int32(11000) + liquidityExpect = bigint(100_000_000) + + currentTick = int32(10000) +) + +func init() { + std.TestSetOrigCaller(gsa) + InitManual() +} + +func TestCreatePool(t *testing.T) { + std.TestSetOrigCaller(pc01) + + pc01Before := gns.BalanceOf(a2u(pc01)) + gsaBefore := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gsaBefore, 0) + + CreatePool(fooPath, barPath, uint16(100), sqrtPrice) + CreatePool(fooPath, barPath, uint16(500), sqrtPrice) + CreatePool(fooPath, barPath, uint16(3000), sqrtPrice) + CreatePool(fooPath, barPath, uint16(10000), sqrtPrice) + + pc01After := gns.BalanceOf(a2u(pc01)) + gsaAfter := gns.BalanceOf(a2u(gsa)) + + shouldEQ(t, gsaAfter-gsaBefore, 2000) + shouldEQ(t, pc01Before-pc01After, 2000) + shouldEQ(t, len(pools), 4) +} + +func TestCreatePoolNotEnoughGNS(t *testing.T) { + shouldPanicWithMsg(t, func() { CreatePool(fooPath, bazPath, uint16(100), sqrtPrice) }, "insufficient balance") + shouldEQ(t, len(pools), 4) +} + +func shouldEQ(t *testing.T, got, expected interface{}) { + if got != expected { + t.Errorf("got %v, expected %v", got, expected) + } +} + +func shouldPanicWithMsg(t *testing.T, f func(), msg string) { + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } else { + if r != msg { + t.Errorf("excepted panic(%v), got(%v)", msg, r) + } + } + }() + f() +} diff --git a/pool/_TEST_pool_multi_lp_fee_api_test.gnoa b/pool/_TEST_pool_multi_lp_fee_api_test.gnoa index 4a060b2e..1b6d1668 100644 --- a/pool/_TEST_pool_multi_lp_fee_api_test.gnoa +++ b/pool/_TEST_pool_multi_lp_fee_api_test.gnoa @@ -15,6 +15,7 @@ import ( var ( gsa = testutils.TestAddress("gsa") // Gnoswap Admin lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 tr01 = testutils.TestAddress("tr01") // Trader 01 poolAddr = std.DerivePkgAddr("gno.land/r/pool") @@ -41,6 +42,7 @@ func TestCreatePool(t *testing.T) { jsonStr := gjson.Parse(ApiGetPools()) shouldEQ(t, len(jsonStr.Get("response.data").Array()), 0) + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, 130622891405341611593710811006) std.TestSkipHeights(1) // 130621891405341611593710811006 // 9999 @@ -83,22 +85,22 @@ func TestSwap(t *testing.T) { shouldEQ(t, jsonStr.Get("response.data.liquidity").Int(), 100000000) shouldEQ(t, len(jsonStr.Get("response.data.positions").Array()), 1) - test_price_01 := bigint(MIN_SQRT_RATIO + 1) // maximum price // swap 0 -> 1 - test_price_10 := bigint(MAX_SQRT_RATIO - 1) // minimum price // swap 1 -> 0 - // SWAP std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee, tr01, true, bigint(150000), test_price_01) + Swap(fooPath, barPath, pFee, tr01, true, bigint(150000), MIN_PRICE, std.GetOrigCaller()) + std.TestSkipHeights(1) + + // Swap(fooPath, barPath, pFee, tr01, true, bigint(1500000), MIN_PRICE, std.GetOrigCaller()) // two iteration // s0: 1_500_000 // s1: -3_626_984 // currentTick: 7668 jsonStr = gjson.Parse(ApiGetPool("gno.land/r/bar:gno.land/r/foo:500")) shouldEQ(t, jsonStr.Get("response.data.token0_balance").Int(), 3107550) shouldEQ(t, jsonStr.Get("response.data.token1_balance").Int(), 7635058) shouldEQ(t, jsonStr.Get("response.data.liquidity").Int(), 100000000) shouldEQ(t, len(jsonStr.Get("response.data.positions").Array()), 1) - Swap(fooPath, barPath, pFee, tr01, false, bigint(601851), test_price_10) - // Swap(fooPath, barPath, pFee, tr01, true, bigint(1500000), test_price_01) // two iteration // s0: 1_500_000 // s1: -3_626_984 // currentTick: 7668 + Swap(fooPath, barPath, pFee, tr01, false, bigint(601851), MAX_PRICE, std.GetOrigCaller()) std.TestSkipHeights(1) + jsonStr = gjson.Parse(ApiGetPool("gno.land/r/bar:gno.land/r/foo:500")) shouldEQ(t, jsonStr.Get("response.data.token0_balance").Int(), 1496418) shouldEQ(t, jsonStr.Get("response.data.token1_balance").Int(), 8236909) diff --git a/pool/_TEST_pool_multi_lp_fee_test.gno b/pool/_TEST_pool_multi_lp_fee_test.gnoa similarity index 72% rename from pool/_TEST_pool_multi_lp_fee_test.gno rename to pool/_TEST_pool_multi_lp_fee_test.gnoa index 83ea0ce4..9da0842c 100644 --- a/pool/_TEST_pool_multi_lp_fee_test.gno +++ b/pool/_TEST_pool_multi_lp_fee_test.gnoa @@ -18,6 +18,8 @@ var ( tr01 = testutils.TestAddress("tr01") // Trader 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") ) @@ -37,6 +39,8 @@ var ( func TestFactoryCreatePool(t *testing.T) { std.TestSetOrigCaller(gsa) InitManual() + + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, 130622891405341611593710811006) // 130621891405341611593710811006 // 9999 // 130622891405341611593710811006 // 10000 @@ -70,40 +74,40 @@ func TestSwap(t *testing.T) { { // balance before swap - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - tr01OldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - tr01OldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + tr01OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + tr01OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - lp01OldToken0Bal := balanceOfByRegisterCall(fooPath, lp01) - lp01OldToken1Bal := balanceOfByRegisterCall(barPath, lp01) + lp01OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp01) + lp01OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp01) - lp02OldToken0Bal := balanceOfByRegisterCall(fooPath, lp02) - lp02OldToken1Bal := balanceOfByRegisterCall(barPath, lp02) + lp02OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp02) + lp02OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp02) // SWAP std.TestSetOrigCaller(tr01) - s0, s1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(100000), MIN_PRICE) // one iteration - yes fee(only lp02) + s0, s1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(100000), MIN_PRICE, std.GetOrigCaller()) // one iteration - yes fee(only lp02) shouldNEQ(t, s0, bigint(0)) shouldNEQ(t, s1, bigint(0)) // balance after swap - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, GetOrigPkgAddr()) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, GetOrigPkgAddr()) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, GetOrigPkgAddr()) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, GetOrigPkgAddr()) - tr01NewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - tr01NewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + tr01NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + tr01NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - lp01NewToken0Bal := balanceOfByRegisterCall(fooPath, lp01) - lp01NewToken1Bal := balanceOfByRegisterCall(barPath, lp01) + lp01NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp01) + lp01NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp01) - lp02NewToken0Bal := balanceOfByRegisterCall(fooPath, lp02) - lp02NewToken1Bal := balanceOfByRegisterCall(barPath, lp02) + lp02NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp02) + lp02NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp02) // To collect fee wihout removing liquidity // burn 0 diff --git a/pool/_TEST_pool_multi_lp_test.gnoa b/pool/_TEST_pool_multi_lp_test.gnoa index c23a5310..021339ec 100644 --- a/pool/_TEST_pool_multi_lp_test.gnoa +++ b/pool/_TEST_pool_multi_lp_test.gnoa @@ -19,6 +19,8 @@ var ( tr01 = testutils.TestAddress("tr01") // Trader 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") ) @@ -41,6 +43,8 @@ var ( func TestFactoryCreatePool(t *testing.T) { std.TestSetOrigCaller(gsa) InitManual() + + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006) // sqrtPrice @@ -208,17 +212,17 @@ func TestSwap(t *testing.T) { Mint(fooPath, barPath, pFee, posAddr, test_tickLower, test_tickUpper, test_liquidityExpect*10000) { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - lp02OldToken0Bal := balanceOfByRegisterCall(fooPath, lp02) - lp02OldToken1Bal := balanceOfByRegisterCall(barPath, lp02) + lp02OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp02) + lp02OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp02) - lp03OldToken0Bal := balanceOfByRegisterCall(fooPath, lp03) - lp03OldToken1Bal := balanceOfByRegisterCall(barPath, lp03) + lp03OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp03) + lp03OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp03) std.TestSetOrigCaller(tr01) amount0, amount1 := Swap( @@ -229,16 +233,17 @@ func TestSwap(t *testing.T) { true, bigint(10000), MIN_PRICE, + std.GetOrigCaller(), ) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -249,15 +254,15 @@ func TestSwap(t *testing.T) { Burn(fooPath, barPath, pFee, test_tickLower, test_tickUpper, test_liquidityExpect*1000) Collect(fooPath, barPath, pFee, lp02, test_tickLower, test_tickUpper, 50000000, 50000000) - lp02NewToken0Bal := balanceOfByRegisterCall(fooPath, lp02) - lp02NewToken1Bal := balanceOfByRegisterCall(barPath, lp02) + lp02NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp02) + lp02NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp02) std.TestSetOrigCaller(lp03) Burn(fooPath, barPath, pFee, test_tickLower, test_tickUpper, test_liquidityExpect*1000) Collect(fooPath, barPath, pFee, lp03, test_tickLower, test_tickUpper, 50000000, 50000000) - lp03NewToken0Bal := balanceOfByRegisterCall(fooPath, lp03) - lp03NewToken1Bal := balanceOfByRegisterCall(barPath, lp03) + lp03NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, lp03) + lp03NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, lp03) } // Mint again @@ -268,23 +273,23 @@ func TestSwap(t *testing.T) { Mint(fooPath, barPath, pFee, lp03, test_tickLower, test_tickUpper, test_liquidityExpect*1000) { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) std.TestSetOrigCaller(tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(5000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(5000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -293,23 +298,23 @@ func TestSwap(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) std.TestSetOrigCaller(tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(1000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, bigint(1000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -318,23 +323,23 @@ func TestSwap(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) std.TestSetOrigCaller(tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, false, bigint(16000), (MAX_SQRT_RATIO - 1)) + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, false, bigint(16000), MAX_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -368,38 +373,38 @@ func TestCollectProtocol(t *testing.T) { // Make ProtocolFee via Swap by tr01 ( Mint by lp01 ) std.TestSetOrigCaller(lp01) { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee, tr01, true, 100000, MIN_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee, tr01, true, 100000, MIN_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) } { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee, tr01, false, 100000, MAX_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee, tr01, false, 100000, MAX_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) diff --git a/pool/_TEST_pool_multi_token_test.gnoa b/pool/_TEST_pool_multi_token_test.gnoa index b3e9e6f0..fe7378b4 100644 --- a/pool/_TEST_pool_multi_token_test.gnoa +++ b/pool/_TEST_pool_multi_token_test.gnoa @@ -13,6 +13,7 @@ var ( gsa = testutils.TestAddress("gsa") // Gnoswap Admin lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 tr01 = testutils.TestAddress("tr01") // Trader 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") @@ -42,12 +43,14 @@ func TestInit(t *testing.T) { // 2. Create Foo:Bar Pool func TestCreateFooBarPool(t *testing.T) { + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006) shouldEQ(t, len(pools), 1) } // 3. Create Bar:Baz Pool func TestCreateBarBazPool(t *testing.T) { + std.TestSetOrigCaller(pc01) CreatePool(barPath, bazPath, pFee, 130621891405341611593710811006) shouldEQ(t, len(pools), 2) } @@ -88,10 +91,10 @@ func TestMintBarBazLiquidity(t *testing.T) { func TestSwapFooBarFooToBar(t *testing.T) { std.TestSetOrigCaller(tr01) - oldTr01FooBalance := balanceOfByRegisterCall(fooPath, tr01) - oldTr01BarBalance := balanceOfByRegisterCall(barPath, tr01) - oldPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldTr01Token1Balance := balanceOfByRegisterCall(fooPath, tr01) + oldTr01Token0Balance := balanceOfByRegisterCall(barPath, tr01) + oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) poolIn, poolOut := Swap( fooPath, @@ -101,28 +104,29 @@ func TestSwapFooBarFooToBar(t *testing.T) { true, bigint(16000), MIN_PRICE, + std.GetOrigCaller(), ) shouldEQ(t, poolIn, bigint(16000)) shouldEQ(t, poolOut, bigint(-43457)) - newTr01FooBalance := balanceOfByRegisterCall(fooPath, tr01) - newTr01BarBalance := balanceOfByRegisterCall(barPath, tr01) - newPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newTr01Token1Balance := balanceOfByRegisterCall(fooPath, tr01) + newTr01Token0Balance := balanceOfByRegisterCall(barPath, tr01) + newPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) - shouldEQ(t, oldTr01FooBalance-newTr01FooBalance, bigint(16000)) - shouldEQ(t, newTr01BarBalance-oldTr01BarBalance, bigint(43457)) - shouldEQ(t, newPoolFooBalance-oldPoolFooBalance, bigint(16000)) - shouldEQ(t, oldPoolBarBalance-newPoolBarBalance, bigint(43457)) + shouldEQ(t, oldTr01Token0Balance-newTr01Token0Balance, bigint(16000)) + shouldEQ(t, newTr01Token1Balance-oldTr01Token1Balance, bigint(43457)) + shouldEQ(t, newPoolToken0Balance-oldPoolToken0Balance, bigint(16000)) + shouldEQ(t, oldPoolToken1Balance-newPoolToken1Balance, bigint(43457)) } // 7. Swap Bar:Baz Bar > Baz by tr01 func TestSwapBarBazBarToBaz(t *testing.T) { std.TestSetOrigCaller(tr01) - oldTr01BarBalance := balanceOfByRegisterCall(barPath, tr01) + oldTr01Token0Balance := balanceOfByRegisterCall(barPath, tr01) oldTr01BazBalance := balanceOfByRegisterCall(bazPath, tr01) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) poolIn, poolOut := Swap( @@ -133,18 +137,19 @@ func TestSwapBarBazBarToBaz(t *testing.T) { true, bigint(16000), MIN_PRICE, + std.GetOrigCaller(), ) shouldEQ(t, poolIn, bigint(16000)) shouldEQ(t, poolOut, bigint(-43457)) - newTr01BarBalance := balanceOfByRegisterCall(barPath, tr01) + newTr01Token0Balance := balanceOfByRegisterCall(barPath, tr01) newTr01BazBalance := balanceOfByRegisterCall(bazPath, tr01) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) newPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) - shouldEQ(t, oldTr01BarBalance-newTr01BarBalance, bigint(16000)) + shouldEQ(t, oldTr01Token0Balance-newTr01Token0Balance, bigint(16000)) shouldEQ(t, newTr01BazBalance-oldTr01BazBalance, bigint(43457)) - shouldEQ(t, newPoolBarBalance-oldPoolBarBalance, bigint(16000)) + shouldEQ(t, newPoolToken0Balance-oldPoolToken0Balance, bigint(16000)) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, bigint(43457)) } @@ -153,10 +158,10 @@ func TestCollectFooBarFees(t *testing.T) { std.TestSetOrigCaller(lp01) std.TestSetPrevRealm("gno.land/r/position") - oldLp01FooBalance := balanceOfByRegisterCall(fooPath, lp01) - oldLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) - oldPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldLp01Token1Balance := balanceOfByRegisterCall(fooPath, lp01) + oldLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) + oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) // burn 0 to collect swap fees Burn(fooPath, barPath, pFee, test_tickLower, test_tickUpper, 0) @@ -175,15 +180,15 @@ func TestCollectFooBarFees(t *testing.T) { shouldNEQ(t, c0, bigint(0)) // swap was foo > bar, so only foo has fees shouldEQ(t, c1, bigint(0)) // swap was foo > bar, so bar has no fees - newLp01FooBalance := balanceOfByRegisterCall(fooPath, lp01) - newLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) - newPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newLp01Token1Balance := balanceOfByRegisterCall(fooPath, lp01) + newLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) + newPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) - shouldEQ(t, newLp01FooBalance-oldLp01FooBalance, uint64(c0)) - shouldEQ(t, newLp01BarBalance-oldLp01BarBalance, uint64(c1)) - shouldEQ(t, oldPoolFooBalance-newPoolFooBalance, uint64(c0)) - shouldEQ(t, oldPoolBarBalance-newPoolBarBalance, uint64(c1)) + shouldEQ(t, newLp01Token1Balance-oldLp01Token1Balance, uint64(c0)) + shouldEQ(t, newLp01Token0Balance-oldLp01Token0Balance, uint64(c1)) + shouldEQ(t, oldPoolToken1Balance-newPoolToken1Balance, uint64(c0)) + shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, uint64(c1)) } // 9. Collect Bar:Baz Fees by lp01 @@ -191,9 +196,9 @@ func TestCollectBarBazFees(t *testing.T) { std.TestSetOrigCaller(lp01) std.TestSetPrevRealm("gno.land/r/position") - oldLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) + oldLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) oldLp01BazBalance := balanceOfByRegisterCall(bazPath, lp01) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) // burn 0 to collect swap fees @@ -213,14 +218,14 @@ func TestCollectBarBazFees(t *testing.T) { shouldNEQ(t, c0, bigint(0)) // swap was foo > bar, so only foo has fees shouldEQ(t, c1, bigint(0)) // swap was foo > bar, so bar has no fees - newLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) + newLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) newLp01BazBalance := balanceOfByRegisterCall(bazPath, lp01) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) newPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) - shouldEQ(t, newLp01BarBalance-oldLp01BarBalance, uint64(c0)) + shouldEQ(t, newLp01Token0Balance-oldLp01Token0Balance, uint64(c0)) shouldEQ(t, newLp01BazBalance-oldLp01BazBalance, uint64(c1)) - shouldEQ(t, oldPoolBarBalance-newPoolBarBalance, uint64(c0)) + shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, uint64(c0)) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, uint64(c1)) } @@ -279,10 +284,10 @@ func TestCollectFooBarLiquidity(t *testing.T) { std.TestSetOrigCaller(lp01) std.TestSetPrevRealm("gno.land/r/position") - oldLp01FooBalance := balanceOfByRegisterCall(fooPath, lp01) - oldLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) - oldPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldLp01Token1Balance := balanceOfByRegisterCall(fooPath, lp01) + oldLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) + oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) c0, c1 := Collect( fooPath, @@ -298,15 +303,15 @@ func TestCollectFooBarLiquidity(t *testing.T) { shouldNEQ(t, c0, bigint(0)) shouldNEQ(t, c1, bigint(0)) - newLp01FooBalance := balanceOfByRegisterCall(fooPath, lp01) - newLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) - newPoolFooBalance := balanceOfByRegisterCall(fooPath, poolAddr) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newLp01Token1Balance := balanceOfByRegisterCall(fooPath, lp01) + newLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) + newPoolToken1Balance := balanceOfByRegisterCall(fooPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) - shouldEQ(t, newLp01FooBalance-oldLp01FooBalance, uint64(c0)) - shouldEQ(t, newLp01BarBalance-oldLp01BarBalance, uint64(c1)) - shouldEQ(t, oldPoolFooBalance-newPoolFooBalance, uint64(c0)) - shouldEQ(t, oldPoolBarBalance-newPoolBarBalance, uint64(c1)) + shouldEQ(t, newLp01Token1Balance-oldLp01Token1Balance, uint64(c0)) + shouldEQ(t, newLp01Token0Balance-oldLp01Token0Balance, uint64(c1)) + shouldEQ(t, oldPoolToken1Balance-newPoolToken1Balance, uint64(c0)) + shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, uint64(c1)) } // 13. Collect Bar:Baz burned Liquidity by lp01 @@ -314,9 +319,9 @@ func TestCollectBarBazLiquidity(t *testing.T) { std.TestSetOrigCaller(lp01) std.TestSetPrevRealm("gno.land/r/position") - oldLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) + oldLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) oldLp01BazBalance := balanceOfByRegisterCall(bazPath, lp01) - oldPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + oldPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) c0, c1 := Collect( @@ -333,14 +338,14 @@ func TestCollectBarBazLiquidity(t *testing.T) { shouldNEQ(t, c0, bigint(0)) shouldNEQ(t, c1, bigint(0)) - newLp01BarBalance := balanceOfByRegisterCall(barPath, lp01) + newLp01Token0Balance := balanceOfByRegisterCall(barPath, lp01) newLp01BazBalance := balanceOfByRegisterCall(bazPath, lp01) - newPoolBarBalance := balanceOfByRegisterCall(barPath, poolAddr) + newPoolToken0Balance := balanceOfByRegisterCall(barPath, poolAddr) newPoolBazBalance := balanceOfByRegisterCall(bazPath, poolAddr) - shouldEQ(t, newLp01BarBalance-oldLp01BarBalance, uint64(c0)) + shouldEQ(t, newLp01Token0Balance-oldLp01Token0Balance, uint64(c0)) shouldEQ(t, newLp01BazBalance-oldLp01BazBalance, uint64(c1)) - shouldEQ(t, oldPoolBarBalance-newPoolBarBalance, uint64(c0)) + shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, uint64(c0)) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, uint64(c1)) } diff --git a/pool/_TEST_pool_same_token_pair_diff_fee_test.gnoa b/pool/_TEST_pool_same_token_pair_diff_fee_test.gnoa index 28b87dd5..f116ce49 100644 --- a/pool/_TEST_pool_same_token_pair_diff_fee_test.gnoa +++ b/pool/_TEST_pool_same_token_pair_diff_fee_test.gnoa @@ -14,6 +14,7 @@ var ( gsa = testutils.TestAddress("gsa") // Gnoswap Admin lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 tr01 = testutils.TestAddress("tr01") // Trader 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") @@ -38,6 +39,8 @@ var ( func TestFirstPool(t *testing.T) { std.TestSetOrigCaller(gsa) InitManual() + + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee1, 130621891405341611593710811006) shouldPanic(t, func() { CreatePool(fooPath, barPath, pFee1, 130621891405341611593710811006) }) @@ -57,22 +60,22 @@ func TestFirstPool(t *testing.T) { // Swap several times std.TestSetOrigCaller(tr01) { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(10000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(10000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -81,22 +84,22 @@ func TestFirstPool(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(5000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(5000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -105,22 +108,22 @@ func TestFirstPool(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(1000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, true, bigint(1000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -130,22 +133,22 @@ func TestFirstPool(t *testing.T) { // Swap token1 -> token0 { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, false, bigint(160000), MAX_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee1, tr01, false, bigint(160000), MAX_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -163,38 +166,38 @@ func TestFirstPool(t *testing.T) { // Make ProtocolFee via Swap by tr01 ( Mint by lp01 ) std.TestSetOrigCaller(lp01) { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee1, tr01, true, 100000, MIN_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee1, tr01, true, 100000, MIN_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee1, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee1, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee1, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) } { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee1, tr01, false, 100000, MAX_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee1, tr01, false, 100000, MAX_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee1, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee1, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee1, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) @@ -203,8 +206,9 @@ func TestFirstPool(t *testing.T) { // foo & bar & 3000 func TestSecondPool(t *testing.T) { - std.TestSetOrigCaller(gsa) - // Init() + // std.TestSetOrigCaller(gsa) + // InitManual() + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee2, 130621891405341611593710811006) shouldPanic(t, func() { CreatePool(fooPath, barPath, pFee2, 130621891405341611593710811006) }) @@ -224,22 +228,22 @@ func TestSecondPool(t *testing.T) { // Swap several times std.TestSetOrigCaller(tr01) { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(10000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(10000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -248,22 +252,22 @@ func TestSecondPool(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(5000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(5000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -272,22 +276,22 @@ func TestSecondPool(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(1000), MIN_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, true, bigint(1000), MIN_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -297,22 +301,22 @@ func TestSecondPool(t *testing.T) { // Swap token1 -> token0 { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, false, bigint(160000), MAX_PRICE) + amount0, amount1 := Swap(fooPath, barPath, pFee2, tr01, false, bigint(160000), MAX_PRICE, std.GetOrigCaller()) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -330,38 +334,38 @@ func TestSecondPool(t *testing.T) { // Make ProtocolFee via Swap by tr01 ( Mint by lp01 ) std.TestSetOrigCaller(lp01) { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee2, tr01, true, 100000, MIN_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee2, tr01, true, 100000, MIN_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee2, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee2, tr01, true, 100000, MIN_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee1, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) } { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee2, tr01, false, 100000, MAX_PRICE) // swap token0 -> token1 => fee only in token0 - Swap(fooPath, barPath, pFee2, tr01, false, 100000, MAX_PRICE) // more protocol fee + Swap(fooPath, barPath, pFee2, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee2, tr01, false, 100000, MAX_PRICE, std.GetOrigCaller()) // more protocol fee // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee2, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) diff --git a/pool/_TEST_pool_single_lp_test.gnoa b/pool/_TEST_pool_single_lp_test.gnoa index 8547e9e0..9e36beec 100644 --- a/pool/_TEST_pool_single_lp_test.gnoa +++ b/pool/_TEST_pool_single_lp_test.gnoa @@ -14,6 +14,7 @@ var ( gsa = testutils.TestAddress("gsa") // Gnoswap Admin lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 tr01 = testutils.TestAddress("tr01") // Trader 01 + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 poolAddr = std.DerivePkgAddr("gno.land/r/pool") posAddr = std.DerivePkgAddr("gno.land/r/position") @@ -37,6 +38,8 @@ var ( func TestInitCreatePool(t *testing.T) { std.TestSetOrigCaller(gsa) InitManual() + + std.TestSetOrigCaller(pc01) CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006) // sqrtPrice @@ -207,11 +210,11 @@ func TestSwap(t *testing.T) { test_price := bigint(MIN_PRICE) { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) amount0, amount1 := Swap( fooPath, @@ -221,16 +224,17 @@ func TestSwap(t *testing.T) { true, bigint(10000), test_price, + std.GetOrigCaller(), ) shouldNEQ(t, amount0, bigint(0)) shouldNEQ(t, amount1, bigint(0)) - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -239,19 +243,19 @@ func TestSwap(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, 5000, test_price) // give enough amount to take fees away + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, 5000, test_price, std.GetOrigCaller()) // give enough amount to take fees away - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -260,19 +264,19 @@ func TestSwap(t *testing.T) { } { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, 1000, test_price) // give enough amount to take fees away + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, true, 1000, test_price, std.GetOrigCaller()) // give enough amount to take fees away - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -282,19 +286,19 @@ func TestSwap(t *testing.T) { // Swap token1 -> token0 { - poolOldToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolOldToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userOldToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userOldToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) - amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, false, 16000, MAX_PRICE) // give enough amount to take fees away + amount0, amount1 := Swap(fooPath, barPath, pFee, tr01, false, 16000, MAX_PRICE, std.GetOrigCaller()) // give enough amount to take fees away - poolNewToken0Bal := balanceOfByRegisterCall(fooPath, poolAddr) - poolNewToken1Bal := balanceOfByRegisterCall(barPath, poolAddr) + poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) + poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) - userNewToken0Bal := balanceOfByRegisterCall(fooPath, tr01) - userNewToken1Bal := balanceOfByRegisterCall(barPath, tr01) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, tr01) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, tr01) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(amount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-amount1)) @@ -330,36 +334,36 @@ func TestCollectProtocol(t *testing.T) { // Make ProtocolFee via Swap by tr01 ( Mint by lp01 ) std.TestSetOrigCaller(lp01) { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee, tr01, true, 200000, MIN_PRICE) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee, tr01, true, 200000, MIN_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(barPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, gsaNewToken0Bal-gsaOldToken0Bal, int64(amount0)) shouldEQ(t, gsaNewToken1Bal-gsaOldToken1Bal, int64(amount1)) } { - gsaOldToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaOldToken1Bal := balanceOfByRegisterCall(fooPath, gsa) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetOrigCaller(tr01) - Swap(fooPath, barPath, pFee, tr01, false, 200000, MAX_SQRT_RATIO-1) // swap token0 -> token1 => fee only in token0 + Swap(fooPath, barPath, pFee, tr01, false, 200000, MAX_SQRT_RATIO-1, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 // Gnoswap Admin will collect protocol fee std.TestSetOrigCaller(gsa) amount0, amount1 := CollectProtocol(fooPath, barPath, pFee, gsa, 100000, 100000) - gsaNewToken0Bal := balanceOfByRegisterCall(fooPath, gsa) - gsaNewToken1Bal := balanceOfByRegisterCall(fooPath, gsa) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) } } diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index fc9d482a..8aefc8cc 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -5,6 +5,8 @@ import ( "strconv" "gno.land/p/demo/ufmt" + + gns "gno.land/r/gns" ) var ( @@ -56,6 +58,11 @@ func CreatePool( require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolKey)) if !exist { + // 500 GNS as creation fee + // recipent is same address that receives protocol fee + // r3v4_xxx: change address when publish + gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(std.Address("g1vaekzh6lta047h6lta047h6lta047h6lutmjdk")), 500) + pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96) pools[poolKey] = pool } diff --git a/pool/pool_register.gno b/pool/pool_register.gno index 265d9227..6c8f0b19 100644 --- a/pool/pool_register.gno +++ b/pool/pool_register.gno @@ -47,8 +47,8 @@ func removeGRC20Interface(pkgPath string) { func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) { // only admin can register - // r3v4_xx: below logic can't be used in test case - // r3v4_xx: however must be used in production + // r3v4_xxx: below logic can't be used in test case + // r3v4_xxx: however must be used in production // caller := std.GetOrigCaller() // if caller != APPROVED_CALLER { diff --git a/router/quotation.gno b/router/quotation.gno index f3b59101..80e449b9 100644 --- a/router/quotation.gno +++ b/router/quotation.gno @@ -72,7 +72,7 @@ func quoteForAllPath( recipient: std.GetOrigCaller(), amountSpecified: quoterTarget.pctAmount, - minAmountOut: 1, // r3v4_xx: sqrtPriceLimitX96 + minAmountOut: 1, } // if amountSpecified > 0, proceed forward diff --git a/router/router_register.gno b/router/router_register.gno index f7650b6d..85763981 100644 --- a/router/router_register.gno +++ b/router/router_register.gno @@ -47,8 +47,8 @@ func removeGRC20Interface(pkgPath string) { func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) { // only admin can register - // r3v4_xx: below logic can't be used in test case - // r3v4_xx: however must be used in production + // r3v4_xxx: below logic can't be used in test case + // r3v4_xxx: however must be used in production // caller := std.GetOrigCaller() // if caller != APPROVED_CALLER { diff --git a/staker/reward_math.gno b/staker/reward_math.gno index 0e8e2488..89fd1d23 100644 --- a/staker/reward_math.gno +++ b/staker/reward_math.gno @@ -31,7 +31,7 @@ func rewardMathComputeInternalRewardAmount(tokenId uint64, deposit Deposit) uint // calculate my actual reward amount based on staking duration // !IMPORTANT - // r3v4_xx: assume block calcution will happen every 1 block + // r3v4_xxx: assume block calcution will happen every 1 block // unlikely external, internal reward program is supposed to be started at the same time as gnoswap launch and never end // so there is no need to consider start/end time of reward program // start time is staked time, end time is current time @@ -87,7 +87,7 @@ func rewardMathComputeExternalRewardAmount(tokenId uint64, deposit Deposit, ince // calculate my actual reward amount based on staking duration // !IMPORTANT - // r3v4_xx: assume block calcution will happen every 1 block + // r3v4_xxx: assume block calcution will happen every 1 block // stake start time ( largest value between incentive start time and deposit stake time ) // stake end time ( smallest value between incentive end time and current time) startTime := max(incentive.startTimestamp, deposit.stakeTimestamp)