Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into TRD-759-kyberswap-dex…
Browse files Browse the repository at this point in the history
…-lib-create-kyber-pmm-v-2
  • Loading branch information
thanhpp committed Dec 17, 2024
2 parents 70b165a + fb49260 commit f601c4d
Show file tree
Hide file tree
Showing 85 changed files with 14,966 additions and 92 deletions.
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
41 changes: 13 additions & 28 deletions pkg/entity/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,60 +58,45 @@ type Pool struct {
BlockNumber uint64 `json:"blockNumber,omitempty"`
}

func (p Pool) IsZero() bool { return len(p.Address) == 0 && len(p.Tokens) == 0 }
func (p *Pool) IsZero() bool { return len(p.Address) == 0 && len(p.Tokens) == 0 }

func (p Pool) GetTotalSupply() float64 {
func (p *Pool) GetTotalSupply() float64 {
totalSupplyBF, _ := new(big.Float).SetString(p.TotalSupply)
totalSupply, _ := new(big.Float).Quo(totalSupplyBF, bignumber.TenPowDecimals(18)).Float64()

totalSupply, _ := totalSupplyBF.Quo(totalSupplyBF, bignumber.TenPowDecimals(18)).Float64()
return totalSupply
}

// GetLpToken returns the LpToken of the pool
// If there is a LpToken in the StaticExtra, we use it. If not, we get the pool's address
func (p Pool) GetLpToken() string {

func (p *Pool) GetLpToken() string {
var staticExtra = struct {
LpToken string `json:"lpToken"`
}{}

_ = json.Unmarshal([]byte(p.StaticExtra), &staticExtra)

if len(staticExtra.LpToken) > 0 {
return strings.ToLower(staticExtra.LpToken)
}

return p.Address
}

// HasReserves check if a pool has correct reserves or not
// if there is no reserve in pool, or reserve is empty string, or reserve = "0", this function returns false
// if pool has equals or more than 2 tokens have reserve, this function returns true
func (p Pool) HasReserves() bool {
if (len(p.Reserves)) == 0 {
return false
}

zeroReserveCount := 0
// HasReserves check if a pool has some reserves or not.
// Returns false if there is no reserve in pool, or all reserves are empty string or "0". True otherwise.
func (p *Pool) HasReserves() bool {
for _, reserve := range p.Reserves {
if len(reserve) == 0 || reserve == "0" {
zeroReserveCount += 1
if p.HasReserve(reserve) {
return true
}
}

return len(p.Reserves)-zeroReserveCount >= 2
return false
}

func (p Pool) HasReserve(reserve string) bool {
if len(reserve) == 0 || reserve == "0" {
return false
}

return true
func (p *Pool) HasReserve(reserve string) bool {
return len(reserve) > 0 && reserve != "0"
}

// HasAmplifiedTvl check if the pool has amplifiedTvl or not
func (p Pool) HasAmplifiedTvl() bool {
func (p *Pool) HasAmplifiedTvl() bool {
return p.AmplifiedTvl > 0
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/entity/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestPool_HasReserves(t *testing.T) {
expectedResult: false,
},
{
name: "it should return false when at least one reserve is empty string",
name: "it should return true when at least one reserve is not empty",
pool: Pool{
Address: "address1",
ReserveUsd: 100,
Expand Down Expand Up @@ -202,10 +202,10 @@ func TestPool_HasReserves(t *testing.T) {
StaticExtra: "staticExtra1",
TotalSupply: "totalSupply1",
},
expectedResult: false,
expectedResult: true,
},
{
name: "it should return false when at least one reserve is 0",
name: "it should return true when at least one reserve is not 0",
pool: Pool{
Address: "address1",
ReserveUsd: 100,
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestPool_HasReserves(t *testing.T) {
StaticExtra: "staticExtra1",
TotalSupply: "totalSupply1",
},
expectedResult: false,
expectedResult: true,
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/algebra/integral/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func getOutputTokenDelta10(to, from, liquidity *uint256.Int) (*uint256.Int, erro
func getToken0Delta(priceLower, priceUpper, liquidity *uint256.Int, roundUp bool) (*uint256.Int, error) {
priceDelta := new(uint256.Int).Sub(priceUpper, priceLower)
if priceDelta.Cmp(priceUpper) >= 0 {
return nil, errors.New("price delta must be greater than price upper")
return nil, errors.New("price delta must be smaller than price upper")
}

liquidityShifted := new(uint256.Int).Lsh(liquidity, RESOLUTION)
Expand Down Expand Up @@ -570,7 +570,7 @@ func getToken0Delta(priceLower, priceUpper, liquidity *uint256.Int, roundUp bool

func getToken1Delta(priceLower, priceUpper, liquidity *uint256.Int, roundUp bool) (*uint256.Int, error) {
if priceUpper.Cmp(priceLower) < 0 {
return nil, errors.New("price upper must be greater than price lower")
return nil, errors.New("price upper must be greater or equal than price lower")
}

priceDelta := new(uint256.Int).Sub(priceUpper, priceLower)
Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/algebra/integral/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (d *PoolTracker) fetchRPCData(ctx context.Context, p entity.Pool, blockNumb

res.State = GlobalState{
Price: uint256.MustFromBig(rpcState.Price),
Tick: int32(rpcState.Tick.Uint64()),
Tick: int32(rpcState.Tick.Int64()),
LastFee: rpcState.LastFee,
PluginConfig: rpcState.PluginConfig,
CommunityFee: rpcState.CommunityFee,
Expand Down
3 changes: 3 additions & 0 deletions pkg/liquidity-source/algebra/integral/ticklens.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (d *PoolTracker) getPoolTicksFromSC(ctx context.Context, pool entity.Pool,
// changed, use new value
combined = append(combined, tick)
delete(changedTickMap, t.Index)
} else if changedTickSet.ContainsOne(int64(t.Index)) {
// some changed ticks might be consumed entirely and are not in `changedTickMap`, delete them
logger.Debugf("deleted tick %v %v", pool.Address, t)
} else {
// use old value
combined = append(combined, TickResp{
Expand Down
3 changes: 3 additions & 0 deletions pkg/liquidity-source/algebra/v1/ticklens.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (d *PoolTracker) getPoolTicksFromSC(ctx context.Context, pool entity.Pool,
// changed, use new value
combined = append(combined, tick)
delete(changedTickMap, t.Index)
} else if changedTickSet.ContainsOne(int64(t.Index)) {
// some changed ticks might be consumed entirely and are not in `changedTickMap`, delete them
logger.Debugf("deleted tick %v %v", pool.Address, t)
} else {
// use old value
combined = append(combined, TickResp{
Expand Down
6 changes: 6 additions & 0 deletions pkg/liquidity-source/lo1inch/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package lo1inch

type Config struct {
DexID string `json:"dexID"`
ChainID uint `json:"chainID"`
}
24 changes: 24 additions & 0 deletions pkg/liquidity-source/lo1inch/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lo1inch

import "math/big"

const (
DexType = "lo1inch"

PoolIDPrefix = "lo1inch"
PoolIDSeparator = "_"
)

var (
// BasGas is base gas to executor a tx for LO.
BaseGas = 90000

// GasPerOrderExecutor is gas for executing an order.
GasPerOrderExecutor = 11100
// GasPerOrderRouter need to burn when sending in call data.
GasPerOrderRouter = 12208

// FallbackPercentageOfTotalMakingAmount is fallback percentage of total remain making amount with amount out.
// total remain making amount = total remain making amount(filled orders) + total remain making amount(fallback orders)
FallbackPercentageOfTotalMakingAmount = big.NewFloat(1.3)
)
9 changes: 9 additions & 0 deletions pkg/liquidity-source/lo1inch/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lo1inch

import "errors"

var (
ErrTokenInNotSupported = errors.New("tokenIn is not supported")
ErrNoOrderAvailable = errors.New("no order available")
ErrCannotFulfillAmountIn = errors.New("cannot fulfill amountIn")
)
Loading

0 comments on commit f601c4d

Please sign in to comment.