@swapkit/toolboxes 1.0.0-beta.3 → 1.0.0-beta.31

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.
Files changed (131) hide show
  1. package/dist/{chunk-fjfxga2v.js → chunk-5yxc1e69.js} +1 -1
  2. package/dist/{chunk-fjfxga2v.js.map → chunk-5yxc1e69.js.map} +1 -1
  3. package/dist/chunk-6f98phv2.js +4 -0
  4. package/dist/{chunk-0h4xdrwz.js.map → chunk-6f98phv2.js.map} +2 -2
  5. package/dist/{chunk-0f0249b1.js → chunk-9bqegm61.js} +1 -1
  6. package/dist/{chunk-p1kdg37m.js → chunk-s47y8512.js} +2 -2
  7. package/dist/{chunk-p1kdg37m.js.map → chunk-s47y8512.js.map} +1 -1
  8. package/dist/chunk-vtd17cje.js +3 -0
  9. package/dist/chunk-vtd17cje.js.map +10 -0
  10. package/dist/chunk-zcdeg6h9.js +4 -0
  11. package/dist/chunk-zcdeg6h9.js.map +10 -0
  12. package/dist/src/cosmos/index.cjs +3 -0
  13. package/dist/src/cosmos/index.cjs.map +16 -0
  14. package/dist/src/cosmos/index.js +3 -0
  15. package/dist/src/cosmos/index.js.map +16 -0
  16. package/dist/src/evm/index.cjs +3 -0
  17. package/dist/src/evm/index.cjs.map +18 -0
  18. package/dist/src/evm/index.js +3 -0
  19. package/dist/src/evm/index.js.map +18 -0
  20. package/dist/src/index.cjs +3 -0
  21. package/dist/src/index.cjs.map +10 -0
  22. package/dist/src/index.js +3 -0
  23. package/dist/src/index.js.map +10 -0
  24. package/dist/src/near/index.cjs +3 -0
  25. package/dist/src/near/index.cjs.map +13 -0
  26. package/dist/src/near/index.js +3 -0
  27. package/dist/src/near/index.js.map +13 -0
  28. package/dist/{radix → src/radix}/index.cjs +2 -2
  29. package/dist/src/radix/index.cjs.map +10 -0
  30. package/dist/src/radix/index.js +3 -0
  31. package/dist/src/radix/index.js.map +10 -0
  32. package/dist/src/ripple/index.cjs +3 -0
  33. package/dist/src/ripple/index.cjs.map +10 -0
  34. package/dist/src/ripple/index.js +3 -0
  35. package/dist/src/ripple/index.js.map +10 -0
  36. package/dist/src/solana/index.cjs +3 -0
  37. package/dist/src/solana/index.cjs.map +10 -0
  38. package/dist/src/solana/index.js +3 -0
  39. package/dist/src/solana/index.js.map +10 -0
  40. package/dist/src/substrate/index.cjs +3 -0
  41. package/dist/src/substrate/index.cjs.map +12 -0
  42. package/dist/src/substrate/index.js +3 -0
  43. package/dist/src/substrate/index.js.map +12 -0
  44. package/dist/src/tron/index.cjs +3 -0
  45. package/dist/src/tron/index.cjs.map +11 -0
  46. package/dist/src/tron/index.js +3 -0
  47. package/dist/src/tron/index.js.map +11 -0
  48. package/dist/src/utxo/index.cjs +5 -0
  49. package/dist/src/utxo/index.cjs.map +17 -0
  50. package/dist/src/utxo/index.js +5 -0
  51. package/dist/src/utxo/index.js.map +17 -0
  52. package/package.json +49 -37
  53. package/src/cosmos/thorchainUtils/addressFormat.ts +4 -1
  54. package/src/cosmos/thorchainUtils/messages.ts +2 -2
  55. package/src/cosmos/thorchainUtils/registry.ts +3 -3
  56. package/src/cosmos/toolbox/cosmos.ts +35 -16
  57. package/src/cosmos/toolbox/index.ts +2 -2
  58. package/src/cosmos/toolbox/thorchain.ts +11 -9
  59. package/src/cosmos/util.ts +76 -6
  60. package/src/evm/__tests__/address-validation.test.ts +86 -0
  61. package/src/evm/__tests__/ethereum.test.ts +1 -1
  62. package/src/evm/helpers.ts +4 -3
  63. package/src/evm/toolbox/baseEVMToolbox.ts +37 -25
  64. package/src/evm/toolbox/index.ts +2 -2
  65. package/src/evm/toolbox/op.ts +21 -7
  66. package/src/index.ts +118 -100
  67. package/src/near/__tests__/core.test.ts +80 -0
  68. package/src/near/helpers/contractFactory.ts +22 -0
  69. package/src/near/helpers/core.ts +89 -0
  70. package/src/near/helpers/gasEstimation.ts +110 -0
  71. package/src/near/helpers/index.ts +5 -0
  72. package/src/near/helpers/nep141.ts +110 -0
  73. package/src/near/index.ts +24 -0
  74. package/src/near/toolbox.ts +498 -0
  75. package/src/near/types/contract.ts +48 -0
  76. package/src/near/types/nep141.ts +66 -0
  77. package/src/near/types.ts +57 -0
  78. package/src/radix/index.ts +8 -2
  79. package/src/ripple/index.ts +15 -26
  80. package/src/solana/toolbox.ts +73 -2
  81. package/src/substrate/balance.ts +92 -0
  82. package/src/substrate/substrate.ts +7 -5
  83. package/src/tron/__tests__/toolbox.test.ts +147 -0
  84. package/src/tron/helpers/trc20.abi.ts +40 -0
  85. package/src/tron/index.ts +16 -0
  86. package/src/tron/toolbox.ts +336 -0
  87. package/src/tron/types.ts +31 -0
  88. package/src/utxo/__tests__/zcash-integration.test.ts +114 -0
  89. package/src/utxo/helpers/api.ts +66 -16
  90. package/src/utxo/helpers/bchaddrjs.ts +8 -8
  91. package/src/utxo/helpers/coinselect.ts +4 -2
  92. package/src/utxo/helpers/txSize.ts +4 -3
  93. package/src/utxo/index.ts +1 -0
  94. package/src/utxo/toolbox/bitcoinCash.ts +22 -14
  95. package/src/utxo/toolbox/index.ts +16 -4
  96. package/src/utxo/toolbox/utxo.ts +42 -27
  97. package/src/utxo/toolbox/zcash.ts +208 -0
  98. package/src/utxo/types.ts +2 -0
  99. package/dist/chunk-0h4xdrwz.js +0 -4
  100. package/dist/cosmos/index.cjs +0 -3
  101. package/dist/cosmos/index.cjs.map +0 -16
  102. package/dist/cosmos/index.js +0 -3
  103. package/dist/cosmos/index.js.map +0 -16
  104. package/dist/evm/index.cjs +0 -3
  105. package/dist/evm/index.cjs.map +0 -18
  106. package/dist/evm/index.js +0 -3
  107. package/dist/evm/index.js.map +0 -18
  108. package/dist/index.cjs +0 -3
  109. package/dist/index.cjs.map +0 -10
  110. package/dist/index.js +0 -3
  111. package/dist/index.js.map +0 -10
  112. package/dist/radix/index.cjs.map +0 -10
  113. package/dist/radix/index.js +0 -3
  114. package/dist/radix/index.js.map +0 -10
  115. package/dist/ripple/index.cjs +0 -3
  116. package/dist/ripple/index.cjs.map +0 -10
  117. package/dist/ripple/index.js +0 -3
  118. package/dist/ripple/index.js.map +0 -10
  119. package/dist/solana/index.cjs +0 -3
  120. package/dist/solana/index.cjs.map +0 -10
  121. package/dist/solana/index.js +0 -3
  122. package/dist/solana/index.js.map +0 -10
  123. package/dist/substrate/index.cjs +0 -3
  124. package/dist/substrate/index.cjs.map +0 -11
  125. package/dist/substrate/index.js +0 -3
  126. package/dist/substrate/index.js.map +0 -11
  127. package/dist/utxo/index.cjs +0 -3
  128. package/dist/utxo/index.cjs.map +0 -16
  129. package/dist/utxo/index.js +0 -3
  130. package/dist/utxo/index.js.map +0 -16
  131. /package/dist/{chunk-0f0249b1.js.map → chunk-9bqegm61.js.map} +0 -0
