0
0
Blockchain / Solidityprogramming~20 mins

Sending transactions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blockchain Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ethereum transaction code snippet?

Consider this simplified Ethereum transaction sending code using web3.js. What will be printed to the console?

Blockchain / Solidity
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');

async function sendTx() {
  const accounts = await web3.eth.getAccounts();
  console.log(accounts.length);
}
sendTx();
A0
BAn error because getAccounts is not available on mainnet without unlocked accounts
Cundefined
DA positive number representing accounts length
Attempts:
2 left
💡 Hint

Think about what getAccounts() returns when connected to a public node without unlocked accounts.

Predict Output
intermediate
2:00remaining
What is the output of this Bitcoin transaction fee calculation?

Given this code snippet calculating Bitcoin transaction fee, what is the output?

Blockchain / Solidity
const txSizeBytes = 250;
const feeRateSatsPerByte = 20;
const fee = txSizeBytes * feeRateSatsPerByte;
console.log(fee);
A250
B270
C20
D5000
Attempts:
2 left
💡 Hint

Multiply transaction size by fee rate per byte.

🔧 Debug
advanced
3:00remaining
Why does this Ethereum transaction sending code throw an error?

Look at this code snippet that tries to send an Ethereum transaction. Why does it throw an error?

Blockchain / Solidity
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');

async function sendTx() {
  const tx = {
    from: '0xYourAddress',
    to: '0xRecipientAddress',
    value: web3.utils.toWei('1', 'ether'),
    gas: 21000
  };
  const receipt = await web3.eth.sendTransaction(tx);
  console.log(receipt);
}
sendTx();
AError because gas limit is too low
BError because the private key is not provided or account is not unlocked
CError because value is not converted to wei correctly
DNo error, transaction will succeed
Attempts:
2 left
💡 Hint

Think about what is needed to sign and send a transaction using a public node.

📝 Syntax
advanced
3:00remaining
Which option correctly creates and signs a transaction in Ethereum using ethers.js?

Choose the code snippet that correctly creates and signs a transaction using ethers.js.

A
const wallet = new ethers.Wallet(privateKey);
const tx = { to: recipient, value: ethers.utils.parseEther('1') };
const signedTx = wallet.signTransaction(tx);
console.log(signedTx);
B
const wallet = new ethers.Wallet(privateKey);
const tx = { to: recipient, value: '1' };
const signedTx = wallet.sign(tx);
console.log(signedTx);
C
const wallet = new ethers.Wallet(privateKey);
const tx = { to: recipient, value: ethers.utils.parseEther('1') };
const signedTx = await wallet.signTransaction(tx);
console.log(signedTx);
D
const wallet = new ethers.Wallet(privateKey);
const tx = { to: recipient, value: ethers.utils.parseEther('1') };
const signedTx = wallet.sign(tx);
console.log(signedTx);
Attempts:
2 left
💡 Hint

Check the correct method name for signing a transaction in ethers.js.

🚀 Application
expert
3:00remaining
How many outputs does this Bitcoin transaction have after execution?

Given this simplified Bitcoin transaction creation code, how many outputs will the transaction have?

Blockchain / Solidity
const bitcoin = require('bitcoinjs-lib');
const keyPair = bitcoin.ECPair.makeRandom();
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });

const txb = new bitcoin.TransactionBuilder();
txb.addInput('some-txid', 0);
txb.addOutput('recipient-address', 50000);
txb.addOutput(address, 45000);

const tx = txb.build();
console.log(tx.outs.length);
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Count how many times addOutput is called.