diff --git a/.github/workflows/go_generate.yaml b/.github/workflows/go_generate.yaml new file mode 100644 index 0000000..30a9de7 --- /dev/null +++ b/.github/workflows/go_generate.yaml @@ -0,0 +1,16 @@ +on: [push, pull_request] +name: Check go generate +jobs: + go-generate-check: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' + - name: Run go generate + run: go generate ./... + - name: Check working tree clean + run: if [ -z "$(git status --porcelain)" ]; then exit 0; else exit 1; fi \ No newline at end of file diff --git a/entities/pool.go b/entities/pool.go index 13b00b4..49657f5 100644 --- a/entities/pool.go +++ b/entities/pool.go @@ -1,3 +1,9 @@ +//go:generate go run github.com/tinylib/msgp -unexported -tests=false -v +//msgp:tuple Pool +//msgp:shim *big.Int as:[]byte using:msgpencode.EncodeInt/msgpencode.DecodeInt +//msgp:shim constants.FeeAmount as:uint64 using:uint64/constants.FeeAmount +//msgp:ignore StepComputations SwapResult GetOutputAmountResult GetInputAmountResult TickDataProvider + package entities import ( @@ -37,7 +43,7 @@ type Pool struct { SqrtRatioX96 *big.Int Liquidity *big.Int TickCurrent int - TickDataProvider TickDataProvider + TickDataProvider *TickDataProviderWrapper token0Price *entities.Price token1Price *entities.Price @@ -115,7 +121,7 @@ func NewPool(tokenA, tokenB *entities.Token, fee constants.FeeAmount, sqrtRatioX SqrtRatioX96: sqrtRatioX96, Liquidity: liquidity, TickCurrent: tickCurrent, - TickDataProvider: ticks, // TODO: new tick data provider + TickDataProvider: NewTickDataProviderWrapper(ticks), // TODO: new tick data provider }, nil } @@ -194,7 +200,7 @@ func (p *Pool) GetOutputAmount(inputAmount *entities.CurrencyAmount, sqrtPriceLi swapResult.sqrtRatioX96, swapResult.liquidity, swapResult.currentTick, - p.TickDataProvider, + p.TickDataProvider.Get(), ) if err != nil { return nil, err @@ -236,7 +242,7 @@ func (p *Pool) GetInputAmount(outputAmount *entities.CurrencyAmount, sqrtPriceLi swapResult.sqrtRatioX96, swapResult.liquidity, swapResult.currentTick, - p.TickDataProvider, + p.TickDataProvider.Get(), ) if err != nil { return nil, err diff --git a/entities/pool_gen.go b/entities/pool_gen.go new file mode 100644 index 0000000..a5e7a3b --- /dev/null +++ b/entities/pool_gen.go @@ -0,0 +1,516 @@ +package entities + +// Code generated by github.com/tinylib/msgp DO NOT EDIT. + +import ( + "github.com/KyberNetwork/pancake-v3-sdk/constants" + "github.com/daoleno/uniswap-sdk-core/entities" + "github.com/daoleno/uniswap-sdk-core/msgpencode" + "github.com/tinylib/msgp/msgp" +) + +// DecodeMsg implements msgp.Decodable +func (z *Pool) DecodeMsg(dc *msgp.Reader) (err error) { + var zb0001 uint32 + zb0001, err = dc.ReadArrayHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 9 { + err = msgp.ArrayError{Wanted: 9, Got: zb0001} + return + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "Token0") + return + } + z.Token0 = nil + } else { + if z.Token0 == nil { + z.Token0 = new(entities.Token) + } + err = z.Token0.DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "Token0") + return + } + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "Token1") + return + } + z.Token1 = nil + } else { + if z.Token1 == nil { + z.Token1 = new(entities.Token) + } + err = z.Token1.DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "Token1") + return + } + } + { + var zb0002 uint64 + zb0002, err = dc.ReadUint64() + if err != nil { + err = msgp.WrapError(err, "Fee") + return + } + z.Fee = constants.FeeAmount(zb0002) + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "SqrtRatioX96") + return + } + z.SqrtRatioX96 = nil + } else { + { + var zb0003 []byte + zb0003, err = dc.ReadBytes(msgpencode.EncodeInt(z.SqrtRatioX96)) + if err != nil { + err = msgp.WrapError(err, "SqrtRatioX96") + return + } + z.SqrtRatioX96 = msgpencode.DecodeInt(zb0003) + } + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "Liquidity") + return + } + z.Liquidity = nil + } else { + { + var zb0004 []byte + zb0004, err = dc.ReadBytes(msgpencode.EncodeInt(z.Liquidity)) + if err != nil { + err = msgp.WrapError(err, "Liquidity") + return + } + z.Liquidity = msgpencode.DecodeInt(zb0004) + } + } + z.TickCurrent, err = dc.ReadInt() + if err != nil { + err = msgp.WrapError(err, "TickCurrent") + return + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + z.TickDataProvider = nil + } else { + if z.TickDataProvider == nil { + z.TickDataProvider = new(TickDataProviderWrapper) + } + err = z.TickDataProvider.DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "token0Price") + return + } + z.token0Price = nil + } else { + if z.token0Price == nil { + z.token0Price = new(entities.Price) + } + err = z.token0Price.DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "token0Price") + return + } + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "token1Price") + return + } + z.token1Price = nil + } else { + if z.token1Price == nil { + z.token1Price = new(entities.Price) + } + err = z.token1Price.DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "token1Price") + return + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z *Pool) EncodeMsg(en *msgp.Writer) (err error) { + // array header, size 9 + err = en.Append(0x99) + if err != nil { + return + } + if z.Token0 == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = z.Token0.EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "Token0") + return + } + } + if z.Token1 == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = z.Token1.EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "Token1") + return + } + } + err = en.WriteUint64(uint64(z.Fee)) + if err != nil { + err = msgp.WrapError(err, "Fee") + return + } + if z.SqrtRatioX96 == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = en.WriteBytes(msgpencode.EncodeInt(z.SqrtRatioX96)) + if err != nil { + err = msgp.WrapError(err, "SqrtRatioX96") + return + } + } + if z.Liquidity == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = en.WriteBytes(msgpencode.EncodeInt(z.Liquidity)) + if err != nil { + err = msgp.WrapError(err, "Liquidity") + return + } + } + err = en.WriteInt(z.TickCurrent) + if err != nil { + err = msgp.WrapError(err, "TickCurrent") + return + } + if z.TickDataProvider == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = z.TickDataProvider.EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + } + if z.token0Price == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = z.token0Price.EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "token0Price") + return + } + } + if z.token1Price == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = z.token1Price.EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "token1Price") + return + } + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z *Pool) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // array header, size 9 + o = append(o, 0x99) + if z.Token0 == nil { + o = msgp.AppendNil(o) + } else { + o, err = z.Token0.MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "Token0") + return + } + } + if z.Token1 == nil { + o = msgp.AppendNil(o) + } else { + o, err = z.Token1.MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "Token1") + return + } + } + o = msgp.AppendUint64(o, uint64(z.Fee)) + if z.SqrtRatioX96 == nil { + o = msgp.AppendNil(o) + } else { + o = msgp.AppendBytes(o, msgpencode.EncodeInt(z.SqrtRatioX96)) + } + if z.Liquidity == nil { + o = msgp.AppendNil(o) + } else { + o = msgp.AppendBytes(o, msgpencode.EncodeInt(z.Liquidity)) + } + o = msgp.AppendInt(o, z.TickCurrent) + if z.TickDataProvider == nil { + o = msgp.AppendNil(o) + } else { + o, err = z.TickDataProvider.MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + } + if z.token0Price == nil { + o = msgp.AppendNil(o) + } else { + o, err = z.token0Price.MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "token0Price") + return + } + } + if z.token1Price == nil { + o = msgp.AppendNil(o) + } else { + o, err = z.token1Price.MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "token1Price") + return + } + } + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *Pool) UnmarshalMsg(bts []byte) (o []byte, err error) { + var zb0001 uint32 + zb0001, bts, err = msgp.ReadArrayHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 9 { + err = msgp.ArrayError{Wanted: 9, Got: zb0001} + return + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.Token0 = nil + } else { + if z.Token0 == nil { + z.Token0 = new(entities.Token) + } + bts, err = z.Token0.UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "Token0") + return + } + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.Token1 = nil + } else { + if z.Token1 == nil { + z.Token1 = new(entities.Token) + } + bts, err = z.Token1.UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "Token1") + return + } + } + { + var zb0002 uint64 + zb0002, bts, err = msgp.ReadUint64Bytes(bts) + if err != nil { + err = msgp.WrapError(err, "Fee") + return + } + z.Fee = constants.FeeAmount(zb0002) + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.SqrtRatioX96 = nil + } else { + { + var zb0003 []byte + zb0003, bts, err = msgp.ReadBytesBytes(bts, msgpencode.EncodeInt(z.SqrtRatioX96)) + if err != nil { + err = msgp.WrapError(err, "SqrtRatioX96") + return + } + z.SqrtRatioX96 = msgpencode.DecodeInt(zb0003) + } + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.Liquidity = nil + } else { + { + var zb0004 []byte + zb0004, bts, err = msgp.ReadBytesBytes(bts, msgpencode.EncodeInt(z.Liquidity)) + if err != nil { + err = msgp.WrapError(err, "Liquidity") + return + } + z.Liquidity = msgpencode.DecodeInt(zb0004) + } + } + z.TickCurrent, bts, err = msgp.ReadIntBytes(bts) + if err != nil { + err = msgp.WrapError(err, "TickCurrent") + return + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.TickDataProvider = nil + } else { + if z.TickDataProvider == nil { + z.TickDataProvider = new(TickDataProviderWrapper) + } + bts, err = z.TickDataProvider.UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.token0Price = nil + } else { + if z.token0Price == nil { + z.token0Price = new(entities.Price) + } + bts, err = z.token0Price.UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "token0Price") + return + } + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.token1Price = nil + } else { + if z.token1Price == nil { + z.token1Price = new(entities.Price) + } + bts, err = z.token1Price.UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "token1Price") + return + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z *Pool) Msgsize() (s int) { + s = 1 + if z.Token0 == nil { + s += msgp.NilSize + } else { + s += z.Token0.Msgsize() + } + if z.Token1 == nil { + s += msgp.NilSize + } else { + s += z.Token1.Msgsize() + } + s += msgp.Uint64Size + if z.SqrtRatioX96 == nil { + s += msgp.NilSize + } else { + s += msgp.BytesPrefixSize + len(msgpencode.EncodeInt(z.SqrtRatioX96)) + } + if z.Liquidity == nil { + s += msgp.NilSize + } else { + s += msgp.BytesPrefixSize + len(msgpencode.EncodeInt(z.Liquidity)) + } + s += msgp.IntSize + if z.TickDataProvider == nil { + s += msgp.NilSize + } else { + s += z.TickDataProvider.Msgsize() + } + if z.token0Price == nil { + s += msgp.NilSize + } else { + s += z.token0Price.Msgsize() + } + if z.token1Price == nil { + s += msgp.NilSize + } else { + s += z.token1Price.Msgsize() + } + return +} diff --git a/entities/pool_test.go b/entities/pool_test.go index 85e034f..5a57662 100644 --- a/entities/pool_test.go +++ b/entities/pool_test.go @@ -1,12 +1,14 @@ package entities import ( + "bytes" "math/big" "testing" "github.com/daoleno/uniswap-sdk-core/entities" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" + "github.com/tinylib/msgp/msgp" "github.com/KyberNetwork/pancake-v3-sdk/constants" "github.com/KyberNetwork/pancake-v3-sdk/utils" @@ -186,3 +188,23 @@ func TestGetInputAmount(t *testing.T) { assert.Equal(t, getInputAmountResult.ReturnedAmount.Quotient(), big.NewInt(100)) assert.Equal(t, getInputAmountResult.RemainingAmountOut.Quotient(), big.NewInt(0)) } + +func TestPoolMsgpEndecode(t *testing.T) { + poolWithNilProvider := newTestPool() + poolWithNilProvider.TickDataProvider = nil + pools := []*Pool{ + newTestPool(), + poolWithNilProvider, + } + for _, pool := range pools { + encoded := new(bytes.Buffer) + err := msgp.Encode(encoded, pool) + assert.NoError(t, err) + + decoded := new(Pool) + err = msgp.Decode(encoded, decoded) + assert.NoError(t, err) + + assert.EqualValues(t, pool, decoded) + } +} diff --git a/entities/tickdataprovider.go b/entities/tickdataprovider.go index 6086e61..85ef02f 100644 --- a/entities/tickdataprovider.go +++ b/entities/tickdataprovider.go @@ -1,3 +1,8 @@ +//go:generate go run github.com/tinylib/msgp -unexported -tests=false -v +//msgp:tuple Tick +//msgp:shim *big.Int as:[]byte using:msgpencode.EncodeInt/msgpencode.DecodeInt +//msgp:ignore TickDataProvider + package entities import "math/big" diff --git a/entities/tickdataprovider_gen.go b/entities/tickdataprovider_gen.go new file mode 100644 index 0000000..2671de4 --- /dev/null +++ b/entities/tickdataprovider_gen.go @@ -0,0 +1,193 @@ +package entities + +// Code generated by github.com/tinylib/msgp DO NOT EDIT. + +import ( + "github.com/daoleno/uniswap-sdk-core/msgpencode" + "github.com/tinylib/msgp/msgp" +) + +// DecodeMsg implements msgp.Decodable +func (z *Tick) DecodeMsg(dc *msgp.Reader) (err error) { + var zb0001 uint32 + zb0001, err = dc.ReadArrayHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 3 { + err = msgp.ArrayError{Wanted: 3, Got: zb0001} + return + } + z.Index, err = dc.ReadInt() + if err != nil { + err = msgp.WrapError(err, "Index") + return + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "LiquidityGross") + return + } + z.LiquidityGross = nil + } else { + { + var zb0002 []byte + zb0002, err = dc.ReadBytes(msgpencode.EncodeInt(z.LiquidityGross)) + if err != nil { + err = msgp.WrapError(err, "LiquidityGross") + return + } + z.LiquidityGross = msgpencode.DecodeInt(zb0002) + } + } + if dc.IsNil() { + err = dc.ReadNil() + if err != nil { + err = msgp.WrapError(err, "LiquidityNet") + return + } + z.LiquidityNet = nil + } else { + { + var zb0003 []byte + zb0003, err = dc.ReadBytes(msgpencode.EncodeInt(z.LiquidityNet)) + if err != nil { + err = msgp.WrapError(err, "LiquidityNet") + return + } + z.LiquidityNet = msgpencode.DecodeInt(zb0003) + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z *Tick) EncodeMsg(en *msgp.Writer) (err error) { + // array header, size 3 + err = en.Append(0x93) + if err != nil { + return + } + err = en.WriteInt(z.Index) + if err != nil { + err = msgp.WrapError(err, "Index") + return + } + if z.LiquidityGross == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = en.WriteBytes(msgpencode.EncodeInt(z.LiquidityGross)) + if err != nil { + err = msgp.WrapError(err, "LiquidityGross") + return + } + } + if z.LiquidityNet == nil { + err = en.WriteNil() + if err != nil { + return + } + } else { + err = en.WriteBytes(msgpencode.EncodeInt(z.LiquidityNet)) + if err != nil { + err = msgp.WrapError(err, "LiquidityNet") + return + } + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z *Tick) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // array header, size 3 + o = append(o, 0x93) + o = msgp.AppendInt(o, z.Index) + if z.LiquidityGross == nil { + o = msgp.AppendNil(o) + } else { + o = msgp.AppendBytes(o, msgpencode.EncodeInt(z.LiquidityGross)) + } + if z.LiquidityNet == nil { + o = msgp.AppendNil(o) + } else { + o = msgp.AppendBytes(o, msgpencode.EncodeInt(z.LiquidityNet)) + } + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *Tick) UnmarshalMsg(bts []byte) (o []byte, err error) { + var zb0001 uint32 + zb0001, bts, err = msgp.ReadArrayHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 3 { + err = msgp.ArrayError{Wanted: 3, Got: zb0001} + return + } + z.Index, bts, err = msgp.ReadIntBytes(bts) + if err != nil { + err = msgp.WrapError(err, "Index") + return + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.LiquidityGross = nil + } else { + { + var zb0002 []byte + zb0002, bts, err = msgp.ReadBytesBytes(bts, msgpencode.EncodeInt(z.LiquidityGross)) + if err != nil { + err = msgp.WrapError(err, "LiquidityGross") + return + } + z.LiquidityGross = msgpencode.DecodeInt(zb0002) + } + } + if msgp.IsNil(bts) { + bts, err = msgp.ReadNilBytes(bts) + if err != nil { + return + } + z.LiquidityNet = nil + } else { + { + var zb0003 []byte + zb0003, bts, err = msgp.ReadBytesBytes(bts, msgpencode.EncodeInt(z.LiquidityNet)) + if err != nil { + err = msgp.WrapError(err, "LiquidityNet") + return + } + z.LiquidityNet = msgpencode.DecodeInt(zb0003) + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z *Tick) Msgsize() (s int) { + s = 1 + msgp.IntSize + if z.LiquidityGross == nil { + s += msgp.NilSize + } else { + s += msgp.BytesPrefixSize + len(msgpencode.EncodeInt(z.LiquidityGross)) + } + if z.LiquidityNet == nil { + s += msgp.NilSize + } else { + s += msgp.BytesPrefixSize + len(msgpencode.EncodeInt(z.LiquidityNet)) + } + return +} diff --git a/entities/tickdataprovider_msgpencode.go b/entities/tickdataprovider_msgpencode.go new file mode 100644 index 0000000..31157b3 --- /dev/null +++ b/entities/tickdataprovider_msgpencode.go @@ -0,0 +1,150 @@ +package entities + +import ( + "fmt" + "reflect" + + "github.com/tinylib/msgp/msgp" +) + +var ( + // Mapping from string representation of a TickDataProvider concrete type to the concrete type itself. + // The string representation is used as type discriminator when encoding/decoding TickDataProvider. + tickDataProviderImplMap = map[string]reflect.Type{} +) + +// RegisterTickDataProviderImpl registers the concrete types of an TickDataProvider. +// This function is not thread-safe and should be only call in init(). +func RegisterTickDataProviderImpl(provider TickDataProvider) { + if _, ok := provider.(msgp.Encodable); !ok { + panic("expected provider to implement msgp.Encodable") + } + if _, ok := provider.(msgp.Decodable); !ok { + panic("expected provider to implement msgp.Decodable") + } + if _, ok := provider.(msgp.Marshaler); !ok { + panic("expected provider to implement msgp.Marshaler") + } + if _, ok := provider.(msgp.Unmarshaler); !ok { + panic("expected provider to implement msgp.Unmarshaler") + } + if _, ok := provider.(msgp.Sizer); !ok { + panic("expected provider to implement msgp.Sizer") + } + typ := reflect.ValueOf(provider).Elem().Type() + tickDataProviderImplMap[typ.String()] = typ +} + +// TickDataProviderWrapper is a wrapper of TickDataProvider and is implemented msgp.Encodable, msgp.Decodable, msgp.Marshaler, msgp.Unmarshaler, and msgp.Sizer +type TickDataProviderWrapper struct { + TickDataProvider +} + +func NewTickDataProviderWrapper(provider TickDataProvider) *TickDataProviderWrapper { + if provider == nil { + return nil + } + return &TickDataProviderWrapper{provider} +} + +// Get the inner TickDataProvider, return nil if TickDataProviderWrapper is nil +func (p *TickDataProviderWrapper) Get() TickDataProvider { + if p == nil { + return nil + } + return p.TickDataProvider +} + +// EncodeMsg implements msgp.Encodable +func (p *TickDataProviderWrapper) EncodeMsg(en *msgp.Writer) (err error) { + typ := reflect.ValueOf(p.TickDataProvider).Elem().Type() + err = en.WriteString(typ.String()) + if err != nil { + return + } + + if _, ok := tickDataProviderImplMap[typ.String()]; !ok { + err = fmt.Errorf("unregistered type %s", typ.String()) + return + } + + err = p.TickDataProvider.(msgp.Encodable).EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + return +} + +// DecodeMsg implements msgp.Decodable +func (p *TickDataProviderWrapper) DecodeMsg(dc *msgp.Reader) (err error) { + var typStr string + typStr, err = dc.ReadString() + if err != nil { + return + } + + typ, ok := tickDataProviderImplMap[typStr] + if !ok { + err = fmt.Errorf("unregistered type %s", typStr) + return + } + + providerVal := reflect.New(typ) + err = providerVal.Interface().(msgp.Decodable).DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "TickDataProviderMsgp") + return + } + p.TickDataProvider = providerVal.Interface().(TickDataProvider) + return +} + +// MarshalMsg implements msgp.Marshaler +func (p *TickDataProviderWrapper) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, p.Msgsize()) + typ := reflect.ValueOf(p.TickDataProvider).Elem().Type() + o = msgp.AppendString(o, typ.String()) + + if _, ok := tickDataProviderImplMap[typ.String()]; !ok { + err = fmt.Errorf("unregistered type %s", typ.String()) + return + } + + o, err = p.TickDataProvider.(msgp.Marshaler).MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (p *TickDataProviderWrapper) UnmarshalMsg(bts []byte) (o []byte, err error) { + var typStr string + typStr, bts, err = msgp.ReadStringBytes(bts) + if err != nil { + return + } + + typ, ok := tickDataProviderImplMap[typStr] + if !ok { + err = fmt.Errorf("unregistered type %s", typStr) + return + } + + providerVal := reflect.New(typ) + bts, err = providerVal.Interface().(msgp.Unmarshaler).UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "TickDataProvider") + return + } + p.TickDataProvider = providerVal.Interface().(TickDataProvider) + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (p *TickDataProviderWrapper) Msgsize() int { + typ := reflect.ValueOf(p.TickDataProvider).Elem().Type() + return msgp.StringPrefixSize + len(typ.String()) + p.TickDataProvider.(msgp.Sizer).Msgsize() +} diff --git a/entities/ticklistdataprovider.go b/entities/ticklistdataprovider.go index e6e6429..e236ca3 100644 --- a/entities/ticklistdataprovider.go +++ b/entities/ticklistdataprovider.go @@ -1,5 +1,12 @@ +//go:generate go run github.com/tinylib/msgp -unexported -tests=false -v +//msgp:tuple TickListDataProvider + package entities +func init() { + RegisterTickDataProviderImpl(&TickListDataProvider{}) +} + // A data provider for ticks that is backed by an in-memory array of ticks. type TickListDataProvider struct { ticks []Tick diff --git a/entities/ticklistdataprovider_gen.go b/entities/ticklistdataprovider_gen.go new file mode 100644 index 0000000..b3464b9 --- /dev/null +++ b/entities/ticklistdataprovider_gen.go @@ -0,0 +1,121 @@ +package entities + +// Code generated by github.com/tinylib/msgp DO NOT EDIT. + +import ( + "github.com/tinylib/msgp/msgp" +) + +// DecodeMsg implements msgp.Decodable +func (z *TickListDataProvider) DecodeMsg(dc *msgp.Reader) (err error) { + var zb0001 uint32 + zb0001, err = dc.ReadArrayHeader() + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 1 { + err = msgp.ArrayError{Wanted: 1, Got: zb0001} + return + } + var zb0002 uint32 + zb0002, err = dc.ReadArrayHeader() + if err != nil { + err = msgp.WrapError(err, "ticks") + return + } + if cap(z.ticks) >= int(zb0002) { + z.ticks = (z.ticks)[:zb0002] + } else { + z.ticks = make([]Tick, zb0002) + } + for za0001 := range z.ticks { + err = z.ticks[za0001].DecodeMsg(dc) + if err != nil { + err = msgp.WrapError(err, "ticks", za0001) + return + } + } + return +} + +// EncodeMsg implements msgp.Encodable +func (z *TickListDataProvider) EncodeMsg(en *msgp.Writer) (err error) { + // array header, size 1 + err = en.Append(0x91) + if err != nil { + return + } + err = en.WriteArrayHeader(uint32(len(z.ticks))) + if err != nil { + err = msgp.WrapError(err, "ticks") + return + } + for za0001 := range z.ticks { + err = z.ticks[za0001].EncodeMsg(en) + if err != nil { + err = msgp.WrapError(err, "ticks", za0001) + return + } + } + return +} + +// MarshalMsg implements msgp.Marshaler +func (z *TickListDataProvider) MarshalMsg(b []byte) (o []byte, err error) { + o = msgp.Require(b, z.Msgsize()) + // array header, size 1 + o = append(o, 0x91) + o = msgp.AppendArrayHeader(o, uint32(len(z.ticks))) + for za0001 := range z.ticks { + o, err = z.ticks[za0001].MarshalMsg(o) + if err != nil { + err = msgp.WrapError(err, "ticks", za0001) + return + } + } + return +} + +// UnmarshalMsg implements msgp.Unmarshaler +func (z *TickListDataProvider) UnmarshalMsg(bts []byte) (o []byte, err error) { + var zb0001 uint32 + zb0001, bts, err = msgp.ReadArrayHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err) + return + } + if zb0001 != 1 { + err = msgp.ArrayError{Wanted: 1, Got: zb0001} + return + } + var zb0002 uint32 + zb0002, bts, err = msgp.ReadArrayHeaderBytes(bts) + if err != nil { + err = msgp.WrapError(err, "ticks") + return + } + if cap(z.ticks) >= int(zb0002) { + z.ticks = (z.ticks)[:zb0002] + } else { + z.ticks = make([]Tick, zb0002) + } + for za0001 := range z.ticks { + bts, err = z.ticks[za0001].UnmarshalMsg(bts) + if err != nil { + err = msgp.WrapError(err, "ticks", za0001) + return + } + } + o = bts + return +} + +// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message +func (z *TickListDataProvider) Msgsize() (s int) { + s = 1 + msgp.ArrayHeaderSize + for za0001 := range z.ticks { + s += z.ticks[za0001].Msgsize() + } + return +} diff --git a/go.mod b/go.mod index 3e5487f..bc97eaa 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,14 @@ module github.com/KyberNetwork/pancake-v3-sdk go 1.20 +replace github.com/daoleno/uniswap-sdk-core v0.1.7 => github.com/KyberNetwork/uniswap-sdk-core v0.1.9 + require ( github.com/daoleno/uniswap-sdk-core v0.1.7 github.com/ethereum/go-ethereum v1.10.22 github.com/shopspring/decimal v1.3.1 github.com/stretchr/testify v1.8.0 + github.com/tinylib/msgp v1.1.9 ) require ( @@ -14,8 +17,11 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/philhofer/fwd v1.1.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect - golang.org/x/sys v0.0.0-20220702020025-31831981b65f // indirect + golang.org/x/mod v0.13.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/tools v0.14.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e49d42d..dadb2f3 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ +github.com/KyberNetwork/uniswap-sdk-core v0.1.9 h1:04GqfoYM1vTk55HGlIZfVJ3Wo1W8lsAelW6L+qkfv6c= +github.com/KyberNetwork/uniswap-sdk-core v0.1.9/go.mod h1:ih9PJ/qgEbc1VbWOWWPeiH02a+inIBm2+XNtZmONcyY= github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/daoleno/uniswap-sdk-core v0.1.7 h1:PdZypLSzM5Mu2rFBjXK9XrHDppSt62GkxXjWLpuMAN4= -github.com/daoleno/uniswap-sdk-core v0.1.7/go.mod h1:DPzL8zNicstPzvX74ZeeHsiIUquZRpwviceDHQ8+UQ4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -17,6 +17,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= +github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -29,10 +31,16 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/tinylib/msgp v1.1.9 h1:SHf3yoO2sGA0veCJeCBYLHuttAVFHGm2RHgNodW7wQU= +github.com/tinylib/msgp v1.1.9/go.mod h1:BCXGB54lDD8qUEPmiG0cQQUANC4IUQyB2ItS2UDlO/k= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/sys v0.0.0-20220702020025-31831981b65f h1:xdsejrW/0Wf2diT5CPp3XmKUNbr7Xvw8kYilQ+6qjRY= -golang.org/x/sys v0.0.0-20220702020025-31831981b65f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=