@swapkit/helpers 2.5.0 → 3.0.0-beta.0
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/dist/api/index.cjs +3 -0
- package/dist/api/index.cjs.map +16 -0
- package/dist/api/index.js +3 -0
- package/dist/api/index.js.map +16 -0
- package/dist/chunk-fazw0jvt.js +3 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-nm00d52v.js +4 -0
- package/dist/chunk-nm00d52v.js.map +9 -0
- package/dist/contracts/index.cjs +3 -0
- package/dist/contracts/index.cjs.map +38 -0
- package/dist/contracts/index.js +3 -0
- package/dist/contracts/index.js.map +38 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +29 -0
- package/dist/index.js +3 -3
- package/dist/index.js.map +19 -20
- package/dist/tokens/index.cjs +3 -0
- package/dist/tokens/index.cjs.map +23 -0
- package/dist/tokens/index.js +3 -0
- package/dist/tokens/index.js.map +23 -0
- package/package.json +31 -15
- package/src/api/index.ts +19 -0
- package/src/api/microgard/endpoints.ts +83 -0
- package/src/api/microgard/types.ts +60 -0
- package/src/api/midgard/endpoints.ts +166 -0
- package/src/api/midgard/types.ts +155 -0
- package/src/api/swapkitApi/endpoints.ts +266 -0
- package/src/api/swapkitApi/types.ts +772 -0
- package/src/api/thornode/endpoints.ts +78 -0
- package/src/api/thornode/types.ts +241 -0
- package/src/api/thorswapStatic/endpoints.ts +30 -0
- package/src/api/thorswapStatic/types.ts +18 -0
- package/src/contracts/abis/avaxGeneric.ts +92 -0
- package/src/contracts/abis/avaxWoofi.ts +145 -0
- package/src/contracts/abis/bscGeneric.ts +106 -0
- package/src/contracts/abis/chainflipGateway.ts +330 -0
- package/src/contracts/abis/erc20.ts +99 -0
- package/src/contracts/abis/ethGeneric.ts +92 -0
- package/src/contracts/abis/mayaEvmVaults.ts +331 -0
- package/src/contracts/abis/pancakeV2.ts +145 -0
- package/src/contracts/abis/pangolin.ts +120 -0
- package/src/contracts/abis/sushiswap.ts +120 -0
- package/src/contracts/abis/tcEthVault.ts +650 -0
- package/src/contracts/abis/traderJoe.ts +120 -0
- package/src/contracts/abis/uniswapV2.ts +120 -0
- package/src/contracts/abis/uniswapV2Leg.ts +128 -0
- package/src/contracts/abis/uniswapV3_100.ts +128 -0
- package/src/contracts/abis/uniswapV3_10000.ts +128 -0
- package/src/contracts/abis/uniswapV3_3000.ts +128 -0
- package/src/contracts/abis/uniswapV3_500.ts +128 -0
- package/src/contracts/index.ts +95 -0
- package/src/contracts/routers/index.ts +58 -0
- package/src/contracts/routers/kyber.ts +402 -0
- package/src/contracts/routers/oneinch.ts +2188 -0
- package/src/contracts/routers/pancakeswap.ts +340 -0
- package/src/contracts/routers/pangolin.ts +340 -0
- package/src/contracts/routers/sushiswap.ts +340 -0
- package/src/contracts/routers/traderJoe.ts +340 -0
- package/src/contracts/routers/uniswapv2.ts +340 -0
- package/src/contracts/routers/uniswapv3.ts +254 -0
- package/src/contracts/routers/woofi.ts +171 -0
- package/src/index.ts +11 -12
- package/src/modules/__tests__/assetValue.test.ts +0 -2
- package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
- package/src/modules/assetValue.ts +26 -92
- package/src/modules/bigIntArithmetics.ts +6 -2
- package/src/modules/requestClient.ts +26 -35
- package/src/modules/swapKitConfig.ts +131 -0
- package/src/modules/swapKitError.ts +6 -6
- package/src/tokens/index.ts +15 -0
- package/src/tokens/lists/camelot_v3.ts +15920 -0
- package/src/tokens/lists/caviar_v1.ts +1694 -0
- package/src/tokens/lists/chainflip.ts +104 -0
- package/src/tokens/lists/index.ts +13 -0
- package/src/tokens/lists/jupiter.ts +29606 -0
- package/src/tokens/lists/mayachain.ts +513 -0
- package/src/tokens/lists/oneinch.ts +14238 -0
- package/src/tokens/lists/openocean_v2.ts +11514 -0
- package/src/tokens/lists/pancakeswap.ts +4296 -0
- package/src/tokens/lists/pangolin_v1.ts +175 -0
- package/src/tokens/lists/sushiswap_v2.ts +965 -0
- package/src/tokens/lists/thorchain.ts +669 -0
- package/src/tokens/lists/traderjoe_v2.ts +1384 -0
- package/src/tokens/lists/uniswap_v2.ts +5596 -0
- package/src/tokens/lists/uniswap_v3.ts +5946 -0
- package/src/types/chains.ts +26 -46
- package/src/types/commonTypes.ts +3 -90
- package/src/types/index.ts +3 -4
- package/src/types/sdk.ts +0 -30
- package/src/types/tokens.ts +2 -2
- package/src/types/wallet.ts +37 -21
- package/src/{helpers → utils}/asset.ts +49 -19
- package/src/{helpers → utils}/derivationPath.ts +7 -2
- package/src/{helpers → utils}/memo.ts +0 -65
- package/src/{helpers → utils}/others.ts +12 -5
- package/src/utils/plugin.ts +13 -0
- package/src/{helpers/web3wallets.ts → utils/wallets.ts} +175 -139
- package/src/modules/walletUtils.ts +0 -30
- package/src/types/network.ts +0 -49
- /package/src/{helpers → utils}/__tests__/asset.test.ts +0 -0
- /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
- /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
- /package/src/{helpers → utils}/liquidity.ts +0 -0
- /package/src/{helpers → utils}/validators.ts +0 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export const woofiRouter = [
|
|
2
|
+
{
|
|
3
|
+
inputs: [
|
|
4
|
+
{ internalType: "address", name: "_weth", type: "address" },
|
|
5
|
+
{ internalType: "address", name: "_pool", type: "address" },
|
|
6
|
+
],
|
|
7
|
+
stateMutability: "nonpayable",
|
|
8
|
+
type: "constructor",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
anonymous: false,
|
|
12
|
+
inputs: [
|
|
13
|
+
{ indexed: true, internalType: "address", name: "previousOwner", type: "address" },
|
|
14
|
+
{ indexed: true, internalType: "address", name: "newOwner", type: "address" },
|
|
15
|
+
],
|
|
16
|
+
name: "OwnershipTransferred",
|
|
17
|
+
type: "event",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
anonymous: false,
|
|
21
|
+
inputs: [{ indexed: false, internalType: "address", name: "newPool", type: "address" }],
|
|
22
|
+
name: "WooPoolChanged",
|
|
23
|
+
type: "event",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
anonymous: false,
|
|
27
|
+
inputs: [
|
|
28
|
+
{
|
|
29
|
+
indexed: false,
|
|
30
|
+
internalType: "enum IWooRouterV2.SwapType",
|
|
31
|
+
name: "swapType",
|
|
32
|
+
type: "uint8",
|
|
33
|
+
},
|
|
34
|
+
{ indexed: true, internalType: "address", name: "fromToken", type: "address" },
|
|
35
|
+
{ indexed: true, internalType: "address", name: "toToken", type: "address" },
|
|
36
|
+
{ indexed: false, internalType: "uint256", name: "fromAmount", type: "uint256" },
|
|
37
|
+
{ indexed: false, internalType: "uint256", name: "toAmount", type: "uint256" },
|
|
38
|
+
{ indexed: false, internalType: "address", name: "from", type: "address" },
|
|
39
|
+
{ indexed: true, internalType: "address", name: "to", type: "address" },
|
|
40
|
+
{ indexed: false, internalType: "address", name: "rebateTo", type: "address" },
|
|
41
|
+
],
|
|
42
|
+
name: "WooRouterSwap",
|
|
43
|
+
type: "event",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
inputs: [],
|
|
47
|
+
name: "WETH",
|
|
48
|
+
outputs: [{ internalType: "address", name: "", type: "address" }],
|
|
49
|
+
stateMutability: "view",
|
|
50
|
+
type: "function",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
inputs: [
|
|
54
|
+
{ internalType: "address", name: "approveTarget", type: "address" },
|
|
55
|
+
{ internalType: "address", name: "swapTarget", type: "address" },
|
|
56
|
+
{ internalType: "address", name: "fromToken", type: "address" },
|
|
57
|
+
{ internalType: "address", name: "toToken", type: "address" },
|
|
58
|
+
{ internalType: "uint256", name: "fromAmount", type: "uint256" },
|
|
59
|
+
{ internalType: "uint256", name: "minToAmount", type: "uint256" },
|
|
60
|
+
{ internalType: "address payable", name: "to", type: "address" },
|
|
61
|
+
{ internalType: "bytes", name: "data", type: "bytes" },
|
|
62
|
+
],
|
|
63
|
+
name: "externalSwap",
|
|
64
|
+
outputs: [{ internalType: "uint256", name: "realToAmount", type: "uint256" }],
|
|
65
|
+
stateMutability: "payable",
|
|
66
|
+
type: "function",
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
inputs: [{ internalType: "address", name: "stuckToken", type: "address" }],
|
|
70
|
+
name: "inCaseTokenGotStuck",
|
|
71
|
+
outputs: [],
|
|
72
|
+
stateMutability: "nonpayable",
|
|
73
|
+
type: "function",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
inputs: [{ internalType: "address", name: "", type: "address" }],
|
|
77
|
+
name: "isWhitelisted",
|
|
78
|
+
outputs: [{ internalType: "bool", name: "", type: "bool" }],
|
|
79
|
+
stateMutability: "view",
|
|
80
|
+
type: "function",
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
inputs: [],
|
|
84
|
+
name: "owner",
|
|
85
|
+
outputs: [{ internalType: "address", name: "", type: "address" }],
|
|
86
|
+
stateMutability: "view",
|
|
87
|
+
type: "function",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
inputs: [
|
|
91
|
+
{ internalType: "address", name: "fromToken", type: "address" },
|
|
92
|
+
{ internalType: "address", name: "toToken", type: "address" },
|
|
93
|
+
{ internalType: "uint256", name: "fromAmount", type: "uint256" },
|
|
94
|
+
],
|
|
95
|
+
name: "querySwap",
|
|
96
|
+
outputs: [{ internalType: "uint256", name: "toAmount", type: "uint256" }],
|
|
97
|
+
stateMutability: "view",
|
|
98
|
+
type: "function",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
inputs: [],
|
|
102
|
+
name: "quoteToken",
|
|
103
|
+
outputs: [{ internalType: "address", name: "", type: "address" }],
|
|
104
|
+
stateMutability: "view",
|
|
105
|
+
type: "function",
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
inputs: [],
|
|
109
|
+
name: "renounceOwnership",
|
|
110
|
+
outputs: [],
|
|
111
|
+
stateMutability: "nonpayable",
|
|
112
|
+
type: "function",
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
inputs: [{ internalType: "address", name: "newPool", type: "address" }],
|
|
116
|
+
name: "setPool",
|
|
117
|
+
outputs: [],
|
|
118
|
+
stateMutability: "nonpayable",
|
|
119
|
+
type: "function",
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
inputs: [
|
|
123
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
124
|
+
{ internalType: "bool", name: "whitelisted", type: "bool" },
|
|
125
|
+
],
|
|
126
|
+
name: "setWhitelisted",
|
|
127
|
+
outputs: [],
|
|
128
|
+
stateMutability: "nonpayable",
|
|
129
|
+
type: "function",
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
inputs: [
|
|
133
|
+
{ internalType: "address", name: "fromToken", type: "address" },
|
|
134
|
+
{ internalType: "address", name: "toToken", type: "address" },
|
|
135
|
+
{ internalType: "uint256", name: "fromAmount", type: "uint256" },
|
|
136
|
+
{ internalType: "uint256", name: "minToAmount", type: "uint256" },
|
|
137
|
+
{ internalType: "address payable", name: "to", type: "address" },
|
|
138
|
+
{ internalType: "address", name: "rebateTo", type: "address" },
|
|
139
|
+
],
|
|
140
|
+
name: "swap",
|
|
141
|
+
outputs: [{ internalType: "uint256", name: "realToAmount", type: "uint256" }],
|
|
142
|
+
stateMutability: "payable",
|
|
143
|
+
type: "function",
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
inputs: [{ internalType: "address", name: "newOwner", type: "address" }],
|
|
147
|
+
name: "transferOwnership",
|
|
148
|
+
outputs: [],
|
|
149
|
+
stateMutability: "nonpayable",
|
|
150
|
+
type: "function",
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
inputs: [
|
|
154
|
+
{ internalType: "address", name: "fromToken", type: "address" },
|
|
155
|
+
{ internalType: "address", name: "toToken", type: "address" },
|
|
156
|
+
{ internalType: "uint256", name: "fromAmount", type: "uint256" },
|
|
157
|
+
],
|
|
158
|
+
name: "tryQuerySwap",
|
|
159
|
+
outputs: [{ internalType: "uint256", name: "toAmount", type: "uint256" }],
|
|
160
|
+
stateMutability: "view",
|
|
161
|
+
type: "function",
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
inputs: [],
|
|
165
|
+
name: "wooPool",
|
|
166
|
+
outputs: [{ internalType: "contract IWooPPV2", name: "", type: "address" }],
|
|
167
|
+
stateMutability: "view",
|
|
168
|
+
type: "function",
|
|
169
|
+
},
|
|
170
|
+
{ stateMutability: "payable", type: "receive" },
|
|
171
|
+
];
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
export * from "@swapkit/contracts";
|
|
2
|
-
export * from "./types/index";
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* Helpers
|
|
6
3
|
*/
|
|
7
|
-
export * from "./
|
|
8
|
-
export * from "./
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./
|
|
11
|
-
export * from "./
|
|
12
|
-
export * from "./
|
|
13
|
-
export * from "./
|
|
14
|
-
|
|
4
|
+
export * from "./utils/asset";
|
|
5
|
+
export * from "./utils/derivationPath";
|
|
6
|
+
export * from "./utils/liquidity";
|
|
7
|
+
export * from "./utils/memo";
|
|
8
|
+
export * from "./utils/others";
|
|
9
|
+
export * from "./utils/plugin";
|
|
10
|
+
export * from "./utils/validators";
|
|
11
|
+
export * from "./utils/wallets";
|
|
15
12
|
/**
|
|
16
13
|
* Modules
|
|
17
14
|
*/
|
|
@@ -20,4 +17,6 @@ export * from "./modules/bigIntArithmetics";
|
|
|
20
17
|
export * from "./modules/requestClient";
|
|
21
18
|
export * from "./modules/swapKitError";
|
|
22
19
|
export * from "./modules/swapKitNumber";
|
|
23
|
-
export * from "./modules/
|
|
20
|
+
export * from "./modules/swapKitConfig";
|
|
21
|
+
|
|
22
|
+
export * from "./types/index";
|
|
@@ -758,12 +758,10 @@ describe("AssetValue", () => {
|
|
|
758
758
|
const xrdAsset = AssetValue.from({ chain: Chain.Radix });
|
|
759
759
|
expect(xrdAsset).toEqual(
|
|
760
760
|
expect.objectContaining({
|
|
761
|
-
address: undefined,
|
|
762
761
|
chain: Chain.Radix,
|
|
763
762
|
decimal: BaseDecimal.XRD,
|
|
764
763
|
isGasAsset: true,
|
|
765
764
|
isSynthetic: false,
|
|
766
|
-
symbol: "XRD",
|
|
767
765
|
ticker: "XRD",
|
|
768
766
|
type: "Native",
|
|
769
767
|
}),
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { Chain } from "../../types";
|
|
3
|
+
import { SKConfig } from "../swapKitConfig";
|
|
4
|
+
|
|
5
|
+
describe("swapKitConfig", () => {
|
|
6
|
+
test("properly sets api keys", () => {
|
|
7
|
+
const initialState = SKConfig.get("apiKeys").swapKit;
|
|
8
|
+
expect(initialState).toBe("");
|
|
9
|
+
|
|
10
|
+
SKConfig.setApiKey("swapKit", "123");
|
|
11
|
+
|
|
12
|
+
const changedState = SKConfig.get("apiKeys").swapKit;
|
|
13
|
+
expect(changedState).toBe("123");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("do not drop if other keys are changed", () => {
|
|
17
|
+
SKConfig.setApiKey("swapKit", "123");
|
|
18
|
+
expect(SKConfig.get("apiKeys").swapKit).toBe("123");
|
|
19
|
+
|
|
20
|
+
SKConfig.setApiKey("ethplorer", "123");
|
|
21
|
+
expect(SKConfig.get("apiKeys")).toMatchObject({
|
|
22
|
+
swapKit: "123",
|
|
23
|
+
ethplorer: "123",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
SKConfig.setExplorerUrl(Chain.Ethereum, "https://lul.xyz");
|
|
27
|
+
expect(SKConfig.getState()).toMatchObject({
|
|
28
|
+
apiKeys: {
|
|
29
|
+
swapKit: "123",
|
|
30
|
+
ethplorer: "123",
|
|
31
|
+
},
|
|
32
|
+
explorerUrls: {
|
|
33
|
+
[Chain.Ethereum]: "https://lul.xyz",
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains";
|
|
2
|
+
import type { TokenNames, TokenTax } from "../types/tokens";
|
|
1
3
|
import {
|
|
2
4
|
type CommonAssetString,
|
|
3
5
|
CommonAssetStrings,
|
|
@@ -5,11 +7,9 @@ import {
|
|
|
5
7
|
getCommonAssetInfo,
|
|
6
8
|
getDecimal,
|
|
7
9
|
isGasAsset,
|
|
8
|
-
} from "../
|
|
9
|
-
import { warnOnce } from "../
|
|
10
|
-
import { validateIdentifier } from "../
|
|
11
|
-
import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains";
|
|
12
|
-
import type { TokenNames, TokenTax } from "../types/tokens";
|
|
10
|
+
} from "../utils/asset";
|
|
11
|
+
import { warnOnce } from "../utils/others";
|
|
12
|
+
import { validateIdentifier } from "../utils/validators";
|
|
13
13
|
|
|
14
14
|
import type { NumberPrimitives } from "./bigIntArithmetics";
|
|
15
15
|
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics";
|
|
@@ -114,6 +114,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
114
114
|
return AssetValue.from({ asset, value });
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor
|
|
117
118
|
static from<T extends {}>({
|
|
118
119
|
value = 0,
|
|
119
120
|
fromBaseDecimal,
|
|
@@ -142,7 +143,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
142
143
|
|
|
143
144
|
warnOnce(
|
|
144
145
|
!(asyncTokenLookup || tokenDecimal),
|
|
145
|
-
`Couldn't find static decimal for
|
|
146
|
+
`Couldn't find static decimal for one or more tokens on ${chain} (Using default ${BaseDecimal[chain]} decimal as fallback).
|
|
146
147
|
This can result in incorrect calculations and mess with amount sent on transactions.
|
|
147
148
|
You can load static assets by installing @swapkit/tokens package and calling AssetValue.loadStaticAssets()
|
|
148
149
|
or by passing asyncTokenLookup: true to the from() function, which will make it async and return a promise.`,
|
|
@@ -166,93 +167,26 @@ or by passing asyncTokenLookup: true to the from() function, which will make it
|
|
|
166
167
|
return assetValue as ConditionalAssetValueReturn<T>;
|
|
167
168
|
}
|
|
168
169
|
|
|
169
|
-
static loadStaticAssets() {
|
|
170
|
-
|
|
171
|
-
(
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
resolve({ ok: true });
|
|
187
|
-
});
|
|
188
|
-
} catch (error) {
|
|
189
|
-
console.error(error);
|
|
190
|
-
reject({
|
|
191
|
-
ok: false,
|
|
192
|
-
error,
|
|
193
|
-
message:
|
|
194
|
-
"Couldn't load static assets. Ensure you have installed @swapkit/tokens package",
|
|
195
|
-
});
|
|
170
|
+
static async loadStaticAssets() {
|
|
171
|
+
try {
|
|
172
|
+
await import("@swapkit/helpers/tokens").then(({ tokenLists }) => {
|
|
173
|
+
for (const { tokens } of Object.values(tokenLists)) {
|
|
174
|
+
for (const { identifier, chain, ...rest } of tokens) {
|
|
175
|
+
staticTokensMap.set(
|
|
176
|
+
chain === "SOL" ? identifier : (identifier.toUpperCase() as TokenNames),
|
|
177
|
+
{
|
|
178
|
+
identifier,
|
|
179
|
+
decimal: "decimals" in rest ? rest.decimals : BaseDecimal[chain as Chain],
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
}
|
|
196
183
|
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
|
|
203
|
-
*/
|
|
204
|
-
static fromString(asset: string, value: NumberPrimitives = 0) {
|
|
205
|
-
return AssetValue.from({ asset, value, asyncTokenLookup: true });
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
|
|
209
|
-
*/
|
|
210
|
-
static fromIdentifier(
|
|
211
|
-
asset: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames,
|
|
212
|
-
value: NumberPrimitives = 0,
|
|
213
|
-
) {
|
|
214
|
-
return AssetValue.from({ asset: asset as TokenNames, value, asyncTokenLookup: true });
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* @deprecated use AssetValue.from({ asset, value })
|
|
218
|
-
*/
|
|
219
|
-
static fromStringSync(asset: string, value: NumberPrimitives = 0) {
|
|
220
|
-
return AssetValue.from({ asset, value });
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
|
|
224
|
-
*/
|
|
225
|
-
static fromStringWithBase(
|
|
226
|
-
asset: string,
|
|
227
|
-
value: string | bigint = 0n,
|
|
228
|
-
fromBaseDecimal: number = BaseDecimal.THOR,
|
|
229
|
-
) {
|
|
230
|
-
return AssetValue.from({ asyncTokenLookup: true, asset, value, fromBaseDecimal });
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
|
|
234
|
-
*/
|
|
235
|
-
static fromStringWithBaseSync(
|
|
236
|
-
asset: string,
|
|
237
|
-
value: string | bigint = 0n,
|
|
238
|
-
fromBaseDecimal: number = BaseDecimal.THOR,
|
|
239
|
-
) {
|
|
240
|
-
return AssetValue.from({ asset, value, fromBaseDecimal });
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* @deprecated use AssetValue.from({ asset, value })
|
|
244
|
-
*/
|
|
245
|
-
static fromIdentifierSync(asset: TokenNames, value: NumberPrimitives = 0) {
|
|
246
|
-
return AssetValue.from({ asset, value });
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* @deprecated use AssetValue.from({ asset, value }) or AssetValue.from({ chain, value })
|
|
250
|
-
*/
|
|
251
|
-
static fromChainOrSignature(assetOrChain: CommonAssetString, value: NumberPrimitives = 0) {
|
|
252
|
-
if (Object.values(Chain).includes(assetOrChain as Chain)) {
|
|
253
|
-
return AssetValue.from({ chain: assetOrChain as Chain, value });
|
|
184
|
+
});
|
|
185
|
+
return true;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error(error);
|
|
188
|
+
return false;
|
|
254
189
|
}
|
|
255
|
-
return AssetValue.from({ asset: assetOrChain, value });
|
|
256
190
|
}
|
|
257
191
|
}
|
|
258
192
|
|
|
@@ -338,7 +272,7 @@ function safeValue(value: NumberPrimitives, decimal: number) {
|
|
|
338
272
|
: value;
|
|
339
273
|
}
|
|
340
274
|
|
|
341
|
-
// TODO refactor & split into smaller functions
|
|
275
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor & split into smaller functions
|
|
342
276
|
function getAssetInfo(identifier: string) {
|
|
343
277
|
const isSynthetic = identifier.slice(0, 14).includes("/");
|
|
344
278
|
const isTradeAsset = identifier.slice(0, 14).includes("~");
|
|
@@ -131,7 +131,6 @@ export class BigIntArithmetics {
|
|
|
131
131
|
return this.#comparison("eqValue", value);
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
// @ts-expect-error False positive
|
|
135
134
|
getValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
|
|
136
135
|
const value = this.formatBigIntToSafeValue(
|
|
137
136
|
this.bigIntValue,
|
|
@@ -146,10 +145,11 @@ export class BigIntArithmetics {
|
|
|
146
145
|
case "bigint":
|
|
147
146
|
return ((this.bigIntValue * 10n ** BigInt(this.decimal || 8n)) /
|
|
148
147
|
this.decimalMultiplier) as NumberPrimitivesType[T];
|
|
148
|
+
default:
|
|
149
|
+
return value as NumberPrimitivesType[T];
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
// @ts-expect-error
|
|
153
153
|
getBaseValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
|
|
154
154
|
const divisor =
|
|
155
155
|
this.decimalMultiplier / toMultiplier(decimal || this.decimal || BaseDecimal.THOR);
|
|
@@ -162,6 +162,8 @@ export class BigIntArithmetics {
|
|
|
162
162
|
return baseValue.toString() as NumberPrimitivesType[T];
|
|
163
163
|
case "bigint":
|
|
164
164
|
return baseValue as NumberPrimitivesType[T];
|
|
165
|
+
default:
|
|
166
|
+
return baseValue as NumberPrimitivesType[T];
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
|
|
@@ -360,6 +362,8 @@ export class BigIntArithmetics {
|
|
|
360
362
|
return compareToValue <= value;
|
|
361
363
|
case "eqValue":
|
|
362
364
|
return compareToValue === value;
|
|
365
|
+
default:
|
|
366
|
+
return false;
|
|
363
367
|
}
|
|
364
368
|
}
|
|
365
369
|
|
|
@@ -1,75 +1,66 @@
|
|
|
1
|
+
import { SKConfig } from "./swapKitConfig";
|
|
2
|
+
|
|
1
3
|
type Options = Parameters<typeof fetch>[1] & {
|
|
2
4
|
headers?: Record<string, string>;
|
|
3
5
|
apiKey?: string;
|
|
4
|
-
method?: "GET" | "POST";
|
|
5
6
|
onError?: (error: any) => any;
|
|
6
7
|
responseHandler?: (response: any) => any;
|
|
7
8
|
searchParams?: Record<string, string>;
|
|
8
9
|
json?: unknown;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
let clientConfig: Options = {};
|
|
12
|
-
|
|
13
12
|
export const defaultRequestHeaders =
|
|
14
13
|
typeof window !== "undefined"
|
|
15
14
|
? ({} as Record<string, string>)
|
|
16
15
|
: { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function fetchWithConfig(url: string, options: Options) {
|
|
23
|
-
const { apiKey, ...config } = clientConfig;
|
|
17
|
+
async function fetchWithConfig(
|
|
18
|
+
{ url, method }: { url: string; method: "GET" | "POST" },
|
|
19
|
+
options: Options = {},
|
|
20
|
+
) {
|
|
24
21
|
const { searchParams, json, body } = options;
|
|
22
|
+
const urlInstance = new URL(url);
|
|
23
|
+
const isJson = json || url.endsWith(".json");
|
|
24
|
+
const bodyToSend = isJson ? JSON.stringify(json) : body;
|
|
25
|
+
|
|
25
26
|
const headers = {
|
|
26
27
|
...defaultRequestHeaders,
|
|
27
|
-
...config.headers,
|
|
28
28
|
...options.headers,
|
|
29
|
-
...(
|
|
29
|
+
...(isJson ? { "Content-Type": "application/json" } : {}),
|
|
30
30
|
} as Record<string, string>;
|
|
31
31
|
|
|
32
|
-
const bodyToSend = json ? JSON.stringify(json) : body;
|
|
33
|
-
|
|
34
|
-
const urlInstance = new URL(url);
|
|
35
32
|
if (searchParams) {
|
|
36
33
|
urlInstance.search = new URLSearchParams(searchParams).toString();
|
|
37
34
|
}
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
if (SKConfig.get("apiKeys").swapKit) {
|
|
36
|
+
headers["x-api-key"] = SKConfig.get("apiKeys").swapKit;
|
|
37
|
+
}
|
|
40
38
|
|
|
41
39
|
try {
|
|
42
40
|
const response = await fetch(urlInstance.toString(), {
|
|
43
|
-
...config,
|
|
44
41
|
...options,
|
|
42
|
+
method,
|
|
45
43
|
body: bodyToSend,
|
|
46
44
|
headers,
|
|
47
45
|
});
|
|
48
46
|
const body = await response.json();
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return body;
|
|
48
|
+
return options.responseHandler?.(body) || body;
|
|
53
49
|
} catch (error) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
console.error(error);
|
|
50
|
+
return options.onError?.(error) || console.error(error);
|
|
57
51
|
}
|
|
58
52
|
}
|
|
59
53
|
|
|
60
54
|
export const RequestClient = {
|
|
61
55
|
get: async <T>(url: string, options?: Options): Promise<T> =>
|
|
62
|
-
fetchWithConfig(
|
|
56
|
+
fetchWithConfig({ url, method: "GET" }, options),
|
|
63
57
|
post: async <T>(url: string, options?: Options): Promise<T> =>
|
|
64
|
-
fetchWithConfig(
|
|
65
|
-
extend: (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
extend: (newOptions: Options) => RequestClient.extend({ ...extendedConfig, ...newOptions }),
|
|
73
|
-
};
|
|
74
|
-
},
|
|
58
|
+
fetchWithConfig({ url, method: "POST" }, options),
|
|
59
|
+
extend: (extendOptions: Options) => ({
|
|
60
|
+
get: async <T>(url: string, options?: Options): Promise<T> =>
|
|
61
|
+
fetchWithConfig({ url, method: "GET" }, { ...extendOptions, ...options }),
|
|
62
|
+
post: async <T>(url: string, options?: Options): Promise<T> =>
|
|
63
|
+
fetchWithConfig({ url, method: "POST" }, { ...extendOptions, ...options }),
|
|
64
|
+
extend: (newOptions: Options) => RequestClient.extend({ ...extendOptions, ...newOptions }),
|
|
65
|
+
}),
|
|
75
66
|
};
|