typescript-sdk icon indicating copy to clipboard operation
typescript-sdk copied to clipboard

replace getRecentBlockhash with getLatestBlockhash

Open ghost opened this issue 3 years ago • 2 comments

note: replace the deprecated getRecentBlockhash with the new getLatestBlockhash context: https://twitter.com/GenesysGo/status/1508296004398948357

ghost avatar Mar 28 '22 08:03 ghost

Sure, I'd be happy to help! Here's how you can replace getRecentBlockhash with getLatestBlockhash:

  1. Import the web3.js library:
const Web3 = require('web3');
  1. Create a new instance of the Web3 class:
const web3 = new Web3('https://api.mainnet-beta.solana.com');
  1. Replace all instances of getRecentBlockhash with getLatestBlockhash:
const blockhash = await web3.eth.getLatestBlockhash();

That's it! This should replace all instances of the deprecated getRecentBlockhash with the new getLatestBlockhash. Let me know if you have any questions or if there's anything else I can help you with.

staccDOTsol avatar Mar 23 '23 04:03 staccDOTsol

Sure, here's an example of how you can replace getRecentBlockhash with getLatestBlockhash:

Before:

const connection = new Connection(url, 'confirmed');
const recentBlockhash = await connection.getRecentBlockhash();

After:

const connection = new Connection(url, 'confirmed');
const recentBlockhash = (await connection.getLatestBlockhash('recent')).blockhash;

Note that getLatestBlockhash returns an object containing the blockhash and the fee calculator. In this example, we're accessing the blockhash property of the returned object. Also, the 'recent' parameter specifies that we want the most recent blockhash. You can replace it with a specific slot number if needed.

staccDOTsol avatar Mar 24 '23 03:03 staccDOTsol