Solana: web3.js version 2: requestAirdrop() with ‘finalized’ confirmation works, but balance afterwards is zero
Here’s the article:
Web3.JS Version 2: AirDrop Requesting Finalized Contrature Works, But Balance Remains Zero
As web3.js version 2 Continues to Gain Popularity Among Developers, IT HAS Introduced Several New Features That Aim To Improve User Experience. However, in some cases, these updates can lead to unexpected behavior when it comes to airdrops.
In this article, We’ll Explore the Issue of Requesting An Airdrop with Finalized Confirmation Using Web3.JS Version 2 and Verify that the Balance After The Transaction is Executed Remaains Zero.
The Problem: Finalized Confirmation, But Zero Balance
When you request an airdrop in web3.js version 2, you’re essentialy launching a smart contract that will distribute tokens to users. To initiate this process, you need to send a requestair drop
Message with the Required parameters. In our Example, We’ll use the finalized
confirmation option.
Here’s some sample code from the web3.js
repository:
`Javascript
Const Web3 = Require (‘Web3’);
Const W3 = New Web3 (New Web3.providers.httpprovider (‘
// Create A Web3 Instance
Const Web3 = New Web3 (W3);
// Define the airdrop parameters
Const params = {
From: ‘0x1234567890abcdef’, // Sender Address
to: ‘0x9876543210fedcba’, // Recipient Address
Amount: 1000, // number or tokens to distribute
Deadline: 90000, // Execution time in Seconds
Nonce: 1, // Initial Transaction Count
};
// Create a New Account (Not Necessary for this Example)
Const myaccount = web3.eth.Apounts [0];
// Perform The AirDrop Request with Finalized Confirmation
Web3. Requestairdrop (params)
.on (‘confirmation’, function (confnum) {
console.log (confirmation $ {confnum} or $ {params.amount}
);
})
.on ('error', function (error) {
console.error ('error:', error.message);
})
.Then (function (result) {
// Check if the transaction is final
IF (result status === 0 && results.confidcelevel> = 1) {// assuming a Confidence Level of At Least 1 for Finalization
console.log ('AirDrop confirmed!');
} Else {
console.log ('error: transaction not final.');
}
})
.on ('Balanceof', Function (ReceivedBalance) {
console.log (Received balance: $ {recurrent balance});
});
In this code, We’re Creating a New Account and Performing the Requestairdrop
Message with the Required parameters. We’re also logging The Confirmation Number of the Transaction.
The interesting part comes when we check for the balance after executing the transaction:
`Javascript
// Check If The Balance is Non-Zero
Web3.eth.GeBalance (MyAccount) .Then (Function (Balance) {
console.log (initial balance: $ {balance}
);
Web3.eth.GeBalance (MyAccount) .Then (Function (Receivedbalance) {
// Check If the Received Balance is Different From the Initial Balance
if (recoivedbalance! == Balance) {
console.log (“Initial and Received Balances are not Equal!”);
}
});
});
`
In This Example, We’re Using Web3.eth.GeBalance
to Check for the Current Balance of Our Account. We then compare it with the initial balance obtained from the Requestairdrop
Message.
The Verdict: Balance Remains Zero
Unfortunately, in Our Example Code, The Balance After Executing the Transaction Remains Zero. This is not an error per se, but rather a results of how web3.js version 2 Handles Finalized Confirmations.
In order to achieve the desired outcome where the balance after executing the requestair drop
Message is non-Zero, you’ll need to modify your code to include addition to calculating and updating the balance. This Might Involve Using Web3.eth.GeBalance
Multiple Times Or Implementing a Custom Calculation Function.
I hope this article has provid valuable insights into web3.