Skip to content

Commit

Permalink
Update examples and add in helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanz0rz committed Jan 22, 2025
1 parent bb461f8 commit 8981967
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
34 changes: 34 additions & 0 deletions sdk-clients/orderbook/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/1inch/1inch-sdk-go/common"
)
Expand Down Expand Up @@ -98,6 +99,39 @@ func (api *api) GetOrder(ctx context.Context, params GetOrderParams) (*GetOrderB
return NormalizeGetOrderByHashResponse(getOrderByHashResponse)
}

// GetOrderWithSignature first looks up an order by hash, then does a second request to get the signature data
func (api *api) GetOrderWithSignature(ctx context.Context, params GetOrderParams) (*OrderExtendedWithSignature, error) {

// First lookup the order by hash (no signature on this response)
order, err := api.GetOrder(ctx, params)
if err != nil {
return nil, err
}

// For free accounts, this sleep is required to avoid 429 errors
if params.SleepBetweenSubrequests {
time.Sleep(time.Second)
}

// Second, lookup all orders by that creator (these orders will contain the signature data)
allOrdersByCreator, err := api.GetOrdersByCreatorAddress(ctx, GetOrdersByCreatorAddressParams{

Check failure on line 117 in sdk-clients/orderbook/api.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
CreatorAddress: order.OrderMaker,
})

// Filter through the second set of orders to find the signature
for _, o := range allOrdersByCreator {
if o.OrderHash == params.OrderHash {
return &OrderExtendedWithSignature{
GetOrderByHashResponse: order.GetOrderByHashResponse,
LimitOrderDataNormalized: order.LimitOrderDataNormalized,
Signature: o.Signature,
}, nil
}
}

return nil, errors.New("order not found")
}

// GetAllOrders returns all orders in the Limit Order Protocol
func (api *api) GetAllOrders(ctx context.Context, params GetAllOrdersParams) ([]OrderResponse, error) {
u := fmt.Sprintf("/orderbook/v3.0/%d/all", api.chainId)
Expand Down
14 changes: 12 additions & 2 deletions sdk-clients/orderbook/examples/create_and_fill_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,29 @@ func main() {
log.Fatalf("Request completed, but order creation status was a failure: %v\n", createOrderResponse)
}

fmt.Println("Order created! Getting order hash...")

// Sleep to accommodate free-tier API keys
time.Sleep(time.Second)

ordersByCreatorResponse, err := client.GetOrdersByCreatorAddress(ctx, orderbook.GetOrdersByCreatorAddressParams{
CreatorAddress: client.Wallet.Address().Hex(),
})

fmt.Printf("Order created! \nOrder hash: %v\n", ordersByCreatorResponse[0].OrderHash)
fmt.Printf("Order hash: %v\n", ordersByCreatorResponse[0].OrderHash)
fmt.Println("Getting signature...")

// Sleep to accommodate free-tier API keys
time.Sleep(time.Second)

fillOrderData, err := client.GetFillOrderCalldata(ordersByCreatorResponse[0], nil)
orderWithSignature, err := client.GetOrderWithSignature(ctx, orderbook.GetOrderParams{
OrderHash: ordersByCreatorResponse[0].OrderHash,
SleepBetweenSubrequests: true,
})

fmt.Println("Getting retrieved! Filling order...")

fillOrderData, err := client.GetFillOrderCalldata(orderWithSignature, nil)
if err != nil {
log.Fatalf("Failed to get fill order calldata: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions sdk-clients/orderbook/examples/fill_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func main() {
}
client, err := orderbook.NewClient(config)

getOrderResponse, err := client.GetOrder(ctx, orderbook.GetOrderParams{
OrderHash: limitOrderHash,
})
params := orderbook.GetOrderParams{
OrderHash: limitOrderHash,
SleepBetweenSubrequests: true,
}
getOrderResponse, err := client.GetOrderWithSignature(ctx, params)

takerTraits := orderbook.NewTakerTraits(orderbook.TakerTraitsParams{
Extension: getOrderResponse.Data.Extension,
Expand Down
9 changes: 8 additions & 1 deletion sdk-clients/orderbook/orderbook_types_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type GetOrdersByCreatorAddressParams struct {
}

type GetOrderParams struct {
OrderHash string
OrderHash string
SleepBetweenSubrequests bool // For free accounts, this should be set to true to avoid 429 errors when using the GetOrderWithSignature method
}

type GetAllOrdersParams struct {
Expand Down Expand Up @@ -144,6 +145,12 @@ type GetOrderByHashResponseExtended struct {
LimitOrderDataNormalized NormalizedLimitOrderData
}

type OrderExtendedWithSignature struct {
GetOrderByHashResponse
LimitOrderDataNormalized NormalizedLimitOrderData
Signature string
}

type NormalizedLimitOrderData struct {
Salt *big.Int
MakerAsset *big.Int
Expand Down
15 changes: 5 additions & 10 deletions sdk-clients/orderbook/web3data.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ func (c *Client) GetSeriesNonce(ctx context.Context, publicAddress gethCommon.Ad
return nonce, nil
}

func (c *Client) GetFillOrderCalldata(orderResponse *OrderResponse, takerTraits *TakerTraits) ([]byte, error) {

orderResponseExtended, err := NormalizeOrderResponse(orderResponse)
if err != nil {
return nil, err
}
func (c *Client) GetFillOrderCalldata(order *OrderExtendedWithSignature, takerTraits *TakerTraits) ([]byte, error) {

var function string
if orderResponseExtended.Data.Extension == "0x" {
if order.Data.Extension == "0x" {
function = "fillOrder"
} else {
if takerTraits == nil {
Expand All @@ -57,7 +52,7 @@ func (c *Client) GetFillOrderCalldata(orderResponse *OrderResponse, takerTraits
function = "fillOrderArgs"
}

compressedSignature, err := CompressSignature(orderResponseExtended.Signature[2:])
compressedSignature, err := CompressSignature(order.Signature[2:])
if err != nil {
return nil, err
}
Expand All @@ -76,13 +71,13 @@ func (c *Client) GetFillOrderCalldata(orderResponse *OrderResponse, takerTraits

switch function {
case "fillOrder":
fillOrderData, err = c.AggregationRouterV6.Pack(function, orderResponseExtended.LimitOrderDataNormalized, rCompressed, vsCompressed, orderResponseExtended.LimitOrderDataNormalized.TakingAmount, big.NewInt(0))
fillOrderData, err = c.AggregationRouterV6.Pack(function, order.LimitOrderDataNormalized, rCompressed, vsCompressed, order.LimitOrderDataNormalized.TakingAmount, big.NewInt(0))
if err != nil {
return nil, err
}
case "fillOrderArgs":
takerTraitsEncoded := takerTraits.Encode()
fillOrderData, err = c.AggregationRouterV6.Pack(function, orderResponseExtended.LimitOrderDataNormalized, rCompressed, vsCompressed, orderResponseExtended.LimitOrderDataNormalized.TakingAmount, takerTraitsEncoded.TraitFlags, takerTraitsEncoded.Args)
fillOrderData, err = c.AggregationRouterV6.Pack(function, order.LimitOrderDataNormalized, rCompressed, vsCompressed, order.LimitOrderDataNormalized.TakingAmount, takerTraitsEncoded.TraitFlags, takerTraitsEncoded.Args)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8981967

Please sign in to comment.