@swapkit/helpers 3.0.0-beta.0 → 3.0.0-beta.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/dist/api/index.cjs +2 -2
- package/dist/api/index.cjs.map +6 -6
- package/dist/api/index.js +2 -2
- package/dist/api/index.js.map +6 -6
- package/dist/chunk-1gwdyyeh.js +4 -0
- package/dist/chunk-1gwdyyeh.js.map +10 -0
- package/dist/chunk-2bhvezjh.js +4 -0
- package/dist/chunk-2bhvezjh.js.map +10 -0
- package/dist/chunk-3ptwhtpd.js +3 -0
- package/dist/chunk-3ptwhtpd.js.map +10 -0
- package/dist/{chunk-nm00d52v.js → chunk-3wnfcm30.js} +2 -2
- package/dist/{chunk-nm00d52v.js.map → chunk-3wnfcm30.js.map} +1 -1
- package/dist/chunk-40epvqbm.js +3 -0
- package/dist/chunk-40epvqbm.js.map +10 -0
- package/dist/chunk-49nsjaay.js +3 -0
- package/dist/chunk-49nsjaay.js.map +10 -0
- package/dist/chunk-4b66tryh.js +4 -0
- package/dist/chunk-4b66tryh.js.map +10 -0
- package/dist/chunk-4kzjrrjv.js +3 -0
- package/dist/chunk-4kzjrrjv.js.map +10 -0
- package/dist/chunk-5m49s8z8.js +4 -0
- package/dist/chunk-5m49s8z8.js.map +10 -0
- package/dist/chunk-9b6y7r75.js +4 -0
- package/dist/chunk-9b6y7r75.js.map +10 -0
- package/dist/chunk-9zcbvqq2.js +3 -0
- package/dist/chunk-9zcbvqq2.js.map +10 -0
- package/dist/chunk-a0bpx1q3.js +4 -0
- package/dist/chunk-a0bpx1q3.js.map +10 -0
- package/dist/chunk-a0fxkkfh.js +4 -0
- package/dist/chunk-a0fxkkfh.js.map +10 -0
- package/dist/chunk-a6bjd9ar.js +3 -0
- package/dist/chunk-a6bjd9ar.js.map +10 -0
- package/dist/chunk-bg6pz5qh.js +3 -0
- package/dist/chunk-bg6pz5qh.js.map +10 -0
- package/dist/chunk-cpdcnp2x.js +4 -0
- package/dist/chunk-cpdcnp2x.js.map +10 -0
- package/dist/chunk-fr86y3rx.js +4 -0
- package/dist/chunk-fr86y3rx.js.map +10 -0
- package/dist/chunk-ftdtdkhk.js +3 -0
- package/dist/chunk-ftdtdkhk.js.map +10 -0
- package/dist/chunk-gjeaw024.js +4 -0
- package/dist/chunk-gjeaw024.js.map +10 -0
- package/dist/chunk-hdcdd2cf.js +4 -0
- package/dist/chunk-hdcdd2cf.js.map +10 -0
- package/dist/chunk-q2pb6ggs.js +3 -0
- package/dist/chunk-q2pb6ggs.js.map +10 -0
- package/dist/chunk-t9s9811k.js +3 -0
- package/dist/chunk-t9s9811k.js.map +10 -0
- package/dist/chunk-v4dvhh90.js +4 -0
- package/dist/chunk-v4dvhh90.js.map +10 -0
- package/dist/chunk-vzptz52h.js +4 -0
- package/dist/chunk-vzptz52h.js.map +10 -0
- package/dist/chunk-wyr5d8ad.js +3 -0
- package/dist/chunk-wyr5d8ad.js.map +10 -0
- package/dist/chunk-x0jsq6ag.js +3 -0
- package/dist/chunk-x0jsq6ag.js.map +10 -0
- package/dist/chunk-x2fe9scs.js +3 -0
- package/dist/chunk-x2fe9scs.js.map +10 -0
- package/dist/chunk-xm5jkehh.js +4 -0
- package/dist/chunk-xm5jkehh.js.map +10 -0
- package/dist/chunk-xzb1nshn.js +3 -0
- package/dist/chunk-xzb1nshn.js.map +10 -0
- package/dist/contracts/index.cjs.map +2 -2
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/index.js.map +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +19 -18
- package/dist/index.js +3 -3
- package/dist/index.js.map +19 -18
- package/dist/tokens/index.cjs +2 -2
- package/dist/tokens/index.cjs.map +3 -16
- package/dist/tokens/index.js +2 -2
- package/dist/tokens/index.js.map +3 -16
- package/package.json +8 -5
- package/src/api/midgard/endpoints.ts +120 -132
- package/src/api/swapkitApi/endpoints.ts +53 -158
- package/src/api/swapkitApi/types.ts +318 -514
- package/src/api/thorswapStatic/endpoints.ts +18 -2
- package/src/contracts/index.ts +1 -1
- package/src/index.ts +15 -10
- package/src/modules/__tests__/assetValue.test.ts +50 -22
- package/src/modules/__tests__/swapKitConfig.test.ts +3 -3
- package/src/modules/assetValue.ts +77 -67
- package/src/modules/requestClient.ts +52 -55
- package/src/modules/swapKitConfig.ts +8 -5
- package/src/modules/swapKitError.ts +18 -11
- package/src/tokens/lists/index.ts +1 -0
- package/src/types/chains.ts +22 -5
- package/src/types/commonTypes.ts +1 -1
- package/src/types/derivationPath.ts +11 -24
- package/src/types/radix.ts +2 -2
- package/src/types/sdk.ts +0 -7
- package/src/types/tokens.ts +13 -16
- package/src/types/wallet.ts +42 -21
- package/src/utils/asset.ts +118 -217
- package/src/utils/derivationPath.ts +24 -7
- package/src/utils/plugin.ts +1 -1
- package/src/utils/tokens.ts +73 -0
- package/src/utils/validators.ts +3 -2
- package/src/utils/wallets.ts +19 -10
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "swapkit-oss",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"
|
|
4
|
+
"ts-pattern": "5.7.0",
|
|
5
|
+
"zod": "3.24.3",
|
|
5
6
|
"zustand": "5.0.3"
|
|
6
7
|
},
|
|
7
8
|
"description": "SwapKit - Helpers",
|
|
8
9
|
"devDependencies": {
|
|
9
|
-
"@swapkit/toolboxes": "1.0.0-beta.
|
|
10
|
+
"@swapkit/toolboxes": "1.0.0-beta.1"
|
|
10
11
|
},
|
|
11
12
|
"exports": {
|
|
12
13
|
".": {
|
|
@@ -38,18 +39,20 @@
|
|
|
38
39
|
"license": "Apache-2.0",
|
|
39
40
|
"name": "@swapkit/helpers",
|
|
40
41
|
"repository": {
|
|
42
|
+
"directory": "packages/helpers",
|
|
41
43
|
"type": "git",
|
|
42
44
|
"url": "git+https://github.com/thorswap/SwapKit.git"
|
|
43
45
|
},
|
|
44
46
|
"scripts": {
|
|
45
47
|
"build": "bun run ./build.ts",
|
|
48
|
+
"build:clean": "rm -rf dist && bun run ./build.ts",
|
|
46
49
|
"clean": "rm -rf dist node_modules *.tsbuildinfo",
|
|
47
|
-
"lint": "biome check --write ./src",
|
|
48
50
|
"generate-tokens": "bun --bun ./fetchTokenLists.ts",
|
|
49
51
|
"test": "bun test",
|
|
50
52
|
"test:ci": "bun test --coverage",
|
|
51
|
-
"type-check": "tsc --noEmit"
|
|
53
|
+
"type-check": "bun tsc --noEmit",
|
|
54
|
+
"type-check:go": "tsgo"
|
|
52
55
|
},
|
|
53
56
|
"type": "module",
|
|
54
|
-
"version": "3.0.0-beta.
|
|
57
|
+
"version": "3.0.0-beta.1"
|
|
55
58
|
}
|
|
@@ -7,160 +7,148 @@ import type {
|
|
|
7
7
|
THORNameDetails,
|
|
8
8
|
} from "./types";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// As we default to TC and this is the only place that checks for opposite
|
|
13
|
-
function getMidgardBaseUrl(isMayachain = false) {
|
|
14
|
-
return isMayachain ? "https://midgard.mayachain.info" : "https://midgard.ninerealms.com";
|
|
10
|
+
function getMidgardBaseUrl(isThorchain = true) {
|
|
11
|
+
return isThorchain ? "https://midgard.ninerealms.com" : "https://midgard.mayachain.info";
|
|
15
12
|
}
|
|
16
13
|
|
|
17
|
-
function getNameServiceBaseUrl(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
: `${getMidgardBaseUrl(isMayachain)}/v2/thorname`;
|
|
14
|
+
function getNameServiceBaseUrl(isThorchain = true) {
|
|
15
|
+
const baseUrl = getMidgardBaseUrl(isThorchain);
|
|
16
|
+
return isThorchain ? `${baseUrl}/v2/thorname` : `${baseUrl}/v2/mayaname`;
|
|
21
17
|
}
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
`${
|
|
26
|
-
|
|
19
|
+
function getBorrowerDetailRaw(baseUrl: string) {
|
|
20
|
+
return function getBorrowerDetail(address: string) {
|
|
21
|
+
return RequestClient.get<BorrowerDetails>(`${baseUrl}/v2/borrower/${address}`);
|
|
22
|
+
};
|
|
27
23
|
}
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
return
|
|
25
|
+
function getSaverDetailRaw(baseUrl: string) {
|
|
26
|
+
return function getSaverDetail(address: string) {
|
|
27
|
+
return RequestClient.get<SaverDetails>(`${baseUrl}/v2/saver/${address}`);
|
|
28
|
+
};
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
function getLiquidityPositionRaw<Chain extends Chain.THORChain | Chain.Maya>(baseUrl: string) {
|
|
32
|
+
return function getLiquidityPosition(
|
|
33
|
+
address: string,
|
|
34
|
+
): Promise<Chain extends Chain.THORChain ? MemberDetailsThorchain : MemberDetailsMayachain> {
|
|
35
|
+
return RequestClient.get<
|
|
36
|
+
Chain extends Chain.THORChain ? MemberDetailsThorchain : MemberDetailsMayachain
|
|
37
|
+
>(`${baseUrl}/v2/member/${address}`);
|
|
38
|
+
};
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
return
|
|
41
|
+
function getNameDetails(baseUrl: string) {
|
|
42
|
+
return function getNameDetails(name: string) {
|
|
43
|
+
return RequestClient.get<THORNameDetails>(`${baseUrl}/lookup/${name}`);
|
|
44
|
+
};
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
return
|
|
48
|
-
`${
|
|
49
|
-
|
|
47
|
+
function getNamesByAddress(baseUrl: string) {
|
|
48
|
+
return function getNamesByAddress(address: string) {
|
|
49
|
+
return RequestClient.get<string[]>(`${baseUrl}/rlookup/${address}`);
|
|
50
|
+
};
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
`${
|
|
55
|
-
|
|
53
|
+
function getNamesByOwner(baseUrl: string) {
|
|
54
|
+
return function getNamesByOwner(address: string) {
|
|
55
|
+
return RequestClient.get<string[]>(`${baseUrl}/owner/${address}`);
|
|
56
|
+
};
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
59
|
+
function getBorrowerDetail(borrowerDetailGetter: ReturnType<typeof getBorrowerDetailRaw>) {
|
|
60
|
+
return async function getBorrowerDetail(address: string) {
|
|
61
|
+
const rawBorrowerDetail = await borrowerDetailGetter(address);
|
|
62
|
+
|
|
63
|
+
return rawBorrowerDetail.pools.map((p) => ({
|
|
64
|
+
collateral_deposited: AssetValue.from({
|
|
65
|
+
asset: p.collateral_asset,
|
|
66
|
+
value: p.collateral_deposited,
|
|
67
|
+
fromBaseDecimal: BaseDecimal.THOR,
|
|
68
|
+
}),
|
|
69
|
+
collateral_withdrawn: AssetValue.from({
|
|
70
|
+
asset: p.collateral_asset,
|
|
71
|
+
value: p.collateral_withdrawn,
|
|
72
|
+
fromBaseDecimal: BaseDecimal.THOR,
|
|
73
|
+
}),
|
|
74
|
+
debt_issued_tor: SwapKitNumber.fromBigInt(BigInt(p.debt_issued_tor), BaseDecimal.THOR),
|
|
75
|
+
debt_repaid_tor: SwapKitNumber.fromBigInt(BigInt(p.debt_repaid_tor), BaseDecimal.THOR),
|
|
76
|
+
last_open_loan_timestamp: p.last_open_loan_timestamp,
|
|
77
|
+
last_repay_loan_timestamp: p.last_repay_loan_timestamp,
|
|
78
|
+
target_assets: p.target_assets.map((asset) => AssetValue.from({ asset })),
|
|
79
|
+
}));
|
|
80
|
+
};
|
|
78
81
|
}
|
|
79
82
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return rawSaverPositions.pools.map((p) => ({
|
|
84
|
-
assetRegisteredAddress: p.assetAddress,
|
|
85
|
-
assetAdded: AssetValue.from({
|
|
86
|
-
asset: p.pool,
|
|
87
|
-
value: p.assetAdded,
|
|
88
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
89
|
-
}),
|
|
90
|
-
assetDeposit: AssetValue.from({
|
|
91
|
-
asset: p.pool,
|
|
92
|
-
value: p.assetDeposit,
|
|
93
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
94
|
-
}),
|
|
95
|
-
assetRedeem: AssetValue.from({
|
|
96
|
-
asset: p.pool,
|
|
97
|
-
value: p.assetRedeem,
|
|
98
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
99
|
-
}),
|
|
100
|
-
assetWithdrawn: AssetValue.from({
|
|
101
|
-
asset: p.pool,
|
|
102
|
-
value: p.assetWithdrawn,
|
|
103
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
104
|
-
}),
|
|
105
|
-
dateLastAdded: p.dateLastAdded,
|
|
106
|
-
dateFirstAdded: p.dateFirstAdded,
|
|
107
|
-
}));
|
|
83
|
+
function getPoolAsset({ asset, value }: { asset: string; value: string }) {
|
|
84
|
+
return AssetValue.from({ asset, value, fromBaseDecimal: BaseDecimal.THOR });
|
|
108
85
|
}
|
|
109
86
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
asset: p.pool,
|
|
117
|
-
value: p.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}),
|
|
125
|
-
assetWithdrawn: AssetValue.from({
|
|
126
|
-
asset: p.pool,
|
|
127
|
-
value: p.assetWithdrawn,
|
|
128
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
129
|
-
}),
|
|
130
|
-
[`${isMayachain ? "cacao" : "rune"}RegisteredAddress`]: p.runeAddress,
|
|
131
|
-
[`${isMayachain ? "cacao" : "rune"}`]: AssetValue.from({
|
|
132
|
-
asset: "THOR.RUNE",
|
|
133
|
-
value: p.runeAdded,
|
|
134
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
135
|
-
}),
|
|
136
|
-
[`${isMayachain ? "cacao" : "rune"}Pending`]: AssetValue.from({
|
|
137
|
-
asset: "THOR.RUNE",
|
|
138
|
-
value: p.runePending,
|
|
139
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
140
|
-
}),
|
|
141
|
-
[`${isMayachain ? "cacao" : "rune"}Withdrawn`]: AssetValue.from({
|
|
142
|
-
asset: "THOR.RUNE",
|
|
143
|
-
value: p.runeWithdrawn,
|
|
144
|
-
fromBaseDecimal: BaseDecimal.THOR,
|
|
145
|
-
}),
|
|
146
|
-
poolShare: new SwapKitNumber(p.liquidityUnits).div(p.pool),
|
|
147
|
-
dateLastAdded: p.dateLastAdded,
|
|
148
|
-
dateFirstAdded: p.dateFirstAdded,
|
|
149
|
-
}));
|
|
87
|
+
function getSaverDetail(saverDetailGetter: ReturnType<typeof getSaverDetailRaw>) {
|
|
88
|
+
return async function getSaverDetail(address: string) {
|
|
89
|
+
const rawSaverPositions = await saverDetailGetter(address);
|
|
90
|
+
|
|
91
|
+
return rawSaverPositions.pools.map((p) => ({
|
|
92
|
+
assetAdded: getPoolAsset({ asset: p.pool, value: p.assetAdded }),
|
|
93
|
+
assetDeposit: getPoolAsset({ asset: p.pool, value: p.assetDeposit }),
|
|
94
|
+
assetRedeem: getPoolAsset({ asset: p.pool, value: p.assetRedeem }),
|
|
95
|
+
assetWithdrawn: getPoolAsset({ asset: p.pool, value: p.assetWithdrawn }),
|
|
96
|
+
assetRegisteredAddress: p.assetAddress,
|
|
97
|
+
dateFirstAdded: p.dateFirstAdded,
|
|
98
|
+
dateLastAdded: p.dateLastAdded,
|
|
99
|
+
}));
|
|
100
|
+
};
|
|
150
101
|
}
|
|
151
102
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
103
|
+
function getLiquidityPosition<IsThorchain extends boolean = true>({
|
|
104
|
+
liquidityPositionGetter,
|
|
105
|
+
isThorchain,
|
|
106
|
+
}: {
|
|
107
|
+
liquidityPositionGetter: ReturnType<
|
|
108
|
+
typeof getLiquidityPositionRaw<IsThorchain extends true ? Chain.THORChain : Chain.Maya>
|
|
109
|
+
>;
|
|
110
|
+
isThorchain: IsThorchain;
|
|
111
|
+
}) {
|
|
112
|
+
return async function getLiquidityPosition(address: string) {
|
|
113
|
+
const rawLiquidityPositions = await liquidityPositionGetter(address);
|
|
114
|
+
const fieldPrefix = isThorchain ? "rune" : "cacao";
|
|
115
|
+
|
|
116
|
+
return rawLiquidityPositions.pools.map((p) => ({
|
|
117
|
+
[`${fieldPrefix}Pending`]: getPoolAsset({ asset: "THOR.RUNE", value: p.runePending }),
|
|
118
|
+
[`${fieldPrefix}RegisteredAddress`]: p.runeAddress,
|
|
119
|
+
[`${fieldPrefix}Withdrawn`]: getPoolAsset({ asset: "THOR.RUNE", value: p.runeWithdrawn }),
|
|
120
|
+
[fieldPrefix]: getPoolAsset({ asset: "THOR.RUNE", value: p.runeAdded }),
|
|
121
|
+
asset: getPoolAsset({ asset: p.pool, value: p.assetAdded }),
|
|
122
|
+
assetPending: getPoolAsset({ asset: p.pool, value: p.assetPending }),
|
|
123
|
+
assetRegisteredAddress: p.assetAddress,
|
|
124
|
+
assetWithdrawn: getPoolAsset({ asset: p.pool, value: p.assetWithdrawn }),
|
|
125
|
+
dateFirstAdded: p.dateFirstAdded,
|
|
126
|
+
dateLastAdded: p.dateLastAdded,
|
|
127
|
+
poolShare: new SwapKitNumber(p.liquidityUnits).div(p.pool),
|
|
128
|
+
}));
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function getMidgardMethodsForProtocol<T extends Chain.THORChain | Chain.Maya>(chain: T) {
|
|
133
|
+
const isThorchain = chain === Chain.THORChain;
|
|
134
|
+
const midgardBaseUrl = getMidgardBaseUrl(isThorchain);
|
|
135
|
+
const nameServiceBaseUrl = getNameServiceBaseUrl(isThorchain);
|
|
136
|
+
const liquidityPositionGetter = getLiquidityPositionRaw<T>(midgardBaseUrl);
|
|
137
|
+
const borrowerDetailGetter = getBorrowerDetailRaw(midgardBaseUrl);
|
|
138
|
+
const saverDetailGetter = getSaverDetailRaw(midgardBaseUrl);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
getLiquidityPositionRaw: liquidityPositionGetter,
|
|
142
|
+
getBorrowerDetailRaw: borrowerDetailGetter,
|
|
143
|
+
getSaverDetailRaw: saverDetailGetter,
|
|
144
|
+
getBorrowerDetail: getBorrowerDetail(borrowerDetailGetter),
|
|
145
|
+
getSaversDetail: getSaverDetail(saverDetailGetter),
|
|
146
|
+
getNameDetails: getNameDetails(nameServiceBaseUrl),
|
|
147
|
+
getNamesByAddress: getNamesByAddress(nameServiceBaseUrl),
|
|
148
|
+
getNamesByOwner: getNamesByOwner(nameServiceBaseUrl),
|
|
149
|
+
getLiquidityPosition: getLiquidityPosition({ liquidityPositionGetter, isThorchain }),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
164
152
|
|
|
165
153
|
export const thorchainMidgard = getMidgardMethodsForProtocol(Chain.THORChain);
|
|
166
154
|
export const mayachainMidgard = getMidgardMethodsForProtocol(Chain.Maya);
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
type Chain,
|
|
3
|
+
type EVMChain,
|
|
4
|
+
EVMChains,
|
|
5
|
+
type ProviderName,
|
|
6
|
+
RequestClient,
|
|
7
|
+
SKConfig,
|
|
8
|
+
SwapKitError,
|
|
9
|
+
isGasAsset,
|
|
10
|
+
} from "@swapkit/helpers";
|
|
3
11
|
|
|
4
12
|
import {
|
|
13
|
+
type BalanceResponse,
|
|
5
14
|
type BrokerDepositChannelParams,
|
|
6
15
|
type DepositChannelResponse,
|
|
7
16
|
DepositChannelResponseSchema,
|
|
@@ -19,56 +28,12 @@ import {
|
|
|
19
28
|
type TrackerResponse,
|
|
20
29
|
} from "./types";
|
|
21
30
|
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return `${isDev ? devApiUrl : apiUrl}${path}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function getAuthHeaders(hash?: string) {
|
|
29
|
-
const { swapKit } = SKConfig.get("apiKeys");
|
|
30
|
-
const { referer } = SKConfig.get("envs");
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
...(swapKit && !hash ? { "x-api-key": swapKit } : {}),
|
|
34
|
-
...(hash && referer ? { "x-payload-hash": hash, referer } : {}),
|
|
35
|
-
};
|
|
31
|
+
export function getTrackerDetails(json: TrackerParams) {
|
|
32
|
+
return RequestClient.post<TrackerResponse>(getApiUrl("/track"), { json });
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
) => {
|
|
41
|
-
const { swapKit } = SKConfig.get("apiKeys");
|
|
42
|
-
const { referer } = SKConfig.get("envs");
|
|
43
|
-
|
|
44
|
-
if (!(referer && swapKit)) return;
|
|
45
|
-
|
|
46
|
-
if (!["POST", "GET"].includes(hashParams.method)) {
|
|
47
|
-
throw new SwapKitError("api_v2_invalid_method_key_hash", {
|
|
48
|
-
message: `Invalid method for params: ${JSON.stringify(hashParams)}`,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const data =
|
|
53
|
-
hashParams.method === "POST"
|
|
54
|
-
? JSON.stringify(hashParams.payload)
|
|
55
|
-
: `${hashParams.url}${swapKit}`;
|
|
56
|
-
|
|
57
|
-
return crypto.createHash("sha256").update(data, "utf8").digest("hex");
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export function getTrackerDetails(payload: TrackerParams) {
|
|
61
|
-
return RequestClient.post<TrackerResponse>(getApiUrl("/track"), {
|
|
62
|
-
json: payload,
|
|
63
|
-
headers: getAuthHeaders(computeHash({ method: "POST", payload })),
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export async function getSwapQuote(searchParams: QuoteRequest) {
|
|
68
|
-
const response = await RequestClient.post<QuoteResponse>(getApiUrl("/quote"), {
|
|
69
|
-
json: searchParams,
|
|
70
|
-
headers: getAuthHeaders(computeHash({ method: "POST", payload: searchParams })),
|
|
71
|
-
});
|
|
35
|
+
export async function getSwapQuote(json: QuoteRequest) {
|
|
36
|
+
const response = await RequestClient.post<QuoteResponse>(getApiUrl("/quote"), { json });
|
|
72
37
|
|
|
73
38
|
if (response.error) {
|
|
74
39
|
throw new SwapKitError("api_v2_server_error", { message: response.error });
|
|
@@ -89,33 +54,32 @@ export async function getSwapQuote(searchParams: QuoteRequest) {
|
|
|
89
54
|
}
|
|
90
55
|
}
|
|
91
56
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
57
|
+
export async function getChainBalance<T extends Chain>({
|
|
58
|
+
chain,
|
|
59
|
+
address,
|
|
60
|
+
scamFilter = true,
|
|
61
|
+
}: { chain: T; address: string; scamFilter?: boolean }) {
|
|
62
|
+
const url = getApiUrl(`/balance?chain=${chain}&address=${address}`);
|
|
63
|
+
const balanceResponse = await RequestClient.get<BalanceResponse>(url);
|
|
64
|
+
const balances = Array.isArray(balanceResponse) ? balanceResponse : [];
|
|
65
|
+
|
|
66
|
+
return scamFilter ? filterAssets(balances) : balances;
|
|
98
67
|
}
|
|
99
68
|
|
|
100
69
|
export function getTokenListProviders() {
|
|
101
70
|
const url = getApiUrl("/providers");
|
|
102
|
-
return RequestClient.get<TokenListProvidersResponse>(url
|
|
103
|
-
headers: getAuthHeaders(computeHash({ method: "GET", url })),
|
|
104
|
-
});
|
|
71
|
+
return RequestClient.get<TokenListProvidersResponse>(url);
|
|
105
72
|
}
|
|
106
73
|
|
|
107
74
|
export function getTokenList(provider: ProviderName) {
|
|
108
75
|
const url = getApiUrl(`/tokens?provider=${provider}`);
|
|
109
|
-
return RequestClient.get<TokensResponseV2>(url
|
|
110
|
-
headers: getAuthHeaders(computeHash({ method: "GET", url })),
|
|
111
|
-
});
|
|
76
|
+
return RequestClient.get<TokensResponseV2>(url);
|
|
112
77
|
}
|
|
113
78
|
|
|
114
79
|
export async function getPrice(body: PriceRequest) {
|
|
115
80
|
const url = getApiUrl("/price");
|
|
116
81
|
const response = await RequestClient.post<PriceResponse>(url, {
|
|
117
82
|
json: body,
|
|
118
|
-
headers: getAuthHeaders(computeHash({ method: "POST", payload: body })),
|
|
119
83
|
});
|
|
120
84
|
|
|
121
85
|
try {
|
|
@@ -133,10 +97,7 @@ export async function getPrice(body: PriceRequest) {
|
|
|
133
97
|
|
|
134
98
|
export async function getGasRate() {
|
|
135
99
|
const url = getApiUrl("/gas");
|
|
136
|
-
|
|
137
|
-
const response = await RequestClient.get<GasResponse>(url, {
|
|
138
|
-
headers: getAuthHeaders(computeHash({ method: "GET", url })),
|
|
139
|
-
});
|
|
100
|
+
const response = await RequestClient.get<GasResponse>(url);
|
|
140
101
|
|
|
141
102
|
try {
|
|
142
103
|
const parsedResponse = GasResponseSchema.safeParse(response);
|
|
@@ -151,97 +112,6 @@ export async function getGasRate() {
|
|
|
151
112
|
}
|
|
152
113
|
}
|
|
153
114
|
|
|
154
|
-
const UNCHAINABLE_PROVIDERS = [
|
|
155
|
-
ProviderName.CAVIAR_V1,
|
|
156
|
-
ProviderName.CHAINFLIP,
|
|
157
|
-
ProviderName.CHAINFLIP_STREAMING,
|
|
158
|
-
ProviderName.MAYACHAIN,
|
|
159
|
-
ProviderName.MAYACHAIN_STREAMING,
|
|
160
|
-
];
|
|
161
|
-
|
|
162
|
-
const CHAINABLE_PROVIDERS = [
|
|
163
|
-
ProviderName.ONEINCH,
|
|
164
|
-
ProviderName.PANCAKESWAP,
|
|
165
|
-
ProviderName.PANGOLIN_V1,
|
|
166
|
-
ProviderName.SUSHISWAP_V2,
|
|
167
|
-
ProviderName.THORCHAIN,
|
|
168
|
-
ProviderName.THORCHAIN_STREAMING,
|
|
169
|
-
ProviderName.TRADERJOE_V2,
|
|
170
|
-
ProviderName.UNISWAP_V2,
|
|
171
|
-
ProviderName.UNISWAP_V3,
|
|
172
|
-
];
|
|
173
|
-
|
|
174
|
-
// TODO update this once the trading pairs are supported by BE api
|
|
175
|
-
export async function getTokenTradingPairs(providers: ProviderName[]) {
|
|
176
|
-
const tradingPairs = new Map<
|
|
177
|
-
string,
|
|
178
|
-
{
|
|
179
|
-
tokens: TokensResponseV2["tokens"];
|
|
180
|
-
providers: ProviderName[];
|
|
181
|
-
}
|
|
182
|
-
>();
|
|
183
|
-
|
|
184
|
-
if (!providers.length) return tradingPairs;
|
|
185
|
-
|
|
186
|
-
const providerRequests = providers.map(async (provider) => {
|
|
187
|
-
const tokenList = await getTokenList(provider);
|
|
188
|
-
return tokenList;
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const providersData = (await Promise.all(providerRequests))
|
|
192
|
-
.filter((provider) => !!provider)
|
|
193
|
-
.map(({ tokens, ...rest }) => ({
|
|
194
|
-
data: {
|
|
195
|
-
...(rest || {}),
|
|
196
|
-
tokens: tokens.map(({ address, ...rest }) => ({
|
|
197
|
-
...rest,
|
|
198
|
-
...(address &&
|
|
199
|
-
[
|
|
200
|
-
"resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd",
|
|
201
|
-
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
|
202
|
-
].includes(address.toLowerCase())
|
|
203
|
-
? {}
|
|
204
|
-
: { address }),
|
|
205
|
-
})),
|
|
206
|
-
},
|
|
207
|
-
...rest,
|
|
208
|
-
}));
|
|
209
|
-
|
|
210
|
-
const chainableTokens = providersData
|
|
211
|
-
.filter(({ data }) => {
|
|
212
|
-
return !UNCHAINABLE_PROVIDERS.includes((data?.provider || "") as ProviderName);
|
|
213
|
-
})
|
|
214
|
-
.reduce(
|
|
215
|
-
(acc, { data }) => (data?.tokens ? acc.concat(data.tokens) : acc),
|
|
216
|
-
[] as TokensResponseV2["tokens"],
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
for (const { data } of providersData) {
|
|
220
|
-
if (!data?.tokens) return;
|
|
221
|
-
|
|
222
|
-
const isProviderChainable =
|
|
223
|
-
data.provider && !UNCHAINABLE_PROVIDERS.includes(data.provider as ProviderName);
|
|
224
|
-
|
|
225
|
-
for (const token of data.tokens) {
|
|
226
|
-
const existingTradingPairs = tradingPairs.get(token.identifier.toLowerCase()) || {
|
|
227
|
-
tokens: [],
|
|
228
|
-
providers: [],
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
const tradingPairsForToken = isProviderChainable
|
|
232
|
-
? { tokens: chainableTokens, providers: CHAINABLE_PROVIDERS }
|
|
233
|
-
: { tokens: data.tokens, providers: data.provider };
|
|
234
|
-
|
|
235
|
-
tradingPairs.set(token.identifier.toLowerCase(), {
|
|
236
|
-
tokens: existingTradingPairs.tokens.concat(tradingPairsForToken.tokens),
|
|
237
|
-
providers: existingTradingPairs.providers.concat(tradingPairsForToken.providers),
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return tradingPairs;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
115
|
export async function getChainflipDepositChannel(body: BrokerDepositChannelParams) {
|
|
246
116
|
const { destinationAddress } = body;
|
|
247
117
|
|
|
@@ -264,3 +134,28 @@ export async function getChainflipDepositChannel(body: BrokerDepositChannelParam
|
|
|
264
134
|
throw new SwapKitError("api_v2_invalid_response", error);
|
|
265
135
|
}
|
|
266
136
|
}
|
|
137
|
+
|
|
138
|
+
function getApiUrl(path?: `/${string}`) {
|
|
139
|
+
const { isDev, apiUrl, devApiUrl } = SKConfig.get("envs");
|
|
140
|
+
|
|
141
|
+
return `${isDev ? devApiUrl : apiUrl}${path}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function evmAssetHasAddress(assetString: string) {
|
|
145
|
+
const [chain, symbol] = assetString.split(".") as [EVMChain, string];
|
|
146
|
+
if (!EVMChains.includes(chain as EVMChain)) return true;
|
|
147
|
+
const splitSymbol = symbol.split("-");
|
|
148
|
+
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
|
|
149
|
+
|
|
150
|
+
return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const potentialScamRegex = new RegExp(
|
|
154
|
+
/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/,
|
|
155
|
+
"gmi",
|
|
156
|
+
);
|
|
157
|
+
function filterAssets(tokens: BalanceResponse) {
|
|
158
|
+
return tokens.filter((token) => {
|
|
159
|
+
return !potentialScamRegex.test(token.identifier) && evmAssetHasAddress(token.identifier);
|
|
160
|
+
});
|
|
161
|
+
}
|