@swapkit/toolboxes 4.0.0-beta.50 → 4.0.0-beta.52
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/src/cosmos/index.cjs +2 -2
- package/dist/src/cosmos/index.cjs.map +10 -6
- package/dist/src/cosmos/index.js +2 -2
- package/dist/src/cosmos/index.js.map +10 -6
- package/dist/src/evm/index.cjs +2 -2
- package/dist/src/evm/index.cjs.map +10 -8
- package/dist/src/evm/index.js +2 -2
- package/dist/src/evm/index.js.map +10 -8
- package/dist/src/index.cjs +4 -2
- package/dist/src/index.cjs.map +54 -4
- package/dist/src/index.js +4 -2
- package/dist/src/index.js.map +54 -4
- package/dist/src/near/index.cjs +2 -2
- package/dist/src/near/index.cjs.map +7 -5
- package/dist/src/near/index.js +2 -2
- package/dist/src/near/index.js.map +7 -5
- package/dist/src/radix/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +2 -2
- package/dist/src/radix/index.js +2 -2
- package/dist/src/radix/index.js.map +2 -2
- package/dist/src/ripple/index.cjs +2 -2
- package/dist/src/ripple/index.cjs.map +2 -2
- package/dist/src/ripple/index.js +2 -2
- package/dist/src/ripple/index.js.map +2 -2
- package/dist/src/solana/index.cjs +2 -2
- package/dist/src/solana/index.cjs.map +5 -4
- package/dist/src/solana/index.js +2 -2
- package/dist/src/solana/index.js.map +5 -4
- package/dist/src/substrate/index.cjs +2 -2
- package/dist/src/substrate/index.cjs.map +6 -5
- package/dist/src/substrate/index.js +2 -2
- package/dist/src/substrate/index.js.map +6 -5
- package/dist/src/tron/index.cjs +2 -2
- package/dist/src/tron/index.cjs.map +6 -5
- package/dist/src/tron/index.js +2 -2
- package/dist/src/tron/index.js.map +6 -5
- package/dist/src/utxo/index.cjs +4 -4
- package/dist/src/utxo/index.cjs.map +10 -7
- package/dist/src/utxo/index.js +4 -4
- package/dist/src/utxo/index.js.map +10 -7
- package/{src/cosmos/index.ts → dist/types/cosmos/index.d.ts} +1 -0
- package/dist/types/cosmos/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
- package/{src/cosmos/thorchainUtils/index.ts → dist/types/cosmos/thorchainUtils/index.d.ts} +1 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +66 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts +93 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/index.d.ts +14 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
- package/{src/cosmos/types.ts → dist/types/cosmos/types.d.ts} +24 -29
- package/dist/types/cosmos/types.d.ts.map +1 -0
- package/dist/types/cosmos/util.d.ts +68 -0
- package/dist/types/cosmos/util.d.ts.map +1 -0
- package/dist/types/evm/api.d.ts +8 -0
- package/dist/types/evm/api.d.ts.map +1 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
- package/dist/types/evm/helpers.d.ts +20 -0
- package/dist/types/evm/helpers.d.ts.map +1 -0
- package/{src/evm/index.ts → dist/types/evm/index.d.ts} +1 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +82 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
- package/dist/types/evm/toolbox/evm.d.ts +367 -0
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
- package/dist/types/evm/toolbox/index.d.ts +89 -0
- package/dist/types/evm/toolbox/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/op.d.ts +62 -0
- package/dist/types/evm/toolbox/op.d.ts.map +1 -0
- package/dist/types/evm/types.d.ts +98 -0
- package/dist/types/evm/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/helpers/core.d.ts +15 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -0
- package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
- package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
- package/dist/types/near/helpers/nep141.d.ts +72 -0
- package/dist/types/near/helpers/nep141.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +10 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/toolbox.d.ts +4 -0
- package/dist/types/near/toolbox.d.ts.map +1 -0
- package/dist/types/near/types/contract.d.ts +38 -0
- package/dist/types/near/types/contract.d.ts.map +1 -0
- package/dist/types/near/types/nep141.d.ts +56 -0
- package/dist/types/near/types/nep141.d.ts.map +1 -0
- package/dist/types/near/types/toolbox.d.ts +57 -0
- package/dist/types/near/types/toolbox.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +44 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +14 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/ripple/index.d.ts +43 -0
- package/dist/types/ripple/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +37 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/toolbox.d.ts +41 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -0
- package/dist/types/substrate/balance.d.ts +17 -0
- package/dist/types/substrate/balance.d.ts.map +1 -0
- package/{src/substrate/index.ts → dist/types/substrate/index.d.ts} +1 -0
- package/dist/types/substrate/index.d.ts.map +1 -0
- package/dist/types/substrate/substrate.d.ts +148 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -0
- package/dist/types/substrate/types.d.ts +100 -0
- package/dist/types/substrate/types.d.ts.map +1 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
- package/dist/types/tron/helpers/trongrid.d.ts +8 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
- package/dist/types/tron/index.d.ts +6 -0
- package/dist/types/tron/index.d.ts.map +1 -0
- package/dist/types/tron/toolbox.d.ts +26 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -0
- package/dist/types/tron/types.d.ts +101 -0
- package/dist/types/tron/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +18 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utxo/helpers/api.d.ts +133 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
- package/dist/types/utxo/helpers/coinselect.d.ts +16 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
- package/{src/utxo/helpers/index.ts → dist/types/utxo/helpers/index.d.ts} +1 -0
- package/dist/types/utxo/helpers/index.d.ts.map +1 -0
- package/dist/types/utxo/helpers/txSize.d.ts +21 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
- package/{src/utxo/index.ts → dist/types/utxo/index.d.ts} +1 -0
- package/dist/types/utxo/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +104 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/index.d.ts +50 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/utxo.d.ts +102 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/zcash.d.ts +83 -0
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
- package/dist/types/utxo/types.d.ts +46 -0
- package/dist/types/utxo/types.d.ts.map +1 -0
- package/package.json +15 -18
- package/dist/chunk-0h4xdrwz.js +0 -5
- package/dist/chunk-0h4xdrwz.js.map +0 -10
- package/dist/chunk-4yap1fvd.js +0 -4
- package/dist/chunk-4yap1fvd.js.map +0 -10
- package/dist/chunk-9bqegm61.js +0 -4
- package/dist/chunk-9bqegm61.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-fazw0jvt.js.map +0 -9
- package/dist/chunk-fjfxga2v.js +0 -4
- package/dist/chunk-fjfxga2v.js.map +0 -10
- package/dist/chunk-s47y8512.js +0 -5
- package/dist/chunk-s47y8512.js.map +0 -9
- package/dist/chunk-vtd17cje.js +0 -4
- package/dist/chunk-vtd17cje.js.map +0 -10
- package/dist/chunk-zcdeg6h9.js +0 -5
- package/dist/chunk-zcdeg6h9.js.map +0 -10
- package/src/cosmos/thorchainUtils/addressFormat.ts +0 -26
- package/src/cosmos/thorchainUtils/messages.ts +0 -262
- package/src/cosmos/thorchainUtils/registry.ts +0 -44
- package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +0 -2800
- package/src/cosmos/thorchainUtils/types/client-types.ts +0 -73
- package/src/cosmos/thorchainUtils/types/index.ts +0 -1
- package/src/cosmos/toolbox/cosmos.ts +0 -375
- package/src/cosmos/toolbox/index.ts +0 -33
- package/src/cosmos/toolbox/thorchain.ts +0 -313
- package/src/cosmos/util.ts +0 -266
- package/src/evm/__tests__/address-validation.test.ts +0 -86
- package/src/evm/__tests__/ethereum.test.ts +0 -141
- package/src/evm/api.ts +0 -21
- package/src/evm/contracts/eth/multicall.ts +0 -165
- package/src/evm/contracts/op/gasOracle.ts +0 -151
- package/src/evm/helpers.ts +0 -194
- package/src/evm/toolbox/baseEVMToolbox.ts +0 -762
- package/src/evm/toolbox/evm.ts +0 -66
- package/src/evm/toolbox/index.ts +0 -52
- package/src/evm/toolbox/op.ts +0 -131
- package/src/evm/types.ts +0 -146
- package/src/index.ts +0 -263
- package/src/near/__tests__/core.test.ts +0 -80
- package/src/near/helpers/contractFactory.ts +0 -22
- package/src/near/helpers/core.ts +0 -91
- package/src/near/helpers/gasEstimation.ts +0 -110
- package/src/near/helpers/index.ts +0 -5
- package/src/near/helpers/nep141.ts +0 -110
- package/src/near/index.ts +0 -24
- package/src/near/toolbox.ts +0 -509
- package/src/near/types/contract.ts +0 -48
- package/src/near/types/nep141.ts +0 -66
- package/src/near/types.ts +0 -57
- package/src/radix/index.ts +0 -156
- package/src/ripple/index.ts +0 -192
- package/src/solana/index.ts +0 -55
- package/src/solana/toolbox.ts +0 -433
- package/src/substrate/balance.ts +0 -92
- package/src/substrate/substrate.ts +0 -320
- package/src/substrate/types.ts +0 -120
- package/src/tron/__tests__/toolbox.test.ts +0 -147
- package/src/tron/helpers/trc20.abi.ts +0 -107
- package/src/tron/helpers/trongrid.ts +0 -54
- package/src/tron/index.ts +0 -17
- package/src/tron/toolbox.ts +0 -650
- package/src/tron/types.ts +0 -120
- package/src/utils.ts +0 -27
- package/src/utxo/__tests__/zcash-integration.test.ts +0 -114
- package/src/utxo/helpers/api.ts +0 -560
- package/src/utxo/helpers/bchaddrjs.ts +0 -183
- package/src/utxo/helpers/coinselect.ts +0 -98
- package/src/utxo/helpers/txSize.ts +0 -104
- package/src/utxo/toolbox/bitcoinCash.ts +0 -320
- package/src/utxo/toolbox/index.ts +0 -90
- package/src/utxo/toolbox/utxo.ts +0 -525
- package/src/utxo/toolbox/zcash.ts +0 -208
- package/src/utxo/types.ts +0 -57
package/src/near/toolbox.ts
DELETED
|
@@ -1,509 +0,0 @@
|
|
|
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
|
-
getValidateNearAddress,
|
|
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
|
-
const nearValidateAddress = await getValidateNearAddress();
|
|
78
|
-
|
|
79
|
-
if (!nearValidateAddress(recipient)) {
|
|
80
|
-
throw new SwapKitError("toolbox_near_invalid_address");
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const account = await getAccount();
|
|
84
|
-
|
|
85
|
-
if (assetValue.isGasAsset === false) {
|
|
86
|
-
// NEP-141 token transfer
|
|
87
|
-
const contractId = assetValue.address;
|
|
88
|
-
if (!contractId) {
|
|
89
|
-
throw new SwapKitError("toolbox_near_missing_contract_address");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const amount = assetValue.getBaseValue("string");
|
|
93
|
-
|
|
94
|
-
return callFunction({
|
|
95
|
-
contractId,
|
|
96
|
-
methodName: "ft_transfer",
|
|
97
|
-
args: {
|
|
98
|
-
receiver_id: recipient,
|
|
99
|
-
amount,
|
|
100
|
-
memo: params.memo,
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
try {
|
|
106
|
-
const transferAmount = assetValue.getBaseValue("string");
|
|
107
|
-
|
|
108
|
-
const result = await account.transfer({
|
|
109
|
-
receiverId: recipient,
|
|
110
|
-
amount: transferAmount,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
return result.transaction_outcome.id;
|
|
114
|
-
} catch (error) {
|
|
115
|
-
throw new SwapKitError("toolbox_near_transfer_failed", { error });
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function createTransaction(params: NearCreateTransactionParams) {
|
|
120
|
-
const { recipient, assetValue, memo, feeRate: gas, attachedDeposit, sender: signerId } = params;
|
|
121
|
-
const validateNearAddress = await getValidateNearAddress();
|
|
122
|
-
|
|
123
|
-
if (!validateNearAddress(recipient)) {
|
|
124
|
-
throw new SwapKitError("toolbox_near_invalid_address", { address: recipient });
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!validateNearAddress(signerId)) {
|
|
128
|
-
throw new SwapKitError("toolbox_near_invalid_address", { address: signerId });
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Handle NEP-141 token transfers
|
|
132
|
-
if (!assetValue.isGasAsset) {
|
|
133
|
-
const contractId = assetValue.address;
|
|
134
|
-
if (!contractId) {
|
|
135
|
-
throw new SwapKitError("toolbox_near_missing_contract_address");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return createContractFunctionCall({
|
|
139
|
-
sender: signerId,
|
|
140
|
-
contractId,
|
|
141
|
-
methodName: "ft_transfer",
|
|
142
|
-
args: {
|
|
143
|
-
receiver_id: recipient,
|
|
144
|
-
amount: assetValue.getBaseValue("string"),
|
|
145
|
-
memo: memo || null,
|
|
146
|
-
},
|
|
147
|
-
gas: gas.toString() || "100000000000000", // 100 TGas default
|
|
148
|
-
attachedDeposit: "1", // 1 yoctoNEAR required for NEP-141 transfers
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Native NEAR transfer
|
|
153
|
-
const { publicKey, nonce } = await getFullAccessPublicKey(provider, signerId);
|
|
154
|
-
const baseAmount = assetValue.getBaseValue("bigint");
|
|
155
|
-
|
|
156
|
-
const { SCHEMA } = await import("near-api-js/lib/transaction");
|
|
157
|
-
const { transactions, utils } = await import("near-api-js");
|
|
158
|
-
|
|
159
|
-
const txActions = [transactions.transfer(baseAmount)];
|
|
160
|
-
|
|
161
|
-
if (memo && attachedDeposit) {
|
|
162
|
-
txActions.push(
|
|
163
|
-
transactions.functionCall("memo", { memo }, BigInt(gas), BigInt(attachedDeposit)),
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const block = await provider.block({ finality: "final" });
|
|
168
|
-
const blockHash = utils.serialize.base_decode(block.header.hash);
|
|
169
|
-
|
|
170
|
-
const transaction = transactions.createTransaction(
|
|
171
|
-
signerId,
|
|
172
|
-
publicKey,
|
|
173
|
-
recipient,
|
|
174
|
-
nonce,
|
|
175
|
-
txActions,
|
|
176
|
-
blockHash,
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
const serializedTx = utils.serialize.serialize(SCHEMA.Transaction, transaction);
|
|
180
|
-
const serializedBase64 = Buffer.from(serializedTx).toString("base64");
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
serialized: serializedBase64,
|
|
184
|
-
publicKey: publicKey.toString(),
|
|
185
|
-
details: {
|
|
186
|
-
signerId,
|
|
187
|
-
nonce: nonce,
|
|
188
|
-
blockHash: utils.serialize.base_encode(blockHash),
|
|
189
|
-
},
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function createContractFunctionCall(params: {
|
|
194
|
-
sender: string;
|
|
195
|
-
contractId: string;
|
|
196
|
-
methodName: string;
|
|
197
|
-
args: any;
|
|
198
|
-
gas: string;
|
|
199
|
-
attachedDeposit: string;
|
|
200
|
-
}) {
|
|
201
|
-
const { sender: accountId } = params;
|
|
202
|
-
|
|
203
|
-
const { publicKey, nonce } = await getFullAccessPublicKey(provider, accountId);
|
|
204
|
-
|
|
205
|
-
const { SCHEMA } = await import("near-api-js/lib/transaction");
|
|
206
|
-
const { transactions, utils } = await import("near-api-js");
|
|
207
|
-
const block = await provider.block({ finality: "final" });
|
|
208
|
-
const blockHash = utils.serialize.base_decode(block.header.hash);
|
|
209
|
-
|
|
210
|
-
const actions = [
|
|
211
|
-
transactions.functionCall(
|
|
212
|
-
params.methodName,
|
|
213
|
-
Buffer.from(JSON.stringify(params.args)),
|
|
214
|
-
BigInt(params.gas),
|
|
215
|
-
BigInt(params.attachedDeposit),
|
|
216
|
-
),
|
|
217
|
-
];
|
|
218
|
-
|
|
219
|
-
const transaction = transactions.createTransaction(
|
|
220
|
-
accountId,
|
|
221
|
-
publicKey,
|
|
222
|
-
params.contractId,
|
|
223
|
-
nonce,
|
|
224
|
-
actions,
|
|
225
|
-
blockHash,
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
const serializedTx = utils.serialize.serialize(SCHEMA.Transaction, transaction);
|
|
229
|
-
const serializedBase64 = Buffer.from(serializedTx).toString("base64");
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
serialized: serializedBase64,
|
|
233
|
-
publicKey: publicKey.toString(),
|
|
234
|
-
details: {
|
|
235
|
-
signerId: accountId,
|
|
236
|
-
receiverId: params.contractId,
|
|
237
|
-
methodName: params.methodName,
|
|
238
|
-
nonce: nonce,
|
|
239
|
-
blockHash: utils.serialize.base_encode(blockHash),
|
|
240
|
-
},
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
async function signTransaction(transaction: Transaction) {
|
|
245
|
-
if (!signer) {
|
|
246
|
-
throw new SwapKitError("toolbox_near_no_signer");
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Use the new signer interface method
|
|
250
|
-
const [_hash, signedTx] = await signer.signTransaction(transaction);
|
|
251
|
-
return signedTx;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async function broadcastTransaction(signedTransaction: SignedTransaction) {
|
|
255
|
-
const result = await provider.sendTransaction(signedTransaction);
|
|
256
|
-
return result.transaction.hash;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
async function estimateTransactionFee(params: NearTransferParams | NearGasEstimateParams) {
|
|
260
|
-
if ("assetValue" in params) {
|
|
261
|
-
const baseTransferCost = "115123062500"; // gas units for transfer
|
|
262
|
-
const receiptCreationCost = "108059500000"; // gas units for receipt
|
|
263
|
-
|
|
264
|
-
const totalGasUnits = BigInt(baseTransferCost) + BigInt(receiptCreationCost);
|
|
265
|
-
|
|
266
|
-
const gasPrice = await getCurrentGasPrice();
|
|
267
|
-
|
|
268
|
-
// NEAR doesn't support fee multipliers - gas price is fixed by the network
|
|
269
|
-
const totalCostYocto = totalGasUnits * BigInt(gasPrice.toString());
|
|
270
|
-
|
|
271
|
-
return AssetValue.from({
|
|
272
|
-
chain: Chain.Near,
|
|
273
|
-
value: totalCostYocto.toString(),
|
|
274
|
-
fromBaseDecimal: BaseDecimal[Chain.Near],
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Handle new gas estimation params
|
|
279
|
-
const account = signer ? await getAccount() : undefined;
|
|
280
|
-
return estimateGas(params, account);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
async function getCurrentGasPrice() {
|
|
284
|
-
try {
|
|
285
|
-
const result = await provider.query({
|
|
286
|
-
request_type: "call_function",
|
|
287
|
-
finality: "final",
|
|
288
|
-
account_id: "system",
|
|
289
|
-
method_name: "gas_price",
|
|
290
|
-
args_base64: "",
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
return result;
|
|
294
|
-
} catch {
|
|
295
|
-
return "100000000"; // 0.0001 NEAR per Tgas
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function createSubAccount(subAccountId: string, publicKey: string, initialBalance: string) {
|
|
300
|
-
if (!signer) {
|
|
301
|
-
throw new SwapKitError("toolbox_near_no_signer");
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const account = await getAccount();
|
|
305
|
-
const { utils } = await import("near-api-js");
|
|
306
|
-
|
|
307
|
-
const balanceInYocto = utils.format.parseNearAmount(initialBalance) || "0";
|
|
308
|
-
|
|
309
|
-
const result = await account.createAccount(
|
|
310
|
-
subAccountId,
|
|
311
|
-
utils.PublicKey.fromString(publicKey),
|
|
312
|
-
BigInt(balanceInYocto),
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
return result.transaction.hash;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
async function callFunction(params: NearFunctionCallParams) {
|
|
319
|
-
try {
|
|
320
|
-
if (!signer) {
|
|
321
|
-
throw new SwapKitError("toolbox_near_no_signer");
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const { transactions } = await import("near-api-js");
|
|
325
|
-
|
|
326
|
-
const { contractId, methodName, args, deposit } = params;
|
|
327
|
-
const account = await getAccount();
|
|
328
|
-
|
|
329
|
-
const estimatedGas = await estimateGas({
|
|
330
|
-
methodName,
|
|
331
|
-
args: args || {},
|
|
332
|
-
contractId,
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
const functionAction = transactions.functionCall(
|
|
336
|
-
methodName,
|
|
337
|
-
args || {},
|
|
338
|
-
estimatedGas.getBaseValue("bigint"),
|
|
339
|
-
BigInt(deposit || "0"),
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
const result = await account.signAndSendTransaction({
|
|
343
|
-
receiverId: contractId,
|
|
344
|
-
actions: [functionAction],
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
return result.transaction_outcome.id;
|
|
348
|
-
} catch (error) {
|
|
349
|
-
throw new SwapKitError("toolbox_near_transfer_failed", { error });
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Create typed contract interface
|
|
354
|
-
async function createContract(contractInterface: NearContractInterface) {
|
|
355
|
-
const { createNearContract } = await import("./helpers/contractFactory");
|
|
356
|
-
const account = await getAccount();
|
|
357
|
-
|
|
358
|
-
return createNearContract({
|
|
359
|
-
account,
|
|
360
|
-
contractId: contractInterface.contractId,
|
|
361
|
-
viewMethods: contractInterface.viewMethods,
|
|
362
|
-
changeMethods: contractInterface.changeMethods,
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
async function executeBatchTransaction(batch: { receiverId: string; actions: any[] }) {
|
|
367
|
-
if (!signer) {
|
|
368
|
-
throw new SwapKitError("toolbox_near_no_signer");
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
if (batch.actions.length === 0) {
|
|
372
|
-
throw new SwapKitError("toolbox_near_empty_batch");
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const account = await getAccount();
|
|
376
|
-
|
|
377
|
-
// Use account.signAndSendTransaction for batch operations
|
|
378
|
-
const result = await account.signAndSendTransaction({
|
|
379
|
-
receiverId: batch.receiverId,
|
|
380
|
-
actions: batch.actions,
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
return result.transaction.hash;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
async function nep141(contractId: string) {
|
|
387
|
-
const account = await getAccount();
|
|
388
|
-
return createNEP141Token({ contractId, account });
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
async function getBalance(address: string) {
|
|
392
|
-
try {
|
|
393
|
-
const account = await getAccount(address);
|
|
394
|
-
|
|
395
|
-
let nativeBalance: AssetValue;
|
|
396
|
-
try {
|
|
397
|
-
const value = await account.getBalance();
|
|
398
|
-
|
|
399
|
-
nativeBalance = AssetValue.from({
|
|
400
|
-
chain: Chain.Near,
|
|
401
|
-
value,
|
|
402
|
-
fromBaseDecimal: BaseDecimal[Chain.Near],
|
|
403
|
-
});
|
|
404
|
-
} catch {
|
|
405
|
-
nativeBalance = AssetValue.from({
|
|
406
|
-
chain: Chain.Near,
|
|
407
|
-
value: "0",
|
|
408
|
-
fromBaseDecimal: BaseDecimal[Chain.Near],
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// // Then, fetch token balances from API
|
|
413
|
-
// let tokenBalances: AssetValue[] = [];
|
|
414
|
-
// try {
|
|
415
|
-
// const apiBalances = await SwapKitApi.getChainBalance({
|
|
416
|
-
// chain: Chain.Near,
|
|
417
|
-
// address,
|
|
418
|
-
// scamFilter,
|
|
419
|
-
// });
|
|
420
|
-
|
|
421
|
-
// tokenBalances = apiBalances
|
|
422
|
-
// .filter(({ identifier }) => identifier !== Chain.Near) // Filter out native NEAR
|
|
423
|
-
// .map(({ identifier, value, decimal }) => {
|
|
424
|
-
// return new AssetValue({
|
|
425
|
-
// decimal: decimal || BaseDecimal[Chain.Near],
|
|
426
|
-
// value,
|
|
427
|
-
// identifier,
|
|
428
|
-
// });
|
|
429
|
-
// });
|
|
430
|
-
// } catch (error) {
|
|
431
|
-
// // If API fails, just return on-chain balance
|
|
432
|
-
// console.warn("Failed to fetch token balances from API:", error);
|
|
433
|
-
// }
|
|
434
|
-
|
|
435
|
-
// Merge native balance with token balances
|
|
436
|
-
// return [nativeBalance, ...tokenBalances];
|
|
437
|
-
return [nativeBalance];
|
|
438
|
-
} catch (error) {
|
|
439
|
-
throw new SwapKitError("toolbox_near_balance_failed", { error });
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
async function estimateGas(params: NearGasEstimateParams, account?: Account) {
|
|
444
|
-
const gasInTGas = await match(params)
|
|
445
|
-
.when(isSimpleTransfer, () => GAS_COSTS.SIMPLE_TRANSFER)
|
|
446
|
-
.when(isContractCall, (p) => getContractMethodGas(p.methodName))
|
|
447
|
-
.when(isBatchTransaction, (p) => estimateBatchGas(p.actions))
|
|
448
|
-
.when(isAccountCreation, () => GAS_COSTS.ACCOUNT_CREATION)
|
|
449
|
-
.when(isContractDeployment, () => GAS_COSTS.CONTRACT_DEPLOYMENT)
|
|
450
|
-
.when(isCustomEstimator, (p) => {
|
|
451
|
-
if (!account) {
|
|
452
|
-
throw new SwapKitError("toolbox_near_no_account");
|
|
453
|
-
}
|
|
454
|
-
return p.customEstimator(account);
|
|
455
|
-
})
|
|
456
|
-
.otherwise(() => {
|
|
457
|
-
throw new SwapKitError("toolbox_near_invalid_gas_params");
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
// Convert TGas to gas price in NEAR
|
|
461
|
-
const gasPrice = await getGasPrice();
|
|
462
|
-
const gasInUnits = BigInt(gasInTGas) * BigInt(10 ** 12); // Convert TGas to gas units
|
|
463
|
-
const costInYoctoNear = gasInUnits * BigInt(gasPrice);
|
|
464
|
-
|
|
465
|
-
return AssetValue.from({
|
|
466
|
-
chain: Chain.Near,
|
|
467
|
-
value: costInYoctoNear,
|
|
468
|
-
fromBaseDecimal: BaseDecimal[Chain.Near],
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// Get current gas price from network
|
|
473
|
-
async function getGasPrice() {
|
|
474
|
-
try {
|
|
475
|
-
const result = await provider.gasPrice(null);
|
|
476
|
-
return result.gas_price || "100000000";
|
|
477
|
-
} catch (_error) {
|
|
478
|
-
// Fallback to default
|
|
479
|
-
return "100000000";
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
return {
|
|
484
|
-
getAddress,
|
|
485
|
-
getPublicKey: async () => (signer ? (await signer.getPublicKey()).toString() : ""),
|
|
486
|
-
provider,
|
|
487
|
-
transfer,
|
|
488
|
-
createTransaction,
|
|
489
|
-
createContractFunctionCall,
|
|
490
|
-
estimateTransactionFee,
|
|
491
|
-
broadcastTransaction,
|
|
492
|
-
signTransaction,
|
|
493
|
-
getBalance,
|
|
494
|
-
validateAddress: await getValidateNearAddress(),
|
|
495
|
-
getSignerFromPhrase: (params: {
|
|
496
|
-
phrase: string;
|
|
497
|
-
derivationPath?: DerivationPathArray;
|
|
498
|
-
index?: number;
|
|
499
|
-
}) => getNearSignerFromPhrase(params),
|
|
500
|
-
getSignerFromPrivateKey: getNearSignerFromPrivateKey,
|
|
501
|
-
callFunction,
|
|
502
|
-
createSubAccount,
|
|
503
|
-
createContract,
|
|
504
|
-
executeBatchTransaction,
|
|
505
|
-
nep141,
|
|
506
|
-
getGasPrice,
|
|
507
|
-
estimateGas,
|
|
508
|
-
};
|
|
509
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
};
|
package/src/near/types/nep141.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
}
|
package/src/near/types.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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<typeof transactions.Transaction, typeof transactions.SignedTransaction>,
|
|
13
|
-
"signTransaction"
|
|
14
|
-
> {}
|
|
15
|
-
|
|
16
|
-
interface NearGeneralSigner
|
|
17
|
-
extends Signer,
|
|
18
|
-
Omit<
|
|
19
|
-
ChainSigner<typeof transactions.Transaction, typeof 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
|
-
}
|