Skip to content

Commit

Permalink
Umi lint fixes and fix leaf index for one test (#73)
Browse files Browse the repository at this point in the history
* Umi lint and formatting fixes

* Fix leaf index in test
  • Loading branch information
danenbm authored Jan 12, 2024
1 parent 63d5bcd commit e9a0934
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
23 changes: 18 additions & 5 deletions clients/js/src/leafAssetId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Context, Pda, PublicKey, TransactionSignature } from '@metaplex-foundation/umi';
import {
Context,
Pda,
PublicKey,
TransactionSignature,
} from '@metaplex-foundation/umi';
import { publicKey, string, u64 } from '@metaplex-foundation/umi/serializers';
import { LeafSchema, MPL_BUBBLEGUM_PROGRAM_ID, getLeafSchemaSerializer } from './generated';
import {
LeafSchema,
MPL_BUBBLEGUM_PROGRAM_ID,
getLeafSchemaSerializer,
} from './generated';

export function findLeafAssetIdPda(
context: Pick<Context, 'programs' | 'eddsa'>,
Expand Down Expand Up @@ -28,7 +37,9 @@ export async function parseLeafFromMintV1Transaction(
const innerInstructions = transaction?.meta.innerInstructions;

if (innerInstructions) {
const leaf = getLeafSchemaSerializer().deserialize(innerInstructions[0].instructions[0].data.slice(8));
const leaf = getLeafSchemaSerializer().deserialize(
innerInstructions[0].instructions[0].data.slice(8)
);
return leaf[0];
}

Expand All @@ -43,9 +54,11 @@ export async function parseLeafFromMintToCollectionV1Transaction(
const innerInstructions = transaction?.meta.innerInstructions;

if (innerInstructions) {
const leaf = getLeafSchemaSerializer().deserialize(innerInstructions[0].instructions[1].data.slice(8));
const leaf = getLeafSchemaSerializer().deserialize(
innerInstructions[0].instructions[1].data.slice(8)
);
return leaf[0];
}

throw new Error('Could not parse leaf from transaction');
}
}
25 changes: 20 additions & 5 deletions clients/js/test/parseMintV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import {
} from '@metaplex-foundation/umi';
import { createNft } from '@metaplex-foundation/mpl-token-metadata';
import test from 'ava';
import { MetadataArgsArgs, fetchMerkleTree, findLeafAssetIdPda, mintToCollectionV1, mintV1, parseLeafFromMintToCollectionV1Transaction, parseLeafFromMintV1Transaction } from '../src';
import {
MetadataArgsArgs,
fetchMerkleTree,
findLeafAssetIdPda,
mintToCollectionV1,
mintV1,
parseLeafFromMintToCollectionV1Transaction,
parseLeafFromMintV1Transaction,
} from '../src';
import { createTree, createUmi } from './_setup';

test('it can parse the leaf from mint instructions', async (t) => {
Expand All @@ -32,13 +40,17 @@ test('it can parse the leaf from mint instructions', async (t) => {

// Test with 10 different leaves to be sure they increment correctly.
for (let nonce = 0; nonce < 10; nonce += 1) {
const { signature } = await mintV1(umi, { leafOwner, merkleTree, metadata }).sendAndConfirm(umi, { confirm: { commitment: 'confirmed' } });
const { signature } = await mintV1(umi, {
leafOwner,
merkleTree,
metadata,
}).sendAndConfirm(umi, { confirm: { commitment: 'confirmed' } });
const leaf = await parseLeafFromMintV1Transaction(umi, signature);
const assetId = findLeafAssetIdPda(umi, { merkleTree, leafIndex: nonce });

t.is(leafOwner, leaf.owner);
t.is(Number(leaf.nonce), nonce);
t.is(leaf.id, assetId[0])
t.is(leaf.id, assetId[0]);
}
});

Expand Down Expand Up @@ -85,11 +97,14 @@ test('it can parse the leaf from mintToCollection instructions)', async (t) => {
collectionMint: collectionMint.publicKey,
}).sendAndConfirm(umi);

const leaf = await parseLeafFromMintToCollectionV1Transaction(umi, signature);
const leaf = await parseLeafFromMintToCollectionV1Transaction(
umi,
signature
);
const assetId = findLeafAssetIdPda(umi, { merkleTree, leafIndex: nonce });

t.is(leafOwner, leaf.owner);
t.is(Number(leaf.nonce), nonce);
t.is(leaf.id, assetId[0])
t.is(leaf.id, assetId[0]);
}
});
4 changes: 1 addition & 3 deletions clients/js/test/updateMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,8 @@ test('it can update metadata using the getAssetWithProof helper with verified co
},
creators: [],
};
const leafIndex = Number(
(await fetchMerkleTree(umi, merkleTree)).tree.activeIndex
);
const leafOwner = generateSigner(umi).publicKey;
const leafIndex = 8;
await mintToCollectionV1(umi, {
leafOwner,
merkleTree,
Expand Down

0 comments on commit e9a0934

Please sign in to comment.