@uswap/toolboxes 4.3.6

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 (275) hide show
  1. package/dist/src/cardano/index.cjs +4 -0
  2. package/dist/src/cardano/index.cjs.map +11 -0
  3. package/dist/src/cardano/index.js +4 -0
  4. package/dist/src/cardano/index.js.map +11 -0
  5. package/dist/src/cosmos/index.cjs +4 -0
  6. package/dist/src/cosmos/index.cjs.map +20 -0
  7. package/dist/src/cosmos/index.js +4 -0
  8. package/dist/src/cosmos/index.js.map +20 -0
  9. package/dist/src/evm/index.cjs +4 -0
  10. package/dist/src/evm/index.cjs.map +20 -0
  11. package/dist/src/evm/index.js +4 -0
  12. package/dist/src/evm/index.js.map +20 -0
  13. package/dist/src/index.cjs +5 -0
  14. package/dist/src/index.cjs.map +67 -0
  15. package/dist/src/index.js +5 -0
  16. package/dist/src/index.js.map +67 -0
  17. package/dist/src/near/index.cjs +4 -0
  18. package/dist/src/near/index.cjs.map +16 -0
  19. package/dist/src/near/index.js +4 -0
  20. package/dist/src/near/index.js.map +16 -0
  21. package/dist/src/radix/index.cjs +4 -0
  22. package/dist/src/radix/index.cjs.map +10 -0
  23. package/dist/src/radix/index.js +4 -0
  24. package/dist/src/radix/index.js.map +10 -0
  25. package/dist/src/ripple/index.cjs +4 -0
  26. package/dist/src/ripple/index.cjs.map +10 -0
  27. package/dist/src/ripple/index.js +4 -0
  28. package/dist/src/ripple/index.js.map +10 -0
  29. package/dist/src/solana/index.cjs +4 -0
  30. package/dist/src/solana/index.cjs.map +11 -0
  31. package/dist/src/solana/index.js +4 -0
  32. package/dist/src/solana/index.js.map +11 -0
  33. package/dist/src/substrate/index.cjs +4 -0
  34. package/dist/src/substrate/index.cjs.map +13 -0
  35. package/dist/src/substrate/index.js +4 -0
  36. package/dist/src/substrate/index.js.map +13 -0
  37. package/dist/src/sui/index.cjs +4 -0
  38. package/dist/src/sui/index.cjs.map +11 -0
  39. package/dist/src/sui/index.js +4 -0
  40. package/dist/src/sui/index.js.map +11 -0
  41. package/dist/src/ton/index.cjs +4 -0
  42. package/dist/src/ton/index.cjs.map +11 -0
  43. package/dist/src/ton/index.js +4 -0
  44. package/dist/src/ton/index.js.map +11 -0
  45. package/dist/src/tron/index.cjs +4 -0
  46. package/dist/src/tron/index.cjs.map +13 -0
  47. package/dist/src/tron/index.js +4 -0
  48. package/dist/src/tron/index.js.map +13 -0
  49. package/dist/src/utxo/index.cjs +5 -0
  50. package/dist/src/utxo/index.cjs.map +21 -0
  51. package/dist/src/utxo/index.js +5 -0
  52. package/dist/src/utxo/index.js.map +21 -0
  53. package/dist/types/cardano/index.d.ts +3 -0
  54. package/dist/types/cardano/index.d.ts.map +1 -0
  55. package/dist/types/cardano/toolbox.d.ts +34 -0
  56. package/dist/types/cardano/toolbox.d.ts.map +1 -0
  57. package/dist/types/cardano/types.d.ts +11 -0
  58. package/dist/types/cardano/types.d.ts.map +1 -0
  59. package/dist/types/cosmos/index.d.ts +5 -0
  60. package/dist/types/cosmos/index.d.ts.map +1 -0
  61. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
  62. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
  63. package/dist/types/cosmos/thorchainUtils/index.d.ts +5 -0
  64. package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
  65. package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
  66. package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
  67. package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
  68. package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
  69. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
  70. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
  71. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +63 -0
  72. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
  73. package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
  74. package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
  75. package/dist/types/cosmos/toolbox/cosmos.d.ts +62 -0
  76. package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
  77. package/dist/types/cosmos/toolbox/index.d.ts +15 -0
  78. package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
  79. package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
  80. package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
  81. package/dist/types/cosmos/types.d.ts +49 -0
  82. package/dist/types/cosmos/types.d.ts.map +1 -0
  83. package/dist/types/cosmos/util.d.ts +74 -0
  84. package/dist/types/cosmos/util.d.ts.map +1 -0
  85. package/dist/types/evm/api.d.ts +8 -0
  86. package/dist/types/evm/api.d.ts.map +1 -0
  87. package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
  88. package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
  89. package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
  90. package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
  91. package/dist/types/evm/helpers.d.ts +6 -0
  92. package/dist/types/evm/helpers.d.ts.map +1 -0
  93. package/dist/types/evm/index.d.ts +5 -0
  94. package/dist/types/evm/index.d.ts.map +1 -0
  95. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +83 -0
  96. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
  97. package/dist/types/evm/toolbox/evm.d.ts +767 -0
  98. package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
  99. package/dist/types/evm/toolbox/index.d.ts +7 -0
  100. package/dist/types/evm/toolbox/index.d.ts.map +1 -0
  101. package/dist/types/evm/toolbox/op.d.ts +76 -0
  102. package/dist/types/evm/toolbox/op.d.ts.map +1 -0
  103. package/dist/types/evm/types.d.ts +108 -0
  104. package/dist/types/evm/types.d.ts.map +1 -0
  105. package/dist/types/index.d.ts +75 -0
  106. package/dist/types/index.d.ts.map +1 -0
  107. package/dist/types/near/helpers/core.d.ts +15 -0
  108. package/dist/types/near/helpers/core.d.ts.map +1 -0
  109. package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
  110. package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
  111. package/dist/types/near/helpers/nep141.d.ts +36 -0
  112. package/dist/types/near/helpers/nep141.d.ts.map +1 -0
  113. package/dist/types/near/index.d.ts +10 -0
  114. package/dist/types/near/index.d.ts.map +1 -0
  115. package/dist/types/near/toolbox.d.ts +32 -0
  116. package/dist/types/near/toolbox.d.ts.map +1 -0
  117. package/dist/types/near/types/contract.d.ts +38 -0
  118. package/dist/types/near/types/contract.d.ts.map +1 -0
  119. package/dist/types/near/types/nep141.d.ts +29 -0
  120. package/dist/types/near/types/nep141.d.ts.map +1 -0
  121. package/dist/types/near/types/toolbox.d.ts +51 -0
  122. package/dist/types/near/types/toolbox.d.ts.map +1 -0
  123. package/dist/types/near/types.d.ts +47 -0
  124. package/dist/types/near/types.d.ts.map +1 -0
  125. package/dist/types/radix/index.d.ts +14 -0
  126. package/dist/types/radix/index.d.ts.map +1 -0
  127. package/dist/types/ripple/index.d.ts +46 -0
  128. package/dist/types/ripple/index.d.ts.map +1 -0
  129. package/dist/types/solana/index.d.ts +23 -0
  130. package/dist/types/solana/index.d.ts.map +1 -0
  131. package/dist/types/solana/toolbox.d.ts +51 -0
  132. package/dist/types/solana/toolbox.d.ts.map +1 -0
  133. package/dist/types/substrate/balance.d.ts +17 -0
  134. package/dist/types/substrate/balance.d.ts.map +1 -0
  135. package/dist/types/substrate/index.d.ts +3 -0
  136. package/dist/types/substrate/index.d.ts.map +1 -0
  137. package/dist/types/substrate/substrate.d.ts +148 -0
  138. package/dist/types/substrate/substrate.d.ts.map +1 -0
  139. package/dist/types/substrate/types.d.ts +100 -0
  140. package/dist/types/substrate/types.d.ts.map +1 -0
  141. package/dist/types/sui/index.d.ts +3 -0
  142. package/dist/types/sui/index.d.ts.map +1 -0
  143. package/dist/types/sui/toolbox.d.ts +19 -0
  144. package/dist/types/sui/toolbox.d.ts.map +1 -0
  145. package/dist/types/sui/types.d.ts +16 -0
  146. package/dist/types/sui/types.d.ts.map +1 -0
  147. package/dist/types/ton/index.d.ts +3 -0
  148. package/dist/types/ton/index.d.ts.map +1 -0
  149. package/dist/types/ton/toolbox.d.ts +14 -0
  150. package/dist/types/ton/toolbox.d.ts.map +1 -0
  151. package/dist/types/ton/types.d.ts +22 -0
  152. package/dist/types/ton/types.d.ts.map +1 -0
  153. package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
  154. package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
  155. package/dist/types/tron/helpers/trongrid.d.ts +8 -0
  156. package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
  157. package/dist/types/tron/index.d.ts +6 -0
  158. package/dist/types/tron/index.d.ts.map +1 -0
  159. package/dist/types/tron/toolbox.d.ts +26 -0
  160. package/dist/types/tron/toolbox.d.ts.map +1 -0
  161. package/dist/types/tron/types.d.ts +103 -0
  162. package/dist/types/tron/types.d.ts.map +1 -0
  163. package/dist/types/types.d.ts +26 -0
  164. package/dist/types/types.d.ts.map +1 -0
  165. package/dist/types/utils.d.ts +4 -0
  166. package/dist/types/utils.d.ts.map +1 -0
  167. package/dist/types/utxo/helpers/api.d.ts +101 -0
  168. package/dist/types/utxo/helpers/api.d.ts.map +1 -0
  169. package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
  170. package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
  171. package/dist/types/utxo/helpers/coinselect.d.ts +17 -0
  172. package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
  173. package/dist/types/utxo/helpers/index.d.ts +5 -0
  174. package/dist/types/utxo/helpers/index.d.ts.map +1 -0
  175. package/dist/types/utxo/helpers/txSize.d.ts +21 -0
  176. package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
  177. package/dist/types/utxo/index.d.ts +7 -0
  178. package/dist/types/utxo/index.d.ts.map +1 -0
  179. package/dist/types/utxo/toolbox/bitcoinCash.d.ts +93 -0
  180. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
  181. package/dist/types/utxo/toolbox/index.d.ts +28 -0
  182. package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
  183. package/dist/types/utxo/toolbox/params.d.ts +32 -0
  184. package/dist/types/utxo/toolbox/params.d.ts.map +1 -0
  185. package/dist/types/utxo/toolbox/utxo.d.ts +103 -0
  186. package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
  187. package/dist/types/utxo/toolbox/validators.d.ts +4 -0
  188. package/dist/types/utxo/toolbox/validators.d.ts.map +1 -0
  189. package/dist/types/utxo/toolbox/zcash.d.ts +72 -0
  190. package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
  191. package/dist/types/utxo/types.d.ts +46 -0
  192. package/dist/types/utxo/types.d.ts.map +1 -0
  193. package/package.json +205 -0
  194. package/src/__tests__/address-validation-all-chains.test.ts +162 -0
  195. package/src/__tests__/addressValidator.test.ts +162 -0
  196. package/src/cardano/__tests__/toolbox.test.ts +48 -0
  197. package/src/cardano/index.ts +2 -0
  198. package/src/cardano/toolbox.ts +168 -0
  199. package/src/cardano/types.ts +10 -0
  200. package/src/cosmos/__tests__/toolbox.test.ts +91 -0
  201. package/src/cosmos/index.ts +4 -0
  202. package/src/cosmos/thorchainUtils/addressFormat.ts +22 -0
  203. package/src/cosmos/thorchainUtils/index.ts +4 -0
  204. package/src/cosmos/thorchainUtils/messages.ts +212 -0
  205. package/src/cosmos/thorchainUtils/registry.ts +43 -0
  206. package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +2800 -0
  207. package/src/cosmos/thorchainUtils/types/client-types.ts +54 -0
  208. package/src/cosmos/thorchainUtils/types/index.ts +1 -0
  209. package/src/cosmos/toolbox/cosmos.ts +345 -0
  210. package/src/cosmos/toolbox/index.ts +35 -0
  211. package/src/cosmos/toolbox/thorchain.ts +249 -0
  212. package/src/cosmos/types.ts +48 -0
  213. package/src/cosmos/util.ts +214 -0
  214. package/src/evm/__tests__/address-validation.test.ts +84 -0
  215. package/src/evm/__tests__/ethereum.test.ts +137 -0
  216. package/src/evm/__tests__/signMessage.test.ts +60 -0
  217. package/src/evm/api.ts +10 -0
  218. package/src/evm/contracts/eth/multicall.ts +165 -0
  219. package/src/evm/contracts/op/gasOracle.ts +145 -0
  220. package/src/evm/helpers.ts +73 -0
  221. package/src/evm/index.ts +4 -0
  222. package/src/evm/toolbox/baseEVMToolbox.ts +695 -0
  223. package/src/evm/toolbox/evm.ts +67 -0
  224. package/src/evm/toolbox/index.ts +44 -0
  225. package/src/evm/toolbox/op.ts +156 -0
  226. package/src/evm/types.ts +146 -0
  227. package/src/index.ts +260 -0
  228. package/src/near/__tests__/core.test.ts +70 -0
  229. package/src/near/helpers/core.ts +85 -0
  230. package/src/near/helpers/gasEstimation.ts +96 -0
  231. package/src/near/helpers/nep141.ts +50 -0
  232. package/src/near/index.ts +21 -0
  233. package/src/near/toolbox.ts +421 -0
  234. package/src/near/types/contract.ts +32 -0
  235. package/src/near/types/nep141.ts +34 -0
  236. package/src/near/types/toolbox.ts +55 -0
  237. package/src/near/types.ts +44 -0
  238. package/src/radix/index.ts +132 -0
  239. package/src/ripple/index.ts +179 -0
  240. package/src/solana/index.ts +36 -0
  241. package/src/solana/toolbox.ts +415 -0
  242. package/src/substrate/balance.ts +88 -0
  243. package/src/substrate/index.ts +2 -0
  244. package/src/substrate/substrate.ts +281 -0
  245. package/src/substrate/types.ts +115 -0
  246. package/src/sui/__tests__/toolbox.test.ts +82 -0
  247. package/src/sui/index.ts +2 -0
  248. package/src/sui/toolbox.ts +165 -0
  249. package/src/sui/types.ts +11 -0
  250. package/src/ton/__tests__/toolbox.test.ts +63 -0
  251. package/src/ton/index.ts +2 -0
  252. package/src/ton/toolbox.ts +136 -0
  253. package/src/ton/types.ts +13 -0
  254. package/src/tron/__tests__/toolbox.test.ts +221 -0
  255. package/src/tron/helpers/trc20.abi.ts +107 -0
  256. package/src/tron/helpers/trongrid.ts +53 -0
  257. package/src/tron/index.ts +21 -0
  258. package/src/tron/toolbox.ts +585 -0
  259. package/src/tron/types.ts +83 -0
  260. package/src/types.ts +28 -0
  261. package/src/utils.ts +27 -0
  262. package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
  263. package/src/utxo/helpers/api.ts +471 -0
  264. package/src/utxo/helpers/bchaddrjs.ts +166 -0
  265. package/src/utxo/helpers/coinselect.ts +92 -0
  266. package/src/utxo/helpers/index.ts +4 -0
  267. package/src/utxo/helpers/txSize.ts +137 -0
  268. package/src/utxo/index.ts +6 -0
  269. package/src/utxo/toolbox/bitcoinCash.ts +243 -0
  270. package/src/utxo/toolbox/index.ts +59 -0
  271. package/src/utxo/toolbox/params.ts +18 -0
  272. package/src/utxo/toolbox/utxo.ts +439 -0
  273. package/src/utxo/toolbox/validators.ts +36 -0
  274. package/src/utxo/toolbox/zcash.ts +245 -0
  275. package/src/utxo/types.ts +39 -0
