@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
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { AssetValue, Chain, SKConfig } from "@swapkit/helpers";
|
|
3
|
-
import { providers } from "near-api-js";
|
|
4
|
-
import { getFullAccessPublicKey } from "../helpers/core";
|
|
5
|
-
import { getNearToolbox } from "../toolbox";
|
|
6
|
-
|
|
7
|
-
const accountId = "ea03292d08136cca439513a33c76af083e5204eceb4ce720320fff84071a447f";
|
|
8
|
-
|
|
9
|
-
const context: {
|
|
10
|
-
provider: providers.JsonRpcProvider;
|
|
11
|
-
toolbox: Awaited<ReturnType<typeof getNearToolbox>>;
|
|
12
|
-
} = {} as any;
|
|
13
|
-
|
|
14
|
-
beforeAll(() => {
|
|
15
|
-
context.provider = new providers.JsonRpcProvider({
|
|
16
|
-
url: SKConfig.get("rpcUrls")[Chain.Near],
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
beforeEach(async () => {
|
|
21
|
-
context.toolbox = await getNearToolbox();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe("NEAR createTransaction", () => {
|
|
25
|
-
test("should retrieve full access public key for valid account", async () => {
|
|
26
|
-
const toolbox = context.toolbox;
|
|
27
|
-
|
|
28
|
-
const transaction = await toolbox.createTransaction({
|
|
29
|
-
recipient: accountId, // Self transfer
|
|
30
|
-
assetValue: AssetValue.from({
|
|
31
|
-
chain: Chain.Near,
|
|
32
|
-
value: "0.001", // Small amount
|
|
33
|
-
}),
|
|
34
|
-
sender: accountId,
|
|
35
|
-
feeRate: 300000000000000, // 300 TGas
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
expect(transaction).toBeDefined();
|
|
39
|
-
expect(transaction.publicKey).toBeDefined();
|
|
40
|
-
expect(transaction.serialized).toBeDefined();
|
|
41
|
-
}, 30000);
|
|
42
|
-
|
|
43
|
-
test("should throw error for invalid account", async () => {
|
|
44
|
-
const provider = context.provider;
|
|
45
|
-
const invalidAccountId = "non-existent-account-12345.testnet";
|
|
46
|
-
|
|
47
|
-
await expect(async () => {
|
|
48
|
-
await getFullAccessPublicKey(provider, invalidAccountId);
|
|
49
|
-
}).toThrow();
|
|
50
|
-
}, 10000);
|
|
51
|
-
|
|
52
|
-
test("should handle network errors gracefully", async () => {
|
|
53
|
-
const invalidProvider = new providers.JsonRpcProvider({ url: "https://invalid-rpc-url.test" });
|
|
54
|
-
|
|
55
|
-
await expect(async () => {
|
|
56
|
-
await getFullAccessPublicKey(invalidProvider, "any-account.testnet");
|
|
57
|
-
}).toThrow();
|
|
58
|
-
}, 10000);
|
|
59
|
-
|
|
60
|
-
test("should work with contract function call transaction", async () => {
|
|
61
|
-
const toolbox = context.toolbox;
|
|
62
|
-
const provider = context.provider;
|
|
63
|
-
|
|
64
|
-
const contractTransaction = await toolbox.createContractFunctionCall({
|
|
65
|
-
sender: accountId,
|
|
66
|
-
contractId: "wrap.testnet", // Known testnet contract
|
|
67
|
-
methodName: "storage_deposit",
|
|
68
|
-
args: { account_id: accountId },
|
|
69
|
-
gas: "300000000000000", // 300 TGas
|
|
70
|
-
attachedDeposit: "1250000000000000000000", // 0.00125 NEAR for storage
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
expect(contractTransaction).toBeDefined();
|
|
74
|
-
expect(contractTransaction.publicKey).toBeDefined();
|
|
75
|
-
|
|
76
|
-
// Verify the public key can be retrieved directly
|
|
77
|
-
const { publicKey } = await getFullAccessPublicKey(provider, accountId);
|
|
78
|
-
expect(publicKey.toString()).toBe(contractTransaction.publicKey);
|
|
79
|
-
}, 30000);
|
|
80
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Account, Contract } from "near-api-js";
|
|
2
|
-
|
|
3
|
-
// Create a Near contract instance
|
|
4
|
-
export async function createNearContract<T extends Contract>({
|
|
5
|
-
account,
|
|
6
|
-
contractId,
|
|
7
|
-
viewMethods,
|
|
8
|
-
changeMethods,
|
|
9
|
-
}: {
|
|
10
|
-
account: Account;
|
|
11
|
-
contractId: string;
|
|
12
|
-
viewMethods: string[];
|
|
13
|
-
changeMethods: string[];
|
|
14
|
-
}): Promise<T> {
|
|
15
|
-
const { Contract } = await import("near-api-js");
|
|
16
|
-
|
|
17
|
-
return new Contract(account, contractId, {
|
|
18
|
-
viewMethods,
|
|
19
|
-
changeMethods,
|
|
20
|
-
useLocalViewExecution: true, // Enable local view execution for efficiency
|
|
21
|
-
}) as T;
|
|
22
|
-
}
|
package/src/near/helpers/core.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { Provider } from "@near-js/providers";
|
|
2
|
-
import { type DerivationPathArray, SwapKitError, derivationPathToString } from "@swapkit/helpers";
|
|
3
|
-
import { type KeyPair, KeyPairSigner } from "near-api-js";
|
|
4
|
-
import type { NearSigner } from "../types";
|
|
5
|
-
|
|
6
|
-
export async function getValidateNearAddress() {
|
|
7
|
-
const { validateAccountId } = await import("near-sdk-js");
|
|
8
|
-
return (address: string) => {
|
|
9
|
-
// Use the official NEAR SDK validation function if available
|
|
10
|
-
try {
|
|
11
|
-
return validateAccountId(address);
|
|
12
|
-
} catch {
|
|
13
|
-
const ACCOUNT_ID_REGEX = /^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/;
|
|
14
|
-
|
|
15
|
-
return address.length >= 2 && address.length <= 64 && ACCOUNT_ID_REGEX.test(address);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export async function getNearSignerFromPhrase(params: {
|
|
21
|
-
phrase: string;
|
|
22
|
-
derivationPath?: DerivationPathArray;
|
|
23
|
-
index?: number;
|
|
24
|
-
}) {
|
|
25
|
-
const { parseSeedPhrase } = await import("near-seed-phrase");
|
|
26
|
-
const { KeyPair } = await import("near-api-js");
|
|
27
|
-
|
|
28
|
-
// Handle derivation path logic here
|
|
29
|
-
// NEAR uses a 3-level derivation path: m/44'/397'/index'
|
|
30
|
-
const index = params.index || 0;
|
|
31
|
-
const derivationPath = params.derivationPath
|
|
32
|
-
? derivationPathToString(params.derivationPath.slice(0, 3) as [number, number, number])
|
|
33
|
-
: `m/44'/397'/${index}'`;
|
|
34
|
-
|
|
35
|
-
const { secretKey } = parseSeedPhrase(params.phrase, derivationPath);
|
|
36
|
-
const keyPair = KeyPair.fromString(secretKey as any);
|
|
37
|
-
|
|
38
|
-
return createNearSignerFromKeyPair(keyPair);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function getNearSignerFromPrivateKey(privateKey: string) {
|
|
42
|
-
const { KeyPair } = await import("near-api-js/lib/utils");
|
|
43
|
-
const keyPair = KeyPair.fromString(privateKey as any);
|
|
44
|
-
return createNearSignerFromKeyPair(keyPair);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
class SKKeyPairSigner extends KeyPairSigner {
|
|
48
|
-
#keyPair: KeyPair;
|
|
49
|
-
|
|
50
|
-
constructor(keyPair: KeyPair) {
|
|
51
|
-
super(keyPair);
|
|
52
|
-
this.#keyPair = keyPair;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
getAddress(): Promise<string> {
|
|
56
|
-
// For implicit accounts, derive account ID from public key
|
|
57
|
-
// NEAR implicit accounts use hex representation of the public key
|
|
58
|
-
const publicKey = this.#keyPair.getPublicKey();
|
|
59
|
-
const hexAddress = Buffer.from(publicKey.data).toString("hex");
|
|
60
|
-
return Promise.resolve(hexAddress);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function createNearSignerFromKeyPair(keyPair: KeyPair): NearSigner {
|
|
65
|
-
const keyPairSigner = new SKKeyPairSigner(keyPair);
|
|
66
|
-
|
|
67
|
-
return keyPairSigner;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export async function getFullAccessPublicKey(provider: Provider, accountId: string) {
|
|
71
|
-
// Get the first full access key for the account
|
|
72
|
-
const response = await provider.query({
|
|
73
|
-
request_type: "view_access_key_list",
|
|
74
|
-
finality: "final",
|
|
75
|
-
account_id: accountId,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
const fullAccessKey = (response as any).keys.find(
|
|
79
|
-
(key: any) => key.access_key.permission === "FullAccess",
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
if (!fullAccessKey) {
|
|
83
|
-
throw new SwapKitError("toolbox_near_invalid_address");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const { utils } = await import("near-api-js");
|
|
87
|
-
const publicKey = utils.PublicKey.fromString(fullAccessKey.public_key);
|
|
88
|
-
const nonce = (fullAccessKey.access_key.nonce as number) || 0;
|
|
89
|
-
|
|
90
|
-
return { publicKey, nonce };
|
|
91
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import type { Account } from "near-api-js";
|
|
2
|
-
import type { NearGasEstimateParams } from "../types/contract";
|
|
3
|
-
|
|
4
|
-
// Gas constants (in TGas - 10^12 gas units)
|
|
5
|
-
export const GAS_COSTS = {
|
|
6
|
-
SIMPLE_TRANSFER: "1", // 1 TGas
|
|
7
|
-
TOKEN_TRANSFER: "100", // 100 TGas
|
|
8
|
-
CONTRACT_CALL: "100", // 100 TGas base
|
|
9
|
-
ACCOUNT_CREATION: "30", // 30 TGas
|
|
10
|
-
CONTRACT_DEPLOYMENT: "200", // 200 TGas
|
|
11
|
-
ACCESS_KEY_ADDITION: "5", // 5 TGas
|
|
12
|
-
ACCESS_KEY_DELETION: "5", // 5 TGas
|
|
13
|
-
STAKE: "10", // 10 TGas
|
|
14
|
-
STORAGE_DEPOSIT: "100", // 100 TGas
|
|
15
|
-
} as const;
|
|
16
|
-
|
|
17
|
-
// Type guards for discriminated union
|
|
18
|
-
export function isSimpleTransfer(
|
|
19
|
-
params: NearGasEstimateParams,
|
|
20
|
-
): params is { recipient: string; amount: string } {
|
|
21
|
-
return "recipient" in params && "amount" in params && !("contractId" in params);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function isContractCall(params: NearGasEstimateParams): params is {
|
|
25
|
-
contractId: string;
|
|
26
|
-
methodName: string;
|
|
27
|
-
args?: Record<string, any>;
|
|
28
|
-
attachedDeposit?: string;
|
|
29
|
-
} {
|
|
30
|
-
return "contractId" in params && "methodName" in params;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function isBatchTransaction(params: NearGasEstimateParams): params is { actions: any[] } {
|
|
34
|
-
return "actions" in params;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function isAccountCreation(params: NearGasEstimateParams): params is {
|
|
38
|
-
newAccountId: string;
|
|
39
|
-
publicKey?: string;
|
|
40
|
-
} {
|
|
41
|
-
return "newAccountId" in params;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function isContractDeployment(
|
|
45
|
-
params: NearGasEstimateParams,
|
|
46
|
-
): params is { contractCode: Uint8Array } {
|
|
47
|
-
return "contractCode" in params;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function isCustomEstimator(params: NearGasEstimateParams): params is {
|
|
51
|
-
customEstimator: (account: Account) => Promise<string>;
|
|
52
|
-
} {
|
|
53
|
-
return "customEstimator" in params;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Helper function to estimate gas for batch actions
|
|
57
|
-
export function estimateBatchGas(actions: any[]) {
|
|
58
|
-
let totalGas = 0;
|
|
59
|
-
|
|
60
|
-
for (const action of actions) {
|
|
61
|
-
switch (action.enum) {
|
|
62
|
-
case "transfer":
|
|
63
|
-
totalGas += Number(GAS_COSTS.SIMPLE_TRANSFER);
|
|
64
|
-
break;
|
|
65
|
-
case "functionCall":
|
|
66
|
-
totalGas += Number(GAS_COSTS.CONTRACT_CALL);
|
|
67
|
-
break;
|
|
68
|
-
case "createAccount":
|
|
69
|
-
totalGas += Number(GAS_COSTS.ACCOUNT_CREATION);
|
|
70
|
-
break;
|
|
71
|
-
case "deployContract":
|
|
72
|
-
totalGas += Number(GAS_COSTS.CONTRACT_DEPLOYMENT);
|
|
73
|
-
break;
|
|
74
|
-
case "addKey":
|
|
75
|
-
totalGas += Number(GAS_COSTS.ACCESS_KEY_ADDITION);
|
|
76
|
-
break;
|
|
77
|
-
case "deleteKey":
|
|
78
|
-
totalGas += Number(GAS_COSTS.ACCESS_KEY_DELETION);
|
|
79
|
-
break;
|
|
80
|
-
case "stake":
|
|
81
|
-
totalGas += Number(GAS_COSTS.STAKE);
|
|
82
|
-
break;
|
|
83
|
-
default:
|
|
84
|
-
totalGas += Number(GAS_COSTS.CONTRACT_CALL);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return totalGas.toString();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Helper function to get gas cost for contract methods
|
|
92
|
-
export function getContractMethodGas(methodName: string) {
|
|
93
|
-
if (methodName === "ft_transfer" || methodName === "ft_transfer_call") {
|
|
94
|
-
return GAS_COSTS.TOKEN_TRANSFER;
|
|
95
|
-
}
|
|
96
|
-
if (methodName === "storage_deposit") {
|
|
97
|
-
return GAS_COSTS.STORAGE_DEPOSIT;
|
|
98
|
-
}
|
|
99
|
-
return GAS_COSTS.CONTRACT_CALL;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Convert TGas string to gas units
|
|
103
|
-
export function tgasToGas(tgas: string): string {
|
|
104
|
-
return (BigInt(tgas) * BigInt(10 ** 12)).toString();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Convert gas units to TGas
|
|
108
|
-
export function gasToTGas(gas: string): string {
|
|
109
|
-
return (BigInt(gas) / BigInt(10 ** 12)).toString();
|
|
110
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import type { Account, Contract } from "near-api-js";
|
|
2
|
-
import { createNearContract } from "./contractFactory";
|
|
3
|
-
|
|
4
|
-
const DEFAULT_STORAGE_DEPOSIT = "1250000000000000000000"; // 0.00125 NEAR
|
|
5
|
-
|
|
6
|
-
// Define NEP-141 contract interface
|
|
7
|
-
interface NEP141Contract extends Contract {
|
|
8
|
-
// View methods
|
|
9
|
-
ft_balance_of(args: { account_id: string }): Promise<string>;
|
|
10
|
-
ft_total_supply(): Promise<string>;
|
|
11
|
-
ft_metadata(): Promise<any>;
|
|
12
|
-
storage_balance_of(args: { account_id: string }): Promise<any>;
|
|
13
|
-
storage_balance_bounds(): Promise<any>;
|
|
14
|
-
|
|
15
|
-
// Change methods
|
|
16
|
-
ft_transfer(args: any, gas: any, deposit: any): Promise<any>;
|
|
17
|
-
ft_transfer_call(args: any, gas: any, deposit: any): Promise<any>;
|
|
18
|
-
storage_deposit(args: any, gas: any, deposit: any): Promise<any>;
|
|
19
|
-
storage_withdraw(args: any, gas: any, deposit: any): Promise<any>;
|
|
20
|
-
storage_unregister(force?: boolean, gas?: any): Promise<any>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function createNEP141Token({
|
|
24
|
-
contractId,
|
|
25
|
-
account,
|
|
26
|
-
}: {
|
|
27
|
-
contractId: string;
|
|
28
|
-
account: Account;
|
|
29
|
-
}) {
|
|
30
|
-
const BN = (await import("bn.js")).default;
|
|
31
|
-
|
|
32
|
-
const contract = await createNearContract<NEP141Contract>({
|
|
33
|
-
account,
|
|
34
|
-
contractId,
|
|
35
|
-
viewMethods: [
|
|
36
|
-
"ft_balance_of",
|
|
37
|
-
"ft_total_supply",
|
|
38
|
-
"ft_metadata",
|
|
39
|
-
"storage_balance_of",
|
|
40
|
-
"storage_balance_bounds",
|
|
41
|
-
],
|
|
42
|
-
changeMethods: [
|
|
43
|
-
"ft_transfer",
|
|
44
|
-
"ft_transfer_call",
|
|
45
|
-
"storage_deposit",
|
|
46
|
-
"storage_withdraw",
|
|
47
|
-
"storage_unregister",
|
|
48
|
-
],
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// Helper to ensure storage before transfers
|
|
52
|
-
const ensureStorageFor = async (accountId: string) => {
|
|
53
|
-
const balance = await contract.storage_balance_of({ account_id: accountId });
|
|
54
|
-
if (!balance) {
|
|
55
|
-
// Get minimum storage requirement
|
|
56
|
-
const bounds = await contract.storage_balance_bounds();
|
|
57
|
-
const deposit = bounds?.min || DEFAULT_STORAGE_DEPOSIT;
|
|
58
|
-
|
|
59
|
-
await contract.storage_deposit(
|
|
60
|
-
{ account_id: accountId },
|
|
61
|
-
new BN("100000000000000"), // 100 TGas
|
|
62
|
-
new BN(deposit),
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
transfer: async (receiverId: string, amount: string, memo?: string) => {
|
|
69
|
-
// Ensure recipient has storage before transfer
|
|
70
|
-
await ensureStorageFor(receiverId);
|
|
71
|
-
|
|
72
|
-
return contract.ft_transfer(
|
|
73
|
-
{ receiver_id: receiverId, amount, memo },
|
|
74
|
-
new BN("100000000000000"), // 100 TGas
|
|
75
|
-
new BN("1"), // 1 yoctoNEAR for security
|
|
76
|
-
);
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
transferCall: async (receiverId: string, amount: string, msg: string, memo?: string) => {
|
|
80
|
-
// Ensure recipient has storage before transfer
|
|
81
|
-
await ensureStorageFor(receiverId);
|
|
82
|
-
|
|
83
|
-
return contract.ft_transfer_call(
|
|
84
|
-
{ receiver_id: receiverId, amount, memo, msg },
|
|
85
|
-
new BN("100000000000000"), // 100 TGas
|
|
86
|
-
new BN("1"), // 1 yoctoNEAR for security
|
|
87
|
-
);
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
balanceOf: (accountId: string) => contract.ft_balance_of({ account_id: accountId }),
|
|
91
|
-
|
|
92
|
-
totalSupply: () => contract.ft_total_supply(),
|
|
93
|
-
|
|
94
|
-
metadata: () => contract.ft_metadata(),
|
|
95
|
-
|
|
96
|
-
storageBalanceOf: (accountId: string) => contract.storage_balance_of({ account_id: accountId }),
|
|
97
|
-
|
|
98
|
-
storageDeposit: (accountId?: string, amount?: string) =>
|
|
99
|
-
contract.storage_deposit(
|
|
100
|
-
{ account_id: accountId },
|
|
101
|
-
new BN("100000000000000"),
|
|
102
|
-
new BN(amount || DEFAULT_STORAGE_DEPOSIT),
|
|
103
|
-
),
|
|
104
|
-
|
|
105
|
-
ensureStorage: ensureStorageFor,
|
|
106
|
-
|
|
107
|
-
// Raw contract access for advanced use cases
|
|
108
|
-
contract,
|
|
109
|
-
};
|
|
110
|
-
}
|
package/src/near/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export * from "./toolbox";
|
|
2
|
-
export * from "./types";
|
|
3
|
-
export * from "./helpers";
|
|
4
|
-
|
|
5
|
-
import type { getNearToolbox } from "./toolbox";
|
|
6
|
-
|
|
7
|
-
export type NearWallet = Awaited<ReturnType<typeof getNearToolbox>>;
|
|
8
|
-
|
|
9
|
-
export type {
|
|
10
|
-
NearContractInterface,
|
|
11
|
-
NearCallParams,
|
|
12
|
-
NearGasEstimateParams,
|
|
13
|
-
} from "./types/contract";
|
|
14
|
-
export type {
|
|
15
|
-
FungibleTokenMetadata,
|
|
16
|
-
StorageBalance,
|
|
17
|
-
StorageBalanceBounds,
|
|
18
|
-
NEP141Contract,
|
|
19
|
-
TokenTransferParams,
|
|
20
|
-
} from "./types/nep141";
|
|
21
|
-
|
|
22
|
-
export { createNEP141Token } from "./helpers/nep141";
|
|
23
|
-
export { tgasToGas, gasToTGas } from "./helpers/gasEstimation";
|
|
24
|
-
export { createNearContract } from "./helpers/contractFactory";
|