From 83f323421a54911ed4d05c903c3570ad432caff1 Mon Sep 17 00:00:00 2001 From: Tanner Moore Date: Mon, 5 Aug 2024 20:17:49 -0600 Subject: [PATCH] Various improvements to usability and naming --- README.md | 2 +- sdk-clients/fusion/api.go | 2 +- sdk-clients/fusion/examples/get_quote/main.go | 4 +-- .../fusion/examples/place_order/main.go | 13 ++++---- sdk-clients/fusion/fusion_types_extended.go | 31 +++++++++++++++++-- sdk-clients/fusion/order.go | 29 +++++++++-------- sdk-clients/fusion/order_test.go | 10 +++--- sdk-clients/fusion/validation.go | 3 +- 8 files changed, 60 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 3ffb92a..acf84c6 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Jump To: *Orderbook API* - [[Docs](https://portal.1inch.dev/documentation/apis/orderbook/introduction) | [SDK Example](https://github.com/1inch/1inch-sdk-go/blob/main/sdk-clients/orderbook/examples/create_order/main.go)] ### Infrastructure -*Balance API* - [[Docs](https://portal.1inch.dev/documentation/apis/balances/introduction) | [SDK Example](https://github.com/1inch/1inch-sdk-go/blob/main/sdk-clients/balances/examples/main.go)] +*Balance API* - [[Docs](https://portal.1inch.dev/documentation/apis/balance/introduction) | [SDK Example](https://github.com/1inch/1inch-sdk-go/blob/main/sdk-clients/balances/examples/main.go)] *Gas Price API* - [[Docs](https://portal.1inch.dev/documentation/apis/gas-price/introduction) | [SDK Example](https://github.com/1inch/1inch-sdk-go/blob/main/sdk-clients/gasprices/examples/main.go)] diff --git a/sdk-clients/fusion/api.go b/sdk-clients/fusion/api.go index bfdcfd4..112c071 100644 --- a/sdk-clients/fusion/api.go +++ b/sdk-clients/fusion/api.go @@ -46,7 +46,7 @@ func (api *api) GetSettlementContract(ctx context.Context) (*SettlementAddressOu return &response, nil } -func (api *api) GetQuote(ctx context.Context, params QuoterControllerGetQuoteParams) (*GetQuoteOutputFixed, error) { +func (api *api) GetQuote(ctx context.Context, params QuoterControllerGetQuoteParamsFixed) (*GetQuoteOutputFixed, error) { u := fmt.Sprintf("/fusion/quoter/v2.0/%d/quote/receive", api.chainId) err := params.Validate() diff --git a/sdk-clients/fusion/examples/get_quote/main.go b/sdk-clients/fusion/examples/get_quote/main.go index adbc1b3..957d230 100644 --- a/sdk-clients/fusion/examples/get_quote/main.go +++ b/sdk-clients/fusion/examples/get_quote/main.go @@ -19,7 +19,7 @@ var ( const ( usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" - amount = 100000000 + amount = "100000000" chainId = 137 ) @@ -39,7 +39,7 @@ func main() { } ctx := context.Background() - response, err := client.GetQuote(ctx, fusion.QuoterControllerGetQuoteParams{ + response, err := client.GetQuote(ctx, fusion.QuoterControllerGetQuoteParamsFixed{ FromTokenAddress: usdc, ToTokenAddress: wmatic, Amount: amount, diff --git a/sdk-clients/fusion/examples/place_order/main.go b/sdk-clients/fusion/examples/place_order/main.go index 56091ab..26416e6 100644 --- a/sdk-clients/fusion/examples/place_order/main.go +++ b/sdk-clients/fusion/examples/place_order/main.go @@ -17,11 +17,10 @@ var ( ) const ( - usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" - wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" - amount = 1500000 - amountString = "1500000" - chainId = 137 + usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" + wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" + amount = "1000000" + chainId = 137 ) func main() { @@ -43,7 +42,7 @@ func main() { fromToken := usdc toToken := wmatic - response, err := client.GetQuote(ctx, fusion.QuoterControllerGetQuoteParams{ + response, err := client.GetQuote(ctx, fusion.QuoterControllerGetQuoteParamsFixed{ FromTokenAddress: fromToken, ToTokenAddress: toToken, Amount: amount, @@ -64,7 +63,7 @@ func main() { WalletAddress: publicAddress, FromTokenAddress: fromToken, ToTokenAddress: toToken, - Amount: amountString, + Amount: amount, Receiver: "0x0000000000000000000000000000000000000000", Preset: fusion.Fast, } diff --git a/sdk-clients/fusion/fusion_types_extended.go b/sdk-clients/fusion/fusion_types_extended.go index ef618d5..5bcb962 100644 --- a/sdk-clients/fusion/fusion_types_extended.go +++ b/sdk-clients/fusion/fusion_types_extended.go @@ -58,8 +58,8 @@ type OrderParams struct { ToTokenAddress string `json:"toTokenAddress"` Amount string `json:"amount"` WalletAddress string `json:"walletAddress"` - Permit string `json:"permit,omitempty"` // without the first 20 bytes of token address - Receiver string `json:"receiver,omitempty"` + Permit string `json:"permit,omitempty"` // without the first 20 bytes of token address + Receiver string `json:"receiver,omitempty"` // Should be set to the full zero address if this order should be filled by anyone Preset GetQuoteOutputRecommendedPreset `json:"preset,omitempty"` Nonce *big.Int `json:"nonce,omitempty"` Fee TakingFeeInfo `json:"fee,omitempty"` @@ -205,3 +205,30 @@ type ExtraData struct { EnablePermit2 bool Source string } + +type QuoterControllerGetQuoteParamsFixed struct { + // FromTokenAddress Address of "FROM" token + FromTokenAddress string `url:"fromTokenAddress" json:"fromTokenAddress"` + + // ToTokenAddress Address of "TO" token + ToTokenAddress string `url:"toTokenAddress" json:"toTokenAddress"` + + // Amount to take from "FROM" token to get "TO" token + Amount string `url:"amount" json:"amount"` + + // WalletAddress An address of the wallet or contract who will create Fusion order + WalletAddress string `url:"walletAddress" json:"walletAddress"` + + // EnableEstimate if enabled then get estimation from 1inch swap builder and generates quoteId, by default is false + EnableEstimate bool `url:"enableEstimate" json:"enableEstimate"` + + // Fee in bps format, 1% is equal to 100bps + Fee float32 `url:"fee,omitempty" json:"fee,omitempty"` + + // IsPermit2 permit2 allowance transfer encoded call + IsPermit2 string `url:"isPermit2,omitempty" json:"isPermit2,omitempty"` + IsLedgerLive bool `url:"isLedgerLive" json:"isLedgerLive"` + + // Permit permit, user approval sign + Permit string `url:"permit,omitempty" json:"permit,omitempty"` +} diff --git a/sdk-clients/fusion/order.go b/sdk-clients/fusion/order.go index f29a5da..2671348 100644 --- a/sdk-clients/fusion/order.go +++ b/sdk-clients/fusion/order.go @@ -28,20 +28,6 @@ func CreateFusionOrderData(quote GetQuoteOutputFixed, orderParams OrderParams, w return nil, nil, fmt.Errorf("error creating auction details: %v", err) } - var nonce *big.Int - if isNonceRequired(orderParams.AllowPartialFills, orderParams.AllowMultipleFills) { - if orderParams.Nonce != nil { - nonce = orderParams.Nonce - } else { - nonce, err = random_number_generation.BigIntMaxFunc(uint40Max) - if err != nil { - return nil, nil, fmt.Errorf("error generating nonce: %v\n", err) - } - } - } else { - nonce = orderParams.Nonce - } - takerAsset := orderParams.ToTokenAddress if takerAsset == NativeToken { takerAssetWrapped, ok := chainToWrapper[NetworkEnum(chainId)] @@ -73,12 +59,25 @@ func CreateFusionOrderData(quote GetQuoteOutputFixed, orderParams OrderParams, w }) } + var nonce *big.Int + if isNonceRequired(orderParams.AllowPartialFills, orderParams.AllowMultipleFills) { + if orderParams.Nonce != nil { + nonce = orderParams.Nonce + } else { + nonce, err = random_number_generation.BigIntMaxFunc(uint40Max) + if err != nil { + return nil, nil, fmt.Errorf("error generating nonce: %v\n", err) + } + } + } else { + nonce = orderParams.Nonce + } + details := Details{ Auction: auctionDetails, Fees: fees, Whitelist: whitelistAddresses, } - extraParams := ExtraParams{ Nonce: nonce, Permit: "", diff --git a/sdk-clients/fusion/order_test.go b/sdk-clients/fusion/order_test.go index 2a084c0..920abee 100644 --- a/sdk-clients/fusion/order_test.go +++ b/sdk-clients/fusion/order_test.go @@ -22,10 +22,10 @@ var ( ) const ( - usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" - wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" - amountString = "1000000000000000000" - chainId = 137 + usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" + wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" + amount = "1000000000000000000" + chainId = 137 ) func TestCreateFusionOrderData(t *testing.T) { @@ -52,7 +52,7 @@ func TestCreateFusionOrderData(t *testing.T) { WalletAddress: publicAddress, FromTokenAddress: wmatic, ToTokenAddress: usdc, - Amount: amountString, + Amount: amount, Receiver: "0x0000000000000000000000000000000000000000", Preset: "fast", }, diff --git a/sdk-clients/fusion/validation.go b/sdk-clients/fusion/validation.go index 72c5236..6517838 100644 --- a/sdk-clients/fusion/validation.go +++ b/sdk-clients/fusion/validation.go @@ -14,10 +14,11 @@ func (params *OrderApiControllerGetActiveOrdersParams) Validate() error { return validate.ConsolidateValidationErorrs(validationErrors) } -func (params *QuoterControllerGetQuoteParams) Validate() error { +func (params *QuoterControllerGetQuoteParamsFixed) Validate() error { var validationErrors []error validationErrors = validate.Parameter(params.FromTokenAddress, "FromTokenAddress", validate.CheckEthereumAddressRequired, validationErrors) validationErrors = validate.Parameter(params.ToTokenAddress, "ToTokenAddress", validate.CheckEthereumAddressRequired, validationErrors) + validationErrors = validate.Parameter(params.Amount, "Amount", validate.CheckBigIntRequired, validationErrors) validationErrors = validate.Parameter(params.Permit, "Permit", validate.CheckPermitHash, validationErrors) return validate.ConsolidateValidationErorrs(validationErrors) }