@@ -0,0 +1,498 @@
1
+ import {
2
+ AssetValue,
3
+ BaseDecimal,
4
+ Chain,
5
+ type DerivationPathArray,
6
+ SKConfig,
7
+ SwapKitError,
8
+ } from "@swapkit/helpers";
9
+ import type { Account } from "near-api-js";
10
+ import type { SignedTransaction, Transaction } from "near-api-js/lib/transaction";
11
+ import {
12
+ getFullAccessPublicKey,
13
+ getNearSignerFromPhrase,
14
+ getNearSignerFromPrivateKey,
15
+ validateNearAddress,
16
+ } from "./helpers";
17
+ import {
18
+ GAS_COSTS,
19
+ estimateBatchGas,
20
+ getContractMethodGas,
21
+ isAccountCreation,
22
+ isBatchTransaction,
23
+ isContractCall,
24
+ isContractDeployment,
25
+ isCustomEstimator,
26
+ isSimpleTransfer,
27
+ } from "./helpers/gasEstimation";
28
+ import { createNEP141Token } from "./helpers/nep141";
29
+ import type {
30
+ NearCreateTransactionParams,
31
+ NearFunctionCallParams,
32
+ NearToolboxParams,
33
+ NearTransferParams,
34
+ } from "./types";
35
+ import type { NearContractInterface, NearGasEstimateParams } from "./types/contract";
36
+
37
+ export async function getNearToolbox(toolboxParams?: NearToolboxParams) {
38
+ const { P, match } = await import("ts-pattern");
39
+ const { providers } = await import("near-api-js");
40
+ const signer = await match(toolboxParams)
41
+ .with({ phrase: P.string }, async (params) => {
42
+ const signer = await getNearSignerFromPhrase(params);
43
+ return signer;
44
+ })
45
+ .with({ signer: P.any }, ({ signer }) => signer)
46
+ .otherwise(() => undefined);
47
+
48
+ const url = SKConfig.get("rpcUrls")[Chain.Near];
49
+
50
+ const provider = new providers.JsonRpcProvider({ url });
51
+
52
+ async function getAccount(address?: string) {
53
+ const { Account } = await import("near-api-js");
54
+
55
+ const _address = address || (await getAddress());
56
+
57
+ const account = new Account(_address, provider, signer);
58
+
59
+ return account;
60
+ }
61
+
62
+ async function getAddress() {
63
+ if (!signer) {
64
+ throw new SwapKitError("toolbox_near_no_signer");
65
+ }
66
+ const address = await signer.getAddress();
67
+ return address;
68
+ }
69
+
70
+ async function transfer(params: NearTransferParams) {
71
+ if (!signer) {
72
+ throw new SwapKitError("toolbox_near_no_signer");
73
+ }
74
+
75
+ const { recipient, assetValue } = params;
76
+
77
+ if (!validateNearAddress(recipient)) {
78
+ throw new SwapKitError("toolbox_near_invalid_address");
79
+ }
80
+
81
+ const account = await getAccount();
82
+
83
+ if (assetValue.isGasAsset === false) {
84
+ // NEP-141 token transfer
85
+ const contractId = assetValue.address;
86
+ if (!contractId) {
87
+ throw new SwapKitError("toolbox_near_missing_contract_address");
88
+ }
89
+
90
+ const amount = assetValue.getBaseValue("string");
91
+
92
+ return callFunction({
93
+ contractId,
94
+ methodName: "ft_transfer",
95
+ args: {
96
+ receiver_id: recipient,
97
+ amount,
98
+ memo: params.memo,
99
+ },
100
+ });
101
+ }
102
+
103
+ try {
104
+ const transferAmount = assetValue.getBaseValue("string");
105
+
106
+ const result = await account.transfer({
107
+ receiverId: recipient,
108
+ amount: transferAmount,
109
+ });
110
+
111
+ return result.transaction.hash;
112
+ } catch (error) {
113
+ throw new SwapKitError("toolbox_near_transfer_failed", { error });
114
+ }
115
+ }
116
+
117
+ async function createTransaction(params: NearCreateTransactionParams) {
118
+ const { recipient, assetValue, memo, feeRate: gas, attachedDeposit, sender: signerId } = params;
119
+
120
+ // Handle NEP-141 token transfers
121
+ if (!assetValue.isGasAsset) {
122
+ const contractId = assetValue.address;
123
+ if (!contractId) {
124
+ throw new SwapKitError("toolbox_near_missing_contract_address");
125
+ }
126
+
127
+ return createContractFunctionCall({
128
+ sender: signerId,
129
+ contractId,
130
+ methodName: "ft_transfer",
131
+ args: {
132
+ receiver_id: recipient,
133
+ amount: assetValue.getBaseValue("string"),
134
+ memo: memo || null,
135
+ },
136
+ gas: gas.toString() || "100000000000000", // 100 TGas default
137
+ attachedDeposit: "1", // 1 yoctoNEAR required for NEP-141 transfers
138
+ });
139
+ }
140
+
141
+ // Native NEAR transfer
142
+ const { publicKey, nonce } = await getFullAccessPublicKey(provider, signerId);
143
+ const baseAmount = assetValue.getBaseValue("bigint");
144
+
145
+ const { SCHEMA } = await import("near-api-js/lib/transaction");
146
+ const { transactions, utils } = await import("near-api-js");
147
+
148
+ const txActions = [transactions.transfer(baseAmount)];
149
+
150
+ if (memo && attachedDeposit) {
151
+ txActions.push(
152
+ transactions.functionCall("memo", { memo }, BigInt(gas), BigInt(attachedDeposit)),
153
+ );
154
+ }
155
+
156
+ const block = await provider.block({ finality: "final" });
157
+ const blockHash = utils.serialize.base_decode(block.header.hash);
158
+
159
+ const transaction = transactions.createTransaction(
160
+ signerId,
161
+ publicKey,
162
+ recipient,
163
+ nonce,
164
+ txActions,
165
+ blockHash,
166
+ );
167
+
168
+ const serializedTx = utils.serialize.serialize(SCHEMA.Transaction, transaction);
169
+ const serializedBase64 = Buffer.from(serializedTx).toString("base64");
170
+
171
+ return {
172
+ serialized: serializedBase64,
173
+ publicKey: publicKey.toString(),
174
+ details: {
175
+ signerId,
176
+ nonce: nonce,
177
+ blockHash: utils.serialize.base_encode(blockHash),
178
+ },
179
+ };
180
+ }
181
+
182
+ async function createContractFunctionCall(params: {
183
+ sender: string;
184
+ contractId: string;
185
+ methodName: string;
186
+ args: any;
187
+ gas: string;
188
+ attachedDeposit: string;
189
+ }) {
190
+ const { sender: accountId } = params;
191
+
192
+ const { publicKey, nonce } = await getFullAccessPublicKey(provider, accountId);
193
+
194
+ const { SCHEMA } = await import("near-api-js/lib/transaction");
195
+ const { transactions, utils } = await import("near-api-js");
196
+ const block = await provider.block({ finality: "final" });
197
+ const blockHash = utils.serialize.base_decode(block.header.hash);
198
+
199
+ const actions = [
200
+ transactions.functionCall(
201
+ params.methodName,
202
+ Buffer.from(JSON.stringify(params.args)),
203
+ BigInt(params.gas),
204
+ BigInt(params.attachedDeposit),
205
+ ),
206
+ ];
207
+
208
+ const transaction = transactions.createTransaction(
209
+ accountId,
210
+ publicKey,
211
+ params.contractId,
212
+ nonce,
213
+ actions,
214
+ blockHash,
215
+ );
216
+
217
+ const serializedTx = utils.serialize.serialize(SCHEMA.Transaction, transaction);
218
+ const serializedBase64 = Buffer.from(serializedTx).toString("base64");
219
+
220
+ return {
221
+ serialized: serializedBase64,
222
+ publicKey: publicKey.toString(),
223
+ details: {
224
+ signerId: accountId,
225
+ receiverId: params.contractId,
226
+ methodName: params.methodName,
227
+ nonce: nonce,
228
+ blockHash: utils.serialize.base_encode(blockHash),
229
+ },
230
+ };
231
+ }
232
+
233
+ async function signTransaction(transaction: Transaction) {
234
+ if (!signer) {
235
+ throw new SwapKitError("toolbox_near_no_signer");
236
+ }
237
+
238
+ // Use the new signer interface method
239
+ const [_hash, signedTx] = await signer.signTransaction(transaction);
240
+ return signedTx;
241
+ }
242
+
243
+ async function broadcastTransaction(signedTransaction: SignedTransaction) {
244
+ const result = await provider.sendTransaction(signedTransaction);
245
+ return result.transaction.hash;
246
+ }
247
+
248
+ async function estimateTransactionFee(params: NearTransferParams | NearGasEstimateParams) {
249
+ if ("assetValue" in params) {
250
+ const baseTransferCost = "115123062500"; // gas units for transfer
251
+ const receiptCreationCost = "108059500000"; // gas units for receipt
252
+
253
+ const totalGasUnits = BigInt(baseTransferCost) + BigInt(receiptCreationCost);
254
+
255
+ const gasPrice = await getCurrentGasPrice();
256
+
257
+ // NEAR doesn't support fee multipliers - gas price is fixed by the network
258
+ const totalCostYocto = totalGasUnits * BigInt(gasPrice.toString());
259
+
260
+ return AssetValue.from({
261
+ chain: Chain.Near,
262
+ value: totalCostYocto.toString(),
263
+ fromBaseDecimal: BaseDecimal[Chain.Near],
264
+ });
265
+ }
266
+
267
+ // Handle new gas estimation params
268
+ const account = signer ? await getAccount() : undefined;
269
+ return estimateGas(params, account);
270
+ }
271
+
272
+ async function getCurrentGasPrice() {
273
+ try {
274
+ const result = await provider.query({
275
+ request_type: "call_function",
276
+ finality: "final",
277
+ account_id: "system",
278
+ method_name: "gas_price",
279
+ args_base64: "",
280
+ });
281
+
282
+ return result;
283
+ } catch {
284
+ return "100000000"; // 0.0001 NEAR per Tgas
285
+ }
286
+ }
287
+
288
+ async function createSubAccount(subAccountId: string, publicKey: string, initialBalance: string) {
289
+ if (!signer) {
290
+ throw new SwapKitError("toolbox_near_no_signer");
291
+ }
292
+
293
+ const account = await getAccount();
294
+ const { utils } = await import("near-api-js");
295
+
296
+ const balanceInYocto = utils.format.parseNearAmount(initialBalance) || "0";
297
+
298
+ const result = await account.createAccount(
299
+ subAccountId,
300
+ utils.PublicKey.fromString(publicKey),
301
+ BigInt(balanceInYocto),
302
+ );
303
+
304
+ return result.transaction.hash;
305
+ }
306
+
307
+ async function callFunction(params: NearFunctionCallParams) {
308
+ try {
309
+ if (!signer) {
310
+ throw new SwapKitError("toolbox_near_no_signer");
311
+ }
312
+
313
+ const { transactions } = await import("near-api-js");
314
+
315
+ const { contractId, methodName, args, deposit } = params;
316
+ const account = await getAccount();
317
+
318
+ const estimatedGas = await estimateGas({
319
+ methodName,
320
+ args: args || {},
321
+ contractId,
322
+ });
323
+
324
+ const functionAction = transactions.functionCall(
325
+ methodName,
326
+ args || {},
327
+ estimatedGas.getBaseValue("bigint"),
328
+ BigInt(deposit || "0"),
329
+ );
330
+
331
+ const result = await account.signAndSendTransaction({
332
+ receiverId: contractId,
333
+ actions: [functionAction],
334
+ });
335
+
336
+ return result.transaction.hash;
337
+ } catch (error) {
338
+ throw new SwapKitError("toolbox_near_transfer_failed", { error });
339
+ }
340
+ }
341
+
342
+ // Create typed contract interface
343
+ async function createContract(contractInterface: NearContractInterface) {
344
+ const { createNearContract } = await import("./helpers/contractFactory");
345
+ const account = await getAccount();
346
+
347
+ return createNearContract({
348
+ account,
349
+ contractId: contractInterface.contractId,
350
+ viewMethods: contractInterface.viewMethods,
351
+ changeMethods: contractInterface.changeMethods,
352
+ });
353
+ }
354
+
355
+ async function executeBatchTransaction(batch: { receiverId: string; actions: any[] }) {
356
+ if (!signer) {
357
+ throw new SwapKitError("toolbox_near_no_signer");
358
+ }
359
+
360
+ if (batch.actions.length === 0) {
361
+ throw new SwapKitError("toolbox_near_empty_batch");
362
+ }
363
+
364
+ const account = await getAccount();
365
+
366
+ // Use account.signAndSendTransaction for batch operations
367
+ const result = await account.signAndSendTransaction({
368
+ receiverId: batch.receiverId,
369
+ actions: batch.actions,
370
+ });
371
+
372
+ return result.transaction.hash;
373
+ }
374
+
375
+ async function nep141(contractId: string) {
376
+ const account = await getAccount();
377
+ return createNEP141Token({ contractId, account });
378
+ }
379
+
380
+ async function getBalance(address: string) {
381
+ try {
382
+ const account = await getAccount(address);
383
+
384
+ let nativeBalance: AssetValue;
385
+ try {
386
+ const value = await account.getBalance();
387
+
388
+ nativeBalance = AssetValue.from({
389
+ chain: Chain.Near,
390
+ value,
391
+ fromBaseDecimal: BaseDecimal[Chain.Near],
392
+ });
393
+ } catch {
394
+ nativeBalance = AssetValue.from({
395
+ chain: Chain.Near,
396
+ value: "0",
397
+ fromBaseDecimal: BaseDecimal[Chain.Near],
398
+ });
399
+ }
400
+
401
+ // // Then, fetch token balances from API
402
+ // let tokenBalances: AssetValue[] = [];
403
+ // try {
404
+ // const apiBalances = await SwapKitApi.getChainBalance({
405
+ // chain: Chain.Near,
406
+ // address,
407
+ // scamFilter,
408
+ // });
409
+
410
+ // tokenBalances = apiBalances
411
+ // .filter(({ identifier }) => identifier !== Chain.Near) // Filter out native NEAR
412
+ // .map(({ identifier, value, decimal }) => {
413
+ // return new AssetValue({
414
+ // decimal: decimal || BaseDecimal[Chain.Near],
415
+ // value,
416
+ // identifier,
417
+ // });
418
+ // });
419
+ // } catch (error) {
420
+ // // If API fails, just return on-chain balance
421
+ // console.warn("Failed to fetch token balances from API:", error);
422
+ // }
423
+
424
+ // Merge native balance with token balances
425
+ // return [nativeBalance, ...tokenBalances];
426
+ return [nativeBalance];
427
+ } catch (error) {
428
+ throw new SwapKitError("toolbox_near_balance_failed", { error });
429
+ }
430
+ }
431
+
432
+ async function estimateGas(params: NearGasEstimateParams, account?: Account) {
433
+ const gasInTGas = await match(params)
434
+ .when(isSimpleTransfer, () => GAS_COSTS.SIMPLE_TRANSFER)
435
+ .when(isContractCall, (p) => getContractMethodGas(p.methodName))
436
+ .when(isBatchTransaction, (p) => estimateBatchGas(p.actions))
437
+ .when(isAccountCreation, () => GAS_COSTS.ACCOUNT_CREATION)
438
+ .when(isContractDeployment, () => GAS_COSTS.CONTRACT_DEPLOYMENT)
439
+ .when(isCustomEstimator, (p) => {
440
+ if (!account) {
441
+ throw new SwapKitError("toolbox_near_no_account");
442
+ }
443
+ return p.customEstimator(account);
444
+ })
445
+ .otherwise(() => {
446
+ throw new SwapKitError("toolbox_near_invalid_gas_params");
447
+ });
448
+
449
+ // Convert TGas to gas price in NEAR
450
+ const gasPrice = await getGasPrice();
451
+ const gasInUnits = BigInt(gasInTGas) * BigInt(10 ** 12); // Convert TGas to gas units
452
+ const costInYoctoNear = gasInUnits * BigInt(gasPrice);
453
+
454
+ return AssetValue.from({
455
+ chain: Chain.Near,
456
+ value: costInYoctoNear,
457
+ fromBaseDecimal: BaseDecimal[Chain.Near],
458
+ });
459
+ }
460
+
461
+ // Get current gas price from network
462
+ async function getGasPrice() {
463
+ try {
464
+ const result = await provider.gasPrice(null);
465
+ return result.gas_price || "100000000";
466
+ } catch (_error) {
467
+ // Fallback to default
468
+ return "100000000";
469
+ }
470
+ }
471
+
472
+ return {
473
+ getAddress,
474
+ getPublicKey: async () => (signer ? (await signer.getPublicKey()).toString() : ""),
475
+ provider,
476
+ transfer,
477
+ createTransaction,
478
+ createContractFunctionCall,
479
+ estimateTransactionFee,
480
+ broadcastTransaction,
481
+ signTransaction,
482
+ getBalance,
483
+ validateAddress: validateNearAddress,
484
+ getSignerFromPhrase: (params: {
485
+ phrase: string;
486
+ derivationPath?: DerivationPathArray;
487
+ index?: number;
488
+ }) => getNearSignerFromPhrase(params),
489
+ getSignerFromPrivateKey: getNearSignerFromPrivateKey,
490
+ callFunction,
491
+ createSubAccount,
492
+ createContract,
493
+ executeBatchTransaction,
494
+ nep141,
495
+ getGasPrice,
496
+ estimateGas,
497
+ };
498
+ }
@@ -0,0 +1,48 @@
1
+ import type { Account } from "near-api-js";
2
+
3
+ // Custom interface for contract metadata (not in SDK)
4
+ export interface NearContractInterface {
5
+ contractId: string;
6
+ viewMethods: string[];
7
+ changeMethods: string[];
8
+ metadata?: {
9
+ version: string;
10
+ standards: string[]; // e.g., ["nep141", "nep171"]
11
+ };
12
+ }
13
+
14
+ // Enhanced call parameters
15
+ export interface NearCallParams {
16
+ contractId: string;
17
+ methodName: string;
18
+ args?: Record<string, any>;
19
+ gas?: string | any; // BN type
20
+ attachedDeposit?: string | any; // BN type
21
+ isView?: boolean;
22
+ }
23
+
24
+ // Gas estimation using discriminated unions for type inference
25
+ export type NearGasEstimateParams =
26
+ | {
27
+ recipient: string;
28
+ amount: string;
29
+ }
30
+ | {
31
+ contractId: string;
32
+ methodName: string;
33
+ args?: Record<string, any>;
34
+ attachedDeposit?: string;
35
+ }
36
+ | {
37
+ actions: any[]; // Action type from near-api-js
38
+ }
39
+ | {
40
+ newAccountId: string;
41
+ publicKey?: string;
42
+ }
43
+ | {
44
+ contractCode: Uint8Array;
45
+ }
46
+ | {
47
+ customEstimator: (account: Account) => Promise<string>;
48
+ };
@@ -0,0 +1,66 @@
1
+ // NEP-141 Fungible Token Standard Types
2
+
3
+ export interface FungibleTokenMetadata {
4
+ spec: string; // e.g., "ft-1.0.0"
5
+ name: string; // e.g., "Wrapped NEAR"
6
+ symbol: string; // e.g., "wNEAR"
7
+ icon?: string; // Data URL or IPFS link
8
+ reference?: string; // URL to additional metadata
9
+ reference_hash?: string; // Base64-encoded hash of reference content
10
+ decimals: number; // e.g., 24 for NEAR
11
+ }
12
+
13
+ export interface StorageBalance {
14
+ total: string;
15
+ available: string;
16
+ }
17
+
18
+ export interface StorageBalanceBounds {
19
+ min: string;
20
+ max?: string;
21
+ }
22
+
23
+ // NEP-141 Contract Interface
24
+ export interface NEP141Contract {
25
+ // View methods
26
+ ft_balance_of(args: { account_id: string }): Promise<string>;
27
+ ft_total_supply(): Promise<string>;
28
+ ft_metadata(): Promise<FungibleTokenMetadata>;
29
+ storage_balance_of(args: { account_id: string }): Promise<StorageBalance | null>;
30
+ storage_balance_bounds(): Promise<StorageBalanceBounds>;
31
+
32
+ // Change methods
33
+ ft_transfer(
34
+ args: { receiver_id: string; amount: string; memo?: string | null },
35
+ gas?: any, // BN type
36
+ deposit?: any, // BN type
37
+ ): Promise<any>; // Returns transaction result
38
+
39
+ ft_transfer_call(
40
+ args: {
41
+ receiver_id: string;
42
+ amount: string;
43
+ memo?: string | null;
44
+ msg: string;
45
+ },
46
+ gas?: any,
47
+ deposit?: any,
48
+ ): Promise<any>; // Returns transaction result
49
+
50
+ storage_deposit(
51
+ args: { account_id?: string; registration_only?: boolean },
52
+ gas?: any,
53
+ deposit?: any,
54
+ ): Promise<StorageBalance>;
55
+
56
+ storage_withdraw(args: { amount?: string }, gas?: any, deposit?: any): Promise<StorageBalance>;
57
+
58
+ storage_unregister(args: { force?: boolean }, gas?: any, deposit?: any): Promise<boolean>;
59
+ }
60
+
61
+ // Token transfer parameters
62
+ export interface TokenTransferParams {
63
+ recipient: string;
64
+ amount: string;
65
+ memo?: string;
66
+ }
@@ -0,0 +1,57 @@
1
+ import type {
2
+ ChainSigner,
3
+ DerivationPathArray,
4
+ GenericCreateTransactionParams,
5
+ GenericTransferParams,
6
+ } from "@swapkit/helpers";
7
+ import type { KeyPairSigner, Signer, transactions } from "near-api-js";
8
+
9
+ interface NearKeyPairSigner
10
+ extends KeyPairSigner,
11
+ Omit<
12
+ ChainSigner<transactions.Transaction, transactions.SignedTransaction>,
13
+ "signTransaction"
14
+ > {}
15
+
16
+ interface NearGeneralSigner
17
+ extends Signer,
18
+ Omit<
19
+ ChainSigner<transactions.Transaction, transactions.SignedTransaction>,
20
+ "signTransaction"
21
+ > {}
22
+
23
+ // Extend both ChainSigner and NEAR's Signer class, omitting signTransaction
24
+ export type NearSigner = NearKeyPairSigner | NearGeneralSigner;
25
+
26
+ export type NearToolboxParams =
27
+ | { signer?: NearSigner; accountId?: string }
28
+ | { phrase?: string; index?: number; derivationPath?: DerivationPathArray };
29
+
30
+ export interface NearTransferParams extends GenericTransferParams {}
31
+
32
+ export interface NearConfig {
33
+ networkId: "mainnet" | "testnet" | "betanet";
34
+ nodeUrl: string;
35
+ walletUrl?: string;
36
+ helperUrl?: string;
37
+ keyStore?: any;
38
+ }
39
+
40
+ export interface NearFunctionCallParams {
41
+ contractId: string;
42
+ methodName: string;
43
+ args: Uint8Array | Record<string, any>;
44
+ deposit?: bigint | string | number;
45
+ gas?: bigint | string | number;
46
+ }
47
+
48
+ export interface NearCreateTransactionParams extends GenericCreateTransactionParams {
49
+ // NEAR-specific options
50
+ attachedDeposit?: string;
51
+ // Function call parameters
52
+ functionCall?: {
53
+ methodName: string;
54
+ args: object;
55
+ attachedDeposit: string;
56
+ };
57
+ }
@@ -5,7 +5,13 @@ import type {
5
5
  StateEntityFungiblesPageRequest,
6
6
  StateEntityFungiblesPageResponse,
7
7
  } from "@radixdlt/babylon-gateway-api-sdk";
8
- import { AssetValue, Chain, SKConfig, type SKConfigIntegrations } from "@swapkit/helpers";
8
+ import {
9
+ AssetValue,
10
+ Chain,
11
+ SKConfig,
12
+ type SKConfigIntegrations,
13
+ SwapKitError,
14
+ } from "@swapkit/helpers";
9
15
 
10
16
  export type RadixWallet = Awaited<ReturnType<typeof RadixToolbox>>;
11
17
 
@@ -144,7 +150,7 @@ export const RadixToolbox = async ({
144
150
  networkApi,
145
151
  validateAddress: radixValidateAddress,
146
152
  signAndBroadcast: (() => {
147
- throw new Error("Not implemented");
153
+ throw new SwapKitError("toolbox_radix_method_not_supported", { method: "signAndBroadcast" });
148
154
  }) as (params: any) => Promise<string>,
149
155
  };
150
156
  };