From 6364504354d1f09f2caf956e34f7608c88221f5a Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 6 Dec 2024 15:26:12 +0700 Subject: [PATCH 1/7] refactor: update limit order payload --- go.mod | 3 +- go.sum | 10 -- pkg/source/limitorder/http_client_dto.go | 39 +++-- pkg/source/limitorder/pool_simulator.go | 14 +- pkg/source/limitorder/pool_simulator_test.go | 166 +++++++++++++++++++ 5 files changed, 200 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 439520d55..0056f01d4 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/ethereum/go-ethereum v1.14.12 github.com/go-resty/resty/v2 v2.14.0 github.com/goccy/go-json v0.10.3 - github.com/golang/mock v1.6.0 github.com/google/go-cmp v0.6.0 github.com/holiman/uint256 v1.3.1 github.com/klauspost/compress v1.17.8 @@ -31,6 +30,7 @@ require ( github.com/orcaman/concurrent-map v1.0.0 github.com/pkg/errors v0.9.1 github.com/samber/lo v1.38.1 + github.com/shopspring/decimal v1.4.0 github.com/sirupsen/logrus v1.9.3 github.com/sourcegraph/conc v0.3.0 github.com/stretchr/testify v1.9.0 @@ -63,7 +63,6 @@ require ( github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect - github.com/shopspring/decimal v1.4.0 // indirect github.com/supranational/blst v0.3.13 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect diff --git a/go.sum b/go.sum index 05e93785a..1fedaa5a1 100644 --- a/go.sum +++ b/go.sum @@ -101,8 +101,6 @@ github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo= github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ= -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= -github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -231,7 +229,6 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= @@ -244,7 +241,6 @@ go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN8 go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= @@ -255,7 +251,6 @@ golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk= golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= @@ -269,7 +264,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= @@ -300,8 +294,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -341,7 +333,6 @@ golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= @@ -349,7 +340,6 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxb golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE= golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/source/limitorder/http_client_dto.go b/pkg/source/limitorder/http_client_dto.go index 7f51876f4..b43c67a79 100644 --- a/pkg/source/limitorder/http_client_dto.go +++ b/pkg/source/limitorder/http_client_dto.go @@ -72,6 +72,7 @@ type ( Permit string `json:"permit"` Interaction string `json:"interaction"` ExpiredAt int64 `json:"expiredAt"` + IsTakerAssetFee bool `json:"isTakerAssetFee"` AvailableMakingAmount string `json:"availableMakingAmount"` MakerBalanceAllowance string `json:"makerBalanceAllowance"` @@ -112,6 +113,7 @@ type ( Permit string `json:"permit"` Interaction string `json:"interaction"` ExpiredAt int64 `json:"expiredAt"` + IsTakerAssetFee bool `json:"isTakerAssetFee"` AvailableMakingAmount *big.Int `json:"availableMakingAmount"` @@ -132,24 +134,25 @@ func toOrder(ordersData []*orderData) ([]*order, error) { result := make([]*order, len(ordersData)) for i, o := range ordersData { result[i] = &order{ - ID: o.ID, - Salt: o.Salt, - ChainID: o.ChainID, - Signature: o.Signature, - MakerAsset: o.MakerAsset, - TakerAsset: o.TakerAsset, - Maker: o.Maker, - Receiver: o.Receiver, - AllowedSenders: o.AllowedSenders, - FeeRecipient: o.FeeRecipient, - MakerAssetData: o.MakerAssetData, - TakerAssetData: o.TakerAssetData, - GetMakerAmount: o.GetMakerAmount, - GetTakerAmount: o.GetTakerAmount, - Predicate: o.Predicate, - Permit: o.Permit, - Interaction: o.Interaction, - ExpiredAt: o.ExpiredAt, + ID: o.ID, + Salt: o.Salt, + ChainID: o.ChainID, + Signature: o.Signature, + MakerAsset: o.MakerAsset, + TakerAsset: o.TakerAsset, + Maker: o.Maker, + Receiver: o.Receiver, + AllowedSenders: o.AllowedSenders, + FeeRecipient: o.FeeRecipient, + MakerAssetData: o.MakerAssetData, + TakerAssetData: o.TakerAssetData, + GetMakerAmount: o.GetMakerAmount, + GetTakerAmount: o.GetTakerAmount, + Predicate: o.Predicate, + Permit: o.Permit, + Interaction: o.Interaction, + ExpiredAt: o.ExpiredAt, + IsTakerAssetFee: o.IsTakerAssetFee, } makerTokenFeePercent, err := strconv.ParseInt(o.MakerTokenFeePercent, 10, 32) if err != nil { diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index ae4ac9d37..395cc4a30 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -264,7 +264,12 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn continue } - feeAmountWeiByOrder := p.calcFeeAmountPerOrder(order, filledMakingAmountWei) + var feeAmountWeiByOrder *big.Int + if order.IsTakerAssetFee { + feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledTakingAmountWei) + } else { + feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledMakingAmountWei) + } totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) actualAmountOut := new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) @@ -306,7 +311,12 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn break } totalAmountIn = new(big.Int).Sub(totalAmountIn, remainingTakingAmountWei) - feeAmountWeiByOrder := p.calcFeeAmountPerOrder(order, remainingMakingAmountWei) + var feeAmountWeiByOrder *big.Int + if order.IsTakerAssetFee { + feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) + } else { + feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingMakingAmountWei) + } actualAmountOut := new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) diff --git a/pkg/source/limitorder/pool_simulator_test.go b/pkg/source/limitorder/pool_simulator_test.go index 96449d1b3..315ed5b23 100644 --- a/pkg/source/limitorder/pool_simulator_test.go +++ b/pkg/source/limitorder/pool_simulator_test.go @@ -30,6 +30,172 @@ func TestPool_CalcAmountOut(t *testing.T) { want *pool.CalcAmountOutResult err error }{ + { + name: "Should return correct CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut)) and IsTakerAssetFee true", + 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: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + Name: "USDT", + Symbol: "USDT", + Decimals: 6, + Swappable: true, + }, + { + Address: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + Name: "USDC", + Symbol: "USDC", + Decimals: 6, + Swappable: true, + }, + }, + Extra: marshalPoolExtra(&Extra{ + BuyOrders: []*order{ + { + ID: 1383, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature1", + TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + TakingAmount: parseBigInt("200"), + MakingAmount: parseBigInt("400"), + FeeConfig: parseBigInt("100"), + FeeRecipient: "0x0000000000000000000000000000000000000000", + FilledMakingAmount: parseBigInt("0"), + FilledTakingAmount: parseBigInt("0"), + MakerTokenFeePercent: 10, + MakerAssetData: "", + TakerAssetData: "", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + ExpiredAt: 0, + IsTakerAssetFee: true, + }, + { + ID: 1382, + ChainID: "5", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + 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{ + tokenAmountIn: pool.TokenAmount{ + Token: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + Amount: parseBigInt("300"), + AmountUsd: 0, + }, + tokenOut: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + }, + want: &pool.CalcAmountOutResult{ + TokenAmountOut: &pool.TokenAmount{ + Token: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + Amount: parseBigInt("499"), + AmountUsd: 0, + }, + Fee: &pool.TokenAmount{ + Token: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + Amount: big.NewInt(1), + 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: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", + AllowedSenders: "0x0000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", + FeeConfig: "100", + FeeRecipient: "0x0000000000000000000000000000000000000000", + MakerTokenFeePercent: 10, + MakerAssetData: "", + TakerAssetData: "", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Permit: "", + Interaction: "", + Signature: "signature1", + FeeAmount: "1", + }, + { + OrderID: 1382, + FilledTakingAmount: "100", + FilledMakingAmount: "100", + TakingAmount: "300", + MakingAmount: "300", + Salt: "185786982651412687203851465093295409688", + Signature: "signature2", + TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + 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 CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut))", poolEntity: entity.Pool{ From 0877b0986a885be6fcc7e585a99fbe7699add3ce Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 6 Dec 2024 15:31:04 +0700 Subject: [PATCH 2/7] bet --- pkg/source/limitorder/pool_simulator.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index 395cc4a30..7bc3cd4b5 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -330,12 +330,16 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn return totalAmountOutWei, swapInfo, totalFeeAmountWei, nil } -// feeAmount = (params.makingAmount * params.order.makerTokenFeePercent + BPS - 1) / BPS -func (p *PoolSimulator) calcFeeAmountPerOrder(order *order, filledMakingAmount *big.Int) *big.Int { +// calcFeeAmountPerOrder calculates the fee amount for a filled order +// feeAmount = (filledAmount * order.makerTokenFeePercent + BPS - 1) / BPS +// where filledAmount is: +// - filledTakingAmount if order.IsTakerAssetFee is true +// - filledMakingAmount if order.IsTakerAssetFee is false +func (p *PoolSimulator) calcFeeAmountPerOrder(order *order, filledAmount *big.Int) *big.Int { if order.MakerTokenFeePercent == 0 { return constant.ZeroBI } - amount := new(big.Int).Mul(filledMakingAmount, big.NewInt(int64(order.MakerTokenFeePercent))) + amount := new(big.Int).Mul(filledAmount, big.NewInt(int64(order.MakerTokenFeePercent))) return new(big.Int).Div(new(big.Int).Sub(new(big.Int).Add(amount, valueobject.BasisPoint), constant.One), valueobject.BasisPoint) } From c2f2aaa0d80e19ea375e37996428d49559f44012 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Mon, 9 Dec 2024 08:23:18 +0700 Subject: [PATCH 3/7] bet --- pkg/source/limitorder/pool_simulator.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index 7bc3cd4b5..f9aa04c91 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -266,10 +266,15 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn var feeAmountWeiByOrder *big.Int if order.IsTakerAssetFee { + // Calculate fee in taker asset feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledTakingAmountWei) + // Convert fee to maker asset equivalent + feeInMakerAsset := new(big.Float).Mul(new(big.Float).SetInt(feeAmountWeiByOrder), rate) + feeAmountWeiByOrder, _ = feeInMakerAsset.Int(nil) } else { feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledMakingAmountWei) } + totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) actualAmountOut := new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) @@ -310,10 +315,15 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn } break } + totalAmountIn = new(big.Int).Sub(totalAmountIn, remainingTakingAmountWei) var feeAmountWeiByOrder *big.Int if order.IsTakerAssetFee { + // Calculate fee in taker asset feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) + // Convert fee to maker asset equivalent + feeInMakerAsset := new(big.Float).Mul(new(big.Float).SetInt(feeAmountWeiByOrder), rate) + feeAmountWeiByOrder, _ = feeInMakerAsset.Int(nil) } else { feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingMakingAmountWei) } From ff3a5dccd1ddf54f9ac5b9cf72239bfc3c34e691 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Mon, 9 Dec 2024 08:28:32 +0700 Subject: [PATCH 4/7] bet --- pkg/source/limitorder/pool_simulator_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/source/limitorder/pool_simulator_test.go b/pkg/source/limitorder/pool_simulator_test.go index 315ed5b23..1922dee30 100644 --- a/pkg/source/limitorder/pool_simulator_test.go +++ b/pkg/source/limitorder/pool_simulator_test.go @@ -31,7 +31,7 @@ func TestPool_CalcAmountOut(t *testing.T) { err error }{ { - name: "Should return correct CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut)) and IsTakerAssetFee true", + name: "Should return correct CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut)) and IsTakerAssetFee=true", poolEntity: entity.Pool{ Address: "pool_limit_order_", ReserveUsd: 1000000000, @@ -129,12 +129,12 @@ func TestPool_CalcAmountOut(t *testing.T) { want: &pool.CalcAmountOutResult{ TokenAmountOut: &pool.TokenAmount{ Token: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - Amount: parseBigInt("499"), + Amount: parseBigInt("498"), AmountUsd: 0, }, Fee: &pool.TokenAmount{ Token: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - Amount: big.NewInt(1), + Amount: big.NewInt(2), AmountUsd: 0, }, Gas: 136616, @@ -165,7 +165,7 @@ func TestPool_CalcAmountOut(t *testing.T) { Permit: "", Interaction: "", Signature: "signature1", - FeeAmount: "1", + FeeAmount: "2", }, { OrderID: 1382, From e315d60d262bfbc7eea6116d2dc82c0132b45dea Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 10 Dec 2024 16:41:45 +0700 Subject: [PATCH 5/7] bet --- pkg/source/limitorder/pool_simulator.go | 20 ++- pkg/source/limitorder/pool_simulator_test.go | 139 ++++++------------- 2 files changed, 53 insertions(+), 106 deletions(-) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index f9aa04c91..571052c6a 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -265,19 +265,17 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn } var feeAmountWeiByOrder *big.Int + var actualAmountOut *big.Int if order.IsTakerAssetFee { // Calculate fee in taker asset - feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledTakingAmountWei) - // Convert fee to maker asset equivalent - feeInMakerAsset := new(big.Float).Mul(new(big.Float).SetInt(feeAmountWeiByOrder), rate) - feeAmountWeiByOrder, _ = feeInMakerAsset.Int(nil) + feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) + actualAmountOut = new(big.Int).Sub(remainingTakingAmountWei, feeAmountWeiByOrder) } else { feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledMakingAmountWei) + actualAmountOut = new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) } - - totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) - actualAmountOut := new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) + totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) filledOrderInfo := newFilledOrderInfo(order, filledTakingAmountWei.String(), filledMakingAmountWei.String(), feeAmountWeiByOrder.String()) swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) isFulfillAmountIn = true @@ -317,17 +315,17 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn } totalAmountIn = new(big.Int).Sub(totalAmountIn, remainingTakingAmountWei) + var feeAmountWeiByOrder *big.Int + var actualAmountOut *big.Int if order.IsTakerAssetFee { // Calculate fee in taker asset feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) - // Convert fee to maker asset equivalent - feeInMakerAsset := new(big.Float).Mul(new(big.Float).SetInt(feeAmountWeiByOrder), rate) - feeAmountWeiByOrder, _ = feeInMakerAsset.Int(nil) + actualAmountOut = new(big.Int).Sub(remainingTakingAmountWei, feeAmountWeiByOrder) } else { feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingMakingAmountWei) + actualAmountOut = new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder) } - actualAmountOut := new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), feeAmountWeiByOrder.String()) diff --git a/pkg/source/limitorder/pool_simulator_test.go b/pkg/source/limitorder/pool_simulator_test.go index 1922dee30..0a4ba1c24 100644 --- a/pkg/source/limitorder/pool_simulator_test.go +++ b/pkg/source/limitorder/pool_simulator_test.go @@ -31,7 +31,7 @@ func TestPool_CalcAmountOut(t *testing.T) { err error }{ { - name: "Should return correct CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut)) and IsTakerAssetFee=true", + name: "Should return correct CalcAmountOutResult when IsTakerAssetFee=true", poolEntity: entity.Pool{ Address: "pool_limit_order_", ReserveUsd: 1000000000, @@ -43,153 +43,102 @@ func TestPool_CalcAmountOut(t *testing.T) { Reserves: []string{"10000000000000000000", "10000000000000000000"}, Tokens: []*entity.PoolToken{ { - Address: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - Name: "USDT", - Symbol: "USDT", - Decimals: 6, + Address: "0x4200000000000000000000000000000000000006", + Name: "WETH", + Symbol: "WETH", + Decimals: 18, Swappable: true, }, { - Address: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - Name: "USDC", - Symbol: "USDC", - Decimals: 6, + Address: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", + Name: "ALB", + Symbol: "ALB", + Decimals: 18, Swappable: true, }, }, Extra: marshalPoolExtra(&Extra{ - BuyOrders: []*order{ + SellOrders: []*order{ { ID: 1383, ChainID: "5", - Salt: "185786982651412687203851465093295409688", + Salt: "154002081474686290844625688130661839166", Signature: "signature1", - TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + TakerAsset: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", + MakerAsset: "0x4200000000000000000000000000000000000006", Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", AllowedSenders: "0x0000000000000000000000000000000000000000", - TakingAmount: parseBigInt("200"), - MakingAmount: parseBigInt("400"), - FeeConfig: parseBigInt("100"), + TakingAmount: parseBigInt("10094237807104621"), + MakingAmount: parseBigInt("1000000000000"), + FeeConfig: parseBigInt("6277103196897319986124780282469434185881440815774577556840"), FeeRecipient: "0x0000000000000000000000000000000000000000", FilledMakingAmount: parseBigInt("0"), FilledTakingAmount: parseBigInt("0"), - MakerTokenFeePercent: 10, - MakerAssetData: "", - TakerAssetData: "", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", - Permit: "", - Interaction: "", - ExpiredAt: 0, - IsTakerAssetFee: true, - }, - { - ID: 1382, - ChainID: "5", - Salt: "185786982651412687203851465093295409688", - Signature: "signature2", - TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - 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, + MakerTokenFeePercent: 100, MakerAssetData: "", TakerAssetData: "", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e30000000000000000000000002b9b20295b6f1bea547271982f62432f526706e9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b00000000000000000000000000000000000000000000000000000001235001cd00000000000000000000000000000000000000000000000000000000", Permit: "", Interaction: "", ExpiredAt: 0, IsTakerAssetFee: true, }, }, - SellOrders: []*order{}, + BuyOrders: []*order{}, }), TotalSupply: "", }, args: args{ tokenAmountIn: pool.TokenAmount{ - Token: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - Amount: parseBigInt("300"), + Token: "0x4200000000000000000000000000000000000006", + Amount: parseBigInt("1000000000000"), AmountUsd: 0, }, - tokenOut: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", + tokenOut: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", }, want: &pool.CalcAmountOutResult{ TokenAmountOut: &pool.TokenAmount{ - Token: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - Amount: parseBigInt("498"), + Token: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", + Amount: parseBigInt("9993295429033574"), AmountUsd: 0, }, Fee: &pool.TokenAmount{ - Token: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - Amount: big.NewInt(2), + Token: "0x4200000000000000000000000000000000000006", + Amount: parseBigInt("100942378071047"), AmountUsd: 0, }, - Gas: 136616, + Gas: 113308, SwapInfo: SwapInfo{ - AmountIn: "300", - SwapSide: Buy, + AmountIn: "1000000000000", + SwapSide: Sell, FilledOrders: []*FilledOrderInfo{ { OrderID: 1383, - FilledTakingAmount: "200", - FilledMakingAmount: "400", - TakingAmount: "200", - MakingAmount: "400", - Salt: "185786982651412687203851465093295409688", - TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + FilledTakingAmount: "1000000000000", + FilledMakingAmount: "99066419", + TakingAmount: "10094237807104621", + MakingAmount: "1000000000000", + Salt: "154002081474686290844625688130661839166", + TakerAsset: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", + MakerAsset: "0x4200000000000000000000000000000000000006", Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", AllowedSenders: "0x0000000000000000000000000000000000000000", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - FeeConfig: "100", + GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", + GetTakerAmount: "296637bf000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", + FeeConfig: "6277103196897319986124780282469434185881440815774577556840", FeeRecipient: "0x0000000000000000000000000000000000000000", - MakerTokenFeePercent: 10, + MakerTokenFeePercent: 100, MakerAssetData: "", TakerAssetData: "", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", + Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e30000000000000000000000002b9b20295b6f1bea547271982f62432f526706e9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b00000000000000000000000000000000000000000000000000000001235001cd00000000000000000000000000000000000000000000000000000000", Permit: "", Interaction: "", Signature: "signature1", - FeeAmount: "2", - }, - { - OrderID: 1382, - FilledTakingAmount: "100", - FilledMakingAmount: "100", - TakingAmount: "300", - MakingAmount: "300", - Salt: "185786982651412687203851465093295409688", - Signature: "signature2", - TakerAsset: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", - MakerAsset: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174", - Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - FeeConfig: "100", - FeeRecipient: "0x0000000000000000000000000000000000000000", - AllowedSenders: "0x0000000000000000000000000000000000000000", - MakerAssetData: "", - TakerAssetData: "", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000001d7d843dc3b4800000000000000000000000000000000000000000000000000000de0b6b3a7640000", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000002892e28b58ab329741f27fd1ea56dca0192a38840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e3000000000000000000000000a246ec8bf7f2e54cc2f7bfdd869302ae4a08a590000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b0000000000000000000000000000000000000000000000000000000063c1169800000000000000000000000000000000000000000000000000000000", - Permit: "", - Interaction: "", - FeeAmount: "0", + FeeAmount: "100942378071047", }, }, }, From 3153fe1d0b51c39fb60c755f3cbd2d210d74508e Mon Sep 17 00:00:00 2001 From: "le.cao" Date: Wed, 11 Dec 2024 13:27:20 +0700 Subject: [PATCH 6/7] fix --- pkg/source/limitorder/pool_simulator.go | 101 ++++++--- pkg/source/limitorder/pool_simulator_test.go | 226 +++++++++---------- 2 files changed, 178 insertions(+), 149 deletions(-) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index 571052c6a..c82162adb 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -254,9 +254,13 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn continue } - if remainingTakingAmountWei.Cmp(totalAmountIn) >= 0 { - amountOutWei := new(big.Float).Mul(new(big.Float).SetInt(totalAmountIn), rate) - filledTakingAmountWei := totalAmountIn + totalAmountInAfterFee, _ := p.calcTakerAssetFeeAmountExactIn(order, totalAmountIn) + // ideally we should return totalFeeAmountWei in takerAsset here + // but for now it's not used, and we might get mixed up with makerAsset fee, so will ignore for now + + if remainingTakingAmountWei.Cmp(totalAmountInAfterFee) >= 0 { + amountOutWei := new(big.Float).Mul(new(big.Float).SetInt(totalAmountInAfterFee), rate) + filledTakingAmountWei := totalAmountInAfterFee filledMakingAmountWei, _ := amountOutWei.Int(nil) // order too small @@ -264,18 +268,10 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn continue } - var feeAmountWeiByOrder *big.Int - var actualAmountOut *big.Int - if order.IsTakerAssetFee { - // Calculate fee in taker asset - feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) - actualAmountOut = new(big.Int).Sub(remainingTakingAmountWei, feeAmountWeiByOrder) - } else { - feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, filledMakingAmountWei) - actualAmountOut = new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) - } - totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) + feeAmountWeiByOrder := p.calcMakerAsetFeeAmount(order, filledMakingAmountWei) totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) + actualAmountOut := new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder) + totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) filledOrderInfo := newFilledOrderInfo(order, filledTakingAmountWei.String(), filledMakingAmountWei.String(), feeAmountWeiByOrder.String()) swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) isFulfillAmountIn = true @@ -313,19 +309,10 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn } break } - - totalAmountIn = new(big.Int).Sub(totalAmountIn, remainingTakingAmountWei) - - var feeAmountWeiByOrder *big.Int - var actualAmountOut *big.Int - if order.IsTakerAssetFee { - // Calculate fee in taker asset - feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingTakingAmountWei) - actualAmountOut = new(big.Int).Sub(remainingTakingAmountWei, feeAmountWeiByOrder) - } else { - feeAmountWeiByOrder = p.calcFeeAmountPerOrder(order, remainingMakingAmountWei) - actualAmountOut = new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder) - } + _, takerAssetFee := p.calcTakerAssetFeeAmountExactOut(order, remainingTakingAmountWei) + totalAmountIn = new(big.Int).Sub(new(big.Int).Sub(totalAmountIn, takerAssetFee), remainingTakingAmountWei) + feeAmountWeiByOrder := p.calcMakerAsetFeeAmount(order, remainingMakingAmountWei) + actualAmountOut := new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder) totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut) totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder) filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), feeAmountWeiByOrder.String()) @@ -338,19 +325,65 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn return totalAmountOutWei, swapInfo, totalFeeAmountWei, nil } -// calcFeeAmountPerOrder calculates the fee amount for a filled order -// feeAmount = (filledAmount * order.makerTokenFeePercent + BPS - 1) / BPS -// where filledAmount is: -// - filledTakingAmount if order.IsTakerAssetFee is true -// - filledMakingAmount if order.IsTakerAssetFee is false -func (p *PoolSimulator) calcFeeAmountPerOrder(order *order, filledAmount *big.Int) *big.Int { +// feeAmount = (params.makingAmount * params.order.makerTokenFeePercent + BPS - 1) / BPS +func (p *PoolSimulator) calcMakerAsetFeeAmount(order *order, filledMakingAmount *big.Int) *big.Int { + if order.IsTakerAssetFee { + return constant.ZeroBI + } if order.MakerTokenFeePercent == 0 { return constant.ZeroBI } - amount := new(big.Int).Mul(filledAmount, big.NewInt(int64(order.MakerTokenFeePercent))) + amount := new(big.Int).Mul(filledMakingAmount, big.NewInt(int64(order.MakerTokenFeePercent))) return new(big.Int).Div(new(big.Int).Sub(new(big.Int).Add(amount, valueobject.BasisPoint), constant.One), valueobject.BasisPoint) } +// given total takingAmount, calculate fee and takingAmountAfterFee +func (p *PoolSimulator) calcTakerAssetFeeAmountExactIn(order *order, takingAmount *big.Int) (takingAmountAfterFee *big.Int, fee *big.Int) { + if !order.IsTakerAssetFee { + return takingAmount, constant.ZeroBI + } + + feePct := order.MakerTokenFeePercent // reuse same field + if feePct == 0 { + return takingAmount, constant.ZeroBI + } + + // fee = ceiling(takingAmountAfterFee * feePct / BasisPoint) + // takingAmountAfterFee + fee = takingAmount + // => takingAmountAfterFee + ceiling(takingAmountAfterFee * feePct / BasisPoint) = takingAmount + + takingAmountAfterFee = new(big.Int).Div( + new(big.Int).Mul(takingAmount, valueobject.BasisPoint), + new(big.Int).Add( + big.NewInt(int64(feePct)), + valueobject.BasisPoint, + ), + ) + fee = new(big.Int).Sub(takingAmount, takingAmountAfterFee) + return +} + +// given filled takingAmountAfterFee, calculate fee and total takingAmount +func (p *PoolSimulator) calcTakerAssetFeeAmountExactOut(order *order, takingAmountAfterFee *big.Int) (takingAmount *big.Int, fee *big.Int) { + if !order.IsTakerAssetFee { + return takingAmountAfterFee, constant.ZeroBI + } + + feePct := order.MakerTokenFeePercent // reuse same field + if feePct == 0 { + return takingAmountAfterFee, constant.ZeroBI + } + + amount := new(big.Int).Mul(takingAmountAfterFee, big.NewInt(int64(feePct))) + fee = new(big.Int).Div( + new(big.Int).Add(amount, valueobject.BasisPointM1), + valueobject.BasisPoint, + ) + + takingAmount = new(big.Int).Add(takingAmountAfterFee, fee) + return +} + func (p *PoolSimulator) estimateGas(numberOfFilledOrders int) int64 { return p.estimateGasForExecutor(numberOfFilledOrders) + p.estimateGasForRouter(numberOfFilledOrders) } diff --git a/pkg/source/limitorder/pool_simulator_test.go b/pkg/source/limitorder/pool_simulator_test.go index 0a4ba1c24..a99b88a60 100644 --- a/pkg/source/limitorder/pool_simulator_test.go +++ b/pkg/source/limitorder/pool_simulator_test.go @@ -30,121 +30,6 @@ func TestPool_CalcAmountOut(t *testing.T) { want *pool.CalcAmountOutResult err error }{ - { - name: "Should return correct CalcAmountOutResult when IsTakerAssetFee=true", - 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: "0x4200000000000000000000000000000000000006", - Name: "WETH", - Symbol: "WETH", - Decimals: 18, - Swappable: true, - }, - { - Address: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", - Name: "ALB", - Symbol: "ALB", - Decimals: 18, - Swappable: true, - }, - }, - Extra: marshalPoolExtra(&Extra{ - SellOrders: []*order{ - { - ID: 1383, - ChainID: "5", - Salt: "154002081474686290844625688130661839166", - Signature: "signature1", - TakerAsset: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", - MakerAsset: "0x4200000000000000000000000000000000000006", - Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - AllowedSenders: "0x0000000000000000000000000000000000000000", - TakingAmount: parseBigInt("10094237807104621"), - MakingAmount: parseBigInt("1000000000000"), - FeeConfig: parseBigInt("6277103196897319986124780282469434185881440815774577556840"), - FeeRecipient: "0x0000000000000000000000000000000000000000", - FilledMakingAmount: parseBigInt("0"), - FilledTakingAmount: parseBigInt("0"), - MakerTokenFeePercent: 100, - MakerAssetData: "", - TakerAssetData: "", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e30000000000000000000000002b9b20295b6f1bea547271982f62432f526706e9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b00000000000000000000000000000000000000000000000000000001235001cd00000000000000000000000000000000000000000000000000000000", - Permit: "", - Interaction: "", - ExpiredAt: 0, - IsTakerAssetFee: true, - }, - }, - BuyOrders: []*order{}, - }), - TotalSupply: "", - }, - args: args{ - tokenAmountIn: pool.TokenAmount{ - Token: "0x4200000000000000000000000000000000000006", - Amount: parseBigInt("1000000000000"), - AmountUsd: 0, - }, - tokenOut: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", - }, - want: &pool.CalcAmountOutResult{ - TokenAmountOut: &pool.TokenAmount{ - Token: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", - Amount: parseBigInt("9993295429033574"), - AmountUsd: 0, - }, - Fee: &pool.TokenAmount{ - Token: "0x4200000000000000000000000000000000000006", - Amount: parseBigInt("100942378071047"), - AmountUsd: 0, - }, - Gas: 113308, - SwapInfo: SwapInfo{ - AmountIn: "1000000000000", - SwapSide: Sell, - FilledOrders: []*FilledOrderInfo{ - { - OrderID: 1383, - FilledTakingAmount: "1000000000000", - FilledMakingAmount: "99066419", - TakingAmount: "10094237807104621", - MakingAmount: "1000000000000", - Salt: "154002081474686290844625688130661839166", - TakerAsset: "0x1dd2d631c92b1acdfcdd51a0f7145a50130050c4", - MakerAsset: "0x4200000000000000000000000000000000000006", - Maker: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - Receiver: "0xa246ec8bf7f2e54cc2f7bfdd869302ae4a08a590", - AllowedSenders: "0x0000000000000000000000000000000000000000", - GetMakerAmount: "f4a215c3000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", - GetTakerAmount: "296637bf000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000023dca7e2c5526d", - FeeConfig: "6277103196897319986124780282469434185881440815774577556840", - FeeRecipient: "0x0000000000000000000000000000000000000000", - MakerTokenFeePercent: 100, - MakerAssetData: "", - TakerAssetData: "", - Predicate: "961d5b1e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000008869bd40f0fe077f8857ad27e7cecdcafd4e52550000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000044cf6fc6e30000000000000000000000002b9b20295b6f1bea547271982f62432f526706e9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463592c2b00000000000000000000000000000000000000000000000000000001235001cd00000000000000000000000000000000000000000000000000000000", - Permit: "", - Interaction: "", - Signature: "signature1", - FeeAmount: "100942378071047", - }, - }, - }, - }, - err: nil, - }, { name: "Should return correct CalcAmountOutResult when swapSide is BUY(strings.ToLower(tokeIn) <= strings.ToLower(TokenOut))", poolEntity: entity.Pool{ @@ -1370,3 +1255,114 @@ func TestPool_Inventory(t *testing.T) { } } } + +func TestPool_CalcAmountOut_TakerAssetFee(t *testing.T) { + type testorder struct { + id int64 + makingAmount string + takingAmount string + feePct uint32 + IsTakerAssetFee bool + } + + pools := map[string][]testorder{ + "pool1": { + {1001, "10000000", "12000000", 100, true}, + {1002, "10000000", "10000000", 100, true}, + }, + "pool2": { // mixed takerAssetFee and makerAssetFee + {1001, "10000000", "12000000", 100, true}, + {1002, "10000000", "10000000", 100, false}, + }, + "pool3": { // test order on prerelease + {1001, "10000000000000", "99522200853987088", 100, true}, + }, + } + + testcases := []struct { + name string + pool string + amountIn string + expAmountOut string + expOrderIds []int64 + }{ + {"p-fill 1st order", "pool1", "10000000", "8250825", []int64{1001, 1002}}, // 1002 as backup + {"p-fill 1st order", "pool1", "12000000", "9900990", []int64{1001, 1002}}, // 1002 as backup + {"f-fill 1st order", "pool1", "12120000", "10000000", []int64{1001, 1002}}, // 1002 as backup + {"f-fill 1st order, p-fill 2nd one", "pool1", "15000000", "12851485", []int64{1001, 1002}}, + {"f-fill both orders", "pool1", "22220000", "20000000", []int64{1001, 1002}}, + {"cannot be filled", "pool1", "22220003", "", nil}, // we expect 22220001 to not be filled, but due to fee rounding down it need 22220003 + + {"p-fill 1st order", "pool2", "10000000", "8250825", []int64{1001, 1002}}, // 1002 as backup + {"p-fill 1st order", "pool2", "12000000", "9900990", []int64{1001, 1002}}, // 1002 as backup + {"f-fill 1st order", "pool2", "12120000", "10000000", []int64{1001, 1002}}, // 1002 as backup + {"f-fill 1st order, p-fill 2nd one", "pool2", "15000000", "12851200", []int64{1001, 1002}}, + {"f-fill both orders", "pool2", "22120000", "19900000", []int64{1001, 1002}}, + {"cannot be filled", "pool2", "22120001", "", nil}, + + // 49522200853987088 + 495222008539871 + {"p-fill 1st order", "pool3", "50017422862526959", "4975995348680", []int64{1001}}, + } + + sims := lo.MapValues(pools, func(orders []testorder, _ string) *PoolSimulator { + extra := Extra{ + BuyOrders: lo.Map(orders, func(o testorder, _ int) *order { + return &order{ + ID: o.id, + MakingAmount: bignumber.NewBig10(o.makingAmount), + TakingAmount: bignumber.NewBig10(o.takingAmount), + AvailableMakingAmount: bignumber.NewBig10(o.makingAmount), + MakerBalanceAllowance: bignumber.NewBig10("100000000000000000000"), + MakerTokenFeePercent: o.feePct, + IsTakerAssetFee: o.IsTakerAssetFee, + } + }), + } + sExtra, _ := json.Marshal(extra) + poolEnt := entity.Pool{ + Tokens: []*entity.PoolToken{{Address: "A"}, {Address: "B"}}, + Reserves: entity.PoolReserves{"0", "0"}, + StaticExtra: `{"ContractAddress":""}`, + Extra: string(sExtra), + } + p, err := NewPoolSimulator(poolEnt) + require.Nil(t, err) + return p + }) + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + limit := swaplimit.NewInventory("", sims[tc.pool].CalculateLimit()) + res, err := sims[tc.pool].CalcAmountOut(pool.CalcAmountOutParams{ + TokenAmountIn: pool.TokenAmount{ + Token: "A", + Amount: bignumber.NewBig10(tc.amountIn), + }, + TokenOut: "B", + Limit: limit, + }) + + if tc.expOrderIds == nil { + require.NotNil(t, err) + return + } + + require.Nil(t, err) + + assert.Equal(t, tc.expAmountOut, res.TokenAmountOut.Amount.String()) + + si := res.SwapInfo.(SwapInfo) + oid := make([]int64, 0, len(si.FilledOrders)) + oinfo := "" + for _, o := range si.FilledOrders { + if o.FilledTakingAmount == "" { + break + } + oid = append(oid, o.OrderID) + oinfo += fmt.Sprintf("order %v %v %v\n", o.OrderID, o.FilledMakingAmount, o.FilledTakingAmount) + } + assert.Equal(t, tc.expOrderIds, oid, oinfo) + fmt.Println(oinfo) + }) + } +} From 499f57e3ad6a3c2d7e9ffb7d45e9e926bb5b0946 Mon Sep 17 00:00:00 2001 From: "le.cao" Date: Wed, 11 Dec 2024 13:32:32 +0700 Subject: [PATCH 7/7] fix --- pkg/valueobject/extra_fee.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/valueobject/extra_fee.go b/pkg/valueobject/extra_fee.go index b6271cc3d..def1116c4 100644 --- a/pkg/valueobject/extra_fee.go +++ b/pkg/valueobject/extra_fee.go @@ -8,4 +8,5 @@ var ( // BasisPoint is one hundredth of 1 percentage point // https://en.wikipedia.org/wiki/Basis_point BasisPoint = big.NewInt(10000) + BasisPointM1 = big.NewInt(10000 - 1) )