Skip to content

Commit

Permalink
fix: use getLatestBlockhash instead of deprecated getRecentBlockhash
Browse files Browse the repository at this point in the history
this method [has been
removed](https://solana.com/docs/rpc/deprecated/getrecentblockhash) from
the new version of Solana RPC and is no longer available
  • Loading branch information
melonges committed Oct 13, 2024
1 parent 3ca5cc8 commit 44cc4ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions core/src/createTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTransferCheckedInstruction, getAccount, getAssociatedTokenAddress, getMint } from '@solana/spl-token';
import type { Commitment, Connection, PublicKey } from '@solana/web3.js';
import type { GetLatestBlockhashConfig, Connection, PublicKey } from '@solana/web3.js';
import { LAMPORTS_PER_SOL, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { MEMO_PROGRAM_ID, SOL_DECIMALS, TEN } from './constants.js';
Expand Down Expand Up @@ -34,15 +34,15 @@ export interface CreateTransferFields {
* @param connection - A connection to the cluster.
* @param sender - Account that will send the transfer.
* @param fields - Fields of a Solana Pay transfer request URL.
* @param options - Options for `getRecentBlockhash`.
* @param options - Options for `getLatestBlockhash`.
*
* @throws {CreateTransferError}
*/
export async function createTransfer(
connection: Connection,
sender: PublicKey,
{ recipient, amount, splToken, reference, memo }: CreateTransferFields,
{ commitment }: { commitment?: Commitment } = {}
{ commitment }: GetLatestBlockhashConfig = {}
): Promise<Transaction> {
// Check that the sender and recipient accounts exist
const senderInfo = await connection.getAccountInfo(sender);
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function createTransfer(
// Create the transaction
const transaction = new Transaction();
transaction.feePayer = sender;
transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
transaction.recentBlockhash = (await connection.getLatestBlockhash(commitment)).blockhash;

// If a memo is provided, add it to the transaction before adding the transfer instruction
if (memo != null) {
Expand Down
10 changes: 5 additions & 5 deletions core/src/fetchTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Commitment, Connection, PublicKey } from '@solana/web3.js';
import type { GetLatestBlockhashConfig, Connection, PublicKey } from '@solana/web3.js';
import { Transaction } from '@solana/web3.js';
import fetch from 'cross-fetch';
import { toUint8Array } from 'js-base64';
Expand All @@ -17,15 +17,15 @@ export class FetchTransactionError extends Error {
* @param connection - A connection to the cluster.
* @param account - Account that may sign the transaction.
* @param link - `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link).
* @param options - Options for `getRecentBlockhash`.
* @param options - Options for `getLatestBlockhash`.
*
* @throws {FetchTransactionError}
*/
export async function fetchTransaction(
connection: Connection,
account: PublicKey,
link: string | URL,
{ commitment }: { commitment?: Commitment } = {}
{ commitment }: GetLatestBlockhashConfig = {}
): Promise<Transaction> {
const response = await fetch(String(link), {
method: 'POST',
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function fetchTransaction(
} else if (publicKey.equals(account)) {
// If the only signature expected is for `account`, ignore the recent blockhash in the transaction.
if (signatures.length === 1) {
transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
transaction.recentBlockhash = (await connection.getLatestBlockhash(commitment)).blockhash;
}
} else {
throw new FetchTransactionError('missing signature');
Expand All @@ -69,7 +69,7 @@ export async function fetchTransaction(
} else {
// Ignore the fee payer and recent blockhash in the transaction and initialize them.
transaction.feePayer = account;
transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
transaction.recentBlockhash = (await connection.getLatestBlockhash(commitment)).blockhash;
}

return transaction;
Expand Down

0 comments on commit 44cc4ea

Please sign in to comment.