Here is the article:
Web3.js Version 2: Airdrop Requests Final Confirmation, but Balance is Zero
As web3.js version 2 has become increasingly popular with developers, it has introduced a number of new features to improve the user experience. However, in some cases, these updates can lead to unexpected behavior in flight.
In this article, we will examine an issue with an airdrop request with a final confirmation using web3.js version 2, and ensure that the balance is zero after the transaction is completed.
Issue: Final Confirmation, but Balance is Zero
When you request an airdrop in web3.js version 2, you are essentially launching a smart contract that distributes tokens to users. To initiate the process, you need to send a “requestAirdrop” message with the required parameters. In our example, we will use the “final” confirmation option.
Here is some sample code from the “web3.js” repository:
const Web3 = request('web3');
const w3 = new Web3(new Web3.providers.HttpProvider('
// Create a Web3 instance
const web3 = new Web3(w3);
// Set Airdrop parameters
const params = {
from: '0x1234567890abcdef', // sender address
recipient: '0x9876543210fedcba', // recipient address
amount: 1000, // number of characters to be split
deadline: 90000, // execution time in seconds
nonce: 1, // number of first transactions
};
// Create a new account (not needed in this example)
const myAccount = web3.eth.accounts[0];
// Execute airdrop request with final confirmation
web3.requestAirdrop(params)
.on('confirmation', function(confNum) {
console.log(Confirmation ${confNum} / ${params.amount}
);
})
.on('error', function(error) {
console.error('Error:', error.message);
})
.then(function(result) {
// Check if the event has completed
if (result.status === 0 && result.confidencelevel >= 1) { // assuming completion confidence level is at least 1
console.log('Airdrop confirmed!');
} else {
console.log('Error: Transaction not completed.');
}
})
.on('balanceOf', function(receivedBalance) {
console.log(Received Balance: ${receivedBalance}
);
});
In this code, we create a new account and execute the “requestAirdrop” message with the required parameters. We also record the transaction confirmation number.
The interesting part is when we check the balance after the transaction is complete:
// Check that the balance is not zero
web3.eth.getBalance(myAccount).then(function(balance) {
console.log(Starting Balance: ${balance}
);
web3.eth.getBalance(myAccount).then(function(receivedBalance) {
// Check that the received balance is different from the original balance
if (receivedBalance !== balance) {
console.log('The initial and received balance are not the same!');
}
});
});
In this example, we will use the expression “web3.eth.getBalance” to check the current balance of our account. We will then compare it with the initial balance received from the “requestAirdrop” message.
Verdict: the balance remains zero
Unfortunately, in our example code, the balance remains zero after the transaction is completed. This is not a bug per se, but a consequence of how web3.js version 2 handles committed checks.
To achieve the desired result, if the balance is not zero after the “requestAirdrop” message is executed, you will need to modify your code to add additional logic to calculate and update the balance. This may require multiple uses of the “web3.eth.getBalance” function or implementing a custom calculation function.
I hope this article has provided you with valuable insight into web3.