Skip to content

Commit

Permalink
unify insturctions input
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Jan 16, 2025
1 parent 920434c commit e127c0d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
35 changes: 18 additions & 17 deletions clients/js/src/instructions/execute.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import {
Context,
Instruction,
publicKey,
Signer,
TransactionBuilder,
} from '@metaplex-foundation/umi';
import { executeV1, findAssetSignerPda } from '../generated';

export type ExecuteInput = TransactionBuilder | Instruction;
import {
AssetV1,
CollectionV1,
executeV1,
findAssetSignerPda,
} from '../generated';

export type ExecuteInput = TransactionBuilder | Instruction[];

export type ExecuteArgs = Omit<
Parameters<typeof executeV1>[1],
'programId' | 'instructionData'
'programId' | 'instructionData' | 'asset' | 'collection'
> & {
builder?: TransactionBuilder;
instruction?: Instruction;
instructions?: Instruction[];
asset: Pick<AssetV1, 'publicKey'>;
collection?: Pick<CollectionV1, 'publicKey'>;
instructions: ExecuteInput;
signers?: Signer[];
};

Expand All @@ -34,14 +39,8 @@ const executeCommon = (
const signers: Signer[] = [];

let builder: TransactionBuilder = new TransactionBuilder();
if (args.builder) {
builder = args.builder;
} else if (args.instruction) {
builder = new TransactionBuilder().add({
instruction: args.instruction,
signers: args.signers ?? [],
bytesCreatedOnChain: 0,
});
if (args.instructions instanceof TransactionBuilder) {
builder = args.instructions;
} else if (args.instructions) {
args.instructions.forEach((instruction) => {
const ixSigners: Signer[] = [];
Expand All @@ -60,16 +59,18 @@ const executeCommon = (
});
});
} else {
throw new Error('No builder or instruction provided');
throw new Error('No builder or instructions provided');
}

// eslint-disable-next-line no-restricted-syntax
for (const ix of builder.items) {
const [assetSigner] = findAssetSignerPda(context, {
asset: publicKey(args.asset),
asset: args.asset.publicKey,
});
const baseBuilder = executeV1(context, {
...args,
asset: args.asset.publicKey,
collection: args.collection?.publicKey,
assetSigner,
// Forward the programID of the instruction being executed.
programId: ix.instruction.programId,
Expand Down
42 changes: 21 additions & 21 deletions clients/js/test/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ test('it can execute for an asset as the owner', async (t) => {
t.deepEqual(beforeAssetBalance, sol(0.00315648));

await execute(umi, {
asset: asset.publicKey,
builder: transferSol(umi, {
asset,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: recipient.publicKey,
amount: sol(0.5),
Expand Down Expand Up @@ -90,8 +90,8 @@ test('it can execute multiple instructions for an asset as the owner', async (t)
t.deepEqual(beforeAssetBalance, sol(0.00315648));

await execute(umi, {
asset: asset.publicKey,
builder: transferSol(umi, {
asset,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: recipient.publicKey,
amount: sol(0.25),
Expand Down Expand Up @@ -154,8 +154,8 @@ test('it can execute for an asset as the owner with an Instruction', async (t) =
}).getInstructions()[0];

await execute(umi, {
asset: asset.publicKey,
instruction,
asset,
instructions: [instruction],
signers: [createNoopSigner(publicKey(assetSigner))],
}).sendAndConfirm(umi);

Expand Down Expand Up @@ -214,7 +214,7 @@ test('it can execute for an asset as the owner with an Instruction[]', async (t)
.getInstructions();

await execute(umi, {
asset: asset.publicKey,
asset,
instructions,
signers: [createNoopSigner(publicKey(assetSigner))],
}).sendAndConfirm(umi);
Expand Down Expand Up @@ -244,9 +244,9 @@ test('it cannot execute for an asset if not the owner', async (t) => {
const assetSigner = findAssetSignerPda(umi, { asset: asset.publicKey });

const result = execute(umi, {
asset: asset.publicKey,
asset,
authority: attacker,
builder: transferSol(umi, {
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: attacker.publicKey,
amount: sol(0.5),
Expand All @@ -271,9 +271,9 @@ test('it cannot execute for an asset as the update authority', async (t) => {
const assetSigner = findAssetSignerPda(umi, { asset: asset.publicKey });

const result = execute(umi, {
asset: asset.publicKey,
asset,
authority: umi.identity,
builder: transferSol(umi, {
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: newOwner.publicKey,
amount: sol(0.5),
Expand All @@ -298,8 +298,8 @@ test('it cannot execute for an asset in collection if no collection', async (t)
const assetSigner = findAssetSignerPda(umi, { asset: asset.publicKey });

const result = execute(umi, {
asset: asset.publicKey,
builder: transferSol(umi, {
asset,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: newOwner.publicKey,
amount: sol(0.5),
Expand Down Expand Up @@ -331,9 +331,9 @@ test('it can execute for an asset in collection as the owner', async (t) => {
t.deepEqual(beforeAssetBalance, sol(0.00315648));

await execute(umi, {
asset: asset.publicKey,
collection: collection.publicKey,
builder: transferSol(umi, {
asset,
collection,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: recipient.publicKey,
amount: sol(0.5),
Expand Down Expand Up @@ -362,9 +362,9 @@ test('it cannot transfer asset in collection with the wrong collection', async (
const wrongCollection = await createCollection(umi);
const assetSigner = findAssetSignerPda(umi, { asset: asset.publicKey });
const result = execute(umi, {
asset: asset.publicKey,
collection: wrongCollection.publicKey,
builder: transferSol(umi, {
asset,
collection: wrongCollection,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: recipient.publicKey,
amount: sol(0.5),
Expand All @@ -390,8 +390,8 @@ test('it cannot use an invalid system program', async (t) => {
});

const result = execute(umi, {
asset: asset.publicKey,
builder: transferSol(umi, {
asset,
instructions: transferSol(umi, {
source: createNoopSigner(publicKey(assetSigner)),
destination: newOwner.publicKey,
amount: sol(0.5),
Expand Down

0 comments on commit e127c0d

Please sign in to comment.