Skip to content

Commit 441e552

Browse files
committed
initialize ix
1 parent 5246cd5 commit 441e552

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

solana/ts/lib/ntt.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ import {
3838
NttBindings,
3939
getNttProgram,
4040
} from "./bindings.js";
41-
import { chainToBytes, derivePda } from "./utils.js";
41+
import {
42+
BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
43+
chainToBytes,
44+
derivePda,
45+
programDataAddress,
46+
} from "./utils.js";
4247

4348
export namespace NTT {
4449
export interface TransferArgs {
@@ -184,6 +189,49 @@ export namespace NTT {
184189
else throw new Error("Unknown IDL version: " + version);
185190
}
186191

192+
export async function createInitializeInstruction(
193+
program: Program<NttBindings.NativeTokenTransfer<IdlVersion>>,
194+
args: {
195+
payer: PublicKey;
196+
owner: PublicKey;
197+
chain: Chain;
198+
mint: PublicKey;
199+
outboundLimit: bigint;
200+
tokenProgram: PublicKey;
201+
mode: "burning" | "locking";
202+
},
203+
pdas?: Pdas
204+
) {
205+
const mode: any =
206+
args.mode === "burning" ? { burning: {} } : { locking: {} };
207+
const chainId = toChainId(args.chain);
208+
209+
pdas = pdas ?? NTT.pdas(program.programId);
210+
211+
const limit = new BN(args.outboundLimit.toString());
212+
return await program.methods
213+
.initialize({ chainId, limit: limit, mode })
214+
.accountsStrict({
215+
payer: args.payer,
216+
deployer: args.owner,
217+
programData: programDataAddress(program.programId),
218+
config: pdas.configAccount(),
219+
mint: args.mint,
220+
rateLimit: pdas.outboxRateLimitAccount(),
221+
tokenProgram: args.tokenProgram,
222+
tokenAuthority: pdas.tokenAuthority(),
223+
custody: await NTT.custodyAccountAddress(
224+
pdas,
225+
args.mint,
226+
args.tokenProgram
227+
),
228+
bpfLoaderUpgradeableProgram: BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
229+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
230+
systemProgram: SystemProgram.programId,
231+
})
232+
.instruction();
233+
}
234+
187235
export async function createTransferBurnInstruction(
188236
program: Program<NttBindings.NativeTokenTransfer<IdlVersion>>,
189237
config: NttBindings.Config<IdlVersion>,

solana/ts/sdk/ntt.ts

+6-54
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ import {
4242
} from "@wormhole-foundation/sdk-solana-core";
4343
import BN from "bn.js";
4444
import { NTT, NttQuoter, WEI_PER_GWEI } from "../lib/index.js";
45-
import {
46-
BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
47-
programDataAddress,
48-
} from "./utils.js";
4945

5046
import { IdlVersion, NttBindings, getNttProgram } from "../lib/bindings.js";
5147

@@ -177,35 +173,17 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
177173
outboundLimit: bigint;
178174
mode: "burning" | "locking";
179175
}) {
180-
const mode: any =
181-
args.mode === "burning" ? { burning: {} } : { locking: {} };
182-
const chainId = toChainId(args.chain);
183176
const mintInfo = await this.connection.getAccountInfo(args.mint);
184-
if (mintInfo === null) {
177+
if (mintInfo === null)
185178
throw new Error(
186179
"Couldn't determine token program. Mint account is null."
187180
);
188-
}
189181

190-
const tokenProgram = mintInfo.owner;
191-
const limit = new BN(args.outboundLimit.toString());
192-
const ix = await this.program.methods
193-
.initialize({ chainId, limit: limit, mode })
194-
.accountsStrict({
195-
payer: args.payer,
196-
deployer: args.owner,
197-
programData: programDataAddress(this.program.programId),
198-
config: this.pdas.configAccount(),
199-
mint: args.mint,
200-
rateLimit: this.pdas.outboxRateLimitAccount(),
201-
tokenProgram,
202-
tokenAuthority: this.pdas.tokenAuthority(),
203-
custody: await this.custodyAccountAddress(args.mint, tokenProgram),
204-
bpfLoaderUpgradeableProgram: BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
205-
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
206-
systemProgram: SystemProgram.programId,
207-
})
208-
.instruction();
182+
const ix = await NTT.createInitializeInstruction(
183+
this.program,
184+
{ ...args, tokenProgram: mintInfo.owner },
185+
this.pdas
186+
);
209187

210188
const tx = new Transaction();
211189
tx.feePayer = args.payer;
@@ -783,32 +761,6 @@ export class SolanaNtt<N extends Network, C extends SolanaChains>
783761
return xfer;
784762
}
785763

786-
/**
787-
* Returns the address of the custody account. If the config is available
788-
* (i.e. the program is initialised), the mint is derived from the config.
789-
* Otherwise, the mint must be provided.
790-
*/
791-
async custodyAccountAddress(
792-
configOrMint: NttBindings.Config<IdlVersion> | PublicKey,
793-
tokenProgram = splToken.TOKEN_PROGRAM_ID
794-
): Promise<PublicKey> {
795-
if (configOrMint instanceof PublicKey) {
796-
return splToken.getAssociatedTokenAddress(
797-
configOrMint,
798-
this.pdas.tokenAuthority(),
799-
true,
800-
tokenProgram
801-
);
802-
} else {
803-
return splToken.getAssociatedTokenAddress(
804-
configOrMint.mint,
805-
this.pdas.tokenAuthority(),
806-
true,
807-
configOrMint.tokenProgram
808-
);
809-
}
810-
}
811-
812764
async getAddressLookupTable(
813765
useCache = true
814766
): Promise<AddressLookupTableAccount> {

0 commit comments

Comments
 (0)