-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.js
105 lines (88 loc) · 4.37 KB
/
first.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const algosdk = require('algosdk');
const claim = require('./claimbadge');
/**
* utility function to wait on a transaction to be confirmed
* the timeout parameter indicates how many rounds do you wish to check pending transactions for
*/
const waitForConfirmation = async function(algodclient, txId, timeout) {
// Wait until the transaction is confirmed or rejected, or until 'timeout'
// number of rounds have passed.
// Args:
// txId(str): the transaction to wait for
// timeout(int): maximum number of rounds to wait
// Returns:
// pending transaction information, or throws an error if the transaction
// is not confirmed or rejected in the next timeout rounds
if (algodclient == null || txId == null || timeout < 0) {
throw "Bad arguments.";
}
let status = (await algodclient.status().do());
if (status == undefined) throw new Error("Unable to get node status");
let startround = status["last-round"] + 1;
let currentround = startround;
while (currentround < (startround + timeout)) {
let pendingInfo = await algodclient.pendingTransactionInformation(txId).do();
if (pendingInfo != undefined) {
if (pendingInfo["confirmed-round"] !== null && pendingInfo["confirmed-round"] > 0) {
//Got the completed Transaction
return pendingInfo;
} else {
if (pendingInfo["pool-error"] != null && pendingInfo["pool-error"].length > 0) {
// If there was a pool error, then the transaction has been rejected!
throw new Error("Transaction Rejected" + " pool error" + pendingInfo["pool-error"]);
}
}
}
await algodclient.statusAfterBlock(currentround).do();
currentround++;
}
throw new Error("Transaction not confirmed after " + timeout + " rounds!");
};
async function yourFirstTransaction() {
try {
const passphrase = "price clap dilemma swim genius fame lucky crack torch hunt maid palace ladder unlock symptom rubber scale load acoustic drop oval cabbage review abstract embark";
let myAccount = algosdk.mnemonicToSecretKey(passphrase);
console.log("My address: %s", myAccount.addr);
console.log("My passphrase: " + passphrase);
// Add funds
// TestNet Faucet: https://bank.testnet.algorand.network/
// BetaNet Faucet: https://bank.betanet.algodev.network/
// Connect your client
const algodToken = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const algodServer = "http://localhost";
const algodPort = 4001;
let algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
//Check your balance
let accountInfo = await algodClient.accountInformation(myAccount.addr).do();
console.log("Account balance: %d microAlgos", accountInfo.amount);
// Construct the transaction
let params = await algodClient.getTransactionParams().do();
// comment out the next two lines to use suggested fee
params.fee = 1000;
params.flatFee = true;
// receiver defined as TestNet faucet address
const receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A";
const enc = new TextEncoder();
const note = enc.encode("Hello World");
let txn = algosdk.makePaymentTxnWithSuggestedParams(myAccount.addr, receiver, 1000000, undefined, note, params);
// Sign the transaction
// this function is used to group the tx with a badge claim transaction
// it returns the badge claim transaction signed
// this assumes user is already opted int badge
let badgeClaimTx = await claim.claimBadge(15039210, txn);
let signedTxn = txn.signTxn(myAccount.sk);
// Combine the signed transactions
let signed = [];
signed.push(signedTxn);
signed.push(badgeClaimTx);
let tx = (await algodClient.sendRawTransaction(signed).do());
console.log("Transaction : " + tx.txId);
// Wait for confirmation
let confirmedTxn = await waitForConfirmation(algodClient, tx.txId, 4);
//Get the completed Transaction
console.log("Transaction " + tx.txId + " confirmed in round " + confirmedTxn["confirmed-round"]);
} catch (err) {
console.log("err", err);
}
};
yourFirstTransaction();