Unverified Commit df489db1 authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub
Browse files

Tests: Join pool for non-even ratio (backport #1767) (#1780)


* Tests: Join pool for non-even ratio (#1767)

* added new tests

* tests

* wip

* finish tests

* fix test

* typo

* Add comments

* DRY some of the code

Co-authored-by: default avatarstackman27 <sis1001@berkeley.edu>
Co-authored-by: default avatarDev Ojha <dojha@berkeley.edu>
(cherry picked from commit 2cc8fedc

)

# Conflicts:
#	x/gamm/pool-models/balancer/amm_test.go

* Apply suggestions from code review

Co-authored-by: default avatarMatt, Park <45252226+mattverse@users.noreply.github.com>
Co-authored-by: default avatarDev Ojha <ValarDragon@users.noreply.github.com>
parent cb7506e9
Showing with 25 additions and 23 deletions
+25 -23
......@@ -722,6 +722,16 @@ func TestCalcSingleAssetInAndOut_InverseRelationship(t *testing.T) {
}
func TestCalcJoinPoolShares(t *testing.T) {
oneTrillionEvenPoolAssets := []balancer.PoolAsset{
{
Token: sdk.NewInt64Coin("uosmo", 1_000_000_000_000),
Weight: sdk.NewInt(100),
},
{
Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000),
Weight: sdk.NewInt(100),
},
}
// We append shared calcSingleAssetJoinTestCases with multi-asset and edge
// test cases.
//
......@@ -729,18 +739,9 @@ func TestCalcJoinPoolShares(t *testing.T) {
// sharing is needed.
testCases := []calcJoinSharesTestCase{
{
name: "swap equal weights with zero swap fee",
swapFee: sdk.MustNewDecFromStr("0"),
poolAssets: []balancer.PoolAsset{
{
Token: sdk.NewInt64Coin("uosmo", 1_000_000_000_000),
Weight: sdk.NewInt(100),
},
{
Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000),
Weight: sdk.NewInt(100),
},
},
name: "swap equal weights with zero swap fee",
swapFee: sdk.MustNewDecFromStr("0"),
poolAssets: oneTrillionEvenPoolAssets,
tokensIn: sdk.NewCoins(
sdk.NewInt64Coin("uosmo", 25_000),
sdk.NewInt64Coin("uatom", 25_000),
......@@ -774,7 +775,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
// then we perfrom single asset deposit for the remaining 25,000 uatom with the equation below
// Expected output from Balancer paper (https://balancer.fi/whitepaper.pdf) using equation (25) on page 10:
// P_issued = P_supply * ((1 + (A_t * swapFeeRatio / B_t))^W_t - 1)
// 1_249_999_960_937 = (100 * 10^18 + 2.5e12) * (( 1 + (25000 * 1 / 1000000025000))^0.5 - 1) (without fee)
// 1_249_999_960_937 = 100 * 10^18 * (( 1 + (25000 * 1 / 1000000025000))^0.5 - 1) (without fee)
//
// where:
// P_supply = initial pool supply = 100 * 10^18 (set at pool creation, same for all new pools)
......@@ -783,7 +784,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
// W_t = normalized weight of deposited asset in pool = 0.5 (equally weighted two-asset pool)
// swapFeeRatio = (1 - (1 - W_t) * swapFee)
// Plugging all of this in, we get:
// Full Solution without fees: https://www.wolframalpha.com/input?i=%28100+*+10%5E18+%2B+2.5e12+%29*+%28%28+1%2B+++++%2825000+*+%281%29+%2F+1000000025000%29%29%5E0.5+-+1%29
// Full Solution without fees: https://www.wolframalpha.com/input?i=100+*+10%5E18+*+%28%28+1+%2B+++%2825000+*+%281%29+%2F+1000000025000%29%29%5E0.5+-+1%29
// Simplified: P_issued = 2_500_000_000_000 + 1_249_999_960_937
name: "Multi-tokens In: unequal amounts, equal weights with 0 swap fee",
......@@ -794,7 +795,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
sdk.NewInt64Coin("uatom", 50_000),
),
expectShares: sdk.NewInt(2.5e12 + 1249999992187),
expectShares: sdk.NewInt(2.5e12 + 1249999960937),
expectLiq: sdk.NewCoins(
sdk.NewInt64Coin("uosmo", 25_000),
sdk.NewInt64Coin("uatom", 50_000),
......@@ -806,7 +807,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
// then we perfrom single asset deposit for the remaining 25,000 uatom with the equation below
// Expected output from Balancer paper (https://balancer.fi/whitepaper.pdf) using equation (25) on page 10:
// P_issued = P_supply * ((1 + (A_t * swapFeeRatio / B_t))^W_t - 1)
// 1_243_750_000_000 = (100 * 10^18 + 2.5e12)* (( 1 + (25000 * (1 - (1 - 0.5) * 0.01) / 1000000025000))^0.5 - 1)
// 1_243_750_000_000 = 100 * 10^18 * (( 1 + (25000 * (1 - (1 - 0.5) * 0.01) / 1000000025000))^0.5 - 1)
//
// where:
// P_supply = initial pool supply = 100 * 10^18 (set at pool creation, same for all new pools)
......@@ -815,7 +816,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
// W_t = normalized weight of deposited asset in pool = 0.5 (equally weighted two-asset pool)
// swapFeeRatio = (1 - (1 - W_t) * swapFee)
// Plugging all of this in, we get:
// Full solution with fees: https://www.wolframalpha.com/input?i=%28100+*10%5E18%2B2.5e12%29*%28%281%2B+++%2825000*%281+-+%281-0.5%29+*+0.01%29%2F1000000000000%29%29%5E0.5+-+1%29
// Full solution with fees: https://www.wolframalpha.com/input?i=100+*10%5E18*%28%281+%2B+%2825000*%281+-+%281-0.5%29+*+0.01%29%2F1000000000000%29%29%5E0.5+-+1%29
// Simplified: P_issued = 2_500_000_000_000 + 1_243_750_000_000
name: "Multi-tokens In: unequal amounts, equal weights with 0.01 swap fee",
......@@ -826,7 +827,7 @@ func TestCalcJoinPoolShares(t *testing.T) {
sdk.NewInt64Coin("uatom", 50_000),
),
expectShares: sdk.NewInt(2.5e12 + 1243750000000),
expectShares: sdk.NewInt(2.5e12 + 1243749900000),
expectLiq: sdk.NewCoins(
sdk.NewInt64Coin("uosmo", 25_000),
sdk.NewInt64Coin("uatom", 50_000),
......@@ -855,8 +856,8 @@ func TestCalcJoinPoolShares(t *testing.T) {
swapFee: sdk.MustNewDecFromStr("0.03"),
poolAssets: []balancer.PoolAsset{
{
Token: sdk.NewInt64Coin("uosmo", 1_000_000_000_000),
Weight: sdk.NewInt(100),
Token: sdk.NewInt64Coin("uosmo", 2_000_000_000_000),
Weight: sdk.NewInt(500),
},
{
Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000),
......@@ -865,12 +866,13 @@ func TestCalcJoinPoolShares(t *testing.T) {
},
tokensIn: sdk.NewCoins(
sdk.NewInt64Coin("uosmo", 25_000),
sdk.NewInt64Coin("uatom", 25_000),
sdk.NewInt64Coin("uatom", 50_000),
),
expectShares: sdk.NewInt(2500000000000),
expectShares: sdk.NewInt(1250000000000 + 609374990000),
expectLiq: sdk.NewCoins(
sdk.NewInt64Coin("uosmo", 25_000),
sdk.NewInt64Coin("uatom", 25_000),
sdk.NewInt64Coin("uatom", 50_000),
),
},
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment