Skip to content

Commit

Permalink
fix(velodrome) : Prevent from swapping the entire reserve in pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 authored Nov 8, 2024
1 parent 136a17e commit 3549700
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/source/velodrome/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
45 changes: 27 additions & 18 deletions pkg/source/velodrome/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
})
}
}

0 comments on commit 3549700

Please sign in to comment.