Solana: web3.js version 2: requestAirdrop() with ‘finalized’ confirmation works, but balance afterwards is zero
Here’s an article:
Web3.js Version 2: AirDrop Request for Confirmation, but balance remains zero
When Web3.JS version 2 continues to gain popularity to developers, it has introduced some new features to improve the user’s experience. However, in some cases, these updates can lead to unexpected behavior when it comes to air currents.
In this article, we will examine the AirDrop application with the final approval using the Web3.js 2 version, and check that the residue after the operation remains zero.
Problem: Completed Confirmation, but zero balance
When you ask for the AirDrop Web3.js 2, you basically start an intellectual contract that will distribute to chips to consumers. To initiate this process, you need to send a message to the RequestaiRDrop with the required parameters. In our example, we will use the “Finish” option.
Here are some examples of code from Web3.js storage:
`Javascript
Const Web3 = Require (‘Web3’);
CONST W3 = New Web3 (New Web3.Providers.Httpprovider (‘
// Create a copy of Web3
Const Web3 = New Web3 (W3);
// Define AirDrop settings
CONS for days = {
Since: ‘0x1234567890abcdef’, // The sender address
to: ‘0x9876543210Fedcba’, // Recipient address
Amount: 1000, // The number of chips is distributed
Final term: 90000, // execution time in seconds
NONCE: 1, // Initial Number of Operations
};
// Create a new account (no need for this for example)
CONST MyACCOUNT = Web3.eth.comunts [0];
// Complete the AirDrop request with completed confirmation
Web3.requestaRDrop (parameters)
.on (‘Confirmation’, Function (Confnum) {
console.log (Confirm $ {Confnum} $ {for days.amount}
);
})
.on (‘ERROR’, Function (Error) {
console.error (‘Error:, Error.sessage);
})
.Then (function (result) {
// Verify the operation will be completed
If (result.status === 0 && Result.Confidenecelevel> = 1) {// Make assumption
Console.log (‘Airdrop approved!’);
} ELSE {
Console.log (‘Error: surgery is not complete.’);
}
})
.on (‘Balance’, Function (obtained balance) {
console.log (received balance: $ {obtainedbalance}
);
});
`
In this code, we create a new account and make a message “RequestaiRDrop” with the required parameters. We also register the operational approval number.
An interesting part comes when we check for the remainder after the operation:
`Javascript
// Verify the balance is not zero
web3.eth.getbalance (myaccount) .then (function (balance) {
Console.log (The initial balance: $ {balance}
);
web3.eth.getbalance (myaccount) .then (function (obtained balance) {
// Check that the resulting balance is different from the initial balance
IF (Balance it is obtained! == balance) {
Console.log (‘The initial and the resulting balance is not equal!’);
}
});
});
`
In this example, we use Web3.eth.getBalance to check the current balance of our account. We then compared it to the initial balance received from the RequestAIRDROP report.
Verdict: The balance remains equal to zero
Unfortunately, the remainder of our example code that performs surgery remains zero. This is not a mistake per se, but rather the way the Web3.js 2 version handles the approvals.
In order to achieve the desired result, when the remnant of the RequestirDrop message is not zero, you will need to modify your code to include additional logic to calculate and update your balance. This may include the use of Web3.eth.getBalance several times or implementation of the calculation function.
I hope this article has provided valuable insights into Web3.