-
Notifications
You must be signed in to change notification settings - Fork 30
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
add NewPoolSimulatorV2 for uniswapv3 #306
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package uniswapv3 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
"unsafe" | ||
|
||
"encoding/json" | ||
|
||
gojson "github.com/goccy/go-json" | ||
|
||
"github.com/KyberNetwork/logger" | ||
coreEntities "github.com/daoleno/uniswap-sdk-core/entities" | ||
|
@@ -33,7 +37,12 @@ type PoolSimulator struct { | |
} | ||
|
||
func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*PoolSimulator, error) { | ||
var extra Extra | ||
var extra struct { | ||
Liquidity *big.Int `json:"liquidity"` | ||
SqrtPriceX96 *big.Int `json:"sqrtPriceX96"` | ||
Tick *big.Int `json:"tick"` | ||
Ticks []Tick `json:"ticks"` | ||
} | ||
if err := json.Unmarshal([]byte(entityPool.Extra), &extra); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -119,6 +128,97 @@ func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*Poo | |
}, nil | ||
} | ||
|
||
type PoolSimulatorV2 struct { | ||
PoolSimulator | ||
extra struct { | ||
Liquidity *big.Int `json:"liquidity"` | ||
SqrtPriceX96 *big.Int `json:"sqrtPriceX96"` | ||
Tick *big.Int `json:"tick"` | ||
Ticks []TickGob `json:"ticksGob"` | ||
} | ||
} | ||
|
||
func NewPoolSimulatorV2(entityPool entity.Pool, chainID valueobject.ChainID) (*PoolSimulatorV2, error) { | ||
p := &PoolSimulatorV2{} | ||
err := InitPoolSimulatorV2(entityPool, p, chainID) | ||
return p, err | ||
} | ||
|
||
type v3TickList []v3Entities.Tick | ||
|
||
func InitPoolSimulatorV2(entityPool entity.Pool, p *PoolSimulatorV2, chainID valueobject.ChainID) error { | ||
// we'll unmarshal directly into `p.extra` to reuse the allocated tick array there | ||
if err := gojson.Unmarshal([]byte(entityPool.Extra), &p.extra); err != nil { | ||
return err | ||
} | ||
|
||
if p.extra.Tick == nil { | ||
return ErrTickNil | ||
} | ||
|
||
token0 := coreEntities.NewToken(uint(chainID), common.HexToAddress(entityPool.Tokens[0].Address), uint(entityPool.Tokens[0].Decimals), entityPool.Tokens[0].Symbol, entityPool.Tokens[0].Name) | ||
token1 := coreEntities.NewToken(uint(chainID), common.HexToAddress(entityPool.Tokens[1].Address), uint(entityPool.Tokens[1].Decimals), entityPool.Tokens[1].Symbol, entityPool.Tokens[1].Name) | ||
|
||
swapFeeFl := big.NewFloat(entityPool.SwapFee) | ||
swapFee, _ := swapFeeFl.Int(nil) | ||
tokens := make([]string, 2) | ||
reserves := make([]*big.Int, 2) | ||
if len(entityPool.Reserves) == 2 && len(entityPool.Tokens) == 2 { | ||
tokens[0] = entityPool.Tokens[0].Address | ||
reserves[0] = NewBig10(entityPool.Reserves[0]) | ||
tokens[1] = entityPool.Tokens[1].Address | ||
reserves[1] = NewBig10(entityPool.Reserves[1]) | ||
} | ||
|
||
// TickGob should be fully compatible with v3Entities.Tick (GobBigInt is just a wrapper around bigInt) | ||
// this should always be checked with a unit test | ||
// also, uninitialized tick should be ignored at pool-tracker/tick-based-worker already, so no need to check here | ||
v3Ticks := *(*v3TickList)(unsafe.Pointer(&p.extra.Ticks)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this assertion panic if the p.extra.Ticks is empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 've added a testcase for empty list |
||
|
||
// if the tick list is empty, the pool should be ignored | ||
if len(v3Ticks) == 0 { | ||
return ErrV3TicksEmpty | ||
} | ||
|
||
ticks, err := v3Entities.NewTickListDataProvider(v3Ticks, constants.TickSpacings[constants.FeeAmount(entityPool.SwapFee)]) | ||
if err != nil { | ||
fmt.Println("tick err", entityPool.Address, err) | ||
return err | ||
} | ||
|
||
p.V3Pool, err = v3Entities.NewPool( | ||
token0, | ||
token1, | ||
constants.FeeAmount(entityPool.SwapFee), | ||
p.extra.SqrtPriceX96, | ||
p.extra.Liquidity, | ||
int(p.extra.Tick.Int64()), | ||
ticks, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
tickMin := v3Ticks[0].Index | ||
tickMax := v3Ticks[len(v3Ticks)-1].Index | ||
|
||
p.Pool.Info.Address = strings.ToLower(entityPool.Address) | ||
p.Pool.Info.ReserveUsd = entityPool.ReserveUsd | ||
p.Pool.Info.SwapFee = swapFee | ||
p.Pool.Info.Exchange = entityPool.Exchange | ||
p.Pool.Info.Type = entityPool.Type | ||
p.Pool.Info.Tokens = tokens | ||
p.Pool.Info.Reserves = reserves | ||
p.Pool.Info.Checked = false | ||
|
||
p.gas = defaultGas | ||
p.tickMin = tickMin | ||
p.tickMax = tickMax | ||
|
||
return nil | ||
} | ||
|
||
/** | ||
* getSqrtPriceLimit get the price limit of pool based on the initialized ticks that this pool has | ||
*/ | ||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |
"fmt" | ||
"math/big" | ||
"strconv" | ||
|
||
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" | ||
) | ||
|
||
type Gas struct { | ||
|
@@ -59,6 +61,12 @@ type Tick struct { | |
LiquidityNet *big.Int `json:"liquidityNet"` | ||
} | ||
|
||
type TickGob struct { | ||
Index int `json:"index"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we don't need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. um in this PR we only use Gob for 2 bigInt (liq gross and liq net), not the whole struct, so we'll still need these tags |
||
LiquidityGross *bignumber.GobBigInt `json:"liquidityGross"` | ||
LiquidityNet *bignumber.GobBigInt `json:"liquidityNet"` | ||
} | ||
|
||
type Extra struct { | ||
Liquidity *big.Int `json:"liquidity"` | ||
SqrtPriceX96 *big.Int `json:"sqrtPriceX96"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package bignumber | ||
|
||
import ( | ||
"encoding/base64" | ||
"math/big" | ||
) | ||
|
||
// GobBigInt is a wrapper around big.Int that use Gob for json marshal/unmarshal | ||
type GobBigInt big.Int | ||
|
||
// almost all of our bigInt are for (u)int256, so should be only 33 bytes (32 for abs and 1 for version & sign) | ||
const MaxBigIntGobLength = 64 | ||
|
||
func (f *GobBigInt) ToBig() *big.Int { | ||
return (*big.Int)(f) | ||
} | ||
|
||
func (f *GobBigInt) MarshalText() ([]byte, error) { | ||
bi := f.ToBig() | ||
b, err := bi.GobEncode() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(base64.StdEncoding.EncodeToString(b)), nil | ||
} | ||
|
||
func (f *GobBigInt) UnmarshalText(input []byte) error { | ||
bi := f.ToBig() | ||
enc := base64.StdEncoding | ||
b64Len := len(input) | ||
bLen := enc.DecodedLen(b64Len) | ||
|
||
// if it's small enough then allocate temp store on stack to avoid GC | ||
if bLen < MaxBigIntGobLength { | ||
var b [MaxBigIntGobLength]byte | ||
n, err := base64.StdEncoding.Decode(b[:], input) | ||
if err != nil { | ||
return err | ||
} | ||
err = bi.GobDecode(b[:n]) | ||
return err | ||
} | ||
|
||
// otherwise fall back | ||
b := make([]byte, bLen) | ||
n, err := base64.StdEncoding.Decode(b, input) | ||
if err != nil { | ||
return err | ||
} | ||
return bi.GobDecode(b[:n]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package bignumber_test | ||
|
||
import ( | ||
"math/big" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGobBigIntMarshal(t *testing.T) { | ||
var bignum big.Int | ||
for i := 0; i < 100; i++ { | ||
number := rand.Int63() | ||
bi := big.NewInt(number) | ||
|
||
{ | ||
// check marshal unmarshal small int | ||
gbi1 := (*bignumber.GobBigInt)(bi) | ||
bytes, err := gbi1.MarshalText() | ||
require.Nil(t, err) | ||
|
||
gbi2 := (*bignumber.GobBigInt)(new(big.Int)) | ||
err = gbi2.UnmarshalText(bytes) | ||
require.Nil(t, err) | ||
assert.Equal(t, gbi1, gbi2) | ||
} | ||
|
||
// check marshal unmarshal big int (will start falling back to heap after sometime) | ||
bignum.Lsh(&bignum, 64) | ||
bignum.Add(&bignum, bi) | ||
{ | ||
gbi1 := (*bignumber.GobBigInt)(&bignum) | ||
bytes, err := gbi1.MarshalText() | ||
require.Nil(t, err) | ||
|
||
gbi2 := (*bignumber.GobBigInt)(new(big.Int)) | ||
err = gbi2.UnmarshalText(bytes) | ||
require.Nil(t, err) | ||
assert.Equal(t, gbi1, gbi2) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks weird, since we are not pre-loc p. Why do we have to write this type of pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
um this is for caller (for example combine-path lib) that want to simply create a simulator without worrying about pre-allocating simulator instance
(router-service will try to call
InitPoolSimulatorV2
directly on the re-used instance)