Skip to content
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

Updating Orderbook SDK to use the maker endpoints to get signature data #93

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 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 @@ -51,7 +52,7 @@ func (api *api) CreateOrder(ctx context.Context, params CreateOrderParams) (*Cre
// TODO Reusing the same request/response objects until the openapi spec is updated to include the correct object definitions

// GetOrdersByCreatorAddress returns all orders created by a given address in the Limit Order Protocol
func (api *api) GetOrdersByCreatorAddress(ctx context.Context, params GetOrdersByCreatorAddressParams) ([]OrderResponse, error) {
func (api *api) GetOrdersByCreatorAddress(ctx context.Context, params GetOrdersByCreatorAddressParams) ([]*OrderResponse, error) {
u := fmt.Sprintf("/orderbook/v4.0/%d/address/%s", api.chainId, params.CreatorAddress)

err := params.Validate()
Expand All @@ -65,7 +66,7 @@ func (api *api) GetOrdersByCreatorAddress(ctx context.Context, params GetOrdersB
U: u,
}

var ordersResponse []OrderResponse
var ordersResponse []*OrderResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &ordersResponse)
if err != nil {
return nil, err
Expand Down Expand Up @@ -98,6 +99,42 @@ 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{
CreatorAddress: order.OrderMaker,
})
if err != nil {
return nil, err
}

// 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
19 changes: 14 additions & 5 deletions sdk-clients/orderbook/examples/create_and_fill_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,32 @@ 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)

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

fmt.Printf("Order created! \nOrder hash: %v\n", getOrderResponse[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)

getOrderRresponse, err := client.GetOrder(ctx, orderbook.GetOrderParams{
OrderHash: getOrderResponse[0].OrderHash,
orderWithSignature, err := client.GetOrderWithSignature(ctx, orderbook.GetOrderParams{
OrderHash: ordersByCreatorResponse[0].OrderHash,
SleepBetweenSubrequests: true,
})

fillOrderData, err := client.GetFillOrderCalldata(getOrderRresponse, nil)
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)
}

aggregationRouter, err := constants.Get1inchRouterFromChainId(chainId)
if err != nil {
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
15 changes: 13 additions & 2 deletions 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 @@ -92,6 +93,11 @@ type OrderResponse struct {
OrderInvalidReason interface{} `json:"orderInvalidReason"`
}

type OrderResponseExtended struct {
OrderResponse
LimitOrderDataNormalized NormalizedLimitOrderData
}

type GetOrderByHashResponse struct {
ID int `json:"id"`
OrderHash string `json:"orderHash"`
Expand All @@ -101,7 +107,6 @@ type GetOrderByHashResponse struct {
MakerAsset string `json:"makerAsset"`
OrderMaker string `json:"orderMaker"`
OrderStatus int `json:"orderStatus"`
Signature string `json:"signature"`
MakerAmount string `json:"makerAmount"`
RemainingMakerAmount string `json:"remainingMakerAmount"`
MakerBalance string `json:"makerBalance"`
Expand Down Expand Up @@ -140,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
10 changes: 5 additions & 5 deletions sdk-clients/orderbook/web3data.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func (c *Client) GetSeriesNonce(ctx context.Context, publicAddress gethCommon.Ad
return nonce, nil
}

func (c *Client) GetFillOrderCalldata(getOrderResponse *GetOrderByHashResponseExtended, takerTraits *TakerTraits) ([]byte, error) {
func (c *Client) GetFillOrderCalldata(order *OrderExtendedWithSignature, takerTraits *TakerTraits) ([]byte, error) {

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

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

switch function {
case "fillOrder":
fillOrderData, err = c.AggregationRouterV6.Pack(function, getOrderResponse.LimitOrderDataNormalized, rCompressed, vsCompressed, getOrderResponse.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, getOrderResponse.LimitOrderDataNormalized, rCompressed, vsCompressed, getOrderResponse.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
Loading