Skip to content

Commit

Permalink
Updating Classic Swap to use the V6 router, add more examples, and im…
Browse files Browse the repository at this point in the history
…prove tests (#82)
  • Loading branch information
Tanz0rz authored Oct 24, 2024
1 parent 5b7b5c8 commit e3ad3a7
Show file tree
Hide file tree
Showing 22 changed files with 416 additions and 117 deletions.
146 changes: 82 additions & 64 deletions codegen/openapi/aggregation-openapi.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"openapi": "3.0.0",
"paths": {
"/v5.2/1/quote": {
"/v6.0/1/quote": {
"get": {
"operationId": "getQuote",
"summary": "Find the best quote to swap with 1inch Router",
Expand All @@ -12,8 +12,7 @@
"in": "query",
"example": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -22,8 +21,7 @@
"in": "query",
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -32,8 +30,7 @@
"in": "query",
"example": "10000000000000000",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand Down Expand Up @@ -152,6 +149,16 @@
"type": "string",
"x-go-type-skip-optional-pointer": true
}
},
{
"name": "excludedProtocols",
"required": false,
"in": "query",
"description": "excluded supported liquidity sources",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
}
}
],
"responses": {
Expand Down Expand Up @@ -181,7 +188,7 @@
]
}
},
"/v5.2/1/swap": {
"/v6.0/1/swap": {
"get": {
"operationId": "getSwap",
"summary": "Generate calldata to swap on 1inch Router",
Expand All @@ -192,8 +199,7 @@
"in": "query",
"example": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -202,8 +208,7 @@
"in": "query",
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -212,8 +217,7 @@
"in": "query",
"example": "10000000000000000",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -222,8 +226,16 @@
"in": "query",
"description": "The address that calls the 1inch contract",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
"name": "origin",
"required": true,
"in": "query",
"description": "An EOA address that initiate the transaction",
"schema": {
"type": "string"
}
},
{
Expand All @@ -235,8 +247,7 @@
"schema": {
"minimum": 0,
"maximum": 50,
"type": "number",
"x-go-type-skip-optional-pointer": true
"type": "number"
}
},
{
Expand Down Expand Up @@ -356,6 +367,16 @@
"x-go-type-skip-optional-pointer": true
}
},
{
"name": "excludedProtocols",
"required": false,
"in": "query",
"description": "excluded supported liquidity sources",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
}
},
{
"name": "permit",
"required": false,
Expand Down Expand Up @@ -404,6 +425,16 @@
"type": "boolean",
"x-go-type-skip-optional-pointer": true
}
},
{
"name": "usePermit2",
"required": false,
"in": "query",
"description": "Enable this flag in case you did an approval to permit2 smart contract",
"schema": {
"type": "boolean",
"x-go-type-skip-optional-pointer": true
}
}
],
"responses": {
Expand Down Expand Up @@ -433,7 +464,7 @@
]
}
},
"/v5.2/1/approve/spender": {
"/v6.0/1/approve/spender": {
"get": {
"operationId": "getSpender",
"summary": "Address of the 1inch Router that is trusted to spend funds for the swap",
Expand All @@ -455,7 +486,7 @@
]
}
},
"/v5.2/1/approve/transaction": {
"/v6.0/1/approve/transaction": {
"get": {
"operationId": "getApprove",
"summary": "Generate approve calldata to allow 1inch Router to perform a swap",
Expand All @@ -467,8 +498,7 @@
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
"description": "Token address you want to swap",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand Down Expand Up @@ -500,7 +530,7 @@
]
}
},
"/v5.2/1/approve/allowance": {
"/v6.0/1/approve/allowance": {
"get": {
"operationId": "getAllowance",
"summary": "Get the number of tokens that the 1inch Router is allowed to swap",
Expand All @@ -512,8 +542,7 @@
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
"description": "Token address you want to swap",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
},
{
Expand All @@ -522,8 +551,7 @@
"in": "query",
"description": "Wallet address for which you want to check",
"schema": {
"type": "string",
"x-go-type-skip-optional-pointer": true
"type": "string"
}
}
],
Expand All @@ -544,7 +572,7 @@
]
}
},
"/v5.2/1/liquidity-sources": {
"/v6.0/1/liquidity-sources": {
"get": {
"operationId": "getLiquiditySources",
"summary": "List of liquidity sources that are available for routing in the 1inch Aggregation Protocol",
Expand All @@ -566,7 +594,7 @@
]
}
},
"/v5.2/1/tokens": {
"/v6.0/1/tokens": {
"get": {
"operationId": "getTokens",
"summary": "List of tokens that are available for swap in the 1inch Aggregation protocol",
Expand All @@ -577,7 +605,17 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TokensResponse"
"type": "object",
"properties": {
"tokens": {
"type": "object",
"properties": {
"0x111111111117dc0aa78b770fa6a738034120c302": {
"$ref": "#/components/schemas/TokenInfo"
}
}
}
}
}
}
}
Expand All @@ -592,28 +630,13 @@
"info": {
"title": "1inch Swap API",
"description": "\n<h2>Ethereum Network</h2>\nUsing 1inch Swap API, you can find the best route to exchange assets and make the exchange.\n<br><br>\nStep by step:\n1. Lookup addresses of tokens you want to swap, for example ‘0xxx’ , ‘0xxxx’ for DAI -> 1INCH\n2. Check for allowance of 1inch router contract to spend source asset (/approve/allowance)\n3. If necessary, give approval for 1inch router to spend source token (/approve/transaction)\n4. Monitor the best exchange route using (/quote)\n5. When you ready use to perform swap (/swap)",
"version": "5.2",
"version": "6.0",
"contact": {}
},
"tags": [],
"servers": [],
"components": {
"schemas": {
"TokensResponse": {
"type": "object",
"properties": {
"tokens": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/TokenInfo"
},
"x-go-type-skip-optional-pointer": true
}
},
"required": [
"tokens"
]
},
"TokenInfo": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -678,15 +701,13 @@
"QuoteResponse": {
"type": "object",
"properties": {
"fromToken": {
"$ref": "#/components/schemas/TokenInfo",
"x-go-type-skip-optional-pointer": true
"srcToken": {
"$ref": "#/components/schemas/TokenInfo"
},
"toToken": {
"$ref": "#/components/schemas/TokenInfo",
"x-go-type-skip-optional-pointer": true
"dstToken": {
"$ref": "#/components/schemas/TokenInfo"
},
"toAmount": {
"dstAmount": {
"type": "string",
"description": "Expected amount of destination token",
"example": "62131879850006790961",
Expand Down Expand Up @@ -714,7 +735,7 @@
}
},
"required": [
"toAmount"
"dstAmount"
]
},
"HttpExceptionMeta": {
Expand Down Expand Up @@ -828,15 +849,13 @@
"SwapResponse": {
"type": "object",
"properties": {
"fromToken": {
"$ref": "#/components/schemas/TokenInfo",
"x-go-type-skip-optional-pointer": true
"srcToken": {
"$ref": "#/components/schemas/TokenInfo"
},
"toToken": {
"$ref": "#/components/schemas/TokenInfo",
"x-go-type-skip-optional-pointer": true
"dstToken": {
"$ref": "#/components/schemas/TokenInfo"
},
"toAmount": {
"dstAmount": {
"type": "string",
"description": "Expected amount of destination token",
"example": "62131879850006790961",
Expand All @@ -857,12 +876,11 @@
"x-go-type-skip-optional-pointer": true
},
"tx": {
"$ref": "#/components/schemas/TransactionData",
"x-go-type-skip-optional-pointer": true
"$ref": "#/components/schemas/TransactionData"
}
},
"required": [
"toAmount",
"dstAmount",
"tx"
]
},
Expand Down
2 changes: 2 additions & 0 deletions common/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"

"github.com/ethereum/go-ethereum"
gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
Expand All @@ -17,6 +18,7 @@ type Wallet interface {

GetGasTipCap(ctx context.Context) (*big.Int, error)
GetGasPrice(ctx context.Context) (*big.Int, error)
GetGasEstimate(ctx context.Context, msg ethereum.CallMsg) (uint64, error)

Sign(tx *types.Transaction) (*types.Transaction, error)
SignBytes(data []byte) ([]byte, error)
Expand Down
15 changes: 15 additions & 0 deletions internal/transaction-builder/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum"
gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

Expand Down Expand Up @@ -145,6 +146,20 @@ func (t *TransactionBuilder) BuildDynamicTx(ctx context.Context) (*types.Transac
t.gasFeeCap = gasPrice
}

if t.gas == nil {
gas, err := t.wallet.GetGasEstimate(ctx, ethereum.CallMsg{
From: t.wallet.Address(),
To: t.to,
Value: t.value,
GasPrice: t.gasFeeCap,
Data: t.data,
})
if err != nil {
return nil, err
}
t.gas = &gas
}

return types.NewTx(&types.DynamicFeeTx{
ChainID: big.NewInt(t.wallet.ChainId()),
Nonce: *t.nonce,
Expand Down
Loading

0 comments on commit e3ad3a7

Please sign in to comment.