diff --git a/examples/src/tokenBridge.ts b/examples/src/tokenBridge.ts index 3a9ce5edcb..84079f1af0 100644 --- a/examples/src/tokenBridge.ts +++ b/examples/src/tokenBridge.ts @@ -33,7 +33,7 @@ import "@wormhole-foundation/connect-sdk-algorand-tokenbridge"; const rcvChain = wh.getChain("Solana"); // Shortcut to allow transferring native gas token - //const token: TokenId | "native" = "native"; + const token: TokenId | "native" = "native"; // A TokenId is just a `{chain, address}` pair and an alias for ChainAddress // The `address` field must be a parsed address. @@ -42,9 +42,8 @@ import "@wormhole-foundation/connect-sdk-algorand-tokenbridge"; // e.g. // wAvax on Solana // const token = Wormhole.chainAddress("Solana", "3Ftc5hTz9sG4huk79onufGiebJNDMZNL8HYgdMJ9E7JR"); - // wSol on Avax - const token = Wormhole.chainAddress("Avalanche", "0xb10563644a6AB8948ee6d7f5b0a1fb15AaEa1E03"); + // const token = Wormhole.chainAddress("Avalanche", "0xb10563644a6AB8948ee6d7f5b0a1fb15AaEa1E03"); // Normalized given token decimals later but can just pass bigints as base units // Note: The Token bridge will dedust past 8 decimals diff --git a/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts b/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts index 3cd60ec43a..e876389543 100644 --- a/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts +++ b/platforms/solana/protocols/tokenBridge/src/tokenBridge.ts @@ -1,4 +1,5 @@ import { + Platform, Chain, ChainAddress, ChainId, @@ -49,7 +50,6 @@ import { TransactionInstruction, } from '@solana/web3.js'; -import { Platform } from '@wormhole-foundation/sdk-base/src'; import { TokenBridge as TokenBridgeContract } from './tokenBridgeType'; import { createApproveAuthoritySignerInstruction, @@ -210,6 +210,8 @@ export class SolanaTokenBridge payer?: AnySolanaAddress, ): AsyncGenerator> { if (!payer) throw new Error('Payer required to create attestation'); + + const { blockhash } = await SolanaPlatform.latestBlock(this.connection); const senderAddress = new SolanaAddress(payer).unwrap(); // TODO: createNonce().readUInt32LE(0); const nonce = 0; @@ -231,7 +233,6 @@ export class SolanaTokenBridge ); const transaction = new Transaction().add(transferIx, attestIx); - const { blockhash } = await SolanaPlatform.latestBlock(this.connection); transaction.recentBlockhash = blockhash; transaction.feePayer = senderAddress; transaction.partialSign(messageKey); @@ -244,9 +245,9 @@ export class SolanaTokenBridge payer?: AnySolanaAddress, ): AsyncGenerator> { if (!payer) throw new Error('Payer required to create attestation'); - const senderAddress = new SolanaAddress(payer).unwrap(); const { blockhash } = await SolanaPlatform.latestBlock(this.connection); + const senderAddress = new SolanaAddress(payer).unwrap(); // Yield transactions to verify sigs and post the VAA yield* this.coreBridge.postVaa(senderAddress, vaa, blockhash); @@ -275,7 +276,9 @@ export class SolanaTokenBridge ): Promise> { // https://github.com/wormhole-foundation/wormhole-connect/blob/development/sdk/src/contexts/solana/context.ts#L245 + const { blockhash } = await SolanaPlatform.latestBlock(this.connection); const senderAddress = new SolanaAddress(sender).unwrap(); + // TODO: the payer can actually be different from the sender. We need to allow the user to pass in an optional payer const payerPublicKey = senderAddress; @@ -362,7 +365,6 @@ export class SolanaTokenBridge payerPublicKey, //authority ); - const { blockhash } = await SolanaPlatform.latestBlock(this.connection); const transaction = new Transaction(); transaction.recentBlockhash = blockhash; transaction.feePayer = payerPublicKey; @@ -393,8 +395,8 @@ export class SolanaTokenBridge return; } + const { blockhash } = await SolanaPlatform.latestBlock(this.connection); const tokenAddress = new SolanaAddress(token).unwrap(); - const senderAddress = new SolanaAddress(sender).unwrap(); const senderTokenAddress = await getAssociatedTokenAddress( tokenAddress, @@ -493,7 +495,6 @@ export class SolanaTokenBridge tokenBridgeTransferIx, ); - const { blockhash } = await SolanaPlatform.latestBlock(this.connection); transaction.recentBlockhash = blockhash; transaction.feePayer = senderAddress; transaction.partialSign(message); diff --git a/platforms/solana/src/platform.ts b/platforms/solana/src/platform.ts index 70c1e2f600..04416b0e55 100644 --- a/platforms/solana/src/platform.ts +++ b/platforms/solana/src/platform.ts @@ -17,11 +17,11 @@ import { SolanaChain } from './chain'; import { TOKEN_PROGRAM_ID } from '@solana/spl-token'; import { - BlockheightBasedTransactionConfirmationStrategy, Commitment, Connection, ParsedAccountData, PublicKey, + SendOptions, } from '@solana/web3.js'; import { SolanaAddress, SolanaZeroAddress } from './address'; import { @@ -164,23 +164,30 @@ export class SolanaPlatform extends PlatformContext< chain: Chain, rpc: Connection, stxns: SignedTx[], + opts?: SendOptions, ): Promise { const { blockhash, lastValidBlockHeight } = await this.latestBlock(rpc); + // Set the commitment level to match the rpc commitment level + // otherwise, it defaults to finalized + if (!opts) opts = { preflightCommitment: rpc.commitment }; + const txhashes = await Promise.all( stxns.map((stxn) => { - return rpc.sendRawTransaction(stxn); + return rpc.sendRawTransaction(stxn, opts); }), ); await Promise.all( - txhashes.map((txid) => { - const bhs: BlockheightBasedTransactionConfirmationStrategy = { - signature: txid, - blockhash, - lastValidBlockHeight, - }; - return rpc.confirmTransaction(bhs, rpc.commitment); + txhashes.map((signature) => { + return rpc.confirmTransaction( + { + signature, + blockhash, + lastValidBlockHeight, + }, + rpc.commitment, + ); }), );