Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions challenge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,38 @@ const algodClient = algokit.getAlgoClient()
const sender = await algokit.getLocalNetDispenserAccount(algodClient)

const receiver = await algokit.mnemonicAccountFromEnvironment(
{name: 'RECEIVER', fundWith: algokit.algos(100)},
{ name: 'RECEIVER', fundWith: algokit.algos(100) },
algodClient,
)

/*
TODO: edit code below

Description:
The below code is trying to atomically send 2 payment from sender to receiver
account by grouping them into an atomic transaction composer.
However, the code is not working. find and fix the bug so that the 2 payments are
successfully sent atomically.

When solved correctly, the console should print out the following:
"The first payment transaction sent 1000000 microAlgos and the second payment transaction sent 2000000 microAlgos"
*/
)

// Create the first payment transaction
const suggestedParams = await algodClient.getTransactionParams().do();
const ptxn1 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: sender.addr,
suggestedParams,
to: receiver.addr,
amount: 1000000,// 1 ALGO
amount: 1000000, // 1 ALGO
});

/// <reference lib="dom" />

// Create the second payment transaction
const ptxn2 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: sender.addr,
suggestedParams,
to: receiver.addr,
amount: 2000000, // 2 ALGOs
});

// Create signer for the Atomic Transaction
const signer = algosdk.makeBasicAccountTransactionSigner(sender)

// Compose the Atomic Transaction
const atc = new algosdk.AtomicTransactionComposer()
atc.addTransaction({txn: ptxn1, signer: sender})
atc.addTransaction({txn: ptxn2, signer: sender})
atc.addTransaction({ txn: ptxn1, signer: signer })
atc.addTransaction({ txn: ptxn2, signer: signer })

const result = await algokit.sendAtomicTransactionComposer({atc:atc, sendParams: {suppressLog:true}}, algodClient)
// Send the Atomic Transaction
const result = await algokit.sendAtomicTransactionComposer({ atc: atc, sendParams: { suppressLog: true } }, algodClient)
console.log(`The first payment transaction sent ${result.transactions[0].amount} microAlgos and the second payment transaction sent ${result.transactions[1].amount} microAlgos`)