-
Notifications
You must be signed in to change notification settings - Fork 13
/
near.ts
49 lines (44 loc) · 1.31 KB
/
near.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { keyStores, KeyPair, connect, Account } from "near-api-js";
export const TGAS = 1000000000000n;
export const NO_DEPOSIT = "0";
export const ONE_YOCTO = "1";
export interface NearConfig {
networkId: string;
nodeUrl: string;
}
/**
* Loads Near Account from provided keyPair and accountId
* Defaults to TESTNET_CONFIG
* @param keyPair {KeyPair}
* @param accountId {string}
* @param network {NearConfig} network settings
* @returns {Account}
*/
export const nearAccountFromKeyPair = async (config: {
keyPair: KeyPair;
accountId: string;
network: NearConfig;
}): Promise<Account> => {
return createNearAccount(config.accountId, config.network, config.keyPair);
};
/** Minimally sufficient Account instance to construct the signing contract instance.
* Can't be used to change methods.
*/
export const nearAccountFromAccountId = async (
accountId: string,
network: NearConfig
): Promise<Account> => {
return createNearAccount(accountId, network);
};
const createNearAccount = async (
accountId: string,
network: NearConfig,
keyPair?: KeyPair
): Promise<Account> => {
const keyStore = new keyStores.InMemoryKeyStore();
if (keyPair) {
await keyStore.setKey(network.networkId, accountId, keyPair);
}
const near = await connect({ ...network, keyStore });
return near.account(accountId);
};