|
1 | 1 | import { Program, web3 } from "@coral-xyz/anchor";
|
2 | 2 | import * as splToken from "@solana/spl-token";
|
3 |
| -import { createAssociatedTokenAccountInstruction } from "@solana/spl-token"; |
4 | 3 | import {
|
5 | 4 | AddressLookupTableAccount,
|
6 | 5 | Connection,
|
|
9 | 8 | PublicKey,
|
10 | 9 | SystemProgram,
|
11 | 10 | Transaction,
|
| 11 | + TransactionInstruction, |
12 | 12 | TransactionMessage,
|
13 | 13 | VersionedTransaction,
|
14 | 14 | } from "@solana/web3.js";
|
@@ -827,6 +827,57 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
|
827 | 827 | const fromAuthority = payerAddress;
|
828 | 828 | const from = await this.getTokenAccount(fromAuthority);
|
829 | 829 |
|
| 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 | + |
830 | 881 | const transferArgs = NTT.transferArgs(amount, destination, options.queue);
|
831 | 882 |
|
832 | 883 | const txArgs = {
|
@@ -941,7 +992,7 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
|
941 | 992 | const acctInfo = await this.connection.getAccountInfo(ata);
|
942 | 993 | if (acctInfo === null) {
|
943 | 994 | const transaction = new Transaction().add(
|
944 |
| - createAssociatedTokenAccountInstruction( |
| 995 | + splToken.createAssociatedTokenAccountInstruction( |
945 | 996 | senderAddress,
|
946 | 997 | ata,
|
947 | 998 | senderAddress,
|
|
0 commit comments