diff --git a/pkg/source/limitorder/pool_simulator_calc_amount_in.go b/pkg/source/limitorder/pool_simulator_calc_amount_in.go index cc2a0ead9..fe570a66f 100644 --- a/pkg/source/limitorder/pool_simulator_calc_amount_in.go +++ b/pkg/source/limitorder/pool_simulator_calc_amount_in.go @@ -83,7 +83,10 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO rate := new(big.Float).Quo(new(big.Float).SetInt(order.MakingAmount), new(big.Float).SetInt(order.TakingAmount)) amountInWei := new(big.Float).Quo(new(big.Float).SetInt(totalAmountOutBeforeFee), rate) filledMakingAmountWei := totalAmountOutBeforeFee - filledTakingAmountWei, _ := amountInWei.Int(nil) + filledTakingAmountWei, acc := amountInWei.Int(nil) + if acc == big.Below { + filledTakingAmountWei.Add(filledTakingAmountWei, big.NewInt(1)) + } // order too small if filledTakingAmountWei.Cmp(constant.ZeroBI) == 0 { @@ -91,8 +94,8 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO } actualAmountIn, feeAmountWeiByOrder := p.calcTakerAssetFeeAmountExactOut(order, filledTakingAmountWei) - totalFeeAmountWei.Add(totalFeeAmountWei, feeAmountWeiByOrder) - totalAmountInWei.Add(totalAmountInWei, actualAmountIn) + totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) + totalAmountInWei = new(big.Int).Add(totalAmountInWei, actualAmountIn) filledOrderInfo := newFilledOrderInfo(order, filledTakingAmountWei.String(), filledMakingAmountWei.String(), feeAmountWeiByOrder.String()) swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) isFulfillAmountOut = true @@ -120,10 +123,10 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO } break } - totalAmountOut.Sub(totalAmountOut, remainingMakingAmountWei) + totalAmountOut = new(big.Int).Sub(totalAmountOut, remainingMakingAmountWei) _, takerAssetFee := p.calcTakerAssetFeeAmountExactOut(order, remainingTakingAmountWei) actualAmountIn := new(big.Int).Add(remainingTakingAmountWei, takerAssetFee) - totalAmountInWei.Add(totalAmountInWei, actualAmountIn) + totalAmountInWei = new(big.Int).Add(totalAmountInWei, actualAmountIn) totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, takerAssetFee) filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), takerAssetFee.String()) swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) diff --git a/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go b/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go new file mode 100644 index 000000000..25167955b --- /dev/null +++ b/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go @@ -0,0 +1,867 @@ +package limitorder + +import ( + "math/big" + "testing" + + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/swaplimit" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/testutil" + "github.com/stretchr/testify/assert" +) + +const ( + tokenUSDC = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174" + tokenUSDT = "0xc2132d05d31c914a87c6611c10748aeb04b58e8f" +) + +func TestPool_CalcAmountIn(t *testing.T) { + type args struct { + tokenIn string + tokenAmountOut pool.TokenAmount + } + tests := []struct { + name string + poolEntity entity.Pool + args args + want *pool.CalcAmountInResult + err error + }{ + { + name: "Should return correct CalcAmountInResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut))", + poolEntity: entity.Pool{ + Address: "pool_limit_order_", + ReserveUsd: 1000000000, + AmplifiedTvl: 0, + SwapFee: 0, + Exchange: "kyberswap_limit-order", + Type: "limit-order", + Timestamp: 0, + Reserves: []string{"10000000000000000000", "10000000000000000000"}, + Tokens: []*entity.PoolToken{ + { + Address: tokenUSDT, + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: tokenUSDC, + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + BuyOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("200"), + MakingAmount: parseBigInt("400"), + FeeConfig: parseBigInt("100"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("300"), + MakingAmount: parseBigInt("300"), + FeeConfig: parseBigInt("100"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + IsTakerAssetFee: true, + }, + }, + SellOrders: []*order{}, + }), + TotalSupply: "", + }, + args: args{ + tokenAmountOut: pool.TokenAmount{ + Token: tokenUSDT, + Amount: parseBigInt("500"), + AmountUsd: 0, + }, + tokenIn: tokenUSDC, + }, + want: &pool.CalcAmountInResult{ + TokenAmountIn: &pool.TokenAmount{ + Token: tokenUSDC, + Amount: parseBigInt("300"), + AmountUsd: 0, + }, + Fee: &pool.TokenAmount{ + Token: tokenUSDC, + Amount: big.NewInt(0), + AmountUsd: 0, + }, + Gas: 136616, + SwapInfo: SwapInfo{ + AmountIn: "300", + SwapSide: Buy, + FilledOrders: []*FilledOrderInfo{ + { + OrderID: 1383, + FilledTakingAmount: "200", + FilledMakingAmount: "400", + TakingAmount: "200", + MakingAmount: "400", + Salt: "185786982651412687203851465093295409688", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + FeeConfig: "100", + FeeRecipient: "0x0000000000000000000000000000000000000000", + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + Signature: "signature1", + FeeAmount: "0", + }, + { + OrderID: 1382, + FilledTakingAmount: "100", + FilledMakingAmount: "100", + TakingAmount: "300", + MakingAmount: "300", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + FeeConfig: "100", + FeeRecipient: "0x0000000000000000000000000000000000000000", + AllowedSenders: "0x0000000000000000000000000000000000000000", + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + FeeAmount: "0", + }, + }, + }, + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult when swapSide is SELL(strings.ToLower(tokeIn) > strings.ToLower(TokenOut))", + poolEntity: entity.Pool{ + Address: "pool_limit_order_", + ReserveUsd: 1000000000, + AmplifiedTvl: 0, + SwapFee: 0, + Exchange: "kyberswap_limit-order", + Type: "limit-order", + Timestamp: 0, + Reserves: []string{"10000000000000000000", "10000000000000000000"}, + Tokens: []*entity.PoolToken{ + { + Address: tokenUSDT, + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: tokenUSDC, + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + SellOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("992000"), + MakingAmount: parseBigInt("1000000"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("1010000"), + MakingAmount: parseBigInt("1000000"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + }, + BuyOrders: []*order{}, + }), + TotalSupply: "", + }, + args: args{ + tokenAmountOut: pool.TokenAmount{ + Token: tokenUSDC, + Amount: parseBigInt("1215841"), + AmountUsd: 0, + }, + tokenIn: tokenUSDT, + }, + want: &pool.CalcAmountInResult{ + TokenAmountIn: &pool.TokenAmount{ + Token: tokenUSDT, + Amount: parseBigInt("1210000"), + AmountUsd: 0, + }, + Fee: &pool.TokenAmount{ + Token: tokenUSDT, + Amount: big.NewInt(0), + AmountUsd: 0, + }, + Gas: 136616, + SwapInfo: SwapInfo{ + AmountIn: "1210000", + SwapSide: Sell, + FilledOrders: []*FilledOrderInfo{ + { + OrderID: 1383, + FilledTakingAmount: "992000", + FilledMakingAmount: "1000000", + TakingAmount: "992000", + MakingAmount: "1000000", + Salt: "185786982651412687203851465093295409688", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + FeeRecipient: "0x0000000000000000000000000000000000000000", + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + Signature: "signature1", + FeeAmount: "0", + }, + { + OrderID: 1382, + FilledTakingAmount: "218000", + FilledMakingAmount: "215841", + TakingAmount: "1010000", + MakingAmount: "1000000", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + FeeRecipient: "0x0000000000000000000000000000000000000000", + AllowedSenders: "0x0000000000000000000000000000000000000000", + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + FeeAmount: "0", + }, + }, + }, + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut)) and orders has MakerTokenFeePercent", + poolEntity: entity.Pool{ + Address: "pool_limit_order_", + ReserveUsd: 1000000000, + AmplifiedTvl: 0, + SwapFee: 0, + Exchange: "kyberswap_limit-order", + Type: "limit-order", + Timestamp: 0, + Reserves: []string{"10000000000000000000", "10000000000000000000"}, + Tokens: []*entity.PoolToken{ + { + Address: tokenUSDT, + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: tokenUSDC, + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + BuyOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("200"), + MakingAmount: parseBigInt("400"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 100, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + IsTakerAssetFee: true, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("300"), + MakingAmount: parseBigInt("300"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + IsTakerAssetFee: true, + }, + }, + SellOrders: []*order{}, + }), + TotalSupply: "", + }, + args: args{ + tokenAmountOut: pool.TokenAmount{ + Token: tokenUSDT, + Amount: parseBigInt("500"), + AmountUsd: 0, + }, + tokenIn: tokenUSDC, + }, + want: &pool.CalcAmountInResult{ + TokenAmountIn: &pool.TokenAmount{ + Token: tokenUSDC, + Amount: parseBigInt("302"), + AmountUsd: 0, + }, + Fee: &pool.TokenAmount{ + Token: tokenUSDC, + Amount: big.NewInt(2), + AmountUsd: 0, + }, + Gas: 136616, + SwapInfo: SwapInfo{ + AmountIn: "300", + SwapSide: Buy, + FilledOrders: []*FilledOrderInfo{ + { + OrderID: 1383, + FilledTakingAmount: "200", + FilledMakingAmount: "400", + TakingAmount: "200", + MakingAmount: "400", + Salt: "185786982651412687203851465093295409688", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + FeeRecipient: "0x0000000000000000000000000000000000000000", + MakerTokenFeePercent: 100, + MakerAssetData: "", + TakerAssetData: "", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + Signature: "signature1", + FeeAmount: "2", + }, + { + OrderID: 1382, + FilledTakingAmount: "100", + FilledMakingAmount: "100", + TakingAmount: "300", + MakingAmount: "300", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDT, + MakerAsset: tokenUSDC, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + FeeRecipient: "0x0000000000000000000000000000000000000000", + AllowedSenders: "0x0000000000000000000000000000000000000000", + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + FeeAmount: "0", + }, + }, + }, + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult and list orders(include fallback orders) when swapSide is SELL(strings.ToLower(tokeIn) > strings.ToLower(TokenOut))", + poolEntity: entity.Pool{ + Address: "pool_limit_order_", + ReserveUsd: 1000000000, + AmplifiedTvl: 0, + SwapFee: 0, + Exchange: "kyberswap_limit-order", + Type: "limit-order", + Timestamp: 0, + Reserves: []string{"10000000000000000000", "10000000000000000000"}, + Tokens: []*entity.PoolToken{ + { + Address: tokenUSDT, + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: tokenUSDC, + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + SellOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("700"), + MakingAmount: parseBigInt("1400"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("240"), + MakingAmount: parseBigInt("250"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1385, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature4", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("200"), + MakingAmount: parseBigInt("300"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1389, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature5", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("100"), + MakingAmount: parseBigInt("100"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + }, + BuyOrders: []*order{}, + }), + TotalSupply: "", + }, + args: args{ + tokenAmountOut: pool.TokenAmount{ + Token: tokenUSDC, + Amount: parseBigInt("1400"), + AmountUsd: 0, + }, + tokenIn: tokenUSDT, + }, + want: &pool.CalcAmountInResult{ + TokenAmountIn: &pool.TokenAmount{ + Token: tokenUSDT, + Amount: parseBigInt("700"), + AmountUsd: 0, + }, + Fee: &pool.TokenAmount{ + Token: tokenUSDT, + Amount: big.NewInt(0), + AmountUsd: 0, + }, + Gas: 159924, + SwapInfo: SwapInfo{ + AmountIn: "700", + SwapSide: Sell, + FilledOrders: []*FilledOrderInfo{ + { + OrderID: 1383, + FilledTakingAmount: "700", + FilledMakingAmount: "1400", + TakingAmount: "700", + MakingAmount: "1400", + Salt: "185786982651412687203851465093295409688", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + FeeRecipient: "0x0000000000000000000000000000000000000000", + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + Signature: "signature1", + FeeAmount: "0", + }, + { + OrderID: 1382, + FilledTakingAmount: "0", + FilledMakingAmount: "0", + TakingAmount: "240", + MakingAmount: "250", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + FeeRecipient: "0x0000000000000000000000000000000000000000", + AllowedSenders: "0x0000000000000000000000000000000000000000", + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + FeeAmount: "0", + IsFallBack: true, + }, + { + OrderID: 1385, + Salt: "185786982651412687203851465093295409688", + Signature: "signature4", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: "200", + MakingAmount: "300", + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: "0", + FilledTakingAmount: "0", + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + IsFallBack: true, + FeeAmount: "0", + }, + }, + }, + }, + err: nil, + }, + { + name: "Should return correct error(ErrCannotFulfillAmountIn) when cannot fulfill amountIn buy the orders", + poolEntity: entity.Pool{ + Address: "pool_limit_order_", + ReserveUsd: 1000000000, + AmplifiedTvl: 0, + SwapFee: 0, + Exchange: "kyberswap_limit-order", + Type: "limit-order", + Timestamp: 0, + Reserves: []string{"10000000000000000000", "10000000000000000000"}, + Tokens: []*entity.PoolToken{ + { + Address: tokenUSDT, + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: tokenUSDC, + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + SellOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("992000"), + MakingAmount: parseBigInt("1000000"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: tokenUSDC, + MakerAsset: tokenUSDT, + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("1010000"), + MakingAmount: parseBigInt("1000000"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 0, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + }, + }, + BuyOrders: []*order{}, + }), + TotalSupply: "", + }, + args: args{ + tokenAmountOut: pool.TokenAmount{ + Token: tokenUSDC, + Amount: parseBigInt("121000000"), + AmountUsd: 0, + }, + tokenIn: tokenUSDT, + }, + want: nil, + err: ErrCannotFulfillAmountOut, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p, err := NewPoolSimulator(tt.poolEntity) + assert.Equal(t, nil, err) + got, err := testutil.MustConcurrentSafe[*pool.CalcAmountInResult](t, func() (any, error) { + limit := swaplimit.NewInventory("", p.CalculateLimit()) + return p.CalcAmountIn( + pool.CalcAmountInParams{ + TokenAmountOut: tt.args.tokenAmountOut, + TokenIn: tt.args.tokenIn, + Limit: limit, + }) + }) + assert.Equal(t, tt.err, err) + assert.Equal(t, tt.want, got) + }) + } +}