Skip to content

Commit e773afa

Browse files
committed
dont override the commitment level
1 parent 15ca3fc commit e773afa

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

examples/src/helpers/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { getAlgorandSigner } from "@wormhole-foundation/connect-sdk-algorand/src/testing";
1919
import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing";
2020
import { getEvmSigner } from "@wormhole-foundation/connect-sdk-evm/src/testing";
21-
import { getSolanaSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";
21+
import { getSolanaSignAndSendSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";
2222

2323
// Use .env.example as a template for your .env file and populate it with secrets
2424
// for funded accounts on the relevant chain+network combos to run the example
@@ -56,7 +56,7 @@ export async function getStuff<
5656
const platform = chain.platform.utils()._platform;
5757
switch (platform) {
5858
case "Solana":
59-
signer = await getSolanaSigner(await chain.getRpc(), getEnv("SOL_PRIVATE_KEY"));
59+
signer = await getSolanaSignAndSendSigner(await chain.getRpc(), getEnv("SOL_PRIVATE_KEY"));
6060
break;
6161
case "Cosmwasm":
6262
signer = await getCosmwasmSigner(await chain.getRpc(), getEnv("COSMOS_MNEMONIC"));

platforms/solana/src/platform.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,11 @@ export class SolanaPlatform<N extends Network> extends PlatformContext<
209209
rpc: Connection,
210210
commitment?: Commitment,
211211
): Promise<{ blockhash: string; lastValidBlockHeight: number }> {
212-
// Use finalized to prevent blockhash not found errors
213-
// Note: this may mean we have less time to submit transactions?
214-
return rpc.getLatestBlockhash(commitment ?? 'finalized');
212+
return rpc.getLatestBlockhash(commitment ?? rpc.commitment);
215213
}
216214

217215
static async getLatestBlock(rpc: Connection): Promise<number> {
218-
const { lastValidBlockHeight } = await this.latestBlock(rpc, 'confirmed');
216+
const { lastValidBlockHeight } = await this.latestBlock(rpc);
219217
return lastValidBlockHeight;
220218
}
221219

platforms/solana/src/testing/sendSigner.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ComputeBudgetProgram,
23
Connection,
34
Keypair,
45
SendOptions,
@@ -15,6 +16,9 @@ import { SolanaChains } from '../types';
1516
import { SolanaUnsignedTransaction } from '../unsignedTransaction';
1617
import { logTxDetails } from './debug';
1718

19+
// Number of blocks to wait before considering a transaction expired
20+
const SOLANA_EXPIRED_BLOCKHEIGHT = 150;
21+
1822
export class SolanaSendSigner<
1923
N extends Network,
2024
C extends SolanaChains = 'Solana',
@@ -26,6 +30,7 @@ export class SolanaSendSigner<
2630
private _keypair: Keypair,
2731
private _debug: boolean = false,
2832
private _sendOpts?: SendOptions,
33+
private _priotifyFeeAmount?: bigint,
2934
) {
3035
this._sendOpts = this._sendOpts ?? {
3136
preflightCommitment: this._rpc.commitment,
@@ -42,6 +47,7 @@ export class SolanaSendSigner<
4247

4348
// Handles retrying a Transaction if the error is deemed to be
4449
// recoverable. Currently handles:
50+
// - Transaction expired
4551
// - Blockhash not found
4652
// - Not enough bytes (storage account not seen yet)
4753
private retryable(e: any): boolean {
@@ -75,7 +81,6 @@ export class SolanaSendSigner<
7581
async signAndSend(tx: UnsignedTransaction[]): Promise<any[]> {
7682
let { blockhash, lastValidBlockHeight } = await SolanaPlatform.latestBlock(
7783
this._rpc,
78-
'finalized',
7984
);
8085

8186
const txids: string[] = [];
@@ -86,6 +91,13 @@ export class SolanaSendSigner<
8691
} = txn as SolanaUnsignedTransaction<N, C>;
8792
console.log(`Signing: ${description} for ${this.address()}`);
8893

94+
if (this._priotifyFeeAmount)
95+
transaction.add(
96+
ComputeBudgetProgram.setComputeUnitPrice({
97+
microLamports: this._priotifyFeeAmount,
98+
}),
99+
);
100+
89101
if (this._debug) logTxDetails(transaction);
90102

91103
// Try to send the transaction up to 5 times
@@ -103,9 +115,22 @@ export class SolanaSendSigner<
103115
} catch (e) {
104116
if (!this.retryable(e)) throw e;
105117

106-
// If it is retryable, we should grab a new block hash
107-
({ blockhash, lastValidBlockHeight } =
108-
await SolanaPlatform.latestBlock(this._rpc, 'finalized'));
118+
// If it is retryable, we need to grab a new block hash
119+
const {
120+
blockhash: newBlockhash,
121+
lastValidBlockHeight: newBlockHeight,
122+
} = await SolanaPlatform.latestBlock(this._rpc);
123+
124+
// But we should _not_ submit if the blockhash hasnt expired
125+
if (
126+
newBlockHeight - lastValidBlockHeight <
127+
SOLANA_EXPIRED_BLOCKHEIGHT
128+
) {
129+
throw e;
130+
}
131+
132+
lastValidBlockHeight = newBlockHeight;
133+
blockhash = newBlockhash;
109134
}
110135
}
111136
}

platforms/solana/src/testing/signer.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ export class SolanaSigner<N extends Network, C extends SolanaChains = 'Solana'>
2727
}
2828

2929
async sign(tx: UnsignedTransaction[]): Promise<any[]> {
30-
const { blockhash } = await SolanaPlatform.latestBlock(
31-
this._rpc,
32-
'finalized',
33-
);
30+
const { blockhash } = await SolanaPlatform.latestBlock(this._rpc);
3431

3532
const signed = [];
3633
for (const txn of tx) {

0 commit comments

Comments
 (0)