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

Improved SDK struct clarity, improved error handling on examples, matched Orderbook client/configuration structure to Aggregation #66

Merged
merged 2 commits into from
May 16, 2024
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
20 changes: 14 additions & 6 deletions sdk-clients/aggregation/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package aggregation

import (
"github.com/1inch/1inch-sdk-go/common"
"github.com/1inch/1inch-sdk-go/internal/http-executor"
"github.com/1inch/1inch-sdk-go/internal/transaction-builder"
"github.com/1inch/1inch-sdk-go/internal/web3-provider"
http_executor "github.com/1inch/1inch-sdk-go/internal/http-executor"
transaction_builder "github.com/1inch/1inch-sdk-go/internal/transaction-builder"
web3_provider "github.com/1inch/1inch-sdk-go/internal/web3-provider"
)

type Configuration struct {
Expand All @@ -27,12 +27,20 @@ type ConfigurationWallet struct {
TxBuilder common.TransactionBuilderFactory
}

func NewConfiguration(nodeUrl string, privateKey string, chainId uint64, apiUrl string, apiKey string) (*Configuration, error) {
apiCfg, err := NewConfigurationAPI(chainId, apiUrl, apiKey)
type ConfigurationParams struct {
NodeUrl string
PrivateKey string
ChainId uint64
ApiUrl string
ApiKey string
}

func NewConfiguration(params ConfigurationParams) (*Configuration, error) {
apiCfg, err := NewConfigurationAPI(params.ChainId, params.ApiUrl, params.ApiKey)
if err != nil {
return nil, err
}
walletCfg, err := NewConfigurationWallet(nodeUrl, privateKey, chainId)
walletCfg, err := NewConfigurationWallet(params.NodeUrl, params.PrivateKey, params.ChainId)
if err != nil {
return nil, err
}
Expand Down
26 changes: 16 additions & 10 deletions sdk-clients/aggregation/examples/approve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/hex"
"log"
"math/big"
"os"

Expand All @@ -29,15 +30,21 @@ const (
PolygonWeth = "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619"
)

// nodeUrl, privateKey, constants.EthereumChainId, "https://api.1inch.dev", devPortalToken
func main() {
config, err := aggregation.NewConfiguration(nodeUrl, privateKey, constants.EthereumChainId, "https://api.1inch.dev", devPortalToken)
config, err := aggregation.NewConfiguration(aggregation.ConfigurationParams{
NodeUrl: nodeUrl,
PrivateKey: privateKey,
ChainId: constants.EthereumChainId,
ApiUrl: "https://api.1inch.dev",
ApiKey: devPortalToken,
})
if err != nil {
return
log.Fatalf("Failed to create configuration: %v\n", err)
}
client, err := aggregation.NewClient(config)
if err != nil {
panic(err)
return
log.Fatalf("Failed to create client: %v\n", err)
}
ctx := context.Background()

Expand All @@ -59,29 +66,28 @@ func main() {
Amount: amountToSwap.String(),
})
if err != nil {
panic(err)
return
log.Fatalf("Failed to get approve data: %v\n", err)
}
data, err := hex.DecodeString(approveData.Data[2:])
if err != nil {
return
log.Fatalf("Failed to decode approve data: %v\n", err)
}

to := common.HexToAddress(approveData.Data)

tx, err := client.TxBuilder.New().SetData(data).SetTo(&to).Build(ctx)
if err != nil {
return
log.Fatalf("Failed to build approve transaction: %v\n", err)
}

signedTx, err := client.Wallet.Sign(tx)
if err != nil {
return
log.Fatalf("Failed to sign approve transaction: %v\n", err)
}

err = client.Wallet.BroadcastTransaction(ctx, signedTx)
if err != nil {
return
log.Fatalf("Failed to broadcast approve transaction: %v\n", err)
}
}

Expand Down
7 changes: 4 additions & 3 deletions sdk-clients/aggregation/examples/no-wallet-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"log"
"os"

"github.com/1inch/1inch-sdk-go/constants"
Expand All @@ -27,11 +28,11 @@ const (
func main() {
config, err := aggregation.NewConfigurationAPI(constants.PolygonChainId, "https://api.1inch.dev", devPortalToken)
if err != nil {
return
log.Fatalf("Failed to create configuration: %v\n", err)
}
client, err := aggregation.NewClientOnlyAPI(config)
if err != nil {
return
log.Fatalf("Failed to create client: %v\n", err)
}
ctx := context.Background()

Expand All @@ -42,7 +43,7 @@ func main() {
})

if err != nil {
return
log.Fatalf("Failed to get quote: %v\n", err)
}

fmt.Printf("Quote Amount: %+v\n", quote.ToAmount)
Expand Down
21 changes: 14 additions & 7 deletions sdk-clients/aggregation/examples/quote/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"

"github.com/1inch/1inch-sdk-go/constants"
Expand All @@ -23,12 +24,20 @@ var (
)

func main() {
config, err := aggregation.NewConfiguration(nodeUrl, privateKey, constants.PolygonChainId, "https://api.1inch.dev", devPortalToken)
config, err := aggregation.NewConfiguration(aggregation.ConfigurationParams{
NodeUrl: nodeUrl,
PrivateKey: privateKey,
ChainId: constants.PolygonChainId,
ApiUrl: "https://api.1inch.dev",
ApiKey: devPortalToken,
})
if err != nil {
return
log.Fatalf("Failed to create configuration: %v\n", err)
}
client, err := aggregation.NewClient(config)

if err != nil {
log.Fatalf("Failed to create client: %v\n", err)
}
ctx := context.Background()

swapData, err := client.GetSwap(ctx, aggregation.GetSwapParams{
Expand All @@ -39,14 +48,12 @@ func main() {
Slippage: 1,
})
if err != nil {
fmt.Printf("Failed to get swap data: %v\n", err)
return
log.Fatalf("Failed to get swap data: %v\n", err)
}

output, err := json.MarshalIndent(swapData, "", " ")
if err != nil {
fmt.Printf("Failed to marshal swap data: %v\n", err)
return
log.Fatalf("Failed to marshal swap data: %v\n", err)
}
fmt.Printf("%s\n", string(output))
}
Loading
Loading