@swapkit/toolboxes 1.0.0-beta.2 → 1.0.0-beta.20
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/chunk-12xtvbsp.js +4 -0
- package/dist/{chunk-0h4xdrwz.js.map → chunk-12xtvbsp.js.map} +2 -2
- package/dist/{chunk-fjfxga2v.js → chunk-5yxc1e69.js} +1 -1
- package/dist/{chunk-fjfxga2v.js.map → chunk-5yxc1e69.js.map} +1 -1
- package/dist/{chunk-0f0249b1.js → chunk-9bqegm61.js} +1 -1
- package/dist/chunk-kbnwrc5b.js +4 -0
- package/dist/chunk-kbnwrc5b.js.map +10 -0
- package/dist/{chunk-p1kdg37m.js → chunk-s47y8512.js} +2 -2
- package/dist/{chunk-p1kdg37m.js.map → chunk-s47y8512.js.map} +1 -1
- package/dist/chunk-vtd17cje.js +3 -0
- package/dist/chunk-vtd17cje.js.map +10 -0
- package/dist/src/cosmos/index.cjs +3 -0
- package/dist/src/cosmos/index.cjs.map +16 -0
- package/dist/src/cosmos/index.js +3 -0
- package/dist/src/cosmos/index.js.map +16 -0
- package/dist/src/evm/index.cjs +3 -0
- package/dist/src/evm/index.cjs.map +18 -0
- package/dist/src/evm/index.js +3 -0
- package/dist/src/evm/index.js.map +18 -0
- package/dist/src/index.cjs +3 -0
- package/dist/src/index.cjs.map +10 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +10 -0
- package/dist/src/near/index.cjs +3 -0
- package/dist/src/near/index.cjs.map +13 -0
- package/dist/src/near/index.js +3 -0
- package/dist/src/near/index.js.map +13 -0
- package/dist/{radix → src/radix}/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +10 -0
- package/dist/src/radix/index.js +3 -0
- package/dist/src/radix/index.js.map +10 -0
- package/dist/src/ripple/index.cjs +3 -0
- package/dist/src/ripple/index.cjs.map +10 -0
- package/dist/src/ripple/index.js +3 -0
- package/dist/src/ripple/index.js.map +10 -0
- package/dist/src/solana/index.cjs +3 -0
- package/dist/src/solana/index.cjs.map +10 -0
- package/dist/src/solana/index.js +3 -0
- package/dist/src/solana/index.js.map +10 -0
- package/dist/src/substrate/index.cjs +3 -0
- package/dist/src/substrate/index.cjs.map +11 -0
- package/dist/src/substrate/index.js +3 -0
- package/dist/src/substrate/index.js.map +11 -0
- package/dist/src/tron/index.cjs +3 -0
- package/dist/src/tron/index.cjs.map +11 -0
- package/dist/src/tron/index.js +3 -0
- package/dist/src/tron/index.js.map +11 -0
- package/dist/src/utxo/index.cjs +3 -0
- package/dist/src/utxo/index.cjs.map +16 -0
- package/dist/src/utxo/index.js +3 -0
- package/dist/src/utxo/index.js.map +16 -0
- package/package.json +49 -37
- package/src/cosmos/thorchainUtils/addressFormat.ts +4 -1
- package/src/cosmos/thorchainUtils/messages.ts +2 -2
- package/src/cosmos/thorchainUtils/registry.ts +3 -3
- package/src/cosmos/toolbox/cosmos.ts +16 -11
- package/src/cosmos/toolbox/index.ts +2 -2
- package/src/cosmos/toolbox/thorchain.ts +11 -9
- package/src/cosmos/util.ts +13 -6
- package/src/evm/__tests__/ethereum.test.ts +1 -1
- package/src/evm/helpers.ts +4 -3
- package/src/evm/toolbox/baseEVMToolbox.ts +37 -25
- package/src/evm/toolbox/index.ts +2 -2
- package/src/evm/toolbox/op.ts +21 -7
- package/src/index.ts +117 -100
- package/src/near/helpers/contractFactory.ts +22 -0
- package/src/near/helpers/core.ts +86 -0
- package/src/near/helpers/gasEstimation.ts +110 -0
- package/src/near/helpers/index.ts +5 -0
- package/src/near/helpers/nep141.ts +110 -0
- package/src/near/index.ts +24 -0
- package/src/near/toolbox.ts +497 -0
- package/src/near/types/contract.ts +48 -0
- package/src/near/types/nep141.ts +66 -0
- package/src/near/types.ts +58 -0
- package/src/radix/index.ts +8 -2
- package/src/ripple/index.ts +15 -26
- package/src/solana/toolbox.ts +75 -2
- package/src/substrate/substrate.ts +1 -1
- package/src/tron/helpers/trc20.abi.ts +40 -0
- package/src/tron/index.ts +16 -0
- package/src/tron/toolbox.ts +328 -0
- package/src/tron/types.ts +22 -0
- package/src/utxo/helpers/api.ts +30 -16
- package/src/utxo/helpers/bchaddrjs.ts +8 -8
- package/src/utxo/helpers/coinselect.ts +2 -2
- package/src/utxo/helpers/txSize.ts +4 -3
- package/src/utxo/toolbox/bitcoinCash.ts +22 -14
- package/src/utxo/toolbox/index.ts +2 -1
- package/src/utxo/toolbox/utxo.ts +35 -27
- package/src/utxo/types.ts +2 -0
- package/dist/chunk-0h4xdrwz.js +0 -4
- package/dist/cosmos/index.cjs +0 -3
- package/dist/cosmos/index.cjs.map +0 -16
- package/dist/cosmos/index.js +0 -3
- package/dist/cosmos/index.js.map +0 -16
- package/dist/evm/index.cjs +0 -3
- package/dist/evm/index.cjs.map +0 -18
- package/dist/evm/index.js +0 -3
- package/dist/evm/index.js.map +0 -18
- package/dist/index.cjs +0 -3
- package/dist/index.cjs.map +0 -10
- package/dist/index.js +0 -3
- package/dist/index.js.map +0 -10
- package/dist/radix/index.cjs.map +0 -10
- package/dist/radix/index.js +0 -3
- package/dist/radix/index.js.map +0 -10
- package/dist/ripple/index.cjs +0 -3
- package/dist/ripple/index.cjs.map +0 -10
- package/dist/ripple/index.js +0 -3
- package/dist/ripple/index.js.map +0 -10
- package/dist/solana/index.cjs +0 -3
- package/dist/solana/index.cjs.map +0 -10
- package/dist/solana/index.js +0 -3
- package/dist/solana/index.js.map +0 -10
- package/dist/substrate/index.cjs +0 -3
- package/dist/substrate/index.cjs.map +0 -11
- package/dist/substrate/index.js +0 -3
- package/dist/substrate/index.js.map +0 -11
- package/dist/utxo/index.cjs +0 -3
- package/dist/utxo/index.cjs.map +0 -16
- package/dist/utxo/index.js +0 -3
- package/dist/utxo/index.js.map +0 -16
- /package/dist/{chunk-0f0249b1.js.map → chunk-9bqegm61.js.map} +0 -0
package/src/index.ts
CHANGED
|
@@ -6,15 +6,18 @@ import {
|
|
|
6
6
|
FeeOption,
|
|
7
7
|
type GenericCreateTransactionParams,
|
|
8
8
|
type SubstrateChain,
|
|
9
|
+
SwapKitError,
|
|
9
10
|
type UTXOChain,
|
|
10
11
|
} from "@swapkit/helpers";
|
|
11
12
|
import type { getCosmosToolbox } from "@swapkit/toolboxes/cosmos";
|
|
12
13
|
|
|
13
14
|
import type { ETHToolbox, EVMCreateTransactionParams, getEvmToolbox } from "@swapkit/toolboxes/evm";
|
|
15
|
+
import type { getNearToolbox } from "@swapkit/toolboxes/near";
|
|
14
16
|
import type { RadixToolbox } from "@swapkit/toolboxes/radix";
|
|
15
17
|
import type { getRippleToolbox } from "@swapkit/toolboxes/ripple";
|
|
16
18
|
import type { SolanaCreateTransactionParams, getSolanaToolbox } from "@swapkit/toolboxes/solana";
|
|
17
19
|
import type { getSubstrateToolbox } from "@swapkit/toolboxes/substrate";
|
|
20
|
+
import type { createTronToolbox } from "@swapkit/toolboxes/tron";
|
|
18
21
|
import type { getUtxoToolbox } from "@swapkit/toolboxes/utxo";
|
|
19
22
|
|
|
20
23
|
export async function getAddressValidator() {
|
|
@@ -24,11 +27,14 @@ export async function getAddressValidator() {
|
|
|
24
27
|
const { substrateValidateAddress } = await import("@swapkit/toolboxes/substrate");
|
|
25
28
|
const { getUTXOAddressValidator } = await import("@swapkit/toolboxes/utxo");
|
|
26
29
|
const { getSolanaAddressValidator } = await import("@swapkit/toolboxes/solana");
|
|
30
|
+
const { validateNearAddress } = await import("@swapkit/toolboxes/near");
|
|
27
31
|
const { rippleValidateAddress } = await import("@swapkit/toolboxes/ripple");
|
|
28
32
|
const { radixValidateAddress } = await import("@swapkit/toolboxes/radix");
|
|
33
|
+
const { getTronAddressValidator } = await import("@swapkit/toolboxes/tron");
|
|
29
34
|
|
|
30
35
|
const solanaValidateAddress = await getSolanaAddressValidator();
|
|
31
36
|
const utxoValidateAddress = await getUTXOAddressValidator();
|
|
37
|
+
const tronValidateAddress = await getTronAddressValidator();
|
|
32
38
|
|
|
33
39
|
return function validateAddress({ address, chain }: { address: string; chain: Chain }) {
|
|
34
40
|
const isValid = match(chain)
|
|
@@ -52,68 +58,62 @@ export async function getAddressValidator() {
|
|
|
52
58
|
substrateValidateAddress({ address, chain: chain as SubstrateChain }),
|
|
53
59
|
)
|
|
54
60
|
.with(Chain.Radix, () => radixValidateAddress(address))
|
|
61
|
+
.with(Chain.Near, () => validateNearAddress(address))
|
|
55
62
|
.with(Chain.Ripple, () => rippleValidateAddress(address))
|
|
56
63
|
.with(Chain.Solana, () => solanaValidateAddress(address))
|
|
64
|
+
.with(Chain.Tron, () => tronValidateAddress(address))
|
|
57
65
|
.otherwise(() => false);
|
|
58
66
|
|
|
59
67
|
return isValid;
|
|
60
68
|
};
|
|
61
69
|
}
|
|
62
70
|
|
|
63
|
-
export
|
|
64
|
-
const toolbox = await getToolbox(chain);
|
|
65
|
-
|
|
71
|
+
export function getFeeEstimator<T extends keyof CreateTransactionParams>(chain: T) {
|
|
66
72
|
return async function estimateFee(params: CreateTransactionParams[T]) {
|
|
67
|
-
|
|
68
|
-
case Chain.Arbitrum:
|
|
69
|
-
case Chain.Avalanche:
|
|
70
|
-
case Chain.Optimism:
|
|
71
|
-
case Chain.BinanceSmartChain:
|
|
72
|
-
case Chain.Base:
|
|
73
|
-
case Chain.Polygon:
|
|
74
|
-
case Chain.Ethereum: {
|
|
75
|
-
const txObject = await (
|
|
76
|
-
toolbox as Awaited<ReturnType<typeof ETHToolbox>>
|
|
77
|
-
).createTransaction(params as EVMCreateTransactionParams);
|
|
78
|
-
return (toolbox as Awaited<ReturnType<typeof ETHToolbox>>).estimateTransactionFee({
|
|
79
|
-
...txObject,
|
|
80
|
-
feeOption: params.feeOptionKey || FeeOption.Fast,
|
|
81
|
-
chain,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
case Chain.Bitcoin:
|
|
85
|
-
case Chain.BitcoinCash:
|
|
86
|
-
case Chain.Dogecoin:
|
|
87
|
-
case Chain.Dash:
|
|
88
|
-
case Chain.Litecoin: {
|
|
89
|
-
return (toolbox as Awaited<ReturnType<typeof getUtxoToolbox>>).estimateTransactionFee(
|
|
90
|
-
params as CreateTransactionParams[Chain.Bitcoin],
|
|
91
|
-
);
|
|
92
|
-
}
|
|
73
|
+
const { match } = await import("ts-pattern");
|
|
93
74
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
75
|
+
return match(chain as Chain)
|
|
76
|
+
.returnType<Promise<AssetValue>>()
|
|
77
|
+
.with(
|
|
78
|
+
Chain.Arbitrum,
|
|
79
|
+
Chain.Avalanche,
|
|
80
|
+
Chain.Optimism,
|
|
81
|
+
Chain.BinanceSmartChain,
|
|
82
|
+
Chain.Base,
|
|
83
|
+
Chain.Polygon,
|
|
84
|
+
Chain.Ethereum,
|
|
85
|
+
async (chain) => {
|
|
86
|
+
const toolbox = await getToolbox(chain);
|
|
87
|
+
const txObject = await toolbox.createTransaction(params);
|
|
88
|
+
|
|
89
|
+
return (toolbox as Awaited<ReturnType<typeof ETHToolbox>>).estimateTransactionFee({
|
|
90
|
+
...txObject,
|
|
91
|
+
feeOption: params.feeOptionKey || FeeOption.Fast,
|
|
92
|
+
chain,
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
)
|
|
96
|
+
.with(
|
|
97
|
+
Chain.Bitcoin,
|
|
98
|
+
Chain.BitcoinCash,
|
|
99
|
+
Chain.Dogecoin,
|
|
100
|
+
Chain.Dash,
|
|
101
|
+
Chain.Litecoin,
|
|
102
|
+
Chain.Polkadot,
|
|
103
|
+
Chain.Solana,
|
|
104
|
+
Chain.Ripple,
|
|
105
|
+
Chain.Tron,
|
|
106
|
+
Chain.Near,
|
|
107
|
+
async (chain) => {
|
|
108
|
+
const toolbox = await getToolbox(chain);
|
|
109
|
+
return toolbox.estimateTransactionFee(params) as Promise<AssetValue>;
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
.with(Chain.THORChain, Chain.Maya, Chain.Kujira, Chain.Cosmos, async () => {
|
|
98
113
|
const { estimateTransactionFee } = await import("@swapkit/toolboxes/cosmos");
|
|
99
114
|
return estimateTransactionFee(params);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
case Chain.Polkadot: {
|
|
103
|
-
return (
|
|
104
|
-
toolbox as Awaited<ReturnType<typeof getSubstrateToolbox<Chain.Polkadot>>>
|
|
105
|
-
).estimateTransactionFee(params);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
case Chain.Solana: {
|
|
109
|
-
return (toolbox as Awaited<ReturnType<typeof getSolanaToolbox>>).estimateTransactionFee(
|
|
110
|
-
params as CreateTransactionParams[Chain.Solana],
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
default:
|
|
115
|
-
return AssetValue.from({ chain });
|
|
116
|
-
}
|
|
115
|
+
})
|
|
116
|
+
.otherwise(async () => AssetValue.from({ chain }));
|
|
117
117
|
};
|
|
118
118
|
}
|
|
119
119
|
|
|
@@ -127,8 +127,10 @@ type Toolboxes = {
|
|
|
127
127
|
[key in SubstrateChain]: Awaited<ReturnType<typeof getSubstrateToolbox>>;
|
|
128
128
|
} & {
|
|
129
129
|
[Chain.Radix]: Awaited<ReturnType<typeof RadixToolbox>>;
|
|
130
|
+
[Chain.Near]: Awaited<ReturnType<typeof getNearToolbox>>;
|
|
130
131
|
[Chain.Ripple]: Awaited<ReturnType<typeof getRippleToolbox>>;
|
|
131
132
|
[Chain.Solana]: Awaited<ReturnType<typeof getSolanaToolbox>>;
|
|
133
|
+
[Chain.Tron]: Awaited<ReturnType<typeof createTronToolbox>>;
|
|
132
134
|
};
|
|
133
135
|
|
|
134
136
|
type ToolboxParams = { [key in EVMChain]: Parameters<typeof getEvmToolbox>[1] } & {
|
|
@@ -139,8 +141,10 @@ type ToolboxParams = { [key in EVMChain]: Parameters<typeof getEvmToolbox>[1] }
|
|
|
139
141
|
[key in SubstrateChain]: Parameters<typeof getSubstrateToolbox>[1];
|
|
140
142
|
} & {
|
|
141
143
|
[Chain.Radix]: Parameters<typeof RadixToolbox>[0];
|
|
144
|
+
[Chain.Near]: Parameters<typeof getNearToolbox>[0];
|
|
142
145
|
[Chain.Ripple]: Parameters<typeof getRippleToolbox>[0];
|
|
143
146
|
[Chain.Solana]: Parameters<typeof getSolanaToolbox>[0];
|
|
147
|
+
[Chain.Tron]: Parameters<typeof createTronToolbox>[0];
|
|
144
148
|
};
|
|
145
149
|
|
|
146
150
|
type CreateTransactionParams = { [key in EVMChain]: EVMCreateTransactionParams } & {
|
|
@@ -151,85 +155,98 @@ type CreateTransactionParams = { [key in EVMChain]: EVMCreateTransactionParams }
|
|
|
151
155
|
[key in SubstrateChain]: GenericCreateTransactionParams;
|
|
152
156
|
} & {
|
|
153
157
|
[Chain.Radix]: GenericCreateTransactionParams;
|
|
158
|
+
[Chain.Ripple]: GenericCreateTransactionParams;
|
|
159
|
+
[Chain.Near]: GenericCreateTransactionParams;
|
|
154
160
|
[Chain.Solana]: SolanaCreateTransactionParams;
|
|
161
|
+
[Chain.Tron]: GenericCreateTransactionParams;
|
|
155
162
|
};
|
|
156
163
|
|
|
157
164
|
export async function getToolbox<T extends keyof Toolboxes>(
|
|
158
165
|
chain: T,
|
|
159
166
|
params?: ToolboxParams[T],
|
|
160
167
|
): Promise<Toolboxes[T]> {
|
|
161
|
-
|
|
162
|
-
case Chain.Arbitrum:
|
|
163
|
-
case Chain.Avalanche:
|
|
164
|
-
case Chain.Optimism:
|
|
165
|
-
case Chain.BinanceSmartChain:
|
|
166
|
-
case Chain.Base:
|
|
167
|
-
case Chain.Polygon:
|
|
168
|
-
case Chain.Ethereum: {
|
|
169
|
-
const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
|
|
170
|
-
const evmToolbox = await getEvmToolbox(chain, params as Parameters<typeof getEvmToolbox>[1]);
|
|
171
|
-
return evmToolbox as Toolboxes[T];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
case Chain.Litecoin:
|
|
175
|
-
case Chain.Dash:
|
|
176
|
-
case Chain.Dogecoin:
|
|
177
|
-
case Chain.BitcoinCash:
|
|
178
|
-
case Chain.Bitcoin: {
|
|
179
|
-
const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
180
|
-
const utxoToolbox = await getUtxoToolbox(
|
|
181
|
-
chain,
|
|
182
|
-
params as Parameters<typeof getUtxoToolbox>[1],
|
|
183
|
-
);
|
|
184
|
-
return utxoToolbox as Toolboxes[T];
|
|
185
|
-
}
|
|
168
|
+
const { match } = await import("ts-pattern");
|
|
186
169
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
170
|
+
return match(chain as Chain)
|
|
171
|
+
.returnType<Promise<Toolboxes[T]>>()
|
|
172
|
+
.with(
|
|
173
|
+
Chain.Arbitrum,
|
|
174
|
+
Chain.Avalanche,
|
|
175
|
+
Chain.Optimism,
|
|
176
|
+
Chain.BinanceSmartChain,
|
|
177
|
+
Chain.Base,
|
|
178
|
+
Chain.Polygon,
|
|
179
|
+
Chain.Ethereum,
|
|
180
|
+
async () => {
|
|
181
|
+
const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
|
|
182
|
+
const evmToolbox = await getEvmToolbox(
|
|
183
|
+
chain as EVMChain,
|
|
184
|
+
params as Parameters<typeof getEvmToolbox>[1],
|
|
185
|
+
);
|
|
186
|
+
return evmToolbox as Toolboxes[T];
|
|
187
|
+
},
|
|
188
|
+
)
|
|
189
|
+
.with(
|
|
190
|
+
Chain.Litecoin,
|
|
191
|
+
Chain.Dash,
|
|
192
|
+
Chain.Dogecoin,
|
|
193
|
+
Chain.BitcoinCash,
|
|
194
|
+
Chain.Bitcoin,
|
|
195
|
+
async () => {
|
|
196
|
+
const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
197
|
+
const utxoToolbox = await getUtxoToolbox(
|
|
198
|
+
chain as UTXOChain,
|
|
199
|
+
params as Parameters<typeof getUtxoToolbox>[1],
|
|
200
|
+
);
|
|
201
|
+
return utxoToolbox as Toolboxes[T];
|
|
202
|
+
},
|
|
203
|
+
)
|
|
204
|
+
.with(Chain.Cosmos, Chain.Kujira, Chain.Maya, Chain.THORChain, async () => {
|
|
191
205
|
const { getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
|
|
192
206
|
const cosmosToolbox = await getCosmosToolbox(
|
|
193
|
-
chain,
|
|
207
|
+
chain as CosmosChain,
|
|
194
208
|
params as Parameters<typeof getCosmosToolbox>[1],
|
|
195
209
|
);
|
|
196
|
-
|
|
197
210
|
return cosmosToolbox as Toolboxes[T];
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
case Chain.Chainflip:
|
|
201
|
-
case Chain.Polkadot: {
|
|
211
|
+
})
|
|
212
|
+
.with(Chain.Chainflip, Chain.Polkadot, async () => {
|
|
202
213
|
const { getSubstrateToolbox } = await import("@swapkit/toolboxes/substrate");
|
|
203
214
|
const substrateToolbox = await getSubstrateToolbox(
|
|
204
|
-
chain,
|
|
215
|
+
chain as SubstrateChain,
|
|
205
216
|
params as Parameters<typeof getSubstrateToolbox>[1],
|
|
206
217
|
);
|
|
207
218
|
return substrateToolbox as Toolboxes[T];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
case Chain.Radix: {
|
|
219
|
+
})
|
|
220
|
+
.with(Chain.Radix, async () => {
|
|
211
221
|
const { RadixToolbox } = await import("@swapkit/toolboxes/radix");
|
|
212
222
|
const radixToolbox = await RadixToolbox(params as Parameters<typeof RadixToolbox>[0]);
|
|
213
223
|
return radixToolbox as Toolboxes[T];
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
case Chain.Ripple: {
|
|
224
|
+
})
|
|
225
|
+
.with(Chain.Ripple, async () => {
|
|
217
226
|
const { getRippleToolbox } = await import("@swapkit/toolboxes/ripple");
|
|
218
227
|
const rippleToolbox = await getRippleToolbox(
|
|
219
228
|
params as Parameters<typeof getRippleToolbox>[0],
|
|
220
229
|
);
|
|
221
230
|
return rippleToolbox as Toolboxes[T];
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
case Chain.Solana: {
|
|
231
|
+
})
|
|
232
|
+
.with(Chain.Solana, async () => {
|
|
225
233
|
const { getSolanaToolbox } = await import("@swapkit/toolboxes/solana");
|
|
226
234
|
const solanaToolbox = await getSolanaToolbox(
|
|
227
235
|
params as Parameters<typeof getSolanaToolbox>[0],
|
|
228
236
|
);
|
|
229
237
|
return solanaToolbox as Toolboxes[T];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
238
|
+
})
|
|
239
|
+
.with(Chain.Tron, async () => {
|
|
240
|
+
const { createTronToolbox } = await import("@swapkit/toolboxes/tron");
|
|
241
|
+
const tronToolbox = await createTronToolbox(params);
|
|
242
|
+
return tronToolbox as Toolboxes[T];
|
|
243
|
+
})
|
|
244
|
+
.with(Chain.Near, async () => {
|
|
245
|
+
const { getNearToolbox } = await import("@swapkit/toolboxes/near");
|
|
246
|
+
const nearToolbox = await getNearToolbox(params as Parameters<typeof getNearToolbox>[0]);
|
|
247
|
+
return nearToolbox as Toolboxes[T];
|
|
248
|
+
})
|
|
249
|
+
.otherwise(() => {
|
|
250
|
+
throw new SwapKitError("toolbox_not_supported", { chain });
|
|
251
|
+
});
|
|
235
252
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Account, Contract } from "near-api-js";
|
|
2
|
+
|
|
3
|
+
// Create a Near contract instance
|
|
4
|
+
export async function createNearContract<T extends Contract>({
|
|
5
|
+
account,
|
|
6
|
+
contractId,
|
|
7
|
+
viewMethods,
|
|
8
|
+
changeMethods,
|
|
9
|
+
}: {
|
|
10
|
+
account: Account;
|
|
11
|
+
contractId: string;
|
|
12
|
+
viewMethods: string[];
|
|
13
|
+
changeMethods: string[];
|
|
14
|
+
}): Promise<T> {
|
|
15
|
+
const { Contract } = await import("near-api-js");
|
|
16
|
+
|
|
17
|
+
return new Contract(account, contractId, {
|
|
18
|
+
viewMethods,
|
|
19
|
+
changeMethods,
|
|
20
|
+
useLocalViewExecution: true, // Enable local view execution for efficiency
|
|
21
|
+
}) as T;
|
|
22
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Provider } from "@near-js/providers";
|
|
2
|
+
import { type DerivationPathArray, SwapKitError, derivationPathToString } from "@swapkit/helpers";
|
|
3
|
+
import { type KeyPair, KeyPairSigner } from "near-api-js";
|
|
4
|
+
import type { NearSigner } from "../types";
|
|
5
|
+
|
|
6
|
+
export async function validateNearAddress(address: string) {
|
|
7
|
+
// Use the official NEAR SDK validation function if available
|
|
8
|
+
try {
|
|
9
|
+
const { validateAccountId } = await import("near-sdk-js");
|
|
10
|
+
return validateAccountId(address);
|
|
11
|
+
} catch {
|
|
12
|
+
const ACCOUNT_ID_REGEX = /^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/;
|
|
13
|
+
|
|
14
|
+
return address.length >= 2 && address.length <= 64 && ACCOUNT_ID_REGEX.test(address);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function getNearSignerFromPhrase(params: {
|
|
19
|
+
phrase: string;
|
|
20
|
+
derivationPath?: DerivationPathArray;
|
|
21
|
+
index?: number;
|
|
22
|
+
}) {
|
|
23
|
+
const { parseSeedPhrase } = await import("near-seed-phrase");
|
|
24
|
+
const { KeyPair } = await import("near-api-js");
|
|
25
|
+
|
|
26
|
+
// Handle derivation path logic here
|
|
27
|
+
// NEAR uses a 3-level derivation path: m/44'/397'/index'
|
|
28
|
+
const index = params.index || 0;
|
|
29
|
+
const derivationPath = params.derivationPath
|
|
30
|
+
? derivationPathToString(params.derivationPath.slice(0, 3) as [number, number, number])
|
|
31
|
+
: `m/44'/397'/${index}'`;
|
|
32
|
+
|
|
33
|
+
const { secretKey } = parseSeedPhrase(params.phrase, derivationPath);
|
|
34
|
+
const keyPair = KeyPair.fromString(secretKey as any);
|
|
35
|
+
|
|
36
|
+
return createNearSignerFromKeyPair(keyPair);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function getNearSignerFromPrivateKey(privateKey: string) {
|
|
40
|
+
const { KeyPair } = await import("near-api-js/lib/utils");
|
|
41
|
+
const keyPair = KeyPair.fromString(privateKey as any);
|
|
42
|
+
return createNearSignerFromKeyPair(keyPair);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class SKKeyPairSigner extends KeyPairSigner {
|
|
46
|
+
#keyPair: KeyPair;
|
|
47
|
+
|
|
48
|
+
constructor(keyPair: KeyPair) {
|
|
49
|
+
super(keyPair);
|
|
50
|
+
this.#keyPair = keyPair;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getAddress(): Promise<string> {
|
|
54
|
+
// For implicit accounts, derive account ID from public key
|
|
55
|
+
// NEAR implicit accounts use hex representation of the public key
|
|
56
|
+
const publicKey = this.#keyPair.getPublicKey();
|
|
57
|
+
const hexAddress = Buffer.from(publicKey.data).toString("hex");
|
|
58
|
+
return Promise.resolve(hexAddress);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createNearSignerFromKeyPair(keyPair: KeyPair): NearSigner {
|
|
63
|
+
const keyPairSigner = new SKKeyPairSigner(keyPair);
|
|
64
|
+
|
|
65
|
+
return keyPairSigner;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function getFullAccessPublicKey(provider: Provider, accountId: string) {
|
|
69
|
+
// Get the first full access key for the account
|
|
70
|
+
const response = await provider.query({
|
|
71
|
+
request_type: "view_access_key_list",
|
|
72
|
+
finality: "final",
|
|
73
|
+
account_id: accountId,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const fullAccessKey = (response as any).keys.find(
|
|
77
|
+
(key: any) => key.access_key.permission === "FullAccess",
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (!fullAccessKey) {
|
|
81
|
+
throw new SwapKitError("toolbox_near_invalid_address");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const { utils } = await import("near-api-js");
|
|
85
|
+
return utils.PublicKey.fromString(fullAccessKey.public_key);
|
|
86
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Account } from "near-api-js";
|
|
2
|
+
import type { NearGasEstimateParams } from "../types/contract";
|
|
3
|
+
|
|
4
|
+
// Gas constants (in TGas - 10^12 gas units)
|
|
5
|
+
export const GAS_COSTS = {
|
|
6
|
+
SIMPLE_TRANSFER: "1", // 1 TGas
|
|
7
|
+
TOKEN_TRANSFER: "100", // 100 TGas
|
|
8
|
+
CONTRACT_CALL: "100", // 100 TGas base
|
|
9
|
+
ACCOUNT_CREATION: "30", // 30 TGas
|
|
10
|
+
CONTRACT_DEPLOYMENT: "200", // 200 TGas
|
|
11
|
+
ACCESS_KEY_ADDITION: "5", // 5 TGas
|
|
12
|
+
ACCESS_KEY_DELETION: "5", // 5 TGas
|
|
13
|
+
STAKE: "10", // 10 TGas
|
|
14
|
+
STORAGE_DEPOSIT: "100", // 100 TGas
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
// Type guards for discriminated union
|
|
18
|
+
export function isSimpleTransfer(
|
|
19
|
+
params: NearGasEstimateParams,
|
|
20
|
+
): params is { recipient: string; amount: string } {
|
|
21
|
+
return "recipient" in params && "amount" in params && !("contractId" in params);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isContractCall(params: NearGasEstimateParams): params is {
|
|
25
|
+
contractId: string;
|
|
26
|
+
methodName: string;
|
|
27
|
+
args?: Record<string, any>;
|
|
28
|
+
attachedDeposit?: string;
|
|
29
|
+
} {
|
|
30
|
+
return "contractId" in params && "methodName" in params;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isBatchTransaction(params: NearGasEstimateParams): params is { actions: any[] } {
|
|
34
|
+
return "actions" in params;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isAccountCreation(params: NearGasEstimateParams): params is {
|
|
38
|
+
newAccountId: string;
|
|
39
|
+
publicKey?: string;
|
|
40
|
+
} {
|
|
41
|
+
return "newAccountId" in params;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isContractDeployment(
|
|
45
|
+
params: NearGasEstimateParams,
|
|
46
|
+
): params is { contractCode: Uint8Array } {
|
|
47
|
+
return "contractCode" in params;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isCustomEstimator(params: NearGasEstimateParams): params is {
|
|
51
|
+
customEstimator: (account: Account) => Promise<string>;
|
|
52
|
+
} {
|
|
53
|
+
return "customEstimator" in params;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Helper function to estimate gas for batch actions
|
|
57
|
+
export function estimateBatchGas(actions: any[]) {
|
|
58
|
+
let totalGas = 0;
|
|
59
|
+
|
|
60
|
+
for (const action of actions) {
|
|
61
|
+
switch (action.enum) {
|
|
62
|
+
case "transfer":
|
|
63
|
+
totalGas += Number(GAS_COSTS.SIMPLE_TRANSFER);
|
|
64
|
+
break;
|
|
65
|
+
case "functionCall":
|
|
66
|
+
totalGas += Number(GAS_COSTS.CONTRACT_CALL);
|
|
67
|
+
break;
|
|
68
|
+
case "createAccount":
|
|
69
|
+
totalGas += Number(GAS_COSTS.ACCOUNT_CREATION);
|
|
70
|
+
break;
|
|
71
|
+
case "deployContract":
|
|
72
|
+
totalGas += Number(GAS_COSTS.CONTRACT_DEPLOYMENT);
|
|
73
|
+
break;
|
|
74
|
+
case "addKey":
|
|
75
|
+
totalGas += Number(GAS_COSTS.ACCESS_KEY_ADDITION);
|
|
76
|
+
break;
|
|
77
|
+
case "deleteKey":
|
|
78
|
+
totalGas += Number(GAS_COSTS.ACCESS_KEY_DELETION);
|
|
79
|
+
break;
|
|
80
|
+
case "stake":
|
|
81
|
+
totalGas += Number(GAS_COSTS.STAKE);
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
totalGas += Number(GAS_COSTS.CONTRACT_CALL);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return totalGas.toString();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Helper function to get gas cost for contract methods
|
|
92
|
+
export function getContractMethodGas(methodName: string) {
|
|
93
|
+
if (methodName === "ft_transfer" || methodName === "ft_transfer_call") {
|
|
94
|
+
return GAS_COSTS.TOKEN_TRANSFER;
|
|
95
|
+
}
|
|
96
|
+
if (methodName === "storage_deposit") {
|
|
97
|
+
return GAS_COSTS.STORAGE_DEPOSIT;
|
|
98
|
+
}
|
|
99
|
+
return GAS_COSTS.CONTRACT_CALL;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Convert TGas string to gas units
|
|
103
|
+
export function tgasToGas(tgas: string): string {
|
|
104
|
+
return (BigInt(tgas) * BigInt(10 ** 12)).toString();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Convert gas units to TGas
|
|
108
|
+
export function gasToTGas(gas: string): string {
|
|
109
|
+
return (BigInt(gas) / BigInt(10 ** 12)).toString();
|
|
110
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Account, Contract } from "near-api-js";
|
|
2
|
+
import { createNearContract } from "./contractFactory";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_STORAGE_DEPOSIT = "1250000000000000000000"; // 0.00125 NEAR
|
|
5
|
+
|
|
6
|
+
// Define NEP-141 contract interface
|
|
7
|
+
interface NEP141Contract extends Contract {
|
|
8
|
+
// View methods
|
|
9
|
+
ft_balance_of(args: { account_id: string }): Promise<string>;
|
|
10
|
+
ft_total_supply(): Promise<string>;
|
|
11
|
+
ft_metadata(): Promise<any>;
|
|
12
|
+
storage_balance_of(args: { account_id: string }): Promise<any>;
|
|
13
|
+
storage_balance_bounds(): Promise<any>;
|
|
14
|
+
|
|
15
|
+
// Change methods
|
|
16
|
+
ft_transfer(args: any, gas: any, deposit: any): Promise<any>;
|
|
17
|
+
ft_transfer_call(args: any, gas: any, deposit: any): Promise<any>;
|
|
18
|
+
storage_deposit(args: any, gas: any, deposit: any): Promise<any>;
|
|
19
|
+
storage_withdraw(args: any, gas: any, deposit: any): Promise<any>;
|
|
20
|
+
storage_unregister(force?: boolean, gas?: any): Promise<any>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function createNEP141Token({
|
|
24
|
+
contractId,
|
|
25
|
+
account,
|
|
26
|
+
}: {
|
|
27
|
+
contractId: string;
|
|
28
|
+
account: Account;
|
|
29
|
+
}) {
|
|
30
|
+
const BN = (await import("bn.js")).default;
|
|
31
|
+
|
|
32
|
+
const contract = await createNearContract<NEP141Contract>({
|
|
33
|
+
account,
|
|
34
|
+
contractId,
|
|
35
|
+
viewMethods: [
|
|
36
|
+
"ft_balance_of",
|
|
37
|
+
"ft_total_supply",
|
|
38
|
+
"ft_metadata",
|
|
39
|
+
"storage_balance_of",
|
|
40
|
+
"storage_balance_bounds",
|
|
41
|
+
],
|
|
42
|
+
changeMethods: [
|
|
43
|
+
"ft_transfer",
|
|
44
|
+
"ft_transfer_call",
|
|
45
|
+
"storage_deposit",
|
|
46
|
+
"storage_withdraw",
|
|
47
|
+
"storage_unregister",
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Helper to ensure storage before transfers
|
|
52
|
+
const ensureStorageFor = async (accountId: string) => {
|
|
53
|
+
const balance = await contract.storage_balance_of({ account_id: accountId });
|
|
54
|
+
if (!balance) {
|
|
55
|
+
// Get minimum storage requirement
|
|
56
|
+
const bounds = await contract.storage_balance_bounds();
|
|
57
|
+
const deposit = bounds?.min || DEFAULT_STORAGE_DEPOSIT;
|
|
58
|
+
|
|
59
|
+
await contract.storage_deposit(
|
|
60
|
+
{ account_id: accountId },
|
|
61
|
+
new BN("100000000000000"), // 100 TGas
|
|
62
|
+
new BN(deposit),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
transfer: async (receiverId: string, amount: string, memo?: string) => {
|
|
69
|
+
// Ensure recipient has storage before transfer
|
|
70
|
+
await ensureStorageFor(receiverId);
|
|
71
|
+
|
|
72
|
+
return contract.ft_transfer(
|
|
73
|
+
{ receiver_id: receiverId, amount, memo },
|
|
74
|
+
new BN("100000000000000"), // 100 TGas
|
|
75
|
+
new BN("1"), // 1 yoctoNEAR for security
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
transferCall: async (receiverId: string, amount: string, msg: string, memo?: string) => {
|
|
80
|
+
// Ensure recipient has storage before transfer
|
|
81
|
+
await ensureStorageFor(receiverId);
|
|
82
|
+
|
|
83
|
+
return contract.ft_transfer_call(
|
|
84
|
+
{ receiver_id: receiverId, amount, memo, msg },
|
|
85
|
+
new BN("100000000000000"), // 100 TGas
|
|
86
|
+
new BN("1"), // 1 yoctoNEAR for security
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
balanceOf: (accountId: string) => contract.ft_balance_of({ account_id: accountId }),
|
|
91
|
+
|
|
92
|
+
totalSupply: () => contract.ft_total_supply(),
|
|
93
|
+
|
|
94
|
+
metadata: () => contract.ft_metadata(),
|
|
95
|
+
|
|
96
|
+
storageBalanceOf: (accountId: string) => contract.storage_balance_of({ account_id: accountId }),
|
|
97
|
+
|
|
98
|
+
storageDeposit: (accountId?: string, amount?: string) =>
|
|
99
|
+
contract.storage_deposit(
|
|
100
|
+
{ account_id: accountId },
|
|
101
|
+
new BN("100000000000000"),
|
|
102
|
+
new BN(amount || DEFAULT_STORAGE_DEPOSIT),
|
|
103
|
+
),
|
|
104
|
+
|
|
105
|
+
ensureStorage: ensureStorageFor,
|
|
106
|
+
|
|
107
|
+
// Raw contract access for advanced use cases
|
|
108
|
+
contract,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export * from "./toolbox";
|
|
2
|
+
export * from "./types";
|
|
3
|
+
export * from "./helpers";
|
|
4
|
+
|
|
5
|
+
import type { getNearToolbox } from "./toolbox";
|
|
6
|
+
|
|
7
|
+
export type NearWallet = Awaited<ReturnType<typeof getNearToolbox>>;
|
|
8
|
+
|
|
9
|
+
export type {
|
|
10
|
+
NearContractInterface,
|
|
11
|
+
NearCallParams,
|
|
12
|
+
NearGasEstimateParams,
|
|
13
|
+
} from "./types/contract";
|
|
14
|
+
export type {
|
|
15
|
+
FungibleTokenMetadata,
|
|
16
|
+
StorageBalance,
|
|
17
|
+
StorageBalanceBounds,
|
|
18
|
+
NEP141Contract,
|
|
19
|
+
TokenTransferParams,
|
|
20
|
+
} from "./types/nep141";
|
|
21
|
+
|
|
22
|
+
export { createNEP141Token } from "./helpers/nep141";
|
|
23
|
+
export { tgasToGas, gasToTGas } from "./helpers/gasEstimation";
|
|
24
|
+
export { createNearContract } from "./helpers/contractFactory";
|