Skip to content

Commit

Permalink
Various improvements to usability and naming (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanz0rz authored Aug 8, 2024
1 parent eab4cd3 commit 79f55df
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
2 changes: 1 addition & 1 deletion sdk-clients/fusion/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions sdk-clients/fusion/examples/get_quote/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
const (
usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359"
wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270"
amount = 100000000
amount = "100000000"
chainId = 137
)

Expand All @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions sdk-clients/fusion/examples/place_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand All @@ -64,7 +63,7 @@ func main() {
WalletAddress: publicAddress,
FromTokenAddress: fromToken,
ToTokenAddress: toToken,
Amount: amountString,
Amount: amount,
Receiver: "0x0000000000000000000000000000000000000000",
Preset: fusion.Fast,
}
Expand Down
31 changes: 29 additions & 2 deletions sdk-clients/fusion/fusion_types_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}
29 changes: 14 additions & 15 deletions sdk-clients/fusion/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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: "",
Expand Down
10 changes: 5 additions & 5 deletions sdk-clients/fusion/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -52,7 +52,7 @@ func TestCreateFusionOrderData(t *testing.T) {
WalletAddress: publicAddress,
FromTokenAddress: wmatic,
ToTokenAddress: usdc,
Amount: amountString,
Amount: amount,
Receiver: "0x0000000000000000000000000000000000000000",
Preset: "fast",
},
Expand Down
3 changes: 2 additions & 1 deletion sdk-clients/fusion/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 79f55df

Please sign in to comment.