@@ -0,0 +1,421 @@
1
+ import type { Account, Contract } from "@near-js/accounts";
2
+ import type { SignedTransaction, Transaction } from "@near-js/transactions";
3
+ import { AssetValue, Chain, getChainConfig, getRPCUrl, SwapKitError } from "@uswap/helpers";
4
+ import { getBalance } from "../utils";
5
+ import {
6
+ getFullAccessPublicKey,
7
+ getNearSignerFromPhrase,
8
+ getNearSignerFromPrivateKey,
9
+ getValidateNearAddress,
10
+ } from "./helpers/core";
11
+ import {
12
+ estimateBatchGas,
13
+ GAS_COSTS,
14
+ getContractMethodGas,
15
+ isAccountCreation,
16
+ isBatchTransaction,
17
+ isContractCall,
18
+ isContractDeployment,
19
+ isCustomEstimator,
20
+ isSimpleTransfer,
21
+ } from "./helpers/gasEstimation";
22
+ import { createNearContract } from "./helpers/nep141";
23
+ import type {
24
+ NearCreateTransactionParams,
25
+ NearFunctionCallParams,
26
+ NearToolboxParams,
27
+ NearTransferParams,
28
+ } from "./types";
29
+ import type { NearContractInterface, NearGasEstimateParams } from "./types/contract";
30
+ import type { NEP141StorageContract } from "./types/nep141";
31
+ import type {
32
+ BatchTransaction,
33
+ ContractFunctionCallParams,
34
+ CreateActionParams,
35
+ GetSignerFromPhraseParams,
36
+ } from "./types/toolbox";
37
+
38
+ export async function getNearToolbox(toolboxParams?: NearToolboxParams) {
39
+ const { P, match } = await import("ts-pattern");
40
+ const { JsonRpcProvider } = await import("@near-js/providers");
41
+ const signer = await match(toolboxParams)
42
+ .with({ phrase: P.string }, async (params) => {
43
+ const signer = await getNearSignerFromPhrase(params);
44
+ return signer;
45
+ })
46
+ .with({ signer: P.any }, ({ signer }) => signer)
47
+ .otherwise(() => undefined);
48
+
49
+ const url = await getRPCUrl(Chain.Near);
50
+
51
+ const provider = new JsonRpcProvider({ url });
52
+
53
+ async function getAccount(address?: string) {
54
+ const { Account } = await import("@near-js/accounts");
55
+
56
+ const _address = address || (await getAddress());
57
+
58
+ const account = new Account(_address, provider, signer);
59
+
60
+ return account;
61
+ }
62
+
63
+ async function getAddress() {
64
+ if (!signer) {
65
+ throw new SwapKitError("toolbox_near_no_signer");
66
+ }
67
+ const address = await signer.getAddress();
68
+ return address;
69
+ }
70
+
71
+ async function checkStorageBalance(params: { contractId: string; accountId: string }) {
72
+ const contract = await createContract<NEP141StorageContract>({
73
+ changeMethods: [],
74
+ contractId: params.contractId,
75
+ viewMethods: ["storage_balance_of"],
76
+ });
77
+
78
+ return contract.storage_balance_of({ account_id: params.accountId });
79
+ }
80
+
81
+ async function transferTokenWithStorageDeposit(params: {
82
+ recipient: string;
83
+ assetValue: AssetValue;
84
+ memo?: string;
85
+ contractId: string;
86
+ }) {
87
+ const storageDeposit = "1250000000000000000000"; // 0.00125 NEAR default
88
+
89
+ const actions = [
90
+ await createAction({
91
+ args: { account_id: params.recipient },
92
+ attachedDeposit: storageDeposit,
93
+ gas: "150000000000000", // 150 TGas for storage_deposit
94
+ methodName: "storage_deposit",
95
+ }),
96
+ await createAction({
97
+ args: {
98
+ amount: params.assetValue.getBaseValue("string"),
99
+ memo: params.memo || null,
100
+ receiver_id: params.recipient,
101
+ },
102
+ attachedDeposit: "1",
103
+ gas: "150000000000000", // 150 TGas for ft_transfer
104
+ methodName: "ft_transfer",
105
+ }),
106
+ ];
107
+
108
+ return executeBatchTransaction({ actions, receiverId: params.contractId });
109
+ }
110
+
111
+ async function transfer(params: NearTransferParams) {
112
+ if (!signer) {
113
+ throw new SwapKitError("toolbox_near_no_signer");
114
+ }
115
+
116
+ const { assetValue, recipient, memo } = params;
117
+ const sender = await getAddress();
118
+
119
+ // Handle NEP-141 token transfers - check if recipient needs storage
120
+ if (!assetValue.isGasAsset && assetValue.address) {
121
+ const storageBalance = await checkStorageBalance({ accountId: recipient, contractId: assetValue.address });
122
+
123
+ if (!storageBalance) {
124
+ return transferTokenWithStorageDeposit({ assetValue, contractId: assetValue.address, memo, recipient });
125
+ }
126
+ }
127
+
128
+ // Standard transfer (native NEAR or token with registered storage)
129
+ const transaction = await createTransaction({ ...params, sender });
130
+ return signAndSendTransaction(transaction);
131
+ }
132
+
133
+ async function createTransaction(params: NearCreateTransactionParams) {
134
+ const { recipient, assetValue, memo, attachedDeposit, sender: signerId, functionCall } = params;
135
+ const validateNearAddress = await getValidateNearAddress();
136
+
137
+ if (!validateNearAddress(recipient)) {
138
+ throw new SwapKitError("toolbox_near_invalid_address", { recipient: recipient });
139
+ }
140
+
141
+ if (!validateNearAddress(signerId)) {
142
+ throw new SwapKitError("toolbox_near_invalid_address", { signerId: signerId });
143
+ }
144
+
145
+ if (functionCall) {
146
+ return createContractFunctionCall({ ...functionCall, sender: signerId });
147
+ }
148
+
149
+ if (!assetValue.isGasAsset) {
150
+ const contractId = assetValue.address;
151
+ if (!contractId) {
152
+ throw new SwapKitError("toolbox_near_missing_contract_address");
153
+ }
154
+
155
+ return createContractFunctionCall({
156
+ args: { amount: assetValue.getBaseValue("string"), memo: memo || null, receiver_id: recipient },
157
+ attachedDeposit: attachedDeposit || "1",
158
+ contractId,
159
+ gas: "250000000000000",
160
+ methodName: "ft_transfer",
161
+ sender: signerId,
162
+ });
163
+ }
164
+
165
+ const { publicKey, nonce } = await getFullAccessPublicKey(provider, signerId);
166
+ const baseAmount = assetValue.getBaseValue("bigint");
167
+
168
+ const { actionCreators, createTransaction } = await import("@near-js/transactions");
169
+ const { baseDecode } = await import("@near-js/utils");
170
+
171
+ const txActions = [actionCreators.transfer(baseAmount)];
172
+
173
+ if (memo && attachedDeposit) {
174
+ txActions.push(actionCreators.functionCall("memo", { memo }, BigInt("250000000000000"), BigInt(attachedDeposit)));
175
+ }
176
+
177
+ const block = await provider.block({ finality: "final" });
178
+ const blockHash = baseDecode(block.header.hash);
179
+
180
+ return createTransaction(signerId, publicKey, recipient, nonce + 1, txActions, blockHash);
181
+ }
182
+
183
+ function serializeTransaction(transaction: Transaction) {
184
+ const serializedTx = transaction.encode();
185
+ return Buffer.from(serializedTx).toString("base64");
186
+ }
187
+
188
+ async function createContractFunctionCall(params: ContractFunctionCallParams) {
189
+ const { sender: accountId } = params;
190
+
191
+ const { publicKey, nonce } = await getFullAccessPublicKey(provider, accountId);
192
+
193
+ const { createTransaction, actionCreators } = await import("@near-js/transactions");
194
+ const { baseDecode } = await import("@near-js/utils");
195
+ const block = await provider.block({ finality: "final" });
196
+ const blockHash = baseDecode(block.header.hash);
197
+
198
+ const actions = [
199
+ actionCreators.functionCall(
200
+ params.methodName,
201
+ Buffer.from(JSON.stringify(params.args)),
202
+ BigInt(params.gas),
203
+ BigInt(params.attachedDeposit),
204
+ ),
205
+ ];
206
+
207
+ const transaction = createTransaction(accountId, publicKey, params.contractId, nonce + 1, actions, blockHash);
208
+
209
+ return transaction;
210
+ }
211
+
212
+ async function createAction(params: CreateActionParams) {
213
+ const { actionCreators } = await import("@near-js/transactions");
214
+
215
+ const action = actionCreators.functionCall(
216
+ params.methodName,
217
+ Buffer.from(JSON.stringify(params.args)),
218
+ BigInt(params.gas),
219
+ BigInt(params.attachedDeposit),
220
+ );
221
+
222
+ return action;
223
+ }
224
+
225
+ async function signTransaction(transaction: Transaction): Promise<SignedTransaction> {
226
+ if (!signer) {
227
+ throw new SwapKitError("toolbox_near_no_signer");
228
+ }
229
+
230
+ const [_hash, signedTx] = await signer.signTransaction(transaction);
231
+ return signedTx;
232
+ }
233
+
234
+ async function broadcastTransaction(signedTransaction: SignedTransaction) {
235
+ const result = await provider.sendTransaction(signedTransaction);
236
+ return result.transaction.hash;
237
+ }
238
+
239
+ async function signAndSendTransaction(transaction: Transaction) {
240
+ if (!signer) {
241
+ throw new SwapKitError("toolbox_near_no_signer");
242
+ }
243
+
244
+ try {
245
+ const signedTx = await signTransaction(transaction);
246
+ const txHash = await broadcastTransaction(signedTx);
247
+ return txHash;
248
+ } catch {
249
+ return signer.signAndSendTransactions?.({ transactions: [transaction] });
250
+ }
251
+ }
252
+
253
+ async function estimateTransactionFee(params: NearTransferParams | NearGasEstimateParams) {
254
+ if ("assetValue" in params) {
255
+ const baseTransferCost = "115123062500";
256
+ const receiptCreationCost = "108059500000";
257
+
258
+ const totalGasUnits = BigInt(baseTransferCost) + BigInt(receiptCreationCost);
259
+
260
+ const gasPrice = await getCurrentGasPrice();
261
+
262
+ const totalCostYocto = totalGasUnits * BigInt(gasPrice.toString());
263
+ const { baseDecimal } = getChainConfig(Chain.Near);
264
+
265
+ return AssetValue.from({ chain: Chain.Near, fromBaseDecimal: baseDecimal, value: totalCostYocto.toString() });
266
+ }
267
+
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
+ account_id: "system",
276
+ args_base64: "",
277
+ finality: "final",
278
+ method_name: "gas_price",
279
+ request_type: "call_function",
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): Promise<string> {
289
+ if (!signer) {
290
+ throw new SwapKitError("toolbox_near_no_signer");
291
+ }
292
+
293
+ const account = await getAccount();
294
+ const { formatNearAmount } = await import("@near-js/utils");
295
+ const { PublicKey } = await import("@near-js/crypto");
296
+
297
+ const balanceInYocto = formatNearAmount(initialBalance) || "0";
298
+
299
+ const result = await account.createAccount(subAccountId, PublicKey.fromString(publicKey), BigInt(balanceInYocto));
300
+
301
+ return result.transaction.hash;
302
+ }
303
+
304
+ async function callFunction(params: NearFunctionCallParams): Promise<string> {
305
+ try {
306
+ if (!signer) {
307
+ throw new SwapKitError("toolbox_near_no_signer");
308
+ }
309
+
310
+ const { actionCreators } = await import("@near-js/transactions");
311
+
312
+ const { contractId, methodName, args, deposit } = params;
313
+ const account = await getAccount();
314
+
315
+ const estimatedGas = await estimateGas({ args: args || {}, contractId, methodName });
316
+
317
+ const functionAction = actionCreators.functionCall(
318
+ methodName,
319
+ args || {},
320
+ estimatedGas.getBaseValue("bigint"),
321
+ BigInt(deposit || "1"),
322
+ );
323
+
324
+ const result = await account.signAndSendTransaction({ actions: [functionAction], receiverId: contractId });
325
+
326
+ return result.transaction_outcome.id;
327
+ } catch (error) {
328
+ throw new SwapKitError("toolbox_near_transfer_failed", { error });
329
+ }
330
+ }
331
+
332
+ // Create typed contract interface
333
+ async function createContract<T extends Contract = Contract>(contractInterface: NearContractInterface): Promise<T> {
334
+ const account = await getAccount();
335
+
336
+ return createNearContract<T>({
337
+ account,
338
+ changeMethods: contractInterface.changeMethods,
339
+ contractId: contractInterface.contractId,
340
+ viewMethods: contractInterface.viewMethods,
341
+ });
342
+ }
343
+
344
+ async function executeBatchTransaction(batch: BatchTransaction): Promise<string> {
345
+ if (!signer) {
346
+ throw new SwapKitError("toolbox_near_no_signer");
347
+ }
348
+
349
+ if (batch.actions.length === 0) {
350
+ throw new SwapKitError("toolbox_near_empty_batch");
351
+ }
352
+
353
+ const account = await getAccount();
354
+
355
+ // Use account.signAndSendTransaction for batch operations
356
+ const result = await account.signAndSendTransaction({ actions: batch.actions, receiverId: batch.receiverId });
357
+
358
+ return result.transaction.hash;
359
+ }
360
+
361
+ async function estimateGas(params: NearGasEstimateParams, account?: Account) {
362
+ const { baseDecimal } = getChainConfig(Chain.Near);
363
+
364
+ const gasInTGas = await match(params)
365
+ .when(isSimpleTransfer, () => GAS_COSTS.SIMPLE_TRANSFER)
366
+ .when(isContractCall, (p) => getContractMethodGas(p.methodName))
367
+ .when(isBatchTransaction, (p) => estimateBatchGas(p.actions))
368
+ .when(isAccountCreation, () => GAS_COSTS.ACCOUNT_CREATION)
369
+ .when(isContractDeployment, () => GAS_COSTS.CONTRACT_DEPLOYMENT)
370
+ .when(isCustomEstimator, (p) => {
371
+ if (!account) {
372
+ throw new SwapKitError("toolbox_near_no_account");
373
+ }
374
+ return p.customEstimator(account);
375
+ })
376
+ .otherwise(() => {
377
+ throw new SwapKitError("toolbox_near_invalid_gas_params");
378
+ });
379
+
380
+ const gasInUnits = BigInt(gasInTGas) * BigInt(10 ** 12);
381
+ const costInYoctoNear = gasInUnits;
382
+
383
+ return AssetValue.from({ chain: Chain.Near, fromBaseDecimal: baseDecimal, value: costInYoctoNear });
384
+ }
385
+
386
+ // Get current gas price from network
387
+ async function getGasPrice() {
388
+ try {
389
+ const result = await provider.gasPrice(null);
390
+ return result.gas_price || "100000000";
391
+ } catch {
392
+ // Fallback to default
393
+ return "100000000";
394
+ }
395
+ }
396
+
397
+ return {
398
+ broadcastTransaction,
399
+ callFunction,
400
+ createAction,
401
+ createContract,
402
+ createContractFunctionCall,
403
+ createSubAccount,
404
+ createTransaction,
405
+ estimateGas,
406
+ estimateTransactionFee,
407
+ executeBatchTransaction,
408
+ getAddress,
409
+ getBalance: getBalance(Chain.Near),
410
+ getGasPrice,
411
+ getPublicKey: async () => (signer ? (await signer.getPublicKey()).toString() : ""),
412
+ getSignerFromPhrase: (params: GetSignerFromPhraseParams) => getNearSignerFromPhrase(params),
413
+ getSignerFromPrivateKey: getNearSignerFromPrivateKey,
414
+ provider,
415
+ serializeTransaction,
416
+ signAndSendTransaction,
417
+ signTransaction,
418
+ transfer,
419
+ validateAddress: await getValidateNearAddress(),
420
+ };
421
+ }
@@ -0,0 +1,32 @@
1
+ import type { Account } from "@near-js/accounts";
2
+ import type { Action } from "@near-js/transactions";
3
+
4
+ // Custom interface for contract metadata (not in SDK)
5
+ export interface NearContractInterface {
6
+ contractId: string;
7
+ viewMethods: string[];
8
+ changeMethods: string[];
9
+ metadata?: {
10
+ version: string;
11
+ standards: string[]; // e.g., ["nep141", "nep171"]
12
+ };
13
+ }
14
+
15
+ // Enhanced call parameters
16
+ export interface NearCallParams {
17
+ contractId: string;
18
+ methodName: string;
19
+ args?: Record<string, any>;
20
+ gas?: string | bigint;
21
+ attachedDeposit?: string | bigint;
22
+ isView?: boolean;
23
+ }
24
+
25
+ // Gas estimation using discriminated unions for type inference
26
+ export type NearGasEstimateParams =
27
+ | { recipient: string; amount: string }
28
+ | { contractId: string; methodName: string; args?: Record<string, any>; attachedDeposit?: string }
29
+ | { actions: Action[] }
30
+ | { newAccountId: string; publicKey?: string }
31
+ | { contractCode: Uint8Array }
32
+ | { customEstimator: (account: Account) => Promise<string> };
@@ -0,0 +1,34 @@
1
+ import type { Contract } from "@near-js/accounts";
2
+
3
+ // NEP-141 Fungible Token Standard Types
4
+
5
+ export interface FungibleTokenMetadata {
6
+ spec: string; // e.g., "ft-1.0.0"
7
+ name: string; // e.g., "Wrapped NEAR"
8
+ symbol: string; // e.g., "wNEAR"
9
+ icon?: string; // Data URL or IPFS link
10
+ reference?: string; // URL to additional metadata
11
+ reference_hash?: string; // Base64-encoded hash of reference content
12
+ decimals: number; // e.g., 24 for NEAR
13
+ }
14
+
15
+ export interface StorageBalance {
16
+ total: string;
17
+ available: string;
18
+ }
19
+
20
+ export interface StorageBalanceBounds {
21
+ min: string;
22
+ max?: string;
23
+ }
24
+
25
+ export interface NEP141StorageContract extends Contract {
26
+ storage_balance_of: (args: { account_id: string }) => Promise<StorageBalance | null>;
27
+ }
28
+
29
+ // Token transfer parameters
30
+ export interface TokenTransferParams {
31
+ recipient: string;
32
+ amount: string;
33
+ memo?: string;
34
+ }
@@ -0,0 +1,55 @@
1
+ import type { Account, Contract } from "@near-js/accounts";
2
+ import type { JsonRpcProvider } from "@near-js/providers";
3
+ import type { Action, SignedTransaction, Transaction } from "@near-js/transactions";
4
+ import type { AssetValue, DerivationPathArray } from "@uswap/helpers";
5
+ import type { NEP141Token } from "../helpers/nep141";
6
+ import type { NearCreateTransactionParams, NearFunctionCallParams, NearSigner, NearTransferParams } from "../types";
7
+ import type { NearContractInterface, NearGasEstimateParams } from "../types/contract";
8
+
9
+ export interface BatchTransaction {
10
+ receiverId: string;
11
+ actions: Action[];
12
+ }
13
+
14
+ export interface ContractFunctionCallParams {
15
+ sender: string;
16
+ contractId: string;
17
+ methodName: string;
18
+ args: Record<string, any>;
19
+ gas: string;
20
+ attachedDeposit: string;
21
+ }
22
+
23
+ export type CreateActionParams = Pick<ContractFunctionCallParams, "methodName" | "args" | "gas" | "attachedDeposit">;
24
+
25
+ export interface GetSignerFromPhraseParams {
26
+ phrase: string;
27
+ derivationPath?: DerivationPathArray;
28
+ index?: number;
29
+ }
30
+
31
+ export interface NearToolbox {
32
+ getAddress: () => Promise<string>;
33
+ getPublicKey: () => Promise<string>;
34
+ provider: typeof JsonRpcProvider;
35
+ transfer: (params: NearTransferParams) => Promise<string>;
36
+ createAction: (params: CreateActionParams) => Promise<Action>;
37
+ createTransaction: (params: NearCreateTransactionParams) => Promise<Transaction>;
38
+ createContractFunctionCall: (params: ContractFunctionCallParams) => Promise<Transaction>;
39
+ estimateTransactionFee: (params: NearTransferParams | NearGasEstimateParams) => Promise<AssetValue>;
40
+ broadcastTransaction: (signedTransaction: SignedTransaction) => Promise<string>;
41
+ signTransaction: (transaction: Transaction) => Promise<SignedTransaction>;
42
+ getBalance: (address: string) => Promise<AssetValue[]>;
43
+ validateAddress: (address: string) => boolean;
44
+ getSignerFromPhrase: (params: GetSignerFromPhraseParams) => Promise<NearSigner>;
45
+ getSignerFromPrivateKey: (privateKey: string) => Promise<NearSigner>;
46
+ callFunction: (params: NearFunctionCallParams) => Promise<string>;
47
+ createSubAccount: (subAccountId: string, publicKey: string, initialBalance: string) => Promise<string>;
48
+ createContract: <T extends Contract = Contract>(contractInterface: NearContractInterface) => Promise<T>;
49
+ executeBatchTransaction: (batch: BatchTransaction) => Promise<string>;
50
+ nep141: (contractId: string) => Promise<NEP141Token>;
51
+ getGasPrice: () => Promise<string>;
52
+ estimateGas: (params: NearGasEstimateParams, account?: Account) => Promise<AssetValue>;
53
+ serializeTransaction: (params: Transaction) => Promise<string>;
54
+ signAndSendTransaction: (params: Transaction) => Promise<string>;
55
+ }
@@ -0,0 +1,44 @@
1
+ import type { Signer } from "@near-js/signers";
2
+ import type { SignedTransaction, Transaction } from "@near-js/transactions";
3
+ import type {
4
+ ChainSigner,
5
+ DerivationPathArray,
6
+ GenericCreateTransactionParams,
7
+ GenericTransferParams,
8
+ } from "@uswap/helpers";
9
+
10
+ export interface NearSigner extends Signer, Omit<ChainSigner<Transaction, SignedTransaction>, "signTransaction"> {
11
+ signAndSendTransactions?(params: { transactions: Transaction[] }): Promise<string>;
12
+ }
13
+
14
+ export type NearToolboxParams =
15
+ | { signer?: NearSigner; accountId?: string }
16
+ | { phrase?: string; index?: number; derivationPath?: DerivationPathArray };
17
+
18
+ export interface NearTransferParams extends GenericTransferParams {}
19
+
20
+ export interface NearConfig {
21
+ networkId: "mainnet" | "testnet" | "betanet";
22
+ nodeUrl: string;
23
+ walletUrl?: string;
24
+ helperUrl?: string;
25
+ keyStore?: any;
26
+ }
27
+
28
+ export interface NearFunctionCallParams {
29
+ contractId: string;
30
+ methodName: string;
31
+ args: Uint8Array | Record<string, any>;
32
+ deposit?: bigint | string | number;
33
+ gas?: bigint | string | number;
34
+ }
35
+
36
+ export interface NearCreateTransactionParams extends Omit<GenericCreateTransactionParams, "feeRate"> {
37
+ attachedDeposit?: string;
38
+ functionCall?: { methodName: string; args: object; attachedDeposit: string; gas: string; contractId: string };
39
+ }
40
+
41
+ export * from "./toolbox";
42
+ export * from "./types/contract";
43
+ export * from "./types/nep141";
44
+ export * from "./types/toolbox";