@swapkit/helpers 1.1.0 → 1.1.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/index.js +3 -3
- package/dist/index.js.map +10 -10
- package/package.json +10 -2
- package/src/helpers/__tests__/derivationPath.ts +16 -0
- package/src/helpers/__tests__/memo.test.ts +16 -0
- package/src/helpers/__tests__/others.test.ts +2 -14
- package/src/helpers/asset.ts +4 -0
- package/src/helpers/derivationPath.ts +24 -14
- package/src/helpers/memo.ts +18 -0
- package/src/helpers/others.ts +0 -9
- package/src/helpers/web3wallets.ts +0 -9
- package/src/types/commonTypes.ts +3 -12
- package/src/types/sdk.ts +18 -1
- package/src/types/wallet.ts +31 -4
package/package.json
CHANGED
|
@@ -2,11 +2,19 @@
|
|
|
2
2
|
"author": "swapkit-oss",
|
|
3
3
|
"description": "SwapKit - Helpers",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@swapkit/tokens": "1.0.
|
|
5
|
+
"@swapkit/tokens": "1.0.2",
|
|
6
6
|
"ky": "1.4.0",
|
|
7
7
|
"picocolors": "1.0.1",
|
|
8
8
|
"zod": "3.23.8"
|
|
9
9
|
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@swapkit/toolbox-cosmos": "1.0.6",
|
|
12
|
+
"@swapkit/toolbox-evm": "1.1.1",
|
|
13
|
+
"@swapkit/toolbox-solana": "1.0.6",
|
|
14
|
+
"@swapkit/toolbox-radix": "1.0.6",
|
|
15
|
+
"@swapkit/toolbox-substrate": "1.1.1",
|
|
16
|
+
"@swapkit/toolbox-utxo": "1.0.6"
|
|
17
|
+
},
|
|
10
18
|
"files": [
|
|
11
19
|
"src/",
|
|
12
20
|
"dist/"
|
|
@@ -30,5 +38,5 @@
|
|
|
30
38
|
},
|
|
31
39
|
"type": "module",
|
|
32
40
|
"types": "./src/index.ts",
|
|
33
|
-
"version": "1.1.
|
|
41
|
+
"version": "1.1.1"
|
|
34
42
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import type { DerivationPathArray } from "../../types";
|
|
4
|
+
import { derivationPathToString } from "../derivationPath";
|
|
5
|
+
|
|
6
|
+
describe("derivationPathToString", () => {
|
|
7
|
+
test("should return the correct string for a full path", () => {
|
|
8
|
+
const path = [1, 2, 3, 4, 5] as DerivationPathArray;
|
|
9
|
+
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4/5");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("should return the correct string for a short path", () => {
|
|
13
|
+
const path = [1, 2, 3, 4] as DerivationPathArray;
|
|
14
|
+
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4");
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -4,6 +4,7 @@ import { Chain, MemoType } from "../../types";
|
|
|
4
4
|
import {
|
|
5
5
|
getMemoForDeposit,
|
|
6
6
|
getMemoForLeaveAndBond,
|
|
7
|
+
getMemoForNamePreferredAssetRegister,
|
|
7
8
|
getMemoForNameRegister,
|
|
8
9
|
getMemoForSaverDeposit,
|
|
9
10
|
getMemoForSaverWithdraw,
|
|
@@ -52,6 +53,21 @@ describe("getMemoForNameRegister", () => {
|
|
|
52
53
|
});
|
|
53
54
|
});
|
|
54
55
|
|
|
56
|
+
describe("getMemoForNamePreferredAssetRegister", () => {
|
|
57
|
+
test("returns correct memo for single side", () => {
|
|
58
|
+
const result = getMemoForNamePreferredAssetRegister({
|
|
59
|
+
name: "asdfg",
|
|
60
|
+
chain: Chain.Ethereum,
|
|
61
|
+
owner: "thor1234",
|
|
62
|
+
asset: "ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48",
|
|
63
|
+
payout: "0x6621d872f17109d6601c49edba526ebcfd332d5d",
|
|
64
|
+
});
|
|
65
|
+
expect(result).toBe(
|
|
66
|
+
"~:asdfg:ETH:0x6621d872f17109d6601c49edba526ebcfd332d5d:thor1234:ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48",
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
55
71
|
describe("getMemoForDeposit", () => {
|
|
56
72
|
test("returns correct memo for single side", () => {
|
|
57
73
|
const result = getMemoForDeposit({ chain: Chain.Ethereum, symbol: "ETH" });
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { Chain
|
|
2
|
+
import { Chain } from "../../types";
|
|
3
3
|
|
|
4
4
|
import { findAssetBy } from "../asset.ts";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
describe("derivationPathToString", () => {
|
|
8
|
-
test("should return the correct string for a full path", () => {
|
|
9
|
-
const path = [1, 2, 3, 4, 5] as DerivationPathArray;
|
|
10
|
-
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4/5");
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("should return the correct string for a short path", () => {
|
|
14
|
-
const path = [1, 2, 3, 4] as DerivationPathArray;
|
|
15
|
-
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4");
|
|
16
|
-
});
|
|
17
|
-
});
|
|
5
|
+
import { getTHORNameCost } from "../others.ts";
|
|
18
6
|
|
|
19
7
|
describe("getTHORNameCost", () => {
|
|
20
8
|
describe("for correct values", () => {
|
package/src/helpers/asset.ts
CHANGED
|
@@ -8,6 +8,10 @@ const getDecimalMethodHex = "0x313ce567";
|
|
|
8
8
|
|
|
9
9
|
export type CommonAssetString = (typeof CommonAssetStrings)[number] | Chain;
|
|
10
10
|
|
|
11
|
+
export type ConditionalAssetValueReturn<T extends boolean> = T extends true
|
|
12
|
+
? Promise<AssetValue[]>
|
|
13
|
+
: AssetValue[];
|
|
14
|
+
|
|
11
15
|
export const CommonAssetStrings = [
|
|
12
16
|
`${Chain.Maya}.MAYA`,
|
|
13
17
|
`${Chain.Ethereum}.THOR`,
|
|
@@ -13,28 +13,38 @@ type Params = {
|
|
|
13
13
|
type?: "legacy" | "ledgerLive" | "nativeSegwitMiddleAccount" | "segwit";
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
const updatedLastIndex = (path: DerivationPathArray, index: number) =>
|
|
17
|
-
...path.slice(0, path.length - 1),
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const updatedLastIndex = (path: DerivationPathArray, index: number) => {
|
|
17
|
+
const newPath = [...path.slice(0, path.length - 1), index];
|
|
18
|
+
return newPath as DerivationPathArray;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function derivationPathToString([network, chainId, account, change, index]:
|
|
22
|
+
| [number, number, number, number, number | undefined]
|
|
23
|
+
| DerivationPathArray) {
|
|
24
|
+
const shortPath = typeof index !== "number";
|
|
25
|
+
|
|
26
|
+
return `m/${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
|
|
27
|
+
}
|
|
20
28
|
|
|
21
29
|
export function getDerivationPathFor({ chain, index, addressIndex = 0, type }: Params) {
|
|
22
30
|
if (EVMChains.includes(chain as EVMChain)) {
|
|
23
|
-
if (type === "legacy") return [44, 60, 0, index];
|
|
24
|
-
if (type === "ledgerLive") return [44, 60, index, 0, addressIndex];
|
|
31
|
+
if (type === "legacy") return [44, 60, 0, index] as DerivationPathArray;
|
|
32
|
+
if (type === "ledgerLive") return [44, 60, index, 0, addressIndex] as DerivationPathArray;
|
|
25
33
|
return updatedLastIndex(NetworkDerivationPath[chain], index);
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
const chainId = chain === Chain.Bitcoin ? 0 : 2;
|
|
36
|
+
const chainId = chain === Chain.Litecoin ? 2 : 0;
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
switch (type) {
|
|
39
|
+
case "nativeSegwitMiddleAccount":
|
|
40
|
+
return [84, chainId, index, 0, addressIndex] as DerivationPathArray;
|
|
41
|
+
case "segwit":
|
|
42
|
+
return [49, chainId, 0, 0, index] as DerivationPathArray;
|
|
43
|
+
case "legacy":
|
|
44
|
+
return [44, chainId, 0, 0, index] as DerivationPathArray;
|
|
45
|
+
default:
|
|
46
|
+
return updatedLastIndex(NetworkDerivationPath[chain], index);
|
|
35
47
|
}
|
|
36
|
-
|
|
37
|
-
return updatedLastIndex(NetworkDerivationPath[chain], index);
|
|
38
48
|
}
|
|
39
49
|
|
|
40
50
|
export function getWalletFormatFor(path: string) {
|
package/src/helpers/memo.ts
CHANGED
|
@@ -72,6 +72,24 @@ export function getMemoForNameRegister({
|
|
|
72
72
|
return `${baseMemo}${ownerAssignmentOrChangePart}`;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
export function getMemoForNamePreferredAssetRegister({
|
|
76
|
+
name,
|
|
77
|
+
chain,
|
|
78
|
+
asset,
|
|
79
|
+
payout,
|
|
80
|
+
owner,
|
|
81
|
+
}: {
|
|
82
|
+
name: string;
|
|
83
|
+
chain: Chain;
|
|
84
|
+
asset: string;
|
|
85
|
+
payout: string;
|
|
86
|
+
owner: string;
|
|
87
|
+
}) {
|
|
88
|
+
const memo = [name, chain, payout, owner, asset].join(":");
|
|
89
|
+
|
|
90
|
+
return `${MemoType.NAME_REGISTER}:${memo}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
75
93
|
export function getMemoForLoan(
|
|
76
94
|
memoType: MemoType.OPEN_LOAN | MemoType.CLOSE_LOAN,
|
|
77
95
|
{
|
package/src/helpers/others.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type ErrorKeys, SwapKitError } from "../modules/swapKitError";
|
|
2
2
|
import { Chain } from "../types";
|
|
3
|
-
import type { DerivationPathArray } from "../types/derivationPath";
|
|
4
3
|
|
|
5
4
|
// 10 rune for register, 1 rune per year
|
|
6
5
|
// MINIMUM_REGISTRATION_FEE = 11
|
|
@@ -25,14 +24,6 @@ export function getMAYANameCost(numberOfYears: number) {
|
|
|
25
24
|
return Math.round((10 + numberOfYears * 1.0512) * 1e10) / 1e10;
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
export function derivationPathToString([network, chainId, account, change, index]:
|
|
29
|
-
| [number, number, number, number, number | undefined]
|
|
30
|
-
| DerivationPathArray) {
|
|
31
|
-
const shortPath = typeof index !== "number";
|
|
32
|
-
|
|
33
|
-
return `m/${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
27
|
export function wrapWithThrow<T>(fn: () => T, errorKey?: ErrorKeys) {
|
|
37
28
|
try {
|
|
38
29
|
return fn();
|
|
@@ -28,15 +28,6 @@ declare const window: {
|
|
|
28
28
|
braveSolana: Todo;
|
|
29
29
|
} & Window;
|
|
30
30
|
|
|
31
|
-
// declare global {
|
|
32
|
-
// interface Window {
|
|
33
|
-
// ethereum: EthereumWindowProvider;
|
|
34
|
-
// trustwallet: EthereumWindowProvider;
|
|
35
|
-
// coinbaseWalletExtension: EthereumWindowProvider;
|
|
36
|
-
// braveSolana: Todo;
|
|
37
|
-
// }
|
|
38
|
-
// }
|
|
39
|
-
|
|
40
31
|
type NetworkParams = {
|
|
41
32
|
chainId: ChainId;
|
|
42
33
|
chainName: string;
|
package/src/types/commonTypes.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { AssetValue } from "../modules/assetValue.ts";
|
|
2
1
|
import type { Chain, CosmosChain, EVMChain, UTXOChain } from "./chains.ts";
|
|
3
|
-
import type {
|
|
2
|
+
import type { ChainWallet } from "./wallet.ts";
|
|
4
3
|
|
|
5
4
|
export type ConnectConfig = {
|
|
6
5
|
stagenet?: boolean;
|
|
@@ -49,22 +48,14 @@ export type ConnectConfig = {
|
|
|
49
48
|
chainflipBrokerUrl?: string;
|
|
50
49
|
};
|
|
51
50
|
|
|
52
|
-
export type AddChainWalletParams<T extends Chain> = {
|
|
53
|
-
address: string;
|
|
54
|
-
balance: AssetValue[];
|
|
55
|
-
walletType: WalletOption;
|
|
56
|
-
chain: T;
|
|
57
|
-
[key: string]: Todo;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
51
|
type ApisType = { [key in UTXOChain]?: string | Todo } & {
|
|
61
52
|
[key in EVMChain]?: string | Todo;
|
|
62
53
|
} & {
|
|
63
54
|
[key in CosmosChain]?: string;
|
|
64
55
|
};
|
|
65
56
|
|
|
66
|
-
export type ConnectWalletParams = {
|
|
67
|
-
addChain: <T extends Chain>(params:
|
|
57
|
+
export type ConnectWalletParams<M = { [key in string]: NotWorth }> = {
|
|
58
|
+
addChain: <T extends Chain>(params: ChainWallet<T> & M) => void;
|
|
68
59
|
apis: ApisType;
|
|
69
60
|
config: ConnectConfig;
|
|
70
61
|
rpcUrls: { [chain in Chain]?: string };
|
package/src/types/sdk.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
+
import type { CovalentApiType, EthplorerApiType } from "@swapkit/toolbox-evm";
|
|
2
|
+
import type { BlockchairApiType } from "@swapkit/toolbox-utxo";
|
|
1
3
|
import { z } from "zod";
|
|
4
|
+
|
|
2
5
|
import type { AssetValue } from "../modules/assetValue";
|
|
6
|
+
import type { Chain, CosmosChain, UTXOChain } from "./chains";
|
|
3
7
|
import type { QuoteResponseRoute } from "./quotes";
|
|
4
8
|
|
|
9
|
+
type CovalentChains =
|
|
10
|
+
| Chain.BinanceSmartChain
|
|
11
|
+
| Chain.Polygon
|
|
12
|
+
| Chain.Avalanche
|
|
13
|
+
| Chain.Arbitrum
|
|
14
|
+
| Chain.Optimism;
|
|
15
|
+
|
|
16
|
+
export type ChainApis = { [key in CovalentChains]?: CovalentApiType } & {
|
|
17
|
+
[key in Chain.Ethereum]?: EthplorerApiType;
|
|
18
|
+
} & { [key in CosmosChain]?: Todo } & {
|
|
19
|
+
[key in UTXOChain]?: BlockchairApiType;
|
|
20
|
+
};
|
|
21
|
+
|
|
5
22
|
export type GenericSwapParams = {
|
|
6
23
|
buyAsset?: AssetValue;
|
|
7
24
|
sellAsset?: AssetValue;
|
|
@@ -37,10 +54,10 @@ export type WalletTxParams = {
|
|
|
37
54
|
};
|
|
38
55
|
|
|
39
56
|
export enum MemoType {
|
|
57
|
+
NAME_REGISTER = "~",
|
|
40
58
|
BOND = "BOND",
|
|
41
59
|
DEPOSIT = "+",
|
|
42
60
|
LEAVE = "LEAVE",
|
|
43
|
-
NAME_REGISTER = "~",
|
|
44
61
|
UNBOND = "UNBOND",
|
|
45
62
|
WITHDRAW = "-",
|
|
46
63
|
OPEN_LOAN = "$+",
|
package/src/types/wallet.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
+
import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
|
|
2
|
+
import type { EVMWallets } from "@swapkit/toolbox-evm";
|
|
3
|
+
import type { SolanaWallet } from "@swapkit/toolbox-solana";
|
|
4
|
+
import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
|
|
5
|
+
import type { UTXOWallets } from "@swapkit/toolbox-utxo";
|
|
1
6
|
import type { Eip1193Provider } from "ethers";
|
|
7
|
+
|
|
2
8
|
import type { AssetValue } from "../modules/assetValue";
|
|
3
9
|
import type { Chain } from "./chains";
|
|
10
|
+
import type { ConnectWalletParams } from "./commonTypes";
|
|
4
11
|
|
|
5
12
|
declare global {
|
|
6
13
|
interface WindowEventMap {
|
|
@@ -8,6 +15,8 @@ declare global {
|
|
|
8
15
|
}
|
|
9
16
|
}
|
|
10
17
|
|
|
18
|
+
export type { CosmosWallets, ThorchainWallets, EVMWallets, SubstrateWallets, UTXOWallets };
|
|
19
|
+
|
|
11
20
|
export enum WalletOption {
|
|
12
21
|
BRAVE = "BRAVE",
|
|
13
22
|
COINBASE_MOBILE = "COINBASE_MOBILE",
|
|
@@ -37,8 +46,8 @@ export enum LedgerErrorCode {
|
|
|
37
46
|
TC_NotFound = 65535,
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
export type ChainWallet = {
|
|
41
|
-
chain:
|
|
49
|
+
export type ChainWallet<T extends Chain> = {
|
|
50
|
+
chain: T;
|
|
42
51
|
address: string;
|
|
43
52
|
balance: AssetValue[];
|
|
44
53
|
walletType: WalletOption;
|
|
@@ -48,8 +57,26 @@ export type ChainWallet = {
|
|
|
48
57
|
|
|
49
58
|
export type EmptyWallet = { [key in Chain]?: unknown };
|
|
50
59
|
export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
|
|
51
|
-
[key in Chain]: ChainWallet &
|
|
52
|
-
|
|
60
|
+
[key in Chain]: ChainWallet<key> & (T extends EmptyWallet ? T[key] : never);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type FullWallet = BaseWallet<
|
|
64
|
+
EVMWallets & UTXOWallets & CosmosWallets & ThorchainWallets & SubstrateWallets & SolanaWallet
|
|
65
|
+
>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @deprecated use FullWallet instead
|
|
69
|
+
*/
|
|
70
|
+
export type Wallet = FullWallet;
|
|
71
|
+
|
|
72
|
+
export type SwapKitWallet<ConnectParams extends Todo[]> = (
|
|
73
|
+
params: ConnectWalletParams,
|
|
74
|
+
) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;
|
|
75
|
+
|
|
76
|
+
export type SwapKitPluginParams<Config = {}> = {
|
|
77
|
+
getWallet: <T extends Chain>(chain: T) => FullWallet[T];
|
|
78
|
+
stagenet?: boolean;
|
|
79
|
+
config: Config;
|
|
53
80
|
};
|
|
54
81
|
|
|
55
82
|
export type EIP6963ProviderInfo = {
|