Skip to content

Commit

Permalink
ft: implement hashflow-v3 CloneState
Browse files Browse the repository at this point in the history
  • Loading branch information
NgoKimPhu committed Dec 16, 2024
1 parent e56b729 commit 25999bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pkg/liquidity-source/hashflow-v3/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ func (p *PoolSimulator) CalcAmountIn(params pool.CalcAmountInParams) (*pool.Calc
}
}

func (p *PoolSimulator) CloneState() pool.IPoolSimulator {
cloned := *p
cloned.ZeroToOnePriceLevels = lo.Map(p.ZeroToOnePriceLevels, func(v PriceLevel, i int) PriceLevel {
v.Quote = new(big.Float).Set(v.Quote)
return v
})
cloned.OneToZeroPriceLevels = lo.Map(p.OneToZeroPriceLevels, func(v PriceLevel, i int) PriceLevel {
v.Quote = new(big.Float).Set(v.Quote)
return v
})
return &cloned
}

func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
var amountInAfterDecimals, decimalsPow, amountInBF big.Float
amountInBF.SetInt(params.TokenAmountIn.Amount)
Expand Down
24 changes: 19 additions & 5 deletions pkg/liquidity-source/hashflow-v3/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"math/big"
"testing"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
)

var (
Expand Down Expand Up @@ -83,11 +84,24 @@ func TestPoolSimulator_GetAmountOut(t *testing.T) {
TokenOut: tokenOMG.Address,
}

result, err := poolSimulator.CalcAmountOut(params)
sim := poolSimulator.CloneState()
cloned := sim.CloneState()
result, err := sim.CalcAmountOut(params)
assert.Equal(t, tc.expectedErr, err)
if tc.expectedErr == nil {
assert.Equal(t, 0, result.TokenAmountOut.Amount.Cmp(tc.expectedAmountOut))
if tc.expectedErr != nil {
return
}
assert.Equal(t, 0, result.TokenAmountOut.Amount.Cmp(tc.expectedAmountOut))

sim.UpdateBalance(pool.UpdateBalanceParams{
TokenAmountIn: params.TokenAmountIn,
TokenAmountOut: *result.TokenAmountOut,
SwapInfo: result.SwapInfo,
})

clonedRes, err := cloned.CalcAmountOut(params)
assert.Equal(t, tc.expectedErr, err)
assert.Equal(t, result, clonedRes)
})
}
}
Expand Down

0 comments on commit 25999bc

Please sign in to comment.