Skip to content

Commit e3ad3a7

Browse files
authored
Updating Classic Swap to use the V6 router, add more examples, and improve tests (#82)
1 parent 5b7b5c8 commit e3ad3a7

File tree

22 files changed

+416
-117
lines changed

22 files changed

+416
-117
lines changed

codegen/openapi/aggregation-openapi.json

+82-64
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.0.0",
33
"paths": {
4-
"/v5.2/1/quote": {
4+
"/v6.0/1/quote": {
55
"get": {
66
"operationId": "getQuote",
77
"summary": "Find the best quote to swap with 1inch Router",
@@ -12,8 +12,7 @@
1212
"in": "query",
1313
"example": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
1414
"schema": {
15-
"type": "string",
16-
"x-go-type-skip-optional-pointer": true
15+
"type": "string"
1716
}
1817
},
1918
{
@@ -22,8 +21,7 @@
2221
"in": "query",
2322
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
2423
"schema": {
25-
"type": "string",
26-
"x-go-type-skip-optional-pointer": true
24+
"type": "string"
2725
}
2826
},
2927
{
@@ -32,8 +30,7 @@
3230
"in": "query",
3331
"example": "10000000000000000",
3432
"schema": {
35-
"type": "string",
36-
"x-go-type-skip-optional-pointer": true
33+
"type": "string"
3734
}
3835
},
3936
{
@@ -152,6 +149,16 @@
152149
"type": "string",
153150
"x-go-type-skip-optional-pointer": true
154151
}
152+
},
153+
{
154+
"name": "excludedProtocols",
155+
"required": false,
156+
"in": "query",
157+
"description": "excluded supported liquidity sources",
158+
"schema": {
159+
"type": "string",
160+
"x-go-type-skip-optional-pointer": true
161+
}
155162
}
156163
],
157164
"responses": {
@@ -181,7 +188,7 @@
181188
]
182189
}
183190
},
184-
"/v5.2/1/swap": {
191+
"/v6.0/1/swap": {
185192
"get": {
186193
"operationId": "getSwap",
187194
"summary": "Generate calldata to swap on 1inch Router",
@@ -192,8 +199,7 @@
192199
"in": "query",
193200
"example": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
194201
"schema": {
195-
"type": "string",
196-
"x-go-type-skip-optional-pointer": true
202+
"type": "string"
197203
}
198204
},
199205
{
@@ -202,8 +208,7 @@
202208
"in": "query",
203209
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
204210
"schema": {
205-
"type": "string",
206-
"x-go-type-skip-optional-pointer": true
211+
"type": "string"
207212
}
208213
},
209214
{
@@ -212,8 +217,7 @@
212217
"in": "query",
213218
"example": "10000000000000000",
214219
"schema": {
215-
"type": "string",
216-
"x-go-type-skip-optional-pointer": true
220+
"type": "string"
217221
}
218222
},
219223
{
@@ -222,8 +226,16 @@
222226
"in": "query",
223227
"description": "The address that calls the 1inch contract",
224228
"schema": {
225-
"type": "string",
226-
"x-go-type-skip-optional-pointer": true
229+
"type": "string"
230+
}
231+
},
232+
{
233+
"name": "origin",
234+
"required": true,
235+
"in": "query",
236+
"description": "An EOA address that initiate the transaction",
237+
"schema": {
238+
"type": "string"
227239
}
228240
},
229241
{
@@ -235,8 +247,7 @@
235247
"schema": {
236248
"minimum": 0,
237249
"maximum": 50,
238-
"type": "number",
239-
"x-go-type-skip-optional-pointer": true
250+
"type": "number"
240251
}
241252
},
242253
{
@@ -356,6 +367,16 @@
356367
"x-go-type-skip-optional-pointer": true
357368
}
358369
},
370+
{
371+
"name": "excludedProtocols",
372+
"required": false,
373+
"in": "query",
374+
"description": "excluded supported liquidity sources",
375+
"schema": {
376+
"type": "string",
377+
"x-go-type-skip-optional-pointer": true
378+
}
379+
},
359380
{
360381
"name": "permit",
361382
"required": false,
@@ -404,6 +425,16 @@
404425
"type": "boolean",
405426
"x-go-type-skip-optional-pointer": true
406427
}
428+
},
429+
{
430+
"name": "usePermit2",
431+
"required": false,
432+
"in": "query",
433+
"description": "Enable this flag in case you did an approval to permit2 smart contract",
434+
"schema": {
435+
"type": "boolean",
436+
"x-go-type-skip-optional-pointer": true
437+
}
407438
}
408439
],
409440
"responses": {
@@ -433,7 +464,7 @@
433464
]
434465
}
435466
},
436-
"/v5.2/1/approve/spender": {
467+
"/v6.0/1/approve/spender": {
437468
"get": {
438469
"operationId": "getSpender",
439470
"summary": "Address of the 1inch Router that is trusted to spend funds for the swap",
@@ -455,7 +486,7 @@
455486
]
456487
}
457488
},
458-
"/v5.2/1/approve/transaction": {
489+
"/v6.0/1/approve/transaction": {
459490
"get": {
460491
"operationId": "getApprove",
461492
"summary": "Generate approve calldata to allow 1inch Router to perform a swap",
@@ -467,8 +498,7 @@
467498
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
468499
"description": "Token address you want to swap",
469500
"schema": {
470-
"type": "string",
471-
"x-go-type-skip-optional-pointer": true
501+
"type": "string"
472502
}
473503
},
474504
{
@@ -500,7 +530,7 @@
500530
]
501531
}
502532
},
503-
"/v5.2/1/approve/allowance": {
533+
"/v6.0/1/approve/allowance": {
504534
"get": {
505535
"operationId": "getAllowance",
506536
"summary": "Get the number of tokens that the 1inch Router is allowed to swap",
@@ -512,8 +542,7 @@
512542
"example": "0x111111111117dc0aa78b770fa6a738034120c302",
513543
"description": "Token address you want to swap",
514544
"schema": {
515-
"type": "string",
516-
"x-go-type-skip-optional-pointer": true
545+
"type": "string"
517546
}
518547
},
519548
{
@@ -522,8 +551,7 @@
522551
"in": "query",
523552
"description": "Wallet address for which you want to check",
524553
"schema": {
525-
"type": "string",
526-
"x-go-type-skip-optional-pointer": true
554+
"type": "string"
527555
}
528556
}
529557
],
@@ -544,7 +572,7 @@
544572
]
545573
}
546574
},
547-
"/v5.2/1/liquidity-sources": {
575+
"/v6.0/1/liquidity-sources": {
548576
"get": {
549577
"operationId": "getLiquiditySources",
550578
"summary": "List of liquidity sources that are available for routing in the 1inch Aggregation Protocol",
@@ -566,7 +594,7 @@
566594
]
567595
}
568596
},
569-
"/v5.2/1/tokens": {
597+
"/v6.0/1/tokens": {
570598
"get": {
571599
"operationId": "getTokens",
572600
"summary": "List of tokens that are available for swap in the 1inch Aggregation protocol",
@@ -577,7 +605,17 @@
577605
"content": {
578606
"application/json": {
579607
"schema": {
580-
"$ref": "#/components/schemas/TokensResponse"
608+
"type": "object",
609+
"properties": {
610+
"tokens": {
611+
"type": "object",
612+
"properties": {
613+
"0x111111111117dc0aa78b770fa6a738034120c302": {
614+
"$ref": "#/components/schemas/TokenInfo"
615+
}
616+
}
617+
}
618+
}
581619
}
582620
}
583621
}
@@ -592,28 +630,13 @@
592630
"info": {
593631
"title": "1inch Swap API",
594632
"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)",
595-
"version": "5.2",
633+
"version": "6.0",
596634
"contact": {}
597635
},
598636
"tags": [],
599637
"servers": [],
600638
"components": {
601639
"schemas": {
602-
"TokensResponse": {
603-
"type": "object",
604-
"properties": {
605-
"tokens": {
606-
"type": "object",
607-
"additionalProperties": {
608-
"$ref": "#/components/schemas/TokenInfo"
609-
},
610-
"x-go-type-skip-optional-pointer": true
611-
}
612-
},
613-
"required": [
614-
"tokens"
615-
]
616-
},
617640
"TokenInfo": {
618641
"type": "object",
619642
"properties": {
@@ -678,15 +701,13 @@
678701
"QuoteResponse": {
679702
"type": "object",
680703
"properties": {
681-
"fromToken": {
682-
"$ref": "#/components/schemas/TokenInfo",
683-
"x-go-type-skip-optional-pointer": true
704+
"srcToken": {
705+
"$ref": "#/components/schemas/TokenInfo"
684706
},
685-
"toToken": {
686-
"$ref": "#/components/schemas/TokenInfo",
687-
"x-go-type-skip-optional-pointer": true
707+
"dstToken": {
708+
"$ref": "#/components/schemas/TokenInfo"
688709
},
689-
"toAmount": {
710+
"dstAmount": {
690711
"type": "string",
691712
"description": "Expected amount of destination token",
692713
"example": "62131879850006790961",
@@ -714,7 +735,7 @@
714735
}
715736
},
716737
"required": [
717-
"toAmount"
738+
"dstAmount"
718739
]
719740
},
720741
"HttpExceptionMeta": {
@@ -828,15 +849,13 @@
828849
"SwapResponse": {
829850
"type": "object",
830851
"properties": {
831-
"fromToken": {
832-
"$ref": "#/components/schemas/TokenInfo",
833-
"x-go-type-skip-optional-pointer": true
852+
"srcToken": {
853+
"$ref": "#/components/schemas/TokenInfo"
834854
},
835-
"toToken": {
836-
"$ref": "#/components/schemas/TokenInfo",
837-
"x-go-type-skip-optional-pointer": true
855+
"dstToken": {
856+
"$ref": "#/components/schemas/TokenInfo"
838857
},
839-
"toAmount": {
858+
"dstAmount": {
840859
"type": "string",
841860
"description": "Expected amount of destination token",
842861
"example": "62131879850006790961",
@@ -857,12 +876,11 @@
857876
"x-go-type-skip-optional-pointer": true
858877
},
859878
"tx": {
860-
"$ref": "#/components/schemas/TransactionData",
861-
"x-go-type-skip-optional-pointer": true
879+
"$ref": "#/components/schemas/TransactionData"
862880
}
863881
},
864882
"required": [
865-
"toAmount",
883+
"dstAmount",
866884
"tx"
867885
]
868886
},

common/wallet.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"math/big"
66

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

1819
GetGasTipCap(ctx context.Context) (*big.Int, error)
1920
GetGasPrice(ctx context.Context) (*big.Int, error)
21+
GetGasEstimate(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
2022

2123
Sign(tx *types.Transaction) (*types.Transaction, error)
2224
SignBytes(data []byte) ([]byte, error)

internal/transaction-builder/transaction_builder.go

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math/big"
77

8+
"github.com/ethereum/go-ethereum"
89
gethCommon "github.com/ethereum/go-ethereum/common"
910
"github.com/ethereum/go-ethereum/core/types"
1011

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

149+
if t.gas == nil {
150+
gas, err := t.wallet.GetGasEstimate(ctx, ethereum.CallMsg{
151+
From: t.wallet.Address(),
152+
To: t.to,
153+
Value: t.value,
154+
GasPrice: t.gasFeeCap,
155+
Data: t.data,
156+
})
157+
if err != nil {
158+
return nil, err
159+
}
160+
t.gas = &gas
161+
}
162+
148163
return types.NewTx(&types.DynamicFeeTx{
149164
ChainID: big.NewInt(t.wallet.ChainId()),
150165
Nonce: *t.nonce,

0 commit comments

Comments
 (0)