-
Notifications
You must be signed in to change notification settings - Fork 244
/
unwrap-near.ts
55 lines (48 loc) · 2.29 KB
/
unwrap-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
50
51
52
53
54
55
import {
functionCall,
getSignerFromKeystore,
getTestnetRpcProvider,
view,
} from '@near-js/client';
import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node';
import { formatNearAmount, parseNearAmount } from '@near-js/utils';
import chalk from 'chalk';
import { join } from 'node:path';
import { homedir } from 'node:os';
// On mainnet it's wrap.near, by the way
const WRAP_NEAR_CONTRACT_ID = "wrap.testnet";
export default async function unwrapNear(accountId: string, unwrapAmount: bigint, wrapContract = WRAP_NEAR_CONTRACT_ID) {
if (!accountId || !unwrapAmount) {
console.log(chalk`{red pnpm unwrapNear -- ACCOUNT_ID UNWRAP_AMOUNT [WRAP_CONTRACT]}`);
return;
}
// initialize testnet RPC provider
const rpcProvider = getTestnetRpcProvider();
// initialize the transaction signer using a pre-existing key for `accountId`
const signer = getSignerFromKeystore(accountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')));
const getStorageBalance = () => view({
account: wrapContract,
method: 'storage_balance_of',
args: { account_id: accountId },
deps: { rpcProvider },
}) as Promise<{ available: string, total: string }>;
const { total: preTotal, available: preAvailable } = (await getStorageBalance()) || {};
await functionCall({
sender: accountId,
receiver: wrapContract,
method: 'near_withdraw',
args: { amount: parseNearAmount(unwrapAmount.toString()) },
deposit: 1n,
deps: {
rpcProvider,
signer,
},
});
const { total: postTotal, available: postAvailable } = await getStorageBalance();
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.green RESULTS} {white Unwrapped ${unwrapAmount}yN with ${wrapContract}}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.white Starting Balance} {white |} {bold.yellow ${formatNearAmount(preAvailable)} / ${formatNearAmount(preTotal)}}`);
console.log(chalk`{bold.white Ending Balance} {white |} {bold.yellow ${formatNearAmount(postAvailable)} / ${formatNearAmount(postTotal)}}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
}