Created
February 11, 2022 19:39
-
-
Save dimitardanailov/75ebcb39a8296499b73723360d65fff4 to your computer and use it in GitHub Desktop.
Algorand transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @resource | |
* https://github.com/PureStake/api-examples/blob/master/javascript-examples/algod_submit_tx.js | |
* https://developer.algorand.org/docs/sdks/javascript/#build-first-transaction | |
*/ | |
export async function pureTransaction() { | |
const regExp = new RegExp(/\,/, 'g') | |
const mnemonic = 'final, equal, social, attack, chimney, spend, swear, civil, sad, jungle, dumb, assault, metal, feature, monitor, marriage, fly, depart, ill, shed, burger, census, swift, absent, garlic'.replace( | |
regExp, | |
'', | |
) | |
const recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic) | |
// Construct the transaction | |
const client = new algosdk.Algodv2(token, ALGO_CLIENT_SERVER, port) | |
let params = await client.getTransactionParams().do() | |
// comment out the next two lines to use suggested fee | |
// params.fee = 1000 | |
// params.flatFee = true | |
const receiver = 'REYCTDA3AWAFPXTN5IEFFCA5LGCJN74RPKSJCFU3GTMZYXX2AWYJIZRGJY' | |
const enc = new TextEncoder() | |
const note = enc.encode('Algorand transaction') | |
let amount = 1000000 // equals 1 ALGO | |
let sender = recoveredAccount.addr | |
let txn = algosdk.makePaymentTxnWithSuggestedParams( | |
sender, | |
receiver, | |
amount, | |
undefined, | |
note, | |
params, | |
) | |
let signedTxn = txn.signTxn(recoveredAccount.sk) | |
let txId = txn.txID().toString() | |
console.log('Signed transaction with txID: %s', txId) | |
// Submit the transaction | |
await client.sendRawTransaction(signedTxn).do() | |
// Wait for confirmation | |
let confirmedTxn = await waitForConfirmation(client, txId, 4) | |
//Get the completed Transaction | |
console.log( | |
'Transaction ' + | |
txId + | |
' confirmed in round ' + | |
confirmedTxn['confirmed-round'], | |
) | |
var string = new TextDecoder().decode(confirmedTxn.txn.txn.note) | |
console.log('Note field: ', string) | |
const accountInfo = await client | |
.accountInformation(recoveredAccount.addr) | |
.do() | |
console.log('Transaction Amount: %d microAlgos', confirmedTxn.txn.txn.amt) | |
console.log('Transaction Fee: %d microAlgos', confirmedTxn.txn.txn.fee) | |
console.log('Account balance: %d microAlgos', accountInfo.amount) | |
} | |
/** | |
* Wait until the transaction is confirmed or rejected, or until 'timeout' | |
* number of rounds have passed. | |
* @param {algosdk.Algodv2} algodClient the Algod V2 client | |
* @param {string} txId the transaction ID to wait for | |
* @param {number} timeout maximum number of rounds to wait | |
* @return {Promise<*>} pending transaction information | |
* @throws Throws an error if the transaction is not confirmed or rejected in the next timeout rounds | |
*/ | |
const waitForConfirmation = async function ( | |
algodClient: any, | |
txId: string, | |
timeout: number, | |
) { | |
if (algodClient == null || txId == null || timeout < 0) { | |
throw new Error('Bad arguments') | |
} | |
const status = await algodClient.status().do() | |
if (status === undefined) { | |
throw new Error('Unable to get node status') | |
} | |
const startround = status['last-round'] + 1 | |
let currentround = startround | |
while (currentround < startround + timeout) { | |
const 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 ' + | |
txId + | |
' rejected - pool error: ' + | |
pendingInfo['pool-error'], | |
) | |
} | |
} | |
} | |
await algodClient.statusAfterBlock(currentround).do() | |
currentround++ | |
} | |
throw new Error( | |
'Transaction ' + txId + ' not confirmed after ' + timeout + ' rounds!', | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment