forked from deprecated-near-examples/transaction-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-tokens-easy.js
55 lines (48 loc) · 2.19 KB
/
send-tokens-easy.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
const nearAPI = require('near-api-js');
const { connect, KeyPair, keyStores, utils } = nearAPI;
//this is required if using a local .env file for private key
require('dotenv').config();
// configure accounts, network, and amount of NEAR to send
// converts NEAR amount into yoctoNEAR (10^-24) using a near-api-js utility
const sender = 'sender.testnet';
const receiver = 'receiver.testnet';
const networkId = 'testnet';
const amount = utils.format.parseNearAmount('1.5');
async function main() {
// sets up an empty keyStore object in memory using near-api-js
const keyStore = new keyStores.InMemoryKeyStore();
// creates a keyPair from the private key provided in your .env file
const keyPair = KeyPair.fromString(process.env.SENDER_PRIVATE_KEY);
// adds the key you just created to your keyStore which can hold multiple keys
await keyStore.setKey(networkId, sender, keyPair);
// configuration used to connect to NEAR
const config = {
networkId,
keyStore,
nodeUrl: `https://rpc.${networkId}.near.org`,
walletUrl: `https://wallet.${networkId}.near.org`,
helperUrl: `https://helper.${networkId}.near.org`,
explorerUrl: `https://explorer.${networkId}.near.org`
};
// connect to NEAR! :)
const near = await connect(config);
// create a NEAR account object
const senderAccount = await near.account(sender);
try {
// here we are using near-api-js utils to convert yoctoNEAR back into a floating point
console.log(`Sending ${utils.format.formatNearAmount(amount)}Ⓝ from ${sender} to ${receiver}...`);
// send those tokens! :)
const result = await senderAccount.sendMoney(receiver, amount);
// console results
console.log('Transaction Results: ', result.transaction);
console.log('--------------------------------------------------------------------------------------------');
console.log('OPEN LINK BELOW to see transaction in NEAR Explorer!');
console.log(`${config.explorerUrl}/transactions/${result.transaction.hash}`);
console.log('--------------------------------------------------------------------------------------------');
} catch(error) {
// return an error if unsuccessful
console.log(error);
}
}
// run the function
main();