Skip to content

Commit 89e9db6

Browse files
authored
sdk: solana - support gas token transfers (wormhole-foundation#603)
1 parent 3c5b47e commit 89e9db6

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

solana/ts/sdk/ntt.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Program, web3 } from "@coral-xyz/anchor";
22
import * as splToken from "@solana/spl-token";
3-
import { createAssociatedTokenAccountInstruction } from "@solana/spl-token";
43
import {
54
AddressLookupTableAccount,
65
Connection,
@@ -9,6 +8,7 @@ import {
98
PublicKey,
109
SystemProgram,
1110
Transaction,
11+
TransactionInstruction,
1212
TransactionMessage,
1313
VersionedTransaction,
1414
} from "@solana/web3.js";
@@ -827,6 +827,57 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
827827
const fromAuthority = payerAddress;
828828
const from = await this.getTokenAccount(fromAuthority);
829829

830+
if (options.wrapNative) {
831+
if (this.tokenAddress !== splToken.NATIVE_MINT.toBase58()) {
832+
throw new Error("Configured token must be native mint for wrapping");
833+
}
834+
835+
const associatedTokenAccount = splToken.getAssociatedTokenAddressSync(
836+
splToken.NATIVE_MINT,
837+
payerAddress
838+
);
839+
840+
const accountExists = await this.connection.getAccountInfo(
841+
associatedTokenAccount
842+
);
843+
844+
const instructions: TransactionInstruction[] = [
845+
// Create the associated token account if it doesn't exist
846+
...(accountExists
847+
? []
848+
: [
849+
splToken.createAssociatedTokenAccountInstruction(
850+
payerAddress,
851+
associatedTokenAccount,
852+
payerAddress,
853+
splToken.NATIVE_MINT
854+
),
855+
]),
856+
857+
SystemProgram.transfer({
858+
fromPubkey: payerAddress,
859+
toPubkey: associatedTokenAccount,
860+
lamports: amount,
861+
}),
862+
863+
// Sync the native token account
864+
splToken.createSyncNativeInstruction(associatedTokenAccount),
865+
];
866+
867+
const { blockhash } = await this.connection.getLatestBlockhash();
868+
869+
const messageV0 = new TransactionMessage({
870+
payerKey: payerAddress,
871+
instructions,
872+
recentBlockhash: blockhash,
873+
}).compileToV0Message();
874+
875+
const transaction = new VersionedTransaction(messageV0);
876+
877+
// NOTE: wrap native is handled separately due to tx size error
878+
yield this.createUnsignedTx({ transaction }, "Ntt.WrapNative");
879+
}
880+
830881
const transferArgs = NTT.transferArgs(amount, destination, options.queue);
831882

832883
const txArgs = {
@@ -941,7 +992,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
941992
const acctInfo = await this.connection.getAccountInfo(ata);
942993
if (acctInfo === null) {
943994
const transaction = new Transaction().add(
944-
createAssociatedTokenAccountInstruction(
995+
splToken.createAssociatedTokenAccountInstruction(
945996
senderAddress,
946997
ata,
947998
senderAddress,

0 commit comments

Comments
 (0)