Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update logic lo #643

Merged
merged 11 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -341,15 +333,13 @@ 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=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
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=
Expand Down
39 changes: 21 additions & 18 deletions pkg/source/limitorder/http_client_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`

Expand All @@ -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 {
Expand Down
69 changes: 62 additions & 7 deletions pkg/source/limitorder/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,21 @@ 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
if filledMakingAmountWei.Cmp(constant.ZeroBI) <= 0 {
continue
}

feeAmountWeiByOrder := p.calcFeeAmountPerOrder(order, filledMakingAmountWei)
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)
Expand Down Expand Up @@ -305,8 +309,9 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn
}
break
}
totalAmountIn = new(big.Int).Sub(totalAmountIn, remainingTakingAmountWei)
feeAmountWeiByOrder := p.calcFeeAmountPerOrder(order, remainingMakingAmountWei)
_, 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)
Expand All @@ -321,14 +326,64 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn
}

// feeAmount = (params.makingAmount * params.order.makerTokenFeePercent + BPS - 1) / BPS
func (p *PoolSimulator) calcFeeAmountPerOrder(order *order, filledMakingAmount *big.Int) *big.Int {
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(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)
}
Expand Down
111 changes: 111 additions & 0 deletions pkg/source/limitorder/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,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)
})
}
}
1 change: 1 addition & 0 deletions pkg/valueobject/extra_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Loading