Skip to content

Commit

Permalink
allow opts to be passed to sendWait on solana platform
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jan 2, 2024
1 parent bfb7a61 commit cef2a53
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
5 changes: 2 additions & 3 deletions examples/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions platforms/solana/protocols/tokenBridge/src/tokenBridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Platform,
Chain,
ChainAddress,
ChainId,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -210,6 +210,8 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
payer?: AnySolanaAddress,
): AsyncGenerator<SolanaUnsignedTransaction<N, C>> {
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;
Expand All @@ -231,7 +233,6 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
);

const transaction = new Transaction().add(transferIx, attestIx);
const { blockhash } = await SolanaPlatform.latestBlock(this.connection);
transaction.recentBlockhash = blockhash;
transaction.feePayer = senderAddress;
transaction.partialSign(messageKey);
Expand All @@ -244,9 +245,9 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
payer?: AnySolanaAddress,
): AsyncGenerator<SolanaUnsignedTransaction<N, C>> {
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);
Expand Down Expand Up @@ -275,7 +276,9 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
): Promise<SolanaUnsignedTransaction<N, C>> {
// 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;

Expand Down Expand Up @@ -362,7 +365,6 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
payerPublicKey, //authority
);

const { blockhash } = await SolanaPlatform.latestBlock(this.connection);
const transaction = new Transaction();
transaction.recentBlockhash = blockhash;
transaction.feePayer = payerPublicKey;
Expand Down Expand Up @@ -393,8 +395,8 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
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,
Expand Down Expand Up @@ -493,7 +495,6 @@ export class SolanaTokenBridge<N extends Network, C extends SolanaChains>
tokenBridgeTransferIx,
);

const { blockhash } = await SolanaPlatform.latestBlock(this.connection);
transaction.recentBlockhash = blockhash;
transaction.feePayer = senderAddress;
transaction.partialSign(message);
Expand Down
25 changes: 16 additions & 9 deletions platforms/solana/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -164,23 +164,30 @@ export class SolanaPlatform<N extends Network> extends PlatformContext<
chain: Chain,
rpc: Connection,
stxns: SignedTx[],
opts?: SendOptions,
): Promise<TxHash[]> {
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,
);
}),
);

Expand Down

0 comments on commit cef2a53

Please sign in to comment.