0

I am working through the hello world example and am getting an error when I compile with node. The issue is

let airdropSignature = await connection.requestAirdrop(

SyntaxError: await is only valid in async functions and the top level bodies of modules

It appears here that the connection.confirmTransaction is deprecated. In my VS code, I see a cross through the "confirmTransaction" portion of my code indicating an error there. How can I correct the code so that I avoid this error? Should the hello_world example in the documentation be updated to reflect this?

1 Answers1

1

Your code is running into two problems:

  1. the confirmTransaction method is not fully deprecated
  2. your code is incorrectly using JavaScript async/await

1. confirmTransaction method is not fully deprecated

The @solana/web3.js method you (and the example code from the Solana docs) are using is not a fully deprecated method. But rather, the desired parameter format changed.

NOTE: You can see the source code of the confirmTransaction method here which may help you see the difference.

So in a sense, the confirmTransaction(string_of_tx_sig) format was deprecated. The new desired parameter format should pass an object as the first parameter, with the signature field defined. Like this:

await connection.confirmTransaction({ signature: airdropSignature });

Updating your code to using this updated parameter format will get rid of the "deprecated method line through". But either way, this should not cause your code to fail like it is.

2. Incorrect usage of async/await

The specific error message you are getting is due to the improper use of JavaScript's async/await functions.

Since you did not include much of your source code, I am guessing that you are attempting to call the await connection.requestAirdrop() function out side of an async function.

Perhaps just in the normal flow of a single JavaScript function like this:

const web3 = require("@solana/web3.js");

let airdropSignature = await connection.requestAirdrop(
  payer.publicKey,
  web3.LAMPORTS_PER_SOL
);

// using the deprecated parameter
await connection.confirmTransaction(airdropSignature);

Attempting to run this code above will result in the specific error message you gave since the requestAirdrop is not being run inside of a async function.

You can fix code in a few different ways, either:

  • creating a new "named" function with the async keyword before it, adding all the code above to within that function, then running that function. or,
  • creating an "arrow" fucntion that does effectively the same thing as the option above

For example:

Using a "named" function:

const web3 = require("@solana/web3.js");

async function main(){
  
  let airdropSignature = await connection.requestAirdrop(
    payer.publicKey,
    web3.LAMPORTS_PER_SOL
  );

  // using the non-deprecated parameter
  await connection.confirmTransaction({ signature: airdropSignature });
}

// now run the "named" function
main()

Using an "arrow" function:

const web3 = require("@solana/web3.js");

// create an run an inline "arrow" function
const runs_automatically = async () => {
  
  let airdropSignature = await connection.requestAirdrop(
    payer.publicKey,
    web3.LAMPORTS_PER_SOL
  );

  // using the non-deprecated parameter
  await connection.confirmTransaction({ signature: airdropSignature });
}

NOTE: We do not need to call our "arrow" function for it to execute the function's internal logic, unlike the "named" function

You can read more about arrow functions here on the Moz JavaScript docs.

nickfrosty
  • 131
  • 1
  • 4