Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Setup refactored Aggregation SDK #44

Merged
merged 32 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
695e190
chore: namings / moving
EnoRage Mar 31, 2024
d02a06c
feat: add config
EnoRage Mar 31, 2024
210501e
chore: test fixes
EnoRage Mar 31, 2024
df071e5
test: fixes to permit logic
EnoRage Mar 31, 2024
8ed2321
test: permit signature
EnoRage Mar 31, 2024
6e3e9cf
chore: naming
EnoRage Mar 31, 2024
46a0ba7
test: add tokenPermit test
EnoRage Mar 31, 2024
226d999
feat: configuration logic
EnoRage Apr 1, 2024
3a23f07
chore: global cleanup of constants
EnoRage Apr 1, 2024
c8d2536
chore: lint
EnoRage Apr 1, 2024
39bb53a
chore: models cleanup
EnoRage Apr 2, 2024
275a610
feat: add tx build for swaps
EnoRage Apr 2, 2024
7bb29c4
chore: validation cleanup
EnoRage Apr 3, 2024
654f66d
feat: add gas logic suggestions
EnoRage Apr 3, 2024
0b6a467
clean: delete helpers and fix validation
EnoRage Apr 3, 2024
b52a751
chore: small changes of cfg
EnoRage Apr 4, 2024
55f321e
Example added for swap with already-approved token (#45)
Tanz0rz Apr 4, 2024
2c9e4be
feat: add legacy transaction builder
EnoRage Apr 4, 2024
6ceadcb
chore: example cleanup
EnoRage Apr 4, 2024
704c5de
feat: implement transaction building
EnoRage Apr 4, 2024
af9ad7a
feat: add transaction builder
EnoRage Apr 5, 2024
64353c0
lint: fixes
EnoRage Apr 5, 2024
72a2af7
fix: tests
EnoRage Apr 5, 2024
0a5924e
test: add tx builder test for build tx
EnoRage Apr 7, 2024
989bc3e
feat: add check for corrupted data during set
EnoRage Apr 7, 2024
5f133b4
test: more cases
EnoRage Apr 7, 2024
1abc0f4
chore: .gitignore
EnoRage Apr 8, 2024
6e94301
chore: tx builder refactor
EnoRage Apr 8, 2024
7a2b712
chore: refactor multicall
EnoRage Apr 8, 2024
a04bf1f
feat: use multicall for permits
EnoRage Apr 9, 2024
e93302f
test: fix
EnoRage Apr 9, 2024
147160f
chore: comment network tests
EnoRage Apr 9, 2024
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.idea/
.vscode/
/tmp
/bin

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride
87 changes: 67 additions & 20 deletions aggregation/aggregation.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,83 @@
package aggregation

import (
"math/big"
"net/url"

"github.com/1inch/1inch-sdk-go/internal/common"
"github.com/1inch/1inch-sdk-go/common"
"github.com/1inch/1inch-sdk-go/internal/http_executor"
"github.com/1inch/1inch-sdk-go/internal/web3_provider"
"github.com/1inch/1inch-sdk-go/internal/transaction-builder"
"github.com/1inch/1inch-sdk-go/internal/web3-provider"
)

type api struct {
httpExecutor common.HttpExecutor
type Configuration struct {
WalletConfiguration *WalletConfiguration
ChainId uint64

ApiKey string
ApiURL string

API api
}

type WalletConfiguration struct {
PrivateKey string
NodeURL string

Wallet common.Wallet
TxBuilder common.TransactionBuilderFactory
}

type Client struct {
api
Wallet common.Wallet
Wallet common.Wallet
TxBuilder common.TransactionBuilderFactory
}

// todo: not done
func DefaultClient() *Client {
// todo: move to input params, that will be validated before
u, _ := url.Parse("https://api.1inch.dev")
executor := http_executor.DefaultHttpClient(u, "")
api := api{
httpExecutor: &executor,
}
type api struct {
chainId uint64
httpExecutor common.HttpExecutor
}

w := web3_provider.DefaultWalletProvider("", "", big.NewInt(1))
func NewClient(cfg *Configuration) (*Client, error) {
c := Client{
api: api,
Wallet: w,
api: cfg.API,
}

if cfg.WalletConfiguration != nil {
c.Wallet = cfg.WalletConfiguration.Wallet
}

return &c, nil
}

func NewDefaultConfiguration(nodeUrl string, privateKey string, chainId uint64, apiUrl string, apiKey string) (*Configuration, error) {
executor, err := http_executor.DefaultHttpClient(apiUrl, apiKey)
if err != nil {
return nil, err
}

a := api{
chainId: chainId,
httpExecutor: executor,
}
return &c

walletCfg, err := NewDefaultWalletConfiguration(nodeUrl, privateKey, chainId)
if err != nil {
return nil, err
}
return &Configuration{
WalletConfiguration: walletCfg,
API: a,
}, nil
}

func NewDefaultWalletConfiguration(nodeUrl string, privateKey string, chainId uint64) (*WalletConfiguration, error) {
w, err := web3_provider.DefaultWalletProvider(privateKey, nodeUrl, chainId)
if err != nil {
return nil, err
}

f := transaction_builder.NewFactory(w)
return &WalletConfiguration{
Wallet: w,
TxBuilder: f,
}, nil
}
53 changes: 24 additions & 29 deletions aggregation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"fmt"

"github.com/1inch/1inch-sdk-go/internal/common"
"github.com/1inch/1inch-sdk-go/aggregation/models"
"github.com/1inch/1inch-sdk-go/common"
)

// GetApproveAllowance returns the allowance the 1inch router has to spend a token on behalf of a wallet
func (api *api) GetApproveAllowance(ctx context.Context, params ApproveAllowanceParams) (*AllowanceResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/allowance", params.ChainId)
func (api *api) GetApproveAllowance(ctx context.Context, params models.ApproveAllowanceParams) (*models.AllowanceResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/allowance", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -23,7 +24,7 @@ func (api *api) GetApproveAllowance(ctx context.Context, params ApproveAllowance
Body: nil,
}

var allowanceResponse AllowanceResponse
var allowanceResponse models.AllowanceResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &allowanceResponse)
if err != nil {
return nil, err
Expand All @@ -33,8 +34,8 @@ func (api *api) GetApproveAllowance(ctx context.Context, params ApproveAllowance
}

// GetApproveSpender returns the address of the 1inch router contract
func (api *api) GetApproveSpender(ctx context.Context, params ApproveSpenderParams) (*SpenderResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/spender", params.ChainId)
func (api *api) GetApproveSpender(ctx context.Context, params models.ApproveSpenderParams) (*models.SpenderResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/spender", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -48,7 +49,7 @@ func (api *api) GetApproveSpender(ctx context.Context, params ApproveSpenderPara
Body: nil,
}

var spender SpenderResponse
var spender models.SpenderResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &spender)
if err != nil {
return nil, err
Expand All @@ -58,8 +59,8 @@ func (api *api) GetApproveSpender(ctx context.Context, params ApproveSpenderPara
}

// GetApproveTransaction returns the transaction data for approving the 1inch router to spend a token on behalf of a wallet
func (api *api) GetApproveTransaction(ctx context.Context, params ApproveTransactionParams) (*ApproveCallDataResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/transaction", params.ChainId)
func (api *api) GetApproveTransaction(ctx context.Context, params models.ApproveTransactionParams) (*models.ApproveCallDataResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/approve/transaction", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -73,7 +74,7 @@ func (api *api) GetApproveTransaction(ctx context.Context, params ApproveTransac
Body: nil,
}

var approveCallData ApproveCallDataResponse
var approveCallData models.ApproveCallDataResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &approveCallData)
if err != nil {
return nil, err
Expand All @@ -82,8 +83,8 @@ func (api *api) GetApproveTransaction(ctx context.Context, params ApproveTransac
}

// GetLiquiditySources returns all liquidity sources tracked by the 1inch Aggregation Protocol for a given chain
func (api *api) GetLiquiditySources(ctx context.Context, params GetLiquiditySourcesParams) (*ProtocolsResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/liquidity-sources", params.ChainId)
func (api *api) GetLiquiditySources(ctx context.Context, params models.GetLiquiditySourcesParams) (*models.ProtocolsResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/liquidity-sources", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -97,7 +98,7 @@ func (api *api) GetLiquiditySources(ctx context.Context, params GetLiquiditySour
Body: nil,
}

var liquiditySources ProtocolsResponse
var liquiditySources models.ProtocolsResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &liquiditySources)
if err != nil {
return nil, err
Expand All @@ -107,8 +108,8 @@ func (api *api) GetLiquiditySources(ctx context.Context, params GetLiquiditySour
}

// GetQuote returns the quote for a potential swap through the Aggregation Protocol
func (api *api) GetQuote(ctx context.Context, params GetQuoteParams) (*QuoteResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/quote", params.ChainId)
func (api *api) GetQuote(ctx context.Context, params models.GetQuoteParams) (*models.QuoteResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/quote", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -121,7 +122,7 @@ func (api *api) GetQuote(ctx context.Context, params GetQuoteParams) (*QuoteResp
U: u,
}

var quote QuoteResponse
var quote models.QuoteResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &quote)
if err != nil {
return nil, err
Expand All @@ -131,28 +132,22 @@ func (api *api) GetQuote(ctx context.Context, params GetQuoteParams) (*QuoteResp
}

// GetSwap returns a swap quote with transaction data that can be used to execute a swap through the Aggregation Protocol
func (api *api) GetSwap(ctx context.Context, params GetSwapParams) (*SwapResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/swap", params.ChainId)
func (api *api) GetSwap(ctx context.Context, params models.AggregationControllerGetSwapParams) (*models.SwapResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/swap", api.chainId)

err := params.Validate()
if err != nil {
return nil, err
}

// Token info is used by certain parts of the SDK and more info by default is helpful to integrators
// Because we use generated structs with concrete types, extra data is forced on regardless of what the user passes in.
params.IncludeTokensInfo = true
params.IncludeGas = true
params.IncludeProtocols = true

payload := common.RequestPayload{
Method: "GET",
Params: params.AggregationControllerGetSwapParams,
Params: params,
U: u,
Body: nil,
}

var swapResponse SwapResponse
var swapResponse models.SwapResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &swapResponse)
if err != nil {
return nil, err
Expand All @@ -162,8 +157,8 @@ func (api *api) GetSwap(ctx context.Context, params GetSwapParams) (*SwapRespons
}

// GetTokens returns all tokens officially tracked by the 1inch Aggregation Protocol for a given chain
func (api *api) GetTokens(ctx context.Context, params GetTokensParams) (*TokensResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/tokens", params.ChainId)
func (api *api) GetTokens(ctx context.Context, params models.GetTokensParams) (*models.TokensResponse, error) {
u := fmt.Sprintf("/swap/v5.2/%d/tokens", api.chainId)

err := params.Validate()
if err != nil {
Expand All @@ -177,7 +172,7 @@ func (api *api) GetTokens(ctx context.Context, params GetTokensParams) (*TokensR
Body: nil,
}

var tokens TokensResponse
var tokens models.TokensResponse
err = api.httpExecutor.ExecuteRequest(ctx, payload, &tokens)
if err != nil {
return nil, err
Expand Down
19 changes: 10 additions & 9 deletions aggregation/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"reflect"
"testing"

"github.com/1inch/1inch-sdk-go/internal/common"
"github.com/1inch/1inch-sdk-go/internal/helpers/consts/chains"
"github.com/1inch/1inch-sdk-go/aggregation/models"
"github.com/1inch/1inch-sdk-go/common"
"github.com/1inch/1inch-sdk-go/constants"
)

type MockHttpExecutor struct {
Expand Down Expand Up @@ -36,8 +37,8 @@ func (m *MockHttpExecutor) ExecuteRequest(ctx context.Context, payload common.Re
func TestGetQuote(t *testing.T) {
ctx := context.Background()

mockedResp := QuoteResponse{
FromToken: &TokenInfo{
mockedResp := models.QuoteResponse{
FromToken: &models.TokenInfo{
Address: "0x6b175474e89094c44da98b954eedeac495271d0f",
Symbol: "DAI",
Name: "Dai Stablecoin",
Expand All @@ -50,7 +51,7 @@ func TestGetQuote(t *testing.T) {
},
Gas: 181416,
ToAmount: "289424403260095",
ToToken: &TokenInfo{
ToToken: &models.TokenInfo{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Symbol: "WETH",
Name: "Wrapped Ether",
Expand All @@ -61,7 +62,7 @@ func TestGetQuote(t *testing.T) {
"tokens",
},
},
Protocols: [][][]SelectedProtocol{
Protocols: [][][]models.SelectedProtocol{
{
{
{
Expand All @@ -80,9 +81,9 @@ func TestGetQuote(t *testing.T) {
}
api := api{httpExecutor: mockExecutor}

params := GetQuoteParams{
ChainId: chains.Ethereum,
AggregationControllerGetQuoteParams: AggregationControllerGetQuoteParams{
params := models.GetQuoteParams{
ChainId: constants.EthereumChainId,
AggregationControllerGetQuoteParams: models.AggregationControllerGetQuoteParams{
Src: "0x6b175474e89094c44da98b954eedeac495271d0f",
Dst: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Amount: "1000000000000000000",
Expand Down
Loading
Loading