-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassociateAccountToken.js
43 lines (34 loc) · 1.49 KB
/
associateAccountToken.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
const { Client, TokenAssociateTransaction, TransferTransaction, AccountBalanceQuery, Hbar, PrivateKey, AccountId} = require("@hashgraph/sdk");
const prompt = require('prompt-sync')();
require("dotenv").config();
async function main() {
// Load hedera credentials
// If you want to go on mainnet delete TESTNET_
const myAccountId = AccountId.fromString(process.env.TESTNET_MY_ACCOUNT_ID);
const myPrivateKey = PrivateKey.fromString(process.env.TESTNET_MY_PRIVATE_KEY);
// Credential error handler
if (myAccountId == null ||
myPrivateKey == null ) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
// Build the client/operator
// If you wanna go mainnet use forMainnet() instead of forTestnet()
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
// Inputs
const tokenId = prompt('TOKEN ID: ');
const accountId = AccountId.fromString(prompt('CUENTA A ASOCIAR: '));
const accountKey = PrivateKey.fromString(prompt('CLAVE: '));
// Associeate token to account to receive transactions
const transaction = await new TokenAssociateTransaction()
.setAccountId(accountId)
.setTokenIds([tokenId])
.freezeWith(client);
// Sign and get response
const signTx = await transaction.sign(accountKey);
const txResponse = await signTx.execute(client);
const receipt = await txResponse.getReceipt(client);
console.log("Resultado: " +receipt.status);
console.log("Data de consenso: "+receipt)
}
main()