From 17ce92849b19b3b0e7fcafde2a22a6c94d3aabaa Mon Sep 17 00:00:00 2001 From: Chien Nguyen Date: Wed, 18 Dec 2024 11:25:17 +0700 Subject: [PATCH] Add test for CalcAmountOut then CalcAmountIn --- .../pool_simulator_calc_amount_in_test.go | 481 ++++++++++++++++++ 1 file changed, 481 insertions(+) diff --git a/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go b/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go index 25167955b..63a207ace 100644 --- a/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go +++ b/pkg/source/limitorder/pool_simulator_calc_amount_in_test.go @@ -9,6 +9,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/swaplimit" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/testutil" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( @@ -865,3 +866,483 @@ func TestPool_CalcAmountIn(t *testing.T) { }) } } + +func TestPool_CalcAmountOut_CalcAmountIn(t *testing.T) { + type args struct { + tokenIn string + amountIn *big.Int + tokenOut string + amountOut *big.Int + } + + tests := []struct { + name string + poolEntity entity.Pool + args args + err error + }{ + { + name: "Should return correct CalcAmountInResult and CalcAmountOutResult 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{ + tokenIn: tokenUSDC, + amountIn: parseBigInt("300"), + tokenOut: tokenUSDT, + amountOut: parseBigInt("500"), + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult and CalcAmountOutResult 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{ + + tokenIn: tokenUSDT, + amountIn: parseBigInt("1210000"), + tokenOut: tokenUSDC, + amountOut: parseBigInt("1215841"), + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult and CalcAmountOutResult 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{ + tokenIn: tokenUSDC, + amountIn: parseBigInt("302"), + tokenOut: tokenUSDT, + amountOut: parseBigInt("500"), + }, + err: nil, + }, + { + name: "Should return correct CalcAmountInResult and CalcAmountOutResult 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{ + tokenIn: tokenUSDT, + amountIn: parseBigInt("700"), + tokenOut: tokenUSDC, + amountOut: parseBigInt("1400"), + }, + err: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p, err := NewPoolSimulator(tt.poolEntity) + assert.Equal(t, nil, err) + calcAmountOutResult, err := testutil.MustConcurrentSafe[*pool.CalcAmountOutResult](t, func() (any, error) { + limit := swaplimit.NewInventory("", p.CalculateLimit()) + return p.CalcAmountOut( + pool.CalcAmountOutParams{ + TokenAmountIn: pool.TokenAmount{ + Token: tt.args.tokenIn, + Amount: tt.args.amountIn, + }, + TokenOut: tt.args.tokenOut, + Limit: limit, + }) + }) + require.NoError(t, err) + + assert.Equal(t, tt.args.amountOut, calcAmountOutResult.TokenAmountOut.Amount) + + calcAmountInResult, err := testutil.MustConcurrentSafe[*pool.CalcAmountInResult](t, func() (any, error) { + limit := swaplimit.NewInventory("", p.CalculateLimit()) + return p.CalcAmountIn( + pool.CalcAmountInParams{ + TokenAmountOut: pool.TokenAmount{ + Token: tt.args.tokenOut, + Amount: tt.args.amountOut, + }, + TokenIn: tt.args.tokenIn, + Limit: limit, + }) + }) + require.NoError(t, err) + + assert.Equal(t, tt.args.amountIn, calcAmountInResult.TokenAmountIn.Amount) + }) + } + +}