Skip to content

Commit

Permalink
Adding TakerTrait, MakerTrait, and Extension structs and improving ov…
Browse files Browse the repository at this point in the history
…erall structure (#58)

Co-authored-by: Nick Kozlov <22479658+EnoRage@users.noreply.github.com>
  • Loading branch information
Tanz0rz and EnoRage authored May 13, 2024
1 parent 90579cc commit ccc73f2
Show file tree
Hide file tree
Showing 17 changed files with 707 additions and 523 deletions.
7 changes: 0 additions & 7 deletions sdk-clients/orderbook/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import (
"github.com/1inch/1inch-sdk-go/common"
)

// Empty Extensions for Orderbook API are represented as 0x instead of a blank string
const emptyExtension = "0x"

// CreateOrder creates an order in the Limit Order Protocol
func (api *api) CreateOrder(ctx context.Context, params CreateOrderParams) (*CreateOrderResponse, error) {
u := fmt.Sprintf("/orderbook/v4.0/%d", api.chainId)

if params.Extension == "" {
params.Extension = emptyExtension
}

err := params.Validate()
if err != nil {
return nil, err
Expand Down
49 changes: 49 additions & 0 deletions sdk-clients/orderbook/bitmask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package orderbook

import (
"fmt"
"math/big"
)

type BitMask struct {
Offset *big.Int
Mask *big.Int
}

// NewBitMask creates a new BitMask with the given start and end bit positions.
func NewBitMask(startBit, endBit *big.Int) *BitMask {
if startBit.Cmp(endBit) >= 0 {
panic("BitMask: startBit must be less than endBit")
}

bitCount := new(big.Int).Sub(endBit, startBit) // endBit - startBit
mask := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(bitCount.Uint64())), big.NewInt(1)) // (1 << bitCount) - 1

return &BitMask{
Offset: startBit,
Mask: mask,
}
}

func (b *BitMask) SetBits(value, bits *big.Int) *big.Int {
// Create the shifted mask
shiftedMask := new(big.Int).Set(b.Mask)
shiftedMask.Lsh(shiftedMask, uint(b.Offset.Uint64()))
// Clear the bits at the mask location
value.And(value, new(big.Int).Not(shiftedMask))
// Shift the bits to the correct location
shiftedBits := new(big.Int).Lsh(bits, uint(b.Offset.Uint64()))
value.Or(value, shiftedBits)
return value
}

// ToString returns the string representation of the mask shifted by the offset.
func (b *BitMask) ToString() string {
shiftedMask := new(big.Int).Lsh(b.Mask, uint(b.Offset.Uint64()))
return fmt.Sprintf("0x%x", shiftedMask)
}

// ToBigInt returns the mask value as a big.Int, shifted by the offset.
func (b *BitMask) ToBigInt() *big.Int {
return new(big.Int).Lsh(b.Mask, uint(b.Offset.Uint64()))
}
142 changes: 142 additions & 0 deletions sdk-clients/orderbook/bitmask_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package orderbook

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBitMask(t *testing.T) {
tests := []struct {
name string
startBit int64
endBit int64
valueToUpdate string
inputBits string
expectedOutputBits string
}{
{
name: "Simple single bit mask",
startBit: 0,
endBit: 1,
valueToUpdate: "0",
inputBits: "1",
expectedOutputBits: "1",
},
{
name: "Set middle bits",
startBit: 4,
endBit: 8,
valueToUpdate: "110000000000",
inputBits: "1111",
expectedOutputBits: "110011110000",
},
{
name: "Clear bits",
startBit: 4,
endBit: 8,
valueToUpdate: "11111111",
inputBits: "0",
expectedOutputBits: "00001111",
},
{
name: "Set bits in an existing value",
startBit: 4,
endBit: 6,
valueToUpdate: "11110000",
inputBits: "11",
expectedOutputBits: "11110000",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
startBit := big.NewInt(tc.startBit)
endBit := big.NewInt(tc.endBit)
valueToUpdate := bitStringToBigInt(tc.valueToUpdate)
inputBits := bitStringToBigInt(tc.inputBits)
expectedOutputBits := bitStringToBigInt(tc.expectedOutputBits)

bitmask := NewBitMask(startBit, endBit)
result := bitmask.SetBits(valueToUpdate, inputBits)
assert.Equal(t, expectedOutputBits, result)
})
}
}

func TestBitMaskToString(t *testing.T) {
tests := []struct {
name string
startBit int64
endBit int64
expectedString string
}{
{
name: "Simple mask",
startBit: 4,
endBit: 8,
expectedString: "0xf0",
},
{
name: "Single bit mask",
startBit: 0,
endBit: 1,
expectedString: "0x1",
},
{
name: "Full byte mask",
startBit: 0,
endBit: 8,
expectedString: "0xff",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
bitmask := NewBitMask(big.NewInt(tc.startBit), big.NewInt(tc.endBit))
assert.Equal(t, tc.expectedString, bitmask.ToString())
})
}
}

