Skip to content

Commit

Permalink
dont override the commitment level
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jan 3, 2024
1 parent 15ca3fc commit 2853f77
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { getAlgorandSigner } from "@wormhole-foundation/connect-sdk-algorand/src/testing";
import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing";
import { getEvmSigner } from "@wormhole-foundation/connect-sdk-evm/src/testing";
import { getSolanaSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";
import { getSolanaSignAndSendSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";

// Use .env.example as a template for your .env file and populate it with secrets
// for funded accounts on the relevant chain+network combos to run the example
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function getStuff<
const platform = chain.platform.utils()._platform;
switch (platform) {
case "Solana":
signer = await getSolanaSigner(await chain.getRpc(), getEnv("SOL_PRIVATE_KEY"));
signer = await getSolanaSignAndSendSigner(await chain.getRpc(), getEnv("SOL_PRIVATE_KEY"));
break;
case "Cosmwasm":
signer = await getCosmwasmSigner(await chain.getRpc(), getEnv("COSMOS_MNEMONIC"));
Expand Down
6 changes: 2 additions & 4 deletions platforms/solana/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,11 @@ export class SolanaPlatform<N extends Network> extends PlatformContext<
rpc: Connection,
commitment?: Commitment,
): Promise<{ blockhash: string; lastValidBlockHeight: number }> {
// Use finalized to prevent blockhash not found errors
// Note: this may mean we have less time to submit transactions?
return rpc.getLatestBlockhash(commitment ?? 'finalized');
return rpc.getLatestBlockhash(commitment ?? rpc.commitment);
}

static async getLatestBlock(rpc: Connection): Promise<number> {
const { lastValidBlockHeight } = await this.latestBlock(rpc, 'confirmed');
const { lastValidBlockHeight } = await this.latestBlock(rpc);
return lastValidBlockHeight;
}

Expand Down
40 changes: 35 additions & 5 deletions platforms/solana/src/testing/sendSigner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ComputeBudgetProgram,
Connection,
Keypair,
SendOptions,
Expand All @@ -15,6 +16,9 @@ import { SolanaChains } from '../types';
import { SolanaUnsignedTransaction } from '../unsignedTransaction';
import { logTxDetails } from './debug';

// Number of blocks to wait before considering a transaction expired
const SOLANA_EXPIRED_BLOCKHEIGHT = 150;

export class SolanaSendSigner<
N extends Network,
C extends SolanaChains = 'Solana',
Expand All @@ -26,6 +30,7 @@ export class SolanaSendSigner<
private _keypair: Keypair,
private _debug: boolean = false,
private _sendOpts?: SendOptions,
private _priotifyFeeAmount?: bigint,
) {
this._sendOpts = this._sendOpts ?? {
preflightCommitment: this._rpc.commitment,
Expand All @@ -42,6 +47,7 @@ export class SolanaSendSigner<

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

const txids: string[] = [];
Expand All @@ -86,10 +91,18 @@ export class SolanaSendSigner<
} = txn as SolanaUnsignedTransaction<N, C>;
console.log(`Signing: ${description} for ${this.address()}`);

if (this._priotifyFeeAmount)
transaction.add(
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: this._priotifyFeeAmount,
}),
);

if (this._debug) logTxDetails(transaction);

// Try to send the transaction up to 5 times
for (let i = 0; i < 5; i++) {
const maxRetries = 5;
for (let i = 0; i < maxRetries; i++) {
try {
transaction.recentBlockhash = blockhash;
transaction.partialSign(this._keypair, ...(extraSigners ?? []));
Expand All @@ -101,11 +114,28 @@ export class SolanaSendSigner<
txids.push(txid);
break;
} catch (e) {
// No point checking if retryable if we're on the last retry
if (i === maxRetries - 1) throw e;

// If it's not retryable, throw
if (!this.retryable(e)) throw e;

// If it is retryable, we should grab a new block hash
({ blockhash, lastValidBlockHeight } =
await SolanaPlatform.latestBlock(this._rpc, 'finalized'));
// If it is retryable, we need to grab a new block hash
const {
blockhash: newBlockhash,
lastValidBlockHeight: newBlockHeight,
} = await SolanaPlatform.latestBlock(this._rpc);

// But we should _not_ submit if the blockhash hasnt expired
if (
newBlockHeight - lastValidBlockHeight <
SOLANA_EXPIRED_BLOCKHEIGHT
) {
throw e;
}

lastValidBlockHeight = newBlockHeight;
blockhash = newBlockhash;
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions platforms/solana/src/testing/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export class SolanaSigner<N extends Network, C extends SolanaChains = 'Solana'>
}

async sign(tx: UnsignedTransaction[]): Promise<any[]> {
const { blockhash } = await SolanaPlatform.latestBlock(
this._rpc,
'finalized',
);
const { blockhash } = await SolanaPlatform.latestBlock(this._rpc);

const signed = [];
for (const txn of tx) {
Expand Down

0 comments on commit 2853f77

Please sign in to comment.