@tokelia/web3-exchanges 15.7.1-tokelia.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +339 -0
- package/dist/esm/index.js +3153 -0
- package/dist/umd/index.js +3161 -0
- package/package.json +88 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 DePay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
## Quickstart
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
yarn add @depay/web3-exchanges
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
or
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install --save @depay/web3-exchanges
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import Exchanges from '@depay/web3-exchanges'
|
|
15
|
+
|
|
16
|
+
Exchanges.uniswap_v2
|
|
17
|
+
Exchanges.orca
|
|
18
|
+
|
|
19
|
+
// scoped
|
|
20
|
+
|
|
21
|
+
Exchanges.ethereum.uniswap_v2 // Ethereum scoped uniswap_v2
|
|
22
|
+
Exchanges.solana.orca
|
|
23
|
+
|
|
24
|
+
let routes
|
|
25
|
+
|
|
26
|
+
routes = await Exchanges.route({
|
|
27
|
+
blockchain: 'ethereum',
|
|
28
|
+
tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
|
|
29
|
+
tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
|
|
30
|
+
amountIn: 1
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
routes = await Exchanges.route({
|
|
34
|
+
blockchain: 'solana',
|
|
35
|
+
tokenIn: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
|
|
36
|
+
tokenOut: '4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R',
|
|
37
|
+
amountIn: 1
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
import { getWallets } from '@depay/web3-wallets'
|
|
41
|
+
|
|
42
|
+
const wallet = getWallets()[0]
|
|
43
|
+
const account = await wallet.account()
|
|
44
|
+
|
|
45
|
+
// check if prep is required to facilitate swap/exchange
|
|
46
|
+
const preparation = await route.getPrep({ account })
|
|
47
|
+
|
|
48
|
+
let permit2
|
|
49
|
+
if(prep?.transaction) {
|
|
50
|
+
await wallet.sendTransaction(prep.transaction)
|
|
51
|
+
} else if (prep?.signature) {
|
|
52
|
+
let signature = await wallet.sign(prep.signature)
|
|
53
|
+
permit2 = {...prep.signature.message, signature}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// use connected wallet to sign and send the swap transaction
|
|
57
|
+
const transaction = await route.getTransaction({ account, permit2 })
|
|
58
|
+
wallet.sendTransaction(transaction)
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Support
|
|
63
|
+
|
|
64
|
+
This library supports the following blockchains:
|
|
65
|
+
|
|
66
|
+
- [Ethereum](https://ethereum.org)
|
|
67
|
+
- [BNB Smart Chain](https://www.binance.org/smartChain)
|
|
68
|
+
- [Polygon](https://polygon.technology)
|
|
69
|
+
- [Solana](https://solana.com)
|
|
70
|
+
- [Optimism](https://www.optimism.io)
|
|
71
|
+
- [Arbitrum](https://arbitrum.io)
|
|
72
|
+
- [Fantom](https://fantom.foundation)
|
|
73
|
+
- [Avalanche](https://www.avax.network)
|
|
74
|
+
- [Gnosis](https://gnosis.io)
|
|
75
|
+
- [Base](https://base.org)
|
|
76
|
+
- [Worldchain](https://worldcoin.org/world-chain)
|
|
77
|
+
|
|
78
|
+
This library supports the following decentralized exchanges:
|
|
79
|
+
|
|
80
|
+
Ethereum:
|
|
81
|
+
- [Uniswap v3](https://uniswap.org)
|
|
82
|
+
- [Uniswap v2](https://uniswap.org)
|
|
83
|
+
- [WETH](https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2)
|
|
84
|
+
|
|
85
|
+
BNB Smart Chain:
|
|
86
|
+
- [PancakeSwap v3](https://pancakeswap.info)
|
|
87
|
+
- [Uniswap v3](https://uniswap.org)
|
|
88
|
+
- [PancakeSwap v2](https://pancakeswap.info)
|
|
89
|
+
- [WBNB](https://bscscan.com/address/0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c)
|
|
90
|
+
|
|
91
|
+
Polygon:
|
|
92
|
+
- [Uniswap v3](https://uniswap.org)
|
|
93
|
+
- [Quickswap](https://quickswap.exchange)
|
|
94
|
+
- [WMATIC](https://polygonscan.com/token/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270)
|
|
95
|
+
|
|
96
|
+
Solana:
|
|
97
|
+
- [Orca](https://orca.so)
|
|
98
|
+
- [Raydium](https://raydium.io/), only CP (Constant Product) and CL (Concentrated Liquidity) pools
|
|
99
|
+
|
|
100
|
+
Optimism:
|
|
101
|
+
- [Uniswap v3](https://uniswap.org)
|
|
102
|
+
- [WETH](https://optimistic.etherscan.io/address/0x4200000000000000000000000000000000000006)
|
|
103
|
+
|
|
104
|
+
Base:
|
|
105
|
+
- [Uniswap v2](https://uniswap.org)
|
|
106
|
+
- [Uniswap v3](https://uniswap.org)
|
|
107
|
+
- [WETH](https://optimistic.etherscan.io/address/0x4200000000000000000000000000000000000006)
|
|
108
|
+
|
|
109
|
+
Arbitrum:
|
|
110
|
+
- [Uniswap v3](https://uniswap.org)
|
|
111
|
+
- [WETH](https://arbiscan.io/address/0x82aF49447D8a07e3bd95BD0d56f35241523fBab1)
|
|
112
|
+
|
|
113
|
+
Fantom:
|
|
114
|
+
- [SpookySwap](https://spookyswap.fi)
|
|
115
|
+
- [WFTM](https://ftmscan.com/token/0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83)
|
|
116
|
+
|
|
117
|
+
Avalanche:
|
|
118
|
+
- [Trader Joe v2.1](https://traderjoexyz.com)
|
|
119
|
+
- [WAVAX](https://snowtrace.io/token/0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7)
|
|
120
|
+
|
|
121
|
+
Gnosis:
|
|
122
|
+
- [Uniswap v3](https://uniswap.org)
|
|
123
|
+
- [WXDAI](https://gnosisscan.io/token/0xe91d153e0b41518a2ce8dd3d7944fa863463a97d)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## Platform specific packaging
|
|
127
|
+
|
|
128
|
+
In case you want to use and package only specific platforms, use the platform-specific package:
|
|
129
|
+
|
|
130
|
+
### EVM (Ethereum Virtual Machine) platform specific packaging
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
import Exchanges from '@depay/web3-exchanges-evm'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### SVM (Solana Virtual Machine) platform specific packaging
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
import Exchanges from '@depay/web3-exchanges-svm'
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Data Structures
|
|
143
|
+
|
|
144
|
+
### Exchange
|
|
145
|
+
|
|
146
|
+
Decentralized exchange data is provided in the following structure:
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
{
|
|
150
|
+
name: String (e.g. uniswap_v2)
|
|
151
|
+
label: String (e.g. Uniswap v2)
|
|
152
|
+
logo: String (base64 encoded PNG)
|
|
153
|
+
protocol: String (uniswap_v2, uniswap_v3 etc.)
|
|
154
|
+
|
|
155
|
+
fee: Array (e.g. [100, 500, 3000, 10000]; available fee teirs on the exchange)
|
|
156
|
+
slippage: Boolean (indicates if exchange has slippage)
|
|
157
|
+
|
|
158
|
+
blockchains: String (e.g. ['ethereum'])
|
|
159
|
+
[blockchain]: {
|
|
160
|
+
router: Object (contains 'address' and 'api' to interact with the exchange router)
|
|
161
|
+
factory: Object (contains 'address' and 'api' to interact with the exchange factory)
|
|
162
|
+
pair: Object (contains 'api' to interact with an exchange pair)
|
|
163
|
+
quoter: Object (contains 'address' and 'api' to interact with the exchange quoter)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Swap
|
|
169
|
+
|
|
170
|
+
A Swap configuration is fed into the `route` function:
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
{
|
|
174
|
+
blockchain: String (e.g. 'ethereum')
|
|
175
|
+
tokenIn: String (e.g. '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb')
|
|
176
|
+
tokenOut: String (e.g. '0xdAC17F958D2ee523a2206206994597C13D831ec7')
|
|
177
|
+
amountIn: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
|
|
178
|
+
amountInMax: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
|
|
179
|
+
amountOut: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
|
|
180
|
+
amountOutMin: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
- `tokenIn`: The token put into the swap (out of wallet)
|
|
185
|
+
- `amountIn`: The exact amount of tokenIn put into the swap (out of wallet)
|
|
186
|
+
- `amountInMax`: The max. amount of tokenIn put into the swap (out of wallet)
|
|
187
|
+
- `tokenOut`: The token expected to come out of the swap (into wallet)
|
|
188
|
+
- `amountOut`: The exact amount of tokenOut expected to come out of the swap (into wallet)
|
|
189
|
+
- `amountOutMin`: The min. amount of tokenOut expected to come out of the swap (into wallet)
|
|
190
|
+
|
|
191
|
+
The following combinations of provided amounts are possible:
|
|
192
|
+
|
|
193
|
+
- Pass `amountOutMin`. Swap will return at least `amountOutMin` into the wallet. `amountIn` will be calculated automatically and can vary.
|
|
194
|
+
- Pass `amountOut`. Swap will take at max `amountInMax` out of the wallet (calculated based on provided, exact `amountOut`). `amountInMax` will be calculated automatically and can vary.
|
|
195
|
+
- Pass `amountInMax`. Swap will take at max `amountInMax` out of the wallet. `amountOut` will be calculated automatically and can vary.
|
|
196
|
+
- Pass `amountIn`. Swap will return at least `amountOutMin` into the the wallet (calculated based on provided, exact `amountIn`). `amountOutMin` will be calculated automatically and can vary.
|
|
197
|
+
|
|
198
|
+
### Route
|
|
199
|
+
|
|
200
|
+
Routes are returned by calling `route` on `Exchanges` or a single `exchange`.
|
|
201
|
+
|
|
202
|
+
A single `Route` has the following structure:
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
{
|
|
206
|
+
blockchain: String (e.g. 'ethereum')
|
|
207
|
+
tokenIn: String (e.g. '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb')
|
|
208
|
+
decimalsIn: Integer (e.g. 18)
|
|
209
|
+
tokenOut: String (e.g. '0xdAC17F958D2ee523a2206206994597C13D831ec7')
|
|
210
|
+
decimalsOut: Integer (e.g. 18)
|
|
211
|
+
path: Array (e.g. ['0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0xdAC17F958D2ee523a2206206994597C13D831ec7'])
|
|
212
|
+
pools: Object ([{ pool }])
|
|
213
|
+
amounts: [BigNumber] (e.g. ['1000000000000000000', '2300000000000000000', '9900000000000000000'])
|
|
214
|
+
amountIn: BigNumber (e.g. '1000000000000000000')
|
|
215
|
+
amountOutMin: BigNumber (e.g. '32000000000000000000')
|
|
216
|
+
amountOut: BigNumber (e.g. '32000000000000000000')
|
|
217
|
+
amountInMax: BigNumber (e.g. '1000000000000000000')
|
|
218
|
+
exchange: Exchange (see [Exchange data structure](#exchange))
|
|
219
|
+
getPrep: async function (returns transaction object for approvals or signature request for permit2)
|
|
220
|
+
getTransaction: async function (returns transaction object –> see @depay/web3-wallets for details)
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
See [@depay/web3-wallets](https://github.com/DePayFi/@depay/web3-wallets#sendtransaction) for details about the transaction format.
|
|
225
|
+
|
|
226
|
+
## Slippage
|
|
227
|
+
|
|
228
|
+
This library applies slippage strategies to amounts for the following combinations:
|
|
229
|
+
|
|
230
|
+
- If `amountOutMin` is provided, slippage is applied to `amountIn`.
|
|
231
|
+
- If `amountOut` is provided, slippage is applied to `amountInMax`.
|
|
232
|
+
- If `amountInMax` is provided, slippage is applied to `amountOut`.
|
|
233
|
+
- If `amountIn` is provided, slippage is applied to `amountOutMax`.
|
|
234
|
+
|
|
235
|
+
### Auto Slippage
|
|
236
|
+
|
|
237
|
+
Auto slippage applies `0.5%` default slippage.
|
|
238
|
+
|
|
239
|
+
For blockchains that allow to receive quotes for previous blocks (`EVM`), auto slippage additionally checks for:
|
|
240
|
+
|
|
241
|
+
- Extreme Direction
|
|
242
|
+
|
|
243
|
+
- Extreme Base Volatility
|
|
244
|
+
|
|
245
|
+
And applies a higher than default slippage (`0.5%`) if required.
|
|
246
|
+
|
|
247
|
+
#### Extreme Directional Price Change
|
|
248
|
+
|
|
249
|
+
If there is a clear directional change of the price for the last 3 blocks,
|
|
250
|
+
the target price will be projected according to the velocity of the last 3 blocks.
|
|
251
|
+
|
|
252
|
+
##### Example Extreme Directional Price Change
|
|
253
|
+
|
|
254
|
+
Current Price: $1'500
|
|
255
|
+
|
|
256
|
+
Last 3 Blocks: $1'500, $1'470, $1'440
|
|
257
|
+
|
|
258
|
+
Velocity: $30 per block
|
|
259
|
+
|
|
260
|
+
Projection: $1'530
|
|
261
|
+
|
|
262
|
+
Slippage 2%
|
|
263
|
+
|
|
264
|
+
Slippage of `2%` will be applied, because it's higher than default slippage.
|
|
265
|
+
|
|
266
|
+
_For downwards projections slippage is not required as the transaction would not fail._
|
|
267
|
+
|
|
268
|
+
#### Extreme Base Volatility
|
|
269
|
+
|
|
270
|
+
If there is extreme base volatility in the last 3 blocks,
|
|
271
|
+
none-directional,
|
|
272
|
+
the target price will be the highest price over the last 3 blocks plus the smallest price change over the last 3 blocks.
|
|
273
|
+
|
|
274
|
+
##### Example Extreme Base Volatility
|
|
275
|
+
|
|
276
|
+
Current Price: $1'500
|
|
277
|
+
|
|
278
|
+
Last 3 Blocks: $1'500, $1'490, $1'520
|
|
279
|
+
|
|
280
|
+
Smallest Change: $10
|
|
281
|
+
|
|
282
|
+
Highest Price: $1'520
|
|
283
|
+
|
|
284
|
+
Projection: $1'530 ($1'520 + $10)
|
|
285
|
+
|
|
286
|
+
Slippage 2%
|
|
287
|
+
|
|
288
|
+
Slippage of `2%` will be applied, because it's higher than default slippage.
|
|
289
|
+
|
|
290
|
+
## Functionalities
|
|
291
|
+
|
|
292
|
+
### Access exchange directly
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
295
|
+
import Exchanges from '@depay/web3-exchanges'
|
|
296
|
+
|
|
297
|
+
Exchanges.uniswap_v2
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Access exchange scoped for a given blockchain
|
|
302
|
+
|
|
303
|
+
```javascript
|
|
304
|
+
import Exchanges from '@depay/web3-exchanges'
|
|
305
|
+
|
|
306
|
+
Exchanges.arbitrum.uniswap_v3
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### route: Routes a Swap configuration and returns routes to perform the Swap
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
import Exchanges from '@depay/web3-exchanges'
|
|
313
|
+
|
|
314
|
+
let routes = Exchanges.route({
|
|
315
|
+
blockchain: 'ethereum'
|
|
316
|
+
tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
|
|
317
|
+
tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
|
|
318
|
+
amountOutMin: 2
|
|
319
|
+
})
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
`route` can also be called on an exchange:
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
import Exchanges from '@depay/web3-exchanges'
|
|
326
|
+
|
|
327
|
+
let route = await Exchanges.ethereum.uniswap_v2.route({
|
|
328
|
+
tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
|
|
329
|
+
tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
|
|
330
|
+
amountIn: 1
|
|
331
|
+
})
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Development
|
|
335
|
+
|
|
336
|
+
```
|
|
337
|
+
yarn install
|
|
338
|
+
yarn dev
|
|
339
|
+
```
|