func TestBitMaskToBigInt(t *testing.T) {
tests := []struct {
name string
startBit int64
endBit int64
expectedBigInt *big.Int
}{
{
name: "Simple mask",
startBit: 4,
endBit: 8,
expectedBigInt: bitStringToBigInt("11110000"),
},
{
name: "Single bit mask",
startBit: 0,
endBit: 1,
expectedBigInt: bitStringToBigInt("00000001"),
},
{
name: "Full byte mask",
startBit: 0,
endBit: 8,
expectedBigInt: bitStringToBigInt("11111111"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
bitmask := NewBitMask(big.NewInt(tc.startBit), big.NewInt(tc.endBit))
assert.Equal(t, tc.expectedBigInt, bitmask.ToBigInt())
})
}
}

func bitStringToBigInt(bitStr string) *big.Int {
i := new(big.Int)
i.SetString(bitStr, 2)
return i
}
7 changes: 3 additions & 4 deletions sdk-clients/orderbook/examples/create_and_fill_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
log.Fatal(fmt.Errorf("failed to get series nonce: %v", err))
}

buildMakerTraitsParams := orderbook.BuildMakerTraitsParams{
makerTraits := orderbook.NewMakerTraits(orderbook.MakerTraitsParams{
AllowedSender: zeroAddress,
ShouldCheckEpoch: false,
UsePermit2: false,
Expand All @@ -70,8 +70,7 @@ func main() {
Expiry: expireAfter,
Nonce: seriesNonce.Int64(),
Series: 0, // TODO: Series 0 always?
}
makerTraits := orderbook.BuildMakerTraits(buildMakerTraitsParams)
})

createOrderResponse, err := client.CreateOrder(ctx, orderbook.CreateOrderParams{
SeriesNonce: seriesNonce,
Expand Down Expand Up @@ -110,7 +109,7 @@ func main() {
OrderHash: getOrderResponse[0].OrderHash,
})

fillOrderData, err := client.GetFillOrderCalldata(getOrderRresponse)
fillOrderData, err := client.GetFillOrderCalldata(getOrderRresponse, nil)

aggregationRouter, err := constants.Get1inchRouterFromChainId(chainId)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions sdk-clients/orderbook/examples/create_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
log.Fatal(fmt.Errorf("failed to get series nonce: %v", err))
}

buildMakerTraitsParams := orderbook.BuildMakerTraitsParams{
makerTraits := orderbook.NewMakerTraits(orderbook.MakerTraitsParams{
AllowedSender: zeroAddress,
ShouldCheckEpoch: false,
UsePermit2: false,
Expand All @@ -69,8 +69,7 @@ func main() {
Expiry: expireAfter,
Nonce: seriesNonce.Int64(),
Series: 0, // TODO: Series 0 always?
}
makerTraits := orderbook.BuildMakerTraits(buildMakerTraitsParams)
})

createOrderResponse, err := client.CreateOrder(ctx, orderbook.CreateOrderParams{
SeriesNonce: seriesNonce,
Expand Down
14 changes: 6 additions & 8 deletions sdk-clients/orderbook/examples/create_order_permit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,20 @@ func main() {
if err != nil {
panic(err)
}

permit, err := client.Wallet.TokenPermit(*permitData)
if err != nil {
log.Fatal(fmt.Errorf("Failed to get permit: %v\n", err))
}

interactions, err := orderbook.GetInteractions(PolygonFRAX, orderbook.Trim0x(permit))
extension, err := orderbook.NewExtension(orderbook.ExtensionParams{
MakerAsset: PolygonFRAX,
Permit: permit,
})
if err != nil {
log.Fatal(fmt.Errorf("Failed to get interactions: %v\n", err))
log.Fatalf("Failed to create extension: %v\n", err)
}

interactionsConcatenated := orderbook.ConcatenateInteractions(interactions)
interactionsOffsets := orderbook.GetOffsets(interactions)
extension := orderbook.BuildExtension(interactionsConcatenated, interactionsOffsets)

makerTraits := orderbook.BuildMakerTraits(orderbook.BuildMakerTraitsParams{
makerTraits := orderbook.NewMakerTraits(orderbook.MakerTraitsParams{
AllowedSender: zeroAddress,
ShouldCheckEpoch: false,
UsePermit2: false,
Expand Down
10 changes: 7 additions & 3 deletions sdk-clients/orderbook/examples/fill_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
)

const (
limitOrderHash = "0x073797847405119e8de253d97b281853748e690065b4ae04e5eda9d282f1015b"
limitOrderHash = "0xddf0907b11b3dba4591372e201117f09f9cdc7ccfff175397bc57eeb7043a59f"
chainId = 137
)

Expand All @@ -39,11 +39,15 @@ func main() {
}
client, err := orderbook.NewClient(config)

getOrderRresponse, err := client.GetOrder(ctx, orderbook.GetOrderParams{
getOrderResponse, err := client.GetOrder(ctx, orderbook.GetOrderParams{
OrderHash: limitOrderHash,
})

fillOrderData, err := client.GetFillOrderCalldata(getOrderRresponse)
takerTraits := orderbook.NewTakerTraits(orderbook.TakerTraitsParams{
Extension: getOrderResponse.Data.Extension,
})

fillOrderData, err := client.GetFillOrderCalldata(getOrderResponse, takerTraits)
if err != nil {
log.Fatalf("Failed to get fill order calldata: %v", err)
}
Expand Down
Loading

0 comments on commit ccc73f2

Please sign in to comment.