@swapkit/core 1.0.0-rc.100 → 1.0.0-rc.103
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/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +18 -16
- package/dist/index.es.js +488 -474
- package/dist/index.es.js.map +1 -1
- package/package.json +13 -13
- package/src/__tests__/helpers.test.ts +63 -58
- package/src/client/index.ts +24 -21
- package/src/helpers/explorerUrls.ts +2 -0
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "swapkit-oss",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@swapkit/helpers": "1.0.0-rc.
|
|
5
|
-
"@swapkit/types": "1.0.0-rc.
|
|
4
|
+
"@swapkit/helpers": "1.0.0-rc.74",
|
|
5
|
+
"@swapkit/types": "1.0.0-rc.40"
|
|
6
6
|
},
|
|
7
7
|
"description": "SwapKit Lib core",
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"@vitest/coverage-istanbul": "1.3.1",
|
|
10
10
|
"vite": "5.1.3",
|
|
11
11
|
"vitest": "1.3.1",
|
|
12
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
13
|
-
"@swapkit/toolbox-cosmos": "1.0.0-rc.
|
|
14
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
15
|
-
"@swapkit/toolbox-substrate": "1.0.0-rc.
|
|
16
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
12
|
+
"@swapkit/tokens": "1.0.0-rc.36",
|
|
13
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.85",
|
|
14
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.80",
|
|
15
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.9",
|
|
16
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.80"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
20
|
-
"@swapkit/toolbox-cosmos": "1.0.0-rc.
|
|
21
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
22
|
-
"@swapkit/toolbox-substrate": "1.0.0-rc.
|
|
23
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
19
|
+
"@swapkit/tokens": "1.0.0-rc.36",
|
|
20
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.85",
|
|
21
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.80",
|
|
22
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.9",
|
|
23
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.80"
|
|
24
24
|
},
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"type": "module",
|
|
47
47
|
"types": "./dist/index.d.ts",
|
|
48
|
-
"version": "1.0.0-rc.
|
|
48
|
+
"version": "1.0.0-rc.103",
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "NODE_OPTIONS=--max_old_space_size=16384 vite build",
|
|
51
51
|
"clean": "rm -rf dist vite.config.ts.* node_modules",
|
|
@@ -1,79 +1,84 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Chain,
|
|
3
|
+
ChainToExplorerUrl,
|
|
4
|
+
CosmosChains,
|
|
5
|
+
EVMChains,
|
|
6
|
+
SubstrateChains,
|
|
7
|
+
UTXOChains,
|
|
8
|
+
} from "@swapkit/types";
|
|
2
9
|
import { describe, expect, test } from "vitest";
|
|
3
10
|
import { getExplorerAddressUrl, getExplorerTxUrl } from "../helpers/explorerUrls.ts";
|
|
4
11
|
|
|
5
12
|
describe("Explorer URLs", () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
describe("CosmosChains", () => {
|
|
14
|
+
for (const chain of CosmosChains.filter((c) => c !== Chain.Cosmos)) {
|
|
15
|
+
test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
|
|
16
|
+
expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
|
|
17
|
+
`${ChainToExplorerUrl[chain]}/tx/123456789`,
|
|
18
|
+
);
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
|
|
21
|
+
`${ChainToExplorerUrl[chain]}/address/asdfg`,
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
17
25
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
`${ChainToExplorerUrl[chain]}/tx/0x123456789`,
|
|
26
|
+
test(`getExplorerTxUrl returns correct URL for ${Chain.Cosmos}`, () => {
|
|
27
|
+
expect(getExplorerTxUrl({ chain: Chain.Cosmos, txHash: "0x123456789" })).toBe(
|
|
28
|
+
`${ChainToExplorerUrl[Chain.Cosmos]}/transactions/0x123456789`,
|
|
22
29
|
);
|
|
23
30
|
|
|
24
|
-
expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
|
|
25
|
-
`${ChainToExplorerUrl[
|
|
31
|
+
expect(getExplorerAddressUrl({ chain: Chain.Cosmos, address: "asdfg" })).toBe(
|
|
32
|
+
`${ChainToExplorerUrl[Chain.Cosmos]}/account/asdfg`,
|
|
26
33
|
);
|
|
27
34
|
});
|
|
28
|
-
}
|
|
35
|
+
});
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
describe("EVMChains & SubstrateChains", () => {
|
|
38
|
+
for (const chain of [...EVMChains, Chain.Polkadot]) {
|
|
39
|
+
test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
|
|
40
|
+
expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
|
|
41
|
+
`${ChainToExplorerUrl[chain]}/tx/0x123456789`,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
|
|
45
|
+
`${ChainToExplorerUrl[chain]}/address/asdfg`,
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
35
49
|
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
test("getExplorerTxUrl adds 0x for EVM like chains", () => {
|
|
51
|
+
expect(getExplorerTxUrl({ chain: Chain.Ethereum, txHash: "12345" })).toBe(
|
|
52
|
+
"https://etherscan.io/tx/0x12345",
|
|
38
53
|
);
|
|
39
54
|
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
test(`getExplorerTxUrl returns correct URL for ${Chain.Cosmos}`, () => {
|
|
43
|
-
expect(getExplorerTxUrl({ chain: Chain.Cosmos, txHash: "0x123456789" })).toBe(
|
|
44
|
-
`${ChainToExplorerUrl[Chain.Cosmos]}/transactions/0x123456789`,
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
expect(getExplorerAddressUrl({ chain: Chain.Cosmos, address: "asdfg" })).toBe(
|
|
48
|
-
`${ChainToExplorerUrl[Chain.Cosmos]}/account/asdfg`,
|
|
49
|
-
);
|
|
50
55
|
});
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
getExplorerTxUrl
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
describe("UTXOChains", () => {
|
|
58
|
+
for (const chain of UTXOChains.filter((c) => c !== Chain.Dash)) {
|
|
59
|
+
test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
|
|
60
|
+
expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
|
|
61
|
+
`${ChainToExplorerUrl[chain]}/transaction/0x123456789`,
|
|
62
|
+
);
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
test("getExplorerTxUrl adds 0x for EVM like chains", () => {
|
|
64
|
-
expect(getExplorerTxUrl({ chain: Chain.Ethereum, txHash: "12345" })).toBe(
|
|
65
|
-
"https://etherscan.io/tx/0x12345",
|
|
66
|
-
);
|
|
64
|
+
expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
|
|
65
|
+
`${ChainToExplorerUrl[chain]}/address/asdfg`,
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
67
69
|
});
|
|
68
70
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
describe("Unsupported chains", () => {
|
|
72
|
+
test("getExplorerTxUrl throws Error for unsupported Chain", () => {
|
|
73
|
+
expect(() =>
|
|
74
|
+
getExplorerTxUrl({ chain: "unsupported" as Chain, txHash: "0x12345" }),
|
|
75
|
+
).toThrowError("Unsupported chain: unsupported");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("getExplorerAddressUrl throws Error for unsupported Chain", () => {
|
|
79
|
+
expect(() =>
|
|
80
|
+
getExplorerAddressUrl({ chain: "unsupported" as Chain, address: "asdfg" }),
|
|
81
|
+
).toThrowError("Unsupported chain: unsupported");
|
|
82
|
+
});
|
|
78
83
|
});
|
|
79
84
|
});
|
package/src/client/index.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { AssetValue, SwapKitError } from "@swapkit/helpers";
|
|
2
2
|
import { Chain } from "@swapkit/types";
|
|
3
|
+
import {
|
|
4
|
+
getExplorerAddressUrl as getAddressUrl,
|
|
5
|
+
getExplorerTxUrl as getTxUrl,
|
|
6
|
+
} from "../helpers/explorerUrls.ts";
|
|
3
7
|
import type {
|
|
4
8
|
ChainWallet,
|
|
5
9
|
ConnectWalletParamsLocal as ConnectWalletParams,
|
|
6
10
|
SwapWithRouteParams,
|
|
7
11
|
} from "../types.ts";
|
|
8
12
|
|
|
9
|
-
type ProviderName = "thorchain" | "chainflip" | "mayachain";
|
|
10
|
-
enum ApproveMode {
|
|
13
|
+
export type ProviderName = "thorchain" | "chainflip" | "mayachain";
|
|
14
|
+
export enum ApproveMode {
|
|
11
15
|
Approve = "approve",
|
|
12
16
|
CheckOnly = "checkOnly",
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
type ApproveReturnType<T extends ApproveMode> = T extends "checkOnly"
|
|
19
|
+
export type ApproveReturnType<T extends ApproveMode> = T extends "checkOnly"
|
|
16
20
|
? Promise<boolean>
|
|
17
21
|
: Promise<string>;
|
|
18
22
|
|
|
@@ -33,7 +37,7 @@ export type SwapParams = (SwapWithRouteParams | GenericSwapParams) & {
|
|
|
33
37
|
};
|
|
34
38
|
};
|
|
35
39
|
|
|
36
|
-
type SwapKitReturnType = SwapKitProviders & {
|
|
40
|
+
export type SwapKitReturnType = SwapKitProviders & {
|
|
37
41
|
getAddress: (chain: Chain) => string;
|
|
38
42
|
getWallet: (chain: Chain) => ChainWallet<Chain> | undefined;
|
|
39
43
|
getWalletWithBalance: (
|
|
@@ -41,6 +45,8 @@ type SwapKitReturnType = SwapKitProviders & {
|
|
|
41
45
|
potentialScamFilter?: boolean,
|
|
42
46
|
) => Promise<ChainWallet<Chain>>;
|
|
43
47
|
getBalance: (chain: Chain, potentialScamFilter?: boolean) => AssetValue[];
|
|
48
|
+
getExplorerTxUrl: (chain: Chain, txHash: string) => string;
|
|
49
|
+
getExplorerAddressUrl: (chain: Chain, address: string) => string;
|
|
44
50
|
swap: (params: SwapParams) => Promise<string>;
|
|
45
51
|
validateAddress: (params: { address: string; chain: Chain }) =>
|
|
46
52
|
| boolean
|
|
@@ -53,9 +59,9 @@ type SwapKitReturnType = SwapKitProviders & {
|
|
|
53
59
|
) => boolean | Promise<boolean>;
|
|
54
60
|
};
|
|
55
61
|
|
|
56
|
-
type Wallets = { [K in Chain]?: ChainWallet<K> };
|
|
57
|
-
type AvailableProviders<T> = T | { [K in ProviderName]?: ProviderMethods };
|
|
58
|
-
type ProviderMethods = {
|
|
62
|
+
export type Wallets = { [K in Chain]?: ChainWallet<K> };
|
|
63
|
+
export type AvailableProviders<T> = T | { [K in ProviderName]?: ProviderMethods };
|
|
64
|
+
export type ProviderMethods = {
|
|
59
65
|
swap: (swapParams: SwapParams) => Promise<string>;
|
|
60
66
|
[key: string]: any;
|
|
61
67
|
};
|
|
@@ -65,20 +71,9 @@ export type SwapKitProvider = ({ wallets, stagenet }: { wallets: Wallets; stagen
|
|
|
65
71
|
methods: ProviderMethods;
|
|
66
72
|
};
|
|
67
73
|
|
|
68
|
-
type
|
|
69
|
-
chains: Chain[];
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
type KeystoreWalletConnectParams = BaseSwapkitWalletConnectParams & {
|
|
73
|
-
phrase: string;
|
|
74
|
-
index?: number;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
type WalletConnectParams = KeystoreWalletConnectParams;
|
|
78
|
-
|
|
79
|
-
type SwapKitWallet = {
|
|
74
|
+
export type SwapKitWallet = {
|
|
80
75
|
connectMethodName: string;
|
|
81
|
-
connect: (params: ConnectWalletParams) => (connectParams:
|
|
76
|
+
connect: (params: ConnectWalletParams) => (connectParams: any) => void;
|
|
82
77
|
};
|
|
83
78
|
|
|
84
79
|
export function SwapKit<
|
|
@@ -98,7 +93,7 @@ export function SwapKit<
|
|
|
98
93
|
config?: Record<string, any>;
|
|
99
94
|
apis: Record<string, any>;
|
|
100
95
|
rpcUrls: Record<string, any>;
|
|
101
|
-
}): SwapKitReturnType & ConnectWalletMethods {
|
|
96
|
+
}): SwapKitReturnType & ConnectWalletMethods & AvailableProviders<ExtendedProviders> {
|
|
102
97
|
const connectedWallets: Wallets = {};
|
|
103
98
|
const availableProviders: AvailableProviders<ExtendedProviders> = {};
|
|
104
99
|
|
|
@@ -197,6 +192,12 @@ export function SwapKit<
|
|
|
197
192
|
function getBalance(chain: Chain) {
|
|
198
193
|
return getWallet(chain)?.balance || [];
|
|
199
194
|
}
|
|
195
|
+
function getExplorerTxUrl(chain: Chain, txHash: string) {
|
|
196
|
+
return getTxUrl({ chain, txHash });
|
|
197
|
+
}
|
|
198
|
+
function getExplorerAddressUrl(chain: Chain, address: string) {
|
|
199
|
+
return getAddressUrl({ chain, address });
|
|
200
|
+
}
|
|
200
201
|
/**
|
|
201
202
|
* TODO: Figure out validation without connecting to wallet
|
|
202
203
|
*/
|
|
@@ -244,6 +245,8 @@ export function SwapKit<
|
|
|
244
245
|
approveAssetValue,
|
|
245
246
|
getAddress,
|
|
246
247
|
getBalance,
|
|
248
|
+
getExplorerAddressUrl,
|
|
249
|
+
getExplorerTxUrl,
|
|
247
250
|
getWallet,
|
|
248
251
|
getWalletWithBalance,
|
|
249
252
|
isAssetValueApproved,
|
|
@@ -15,6 +15,7 @@ export const getExplorerTxUrl = ({ chain, txHash }: { txHash: string; chain: Cha
|
|
|
15
15
|
case Chain.BinanceSmartChain:
|
|
16
16
|
case Chain.Ethereum:
|
|
17
17
|
case Chain.Optimism:
|
|
18
|
+
case Chain.Polkadot:
|
|
18
19
|
case Chain.Polygon:
|
|
19
20
|
return `${baseUrl}/tx/${txHash.startsWith("0x") ? txHash : `0x${txHash}`}`;
|
|
20
21
|
|
|
@@ -48,6 +49,7 @@ export const getExplorerAddressUrl = ({ chain, address }: { address: string; cha
|
|
|
48
49
|
case Chain.Litecoin:
|
|
49
50
|
case Chain.Maya:
|
|
50
51
|
case Chain.Optimism:
|
|
52
|
+
case Chain.Polkadot:
|
|
51
53
|
case Chain.Polygon:
|
|
52
54
|
case Chain.THORChain:
|
|
53
55
|
return `${baseUrl}/address/${address}`;
|