Solana: web3.js version 2: requestAirdrop() with ‘finalized’ confirmation works, but balance afterwards is zero
Here is the article:
** Web3.js Version 2: AIRDROP REQUEST FINAL CONTAINED STATE
As web3.js 2 continues to gain popularity among developers, it has introduced many new features aimed at improving the user experience. In some cases, however, these updates can lead to unexpected behavior when it comes to air pieces.
In this article, we examine the question of asking for an air group with finalized confirmation using Web3.Js 2 and verify that the balance of the transaction remains zero.
The problem: finalized confirmation but zero balance
When you ask for AIRDROP at Web3.js 2, it basically launches a smart contract that distributes tokens to users. To initiate this process, you must send a «Requestairdrop» message with the necessary parameters. In our example, we will use the «Finalized» confirmation.
Here are some sample codes from the web3.js
depot:
`Javascript
Const web3 = needed ('web3');
Const w3 = new web3 (new web3.providers.httpprovider ('
// Create an instance of web3
Const Web3 = New Web3 (W3);
// Determine the parameters of AIRDROP
Const params = {{
Sender: '0x1234567890abcdef', // sender address
To: '0x9876543210fedc', // addressed address
Amount: 1000, // the number of tokens to distribute
Decision: 90000, // Execution time in seconds
Nonce: 1, // initial transaction number
};
// Create a new account (not required for this example)
const myaccount = web3.eth.acounts [0];
// execute the AIRDROP REQUEST by finalized confirmation
Web3.requestairdrop (Params)
.on ('confirmation', function (confnum) {
Console.log ($ {Confnum} confirm {params.amount}
);
})
.on ('error', function (error) {
Console.Error ('error:', error.message);
})
.Tame (function (result) {
// Check that the transaction is final -e
if (result.status === 0 && result.confidencelek> = 1) {// assuming that at least 1 confidence level for finalization
console.log ('Airdrop Confirmed!');
} Else {
console.log ('error: transaction is not completed.');
}
})
.on ('Balance', Function (Baenedbalance) {
console.log (Balance: $ {ventealance});
});
In this code, we create a new account and make the «Requestaird» message with the required parameters. The reinforcement number of the transaction is also logged.
The interesting part arrives when we check the balance after the transaction is executed:
`Javascript
// Check that the balance is not zero
Web3.eth.getbalance (myaccount) .Then (function (balance) {
console.log (initial balance: $ {balance});
Web3.eth.getbalance (myaccount).
// Check that the resulting balance is different from the initial balance
if (veetbalance! == balance) {
console.log ('' The initial and the resulting balance are not equal! ');
}
});
});
In this example, we use «Web3.eth.getbalance» to check the current balance of our account. We then compare it to the initial balance obtained from the «Requestairdrop» message.
The judgment: the balance remains zero
Unfortunately, in our example code, the balance after the transaction is completed. This is not a mistake in itself, but the result of how web3.js 2 handles finalized confirmations.
In order to achieve the desired result, where the balance is not zero after performing the «Requestairdrop» message, you need to change the code to contain additional logic to calculate and update the balance. This may include multiple use of «Web3.eth.getbalance» or implementing an individual calculation function.
I hope this article provided a valuable insight into web3.