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 with final confirmation works, but balance remains zero

Solana: web3.js version 2: requestAirdrop() with 'finalized' confirmation works, but balance afterwards is zero

As web3.js version 2 continues to gain popularity among developers, it has introduced several new features aimed at improving the user experience. However, in some cases, these updates can cause unexpected behavior when it comes to airdrops.

In this article, we will examine an airdrop request with final confirmation using web3.js version 2 and verify that the balance remains zero after the transaction.

Problem: Confirmation completed, but zero balance

When you make an airdrop request in web3.js version 2, you are essentially launching a smart contract that will distribute tokens to users. To initiate this process, you need to send a requestAirdrop message with the required parameters. In our example, we will use the confirmation option “completed”.

Here is some sample code from the web3.js repository:

const Web3 = require('web3');

const w3 = new Web3 (new Web3.providers.HttpProvider('

// Create a web3 instance

const web3 = new Web3(w3);

// Define airdrop parameters

const params = {

from: '0x1234567890abcdef', // sender address

to: '0x9876543210fedcba', // recipient address

amount: 1000, // number of tokens to distribute

timeout: 90000, // execution time in seconds

nonce: 1, // initial number of transactions

};

// Create a new account (not necessary in this example)

const myAccount = web3.eth.accounts[0];

// Perform airdrop request with final confirmation

web3.requestAirdrop(params)

.on('confirmation', function(confNum) {

console.log(Confirmation ${confNum} from ${params.amount});

})

.on('error', function(error) {

console.error('Error:', error.message);

})

.then(function(result) {

// Check if the operation is complete

if (result.status === 0 && result.trustlevel >= 1) { // assuming trustlevel is at least 1 when completing

console.log('Airdrop confirmed!');

} else {

console.log('Error: Operation not complete.');

}

})

.on('balanceOf', function(receivedBalance) {

console.log(Received balance: ${receivedBalance});

});

With this code we create a new account and provide a requestAirdrop message with the required parameters. We also record the transaction confirmation number.

The most interesting part is when we check the balance after the transaction:

// Check if the balance is not zero

web3.eth.getBalance(myAccount).then(function(balance) {

console.log(Initial balance: ${balance});

web3.eth.getBalance(myAccount).then(function(receivedBalance) {

// Check if the received balance is different from the initial balance

if (receivedBalance !== balance) {

console.log('Initial and received balances are not equal!');

}

});

});

In this example we use “web3.eth.getBalance’ to check the current account balance. We then compare it to the initial balance obtained from the requestAirdrop message.

Verdict: Balance remains zero

Unfortunately, in our sample code, the balance remains zero after the transaction. This is not a bug per se, but rather how web3.js version 2 handles final confirmations.

To achieve the desired result of the balance being non-zero after the requestAirdrop message, you will need to modify your code to include additional logic for calculating and updating the balance. This may involve using web3.eth.getBalance multiple times or implementing a custom calculation function.

I hope this article has provided you with some valuable insights into web3.

BINANCE COIN BLAST BLAST


Leave a Reply

Your email address will not be published. Required fields are marked *