Skip to content

Commit 56af8b4

Browse files
authored
Refactoring the SDK structure to be more discoverable (#29)
1 parent c47961f commit 56af8b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+432
-452
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Each SDK will have its own changelog. You can navigate to the SDK of your choice from the root of this repository or click directly to them here
2+
3+
[Developer Portal SDK - Golang](https://github.com/1inch/1inch-sdk/blob/main/golang/CHANGELOG.md)
4+

LICENSE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TBD

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Developer Portal SDKs
22

3-
This repo will host a collection of open source SDKs used to access the Developer Portal APIs.
3+
This repository will host a collection of open source SDKs used to access the Developer Portal APIs.
44

55
## Overview
66

golang/CHANGELOG.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Release(March TBD, 2024)
2+
Tag: [v0.0.3-developer-preview](https://github.com/1inch/1inch-sdk/releases/tag/v0.0.3-developer-preview)
3+
4+
### New Features and Enhancements:
5+
- All non-global query configurations have been moved to the request-level params ([PR](https://github.com/1inch/1inch-sdk/pull/6))
6+
- RPC providers for all chains will now be defined/set at SDK startup
7+
- Query parameters now use concrete types instead of pointers ([PR](https://github.com/1inch/1inch-sdk/pull/16))
8+
- Limit orders created within the SDK now support auto-expiration ([PR](https://github.com/1inch/1inch-sdk/pull/23))
9+
- Permit1 properly supported for limit orders when possible (fallback to Approval if Permit1 does not work) ([commit](https://github.com/1inch/1inch-sdk/commit/f2e79e5f0e81503bfeeff076e41455e86e5a5120))
10+
- When creating a limit order, integrators can error out when an approval is needed. This is useful for integrators who want all onchain actions to be performed manually by their users ([PR](https://github.com/1inch/1inch-sdk/pull/26))
11+
12+
### Optimizations and Bug Fixes:
13+
- Tenderly forks are cleaned up automatically at the beginning of each test run ([PR](https://github.com/1inch/1inch-sdk/pull/6))
14+
- Validation pattern for swagger-generated input params is now fully handled on all endpoints ([PR](https://github.com/1inch/1inch-sdk/pull/8))
15+
- Project-wide validation scripts added to verify validation logic standards ([PR](https://github.com/1inch/1inch-sdk/pull/11))
16+
17+
# Release(January 23, 2024)
18+
Tag: [v0.0.2-developer-preview](https://github.com/1inch/1inch-sdk/releases/tag/v0.0.2-developer-preview)
19+
20+
### New Features and Enhancements:
21+
- **Added Tenderly support for e2e swap tests**
22+
- e2e tests will now create forks, apply state overrides, and run simulations when a Tenderly API key is provided.
23+
- **Added approval type selection**
24+
- Users can choose between `Approve` and `Permit1` (`Permit2` currently unsupported)
25+
- **Implemented nonce cache to address RPC lag**
26+
- Once a wallet has posted a transaction, the nonce of that transaction is tracked and incremented internally by the SDK.
27+
28+
### Optimizations and Bug Fixes:
29+
- Updated orderbook to use string inputs instead of integers to support all of uint256.
30+
- Increased gas limit and reduced permit duration to improve transactions success and debugging.
31+
- Moved Actions into a service within the main client to consolidate SDK structure.
32+
- Simplified tests and refactored onchain actions to have more uniformity across the library.
33+
34+
# Release(January 15, 2024)
35+
Tag: [v0.0.1-developer-preview](https://github.com/1inch/1inch-sdk/releases/tag/v0.0.1-developer-preview)
36+
37+
38+
### New Features and Enhancements:
39+
40+
### Limit Order support
41+
- Enables posting orders to the 1inch Limit Order Protocol
42+
- Enables reading orders from the 1inch Limit Order Protocol
43+
- Most endpoints from the Limit Order API supported
44+
- `has-active-orders-with-permit` REST endpoint still untested
45+
46+
### Aggregator Protocol support
47+
- All REST endpoints supported
48+
- Get quotes and swap data from the Aggregator Protocol
49+
50+
### Onchain execution support
51+
- Execute swaps onchain from within the SDK
52+
53+

golang/DEVELOPMENT.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This SDK will be open for contributions from the community. Contribution guidelines will be added soon!
2+
3+
4+
### Versioning
5+
6+
This library is currently in the developer preview phase (versions 0.x.x). There will be significant changes to the design of this library leading up to a 1.0.0 release. You can expect the API calls, library structure, etc. to break between each release. Once the library version reaches 1.0.0 and beyond, it will follow traditional semver conventions.
7+
8+
### Project structure
9+
10+
This SDK is powered by a [client struct](https://github.com/1inch/1inch-sdk/blob/main/golang/client/client.go) that contains instances of all Services used to talk to the 1inch APIs
11+
12+
Each Service maps 1-to-1 with the underlying Dev Portal REST API. See [SwapService](https://github.com/1inch/1inch-sdk/blob/main/golang/client/swap.go) as an example. Under each function, you will find the matching REST API path)
13+
14+
Each Service uses various types and functions to do its job that are kept separate from the main service file. These can be found in the accompanying folder within the client directory (see the [swap](https://github.com/1inch/1inch-sdk/tree/main/golang/client/swap) package)
15+
16+
### Type generation
17+
18+
Type generation is done using the `generate_types.sh` script. To add a new swagger file or update an existing one, place the swagger file in `swagger-static` and run the script. It will generate the types file and place it in the appropriately-named sub-folder inside the `client` directory
19+
20+
### Swagger file formatting
21+
For consistency, Swagger files should be formatted with `prettier`
22+
23+
This can be installed globally using npm:
24+
25+
`npm install -g prettier`
26+
27+
If using GoLand, you can set up this action to run automatically using File Watchers:
28+
29+
1. Go to Settings or Preferences > Tools > File Watchers.
30+
2. Click the + button to add a new watcher.
31+
3. For `File type`, choose JSON.
32+
4. For `Scope`, choose Project Files.
33+
5. For `Program`, provide the path to the `prettier`. This can be gotten by running `which prettier`.
34+
6. For `Arguments`, use `--write $FilePath$`.
35+
7. For `Output paths to refresh`, use `$FilePath$`.
36+
8. Ensure the Auto-save edited files to trigger the watcher option is checked

golang/README.md

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
11
# Dev Portal Go SDK
22

3-
First and foremost, it is important to note that when using the SDK libraries for the 1inch aggregator or limit order protocols, you will be creating *real* transaction data that can and will be executed onchain. Always be deliberate when calling [CreateOrder](https://github.com/1inch/1inch-sdk/blob/main/golang/client/orderbook.go), [SwapTokens](https://github.com/1inch/1inch-sdk/blob/main/golang/client/swap_actions.go), or [GetSwapData](https://github.com/1inch/1inch-sdk/blob/main/golang/client/swap.go). When filling out the parameters for these functions, make sure you understand concepts like [slippage](https://medium.com/onomy-protocol/what-is-slippage-in-defi-62a0d068feb3) and [MEV](https://chain.link/education-hub/maximal-extractable-value-mev), as well as the difference between [USDC](https://etherscan.io/token/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48) having 6 digits of precision on Ethereum and [DAI](https://etherscan.io/token/0x6b175474e89094c44da98b954eedeac495271d0f) having 18!
3+
The SDK requires a minimum version of Go `1.21`.
44

5-
This SDK is young and there will be many use cases that have not been handled yet, so please try it and give us feedback!
5+
Check out the [release notes](https://github.com/1inch/1inch-sdk/blob/main/golang/CHANGELOG.md) for information about the latest bug fixes, updates, and features added to the SDK.
66

7-
## Overview
7+
This is a Go SDK to simplify interactions with the 1inch Dev Portal APIs. When complete, it will support all endpoints tracked by our official docs [here](https://portal.1inch.dev/documentation/authentication). See the [Current Functionality](#current-functionality) section for an up-to-date view of the SDK functionality.
88

9-
This is a Go SDK to simplify interactions with the 1inch Dev Portal APIs. When complete, it will support all endpoints tracked by our official docs [here](https://portal.1inch.dev/documentation/authentication). See the `Current Functionality` section for an up-to-date view of the SDK functionality.
9+
Beyond mirroring the Developer Portal APIs, this SDK also supports token approvals, permit signature generation, and the execution of 1inch swaps onchain for EOA wallets.
1010

11-
Beyond mirroring the Developer Portal APIs, this SDK also supports token approvals, permit signature generation, and the execution of 1inch swaps onchain for EOA wallets.
11+
Jump To:
12+
* [Supported APIs](#supported-apis)
13+
* [Getting Started](#getting-started)
14+
* [Getting Help](#getting-help)
15+
* [Development](#development)
1216

13-
## Current Functionality
14-
15-
**Supported APIs**
17+
## Supported APIs
1618

1719
*Swap API*
18-
- All endpoints supported
19-
- Ethereum, Polygon, and Arbitrum tested (but should support all 1inch-supported chains)
20-
- Swaps can be executed onchain from within the SDK using `Permit1` when supported and `Approve` in all other cases
20+
- [Developer Portal Docs](https://portal.1inch.dev/documentation/swap)
21+
- [SDK Example](https://github.com/1inch/1inch-sdk/blob/main/golang/client/examples/swap/get_swap/main.go)
2122

2223
*Orderbook API*
23-
- Most endpoints supported
24-
- Posting orders to Ethereum and Polygon is working. Other chains likely will not work at the moment
24+
- [Developer Portal Docs](https://portal.1inch.dev/documentation/orderbook)
25+
- [SDK Example](https://github.com/1inch/1inch-sdk/blob/main/golang/client/examples/orderbook/get_orders/main.go)
2526

26-
## Versioning
27+
## Getting started
2728

28-
This library is currently in the developer preview phase (versions 0.x.x). There will be significant changes to the design of this library leading up to a 1.0.0 release. You can expect the API calls, library structure, etc. to break between each release. Once the library version reaches 1.0.0 and beyond, it will follow traditional semver conventions.
29+
To get started working with the SDK, set up your project for Go modules and retrieve the SDK dependencies with `go get`. This example shows how you can use the SDK to make an API request using the SDK's Swap API service:
2930

30-
## Using the SDK in your project
31+
###### Initialize Project
32+
```
33+
mkdir ~/hello1inch
34+
cd ~/hello1inch
35+
go mod init hello1inch
36+
```
3137

32-
The SDK can be used by first creating a config object, calling the constructor, then accessing the service for the API of interest. For now, the web3 provider and chain are set at the client level, but this will be moved to the request parameters in the future.
38+
###### Add SDK Dependencies
39+
```
40+
go get github.com/1inch/1inch-sdk/golang
41+
```
3342

34-
**Note**: The 1inch Dev Portal Token can be generated at https://portal.1inch.dev
35-
Additionally,
36-
documentation for all API calls can be found at https://portal.1inch.dev/documentation
43+
###### Write Code
44+
In your preferred editor add the following content to `main.go`
3745

38-
Here is a simple program using the SDK that will generate swap data using the 1inch Aggregator:
46+
**Note**: The 1inch Dev Portal Token can be generated at https://portal.1inch.dev
3947

4048
```go
4149
package main
4250

4351
import (
4452
"context"
53+
"encoding/json"
4554
"fmt"
4655
"log"
4756
"os"
4857

4958
"github.com/1inch/1inch-sdk/golang/client"
50-
"github.com/1inch/1inch-sdk/golang/client/swap"
51-
"github.com/1inch/1inch-sdk/golang/helpers"
59+
"github.com/1inch/1inch-sdk/golang/client/models"
5260
"github.com/1inch/1inch-sdk/golang/helpers/consts/amounts"
5361
"github.com/1inch/1inch-sdk/golang/helpers/consts/chains"
5462
"github.com/1inch/1inch-sdk/golang/helpers/consts/tokens"
63+
"github.com/1inch/1inch-sdk/golang/helpers/consts/web3providers"
5564
)
5665

5766
func main() {
@@ -62,7 +71,7 @@ func main() {
6271
Web3HttpProviders: []client.Web3ProviderConfig{
6372
{
6473
ChainId: chains.Polygon,
65-
Url: os.Getenv("WEB_3_HTTP_PROVIDER_URL_WITH_KEY_POLYGON"),
74+
Url: web3providers.Polygon,
6675
},
6776
},
6877
}
@@ -74,12 +83,10 @@ func main() {
7483
}
7584

7685
// Build the config for the swap request
77-
swapParams := swap.GetSwapDataParams{
78-
RequestParams: swap.RequestParams{
79-
ChainId: chains.Polygon,
80-
SkipWarnings: false,
81-
},
82-
AggregationControllerGetSwapParams: swap.AggregationControllerGetSwapParams{
86+
swapParams := models.GetSwapParams{
87+
ChainId: chains.Polygon,
88+
SkipWarnings: false,
89+
AggregationControllerGetSwapParams: models.AggregationControllerGetSwapParams{
8390
Src: tokens.PolygonFrax,
8491
Dst: tokens.PolygonWeth,
8592
From: os.Getenv("WALLET_ADDRESS"),
@@ -89,54 +96,34 @@ func main() {
8996
},
9097
}
9198

92-
swapData, _, err := c.Swap.GetSwapData(context.Background(), swapParams)
99+
swapData, _, err := c.SwapApi.GetSwap(context.Background(), swapParams)
93100
if err != nil {
94101
log.Fatalf("Failed to swap tokens: %v", err)
95102
}
96103

97-
fmt.Printf("\nContract to send transaction to: %v\n", swapData.Tx.To)
98-
fmt.Printf("Transaction data: %v\n", swapData.Tx.Data)
99-
}
104+
swapDataRawIndented, err := json.MarshalIndent(swapData, "", " ")
105+
if err != nil {
106+
log.Fatalf("Failed to marshal swap data: %v", err)
107+
}
100108

109+
fmt.Printf("%s\n", string(swapDataRawIndented))
110+
}
101111
```
102112

103-
More example programs using the SDK can be found in the [examples directory](https://github.com/1inch/1inch-sdk/blob/main/golang/examples)
104-
105-
## Tips
106-
- It is recommended to use private/personal RPC endpoints when using this SDK. Public RPCs tend to have either slow response times, strict rate limits, or both!
107-
108-
## Project structure
113+
###### Compile and Execute
114+
```sh
115+
go run .
116+
```
109117

110-
This SDK is powered by a [client struct](https://github.com/1inch/1inch-sdk/blob/main/golang/client/client.go) that contains instances of all Services used to talk to the 1inch APIs
118+
Documentation for all API calls can be found at https://portal.1inch.dev/documentation
111119

112-
Each Service is simply a struct that contains all endpoints from a given 1inch API (see [SwapService](https://github.com/1inch/1inch-sdk/blob/main/golang/client/swap.go))
120+
More example programs using the SDK can be found in the [examples directory](https://github.com/1inch/1inch-sdk/blob/main/golang/client/examples)
113121

114-
Each Service uses various types and functions to do its job that are kept separate from the main service file. These can be found in the accompanying folder within the client directory (see the [swap](https://github.com/1inch/1inch-sdk/tree/main/golang/client/swap) package)
122+
## Getting Help
115123

116-
## Issues/Suggestions
124+
If you have questions, want to discuss the tool, or have found a bug, please open an [issue](https://github.com/1inch/1inch-sdk/issues) here on GitHub
117125

118-
For any problems you have with the SDK or suggestions for improvements, please create an [issue](https://github.com/1inch/1inch-sdk/issues) here on GitHub
119126

120127
## Development
121128

122-
### Type generation
123-
124-
Type generation is done using the `generate_types.sh` script. To add a new swagger file or update an existing one, place the swagger file in `swagger-static` and run the script. It will generate the types file and place it in the appropriately-named sub-folder inside the `client` directory
125-
126-
### Swagger file formatting
127-
For consistency, Swagger files should be formatted with `prettier`
128-
129-
This can be installed globally using npm:
130-
131-
`npm install -g prettier`
132-
133-
If using GoLand, you can set up this action to run automatically using File Watchers:
134-
135-
1. Go to Settings or Preferences > Tools > File Watchers.
136-
2. Click the + button to add a new watcher.
137-
3. For `File type`, choose JSON.
138-
4. For `Scope`, choose Project Files.
139-
5. For `Program`, provide the path to the `prettier`. This can be gotten by running `which prettier`.
140-
6. For `Arguments`, use `--write $FilePath$`.
141-
7. For `Output paths to refresh`, use `$FilePath$`.
142-
8. Ensure the Auto-save edited files to trigger the watcher option is checked
129+
Please see our [SDK Developer Guide](https://github.com/1inch/1inch-sdk/blob/main/golang/DEVELOPMENT.md) if you would like to contribute

golang/client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ type Client struct {
6868
// A struct that will contain a reference to this client. Used to separate each API into a unique namespace to aid in method discovery
6969
common service
7070
// Isolated namespaces for each API
71-
Actions *ActionService
72-
Swap *SwapService
73-
Orderbook *OrderbookService
71+
Actions *ActionService
72+
SwapApi *SwapService
73+
OrderbookApi *OrderbookService
7474
}
7575

7676
func (c *Client) GetEthClient(chainId int) (*ethclient.Client, error) {
@@ -113,8 +113,8 @@ func NewClient(config Config) (*Client, error) {
113113
c.common.client = c
114114

115115
c.Actions = (*ActionService)(&c.common)
116-
c.Swap = (*SwapService)(&c.common)
117-
c.Orderbook = (*OrderbookService)(&c.common)
116+
c.SwapApi = (*SwapService)(&c.common)
117+
c.OrderbookApi = (*OrderbookService)(&c.common)
118118

119119
return c, nil
120120
}

golang/examples/orderbook/create_order/main.go renamed to golang/client/examples/orderbook/create_order/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77

88
"github.com/1inch/1inch-sdk/golang/client"
9-
"github.com/1inch/1inch-sdk/golang/client/orderbook"
9+
"github.com/1inch/1inch-sdk/golang/client/models"
1010
"github.com/1inch/1inch-sdk/golang/helpers"
1111
"github.com/1inch/1inch-sdk/golang/helpers/consts/addresses"
1212
"github.com/1inch/1inch-sdk/golang/helpers/consts/chains"
@@ -33,7 +33,7 @@ func main() {
3333
}
3434

3535
// Build the config for the orders request
36-
createOrderParams := orderbook.CreateOrderParams{
36+
createOrderParams := models.CreateOrderParams{
3737
ChainId: chains.Polygon,
3838
PrivateKey: os.Getenv("WALLET_KEY"),
3939
Maker: os.Getenv("WALLET_ADDRESS"),
@@ -45,7 +45,7 @@ func main() {
4545
}
4646

4747
// Execute orders request
48-
createOrderResponse, _, err := c.Orderbook.CreateOrder(context.Background(), createOrderParams)
48+
createOrderResponse, _, err := c.OrderbookApi.CreateOrder(context.Background(), createOrderParams)
4949
if err != nil {
5050
log.Fatalf("Failed to create order: %v", err)
5151
}

golang/examples/orderbook/get_orders/main.go renamed to golang/client/examples/orderbook/get_orders/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99

1010
"github.com/1inch/1inch-sdk/golang/client"
11-
"github.com/1inch/1inch-sdk/golang/client/orderbook"
11+
"github.com/1inch/1inch-sdk/golang/client/models"
1212
"github.com/1inch/1inch-sdk/golang/helpers/consts/chains"
1313
)
1414

@@ -32,25 +32,25 @@ func main() {
3232
}
3333

3434
// Build the config for the orders request
35-
limitOrdersParams := orderbook.GetAllOrdersParams{
35+
limitOrdersParams := models.GetAllOrdersParams{
3636
ChainId: 137,
37-
LimitOrderV3SubscribedApiControllerGetAllLimitOrdersParams: orderbook.LimitOrderV3SubscribedApiControllerGetAllLimitOrdersParams{
37+
LimitOrderV3SubscribedApiControllerGetAllLimitOrdersParams: models.LimitOrderV3SubscribedApiControllerGetAllLimitOrdersParams{
3838
Page: 1,
3939
Limit: 2,
4040
SortBy: "createDateTime",
4141
},
4242
}
4343

4444
// Execute orders request
45-
allOrdersResponse, _, err := c.Orderbook.GetAllOrders(context.Background(), limitOrdersParams)
45+
allOrdersResponse, _, err := c.OrderbookApi.GetAllOrders(context.Background(), limitOrdersParams)
4646
if err != nil {
4747
log.Fatalf("Failed to get quote: %v", err)
4848
}
4949

5050
prettyPrintOrderResponse(allOrdersResponse)
5151
}
5252

53-
func prettyPrintOrderResponse(orders []orderbook.OrderResponse) {
53+
func prettyPrintOrderResponse(orders []models.OrderResponse) {
5454
for _, order := range orders {
5555
jsonOrder, err := json.MarshalIndent(order, "", " ")
5656
if err != nil {

0 commit comments

Comments
 (0)