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
30 changes: 5 additions & 25 deletions challenge/index.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,31 @@
import algosdk from "algosdk";
import * as algokit from '@algorandfoundation/algokit-utils';

// Set up algod client
const algodClient = algokit.getAlgoClient()

// Retrieve 2 accounts from localnet kmd
const sender = await algokit.getLocalNetDispenserAccount(algodClient)

const receiver = await algokit.mnemonicAccountFromEnvironment(
{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"
*/

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

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

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

const senderTransactionSigner = algokit.getSenderTransactionSigner(sender)
const atc = new algosdk.AtomicTransactionComposer()
atc.addTransaction({txn: ptxn1, signer: sender})
atc.addTransaction({txn: ptxn2, signer: sender})

atc.addTransaction({txn: ptxn1, signer: senderTransactionSigner})
atc.addTransaction({txn: ptxn2, signer: senderTransactionSigner})
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`)

console.log(`The first payment transaction sent ${result.transactions[0].amount} microAlgos and the second payment transaction sent ${result.transactions[1].amount} microAlgos`)