From 35497001c2de82196b08ab37b520cbf0bdcda50a Mon Sep 17 00:00:00 2001 From: SunSpirit <48086732+sunspirit99@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:34:30 +0700 Subject: [PATCH] fix(velodrome) : Prevent from swapping the entire reserve in pool --- pkg/source/velodrome/pool_simulator.go | 2 +- pkg/source/velodrome/pool_simulator_test.go | 45 ++++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/pkg/source/velodrome/pool_simulator.go b/pkg/source/velodrome/pool_simulator.go index 1e7241192..9a1db6412 100644 --- a/pkg/source/velodrome/pool_simulator.go +++ b/pkg/source/velodrome/pool_simulator.go @@ -81,7 +81,7 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.Cal return &pool.CalcAmountOutResult{}, fmt.Errorf("amountOut is %d", amountOut.Int64()) } - if amountOut.Cmp(p.Info.Reserves[tokenOutIndex]) > 0 { + if amountOut.Cmp(p.Info.Reserves[tokenOutIndex]) >= 0 { return &pool.CalcAmountOutResult{}, fmt.Errorf("amountOut is %d bigger than reserve %d", amountOut.Int64(), p.Info.Reserves[tokenOutIndex]) } diff --git a/pkg/source/velodrome/pool_simulator_test.go b/pkg/source/velodrome/pool_simulator_test.go index 2f2334588..56ef6ebba 100644 --- a/pkg/source/velodrome/pool_simulator_test.go +++ b/pkg/source/velodrome/pool_simulator_test.go @@ -20,26 +20,30 @@ func TestCalcAmountOut(t *testing.T) { inAmount string out string expectedOutAmount string + reserveA string + reserveB string + swapFee float64 + expectError bool }{ - {"B", "100", "A", "57341222888"}, - {"A", "100000000000", "B", "174"}, + {"B", "100", "A", "172023517829", "2082415614000308399878", "3631620514949", 0.0005, false}, + {"A", "100000000000", "B", "58", "2082415614000308399878", "3631620514949", 0.0005, false}, + {"A", "1631", "B", "0", "503877670", "1", 0.0005, true}, // do not allow swapping the entire reserve in the pool } - p, err := NewPoolSimulator(entity.Pool{ - Exchange: "", - Type: "", - SwapFee: 0.0005, // from factory https://optimistic.etherscan.io/address/0x25cbddb98b35ab1ff77413456b31ec81a6b6b746#readContract - Reserves: entity.PoolReserves{"2082415614000308399878", "3631620514949"}, - Tokens: []*entity.PoolToken{{Address: "A", Decimals: 18}, {Address: "B", Decimals: 6}}, - StaticExtra: "{\"stable\": false}", - }) - require.Nil(t, err) - - assert.Equal(t, []string{"A"}, p.CanSwapTo("B")) - assert.Equal(t, []string{"B"}, p.CanSwapTo("A")) - for idx, tc := range testcases { t.Run(fmt.Sprintf("test %d", idx), func(t *testing.T) { + p, err := NewPoolSimulator(entity.Pool{ + Exchange: "", + Type: "", + SwapFee: tc.swapFee, + Reserves: entity.PoolReserves{tc.reserveA, tc.reserveB}, + Tokens: []*entity.PoolToken{{Address: "A", Decimals: 18}, {Address: "B", Decimals: 6}}, + StaticExtra: "{\"stable\": true}", + }) + require.Nil(t, err) + + assert.Equal(t, []string{"A"}, p.CanSwapTo("B")) + assert.Equal(t, []string{"B"}, p.CanSwapTo("A")) amountIn := pool.TokenAmount{Token: tc.in, Amount: bignumber.NewBig10(tc.inAmount)} out, err := testutil.MustConcurrentSafe[*pool.CalcAmountOutResult](t, func() (any, error) { @@ -48,9 +52,14 @@ func TestCalcAmountOut(t *testing.T) { TokenOut: tc.out, }) }) - require.Nil(t, err) - assert.Equal(t, bignumber.NewBig10(tc.expectedOutAmount), out.TokenAmountOut.Amount) - assert.Equal(t, tc.out, out.TokenAmountOut.Token) + + if tc.expectError { + assert.Error(t, err, "Expected an error but got nil") + } else { + require.NoError(t, err, "Did not expect an error but got one") + assert.Equal(t, bignumber.NewBig10(tc.expectedOutAmount), out.TokenAmountOut.Amount) + assert.Equal(t, tc.out, out.TokenAmountOut.Token) + } }) } }