forked from near/near-api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap-near.js
67 lines (54 loc) · 1.83 KB
/
wrap-near.js
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
56
57
58
59
60
61
62
63
64
65
66
67
const HELP = `To convert N $NEAR to wNEAR, run this script in the following format:
node wrap-near.js YOU.near N
`;
const { connect, keyStores, transactions, utils } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();
// On mainnet it's wrap.near, by the way
const WRAP_NEAR_CONTRACT_ID = "wrap.testnet";
const credentialsPath = path.join(homedir, ".near-credentials");
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
const config = {
keyStore,
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};
if (process.argv.length !== 4) {
console.info(HELP);
process.exit(1);
}
wrapNear(process.argv[2], process.argv[3]);
async function wrapNear(accountId, wrapAmount) {
const near = await connect(config);
const account = await near.account(accountId);
const actions = [
transactions.functionCall(
"near_deposit", // contract method to deposit NEAR for wNEAR
{},
30000000000000, // attached gas
utils.format.parseNearAmount(wrapAmount) // amount of NEAR to deposit and wrap
),
];
// check if storage has been paid (the account has a wNEAR account)
const storage = await account.viewFunction({
contractId: WRAP_NEAR_CONTRACT_ID,
methodName: "storage_balance_of",
args: { account_id: accountId },
});
// if storage hasn't been paid, pay for storage (create an account)
if (!storage) {
actions.unshift(
transactions.functionCall(
"storage_deposit", // method to create an account
{},
30000000000000, // attached gas
utils.format.parseNearAmount('0.00125') // account creation costs 0.00125 NEAR for storage
)
);
}
// send batched transaction
return account.signAndSendTransaction({
receiverId: WRAP_NEAR_CONTRACT_ID,
actions,
});
}