Skip to content

Commit

Permalink
Add SDK helper to choose compression programs
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Oct 30, 2024
1 parent 14329d8 commit f1fb8f0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
49 changes: 49 additions & 0 deletions clients/js/src/getCompressionPrograms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Context, PublicKey } from '@metaplex-foundation/umi';
import {
SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
SPL_NOOP_PROGRAM_ID,
} from './generated';

export const MPL_ACCOUNT_COMPRESSION_PROGRAM_ID =
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW' as PublicKey<'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'>;

export const MPL_NOOP_PROGRAM_ID =
'mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3' as PublicKey<'mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'>;

// Constants for known genesis blockhashes on Solana.
const SOLANA_MAINNET_GENESIS_HASH =
'5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d';
const SOLANA_DEVNET_GENESIS_HASH =
'EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG';
const SOLANA_TESTNET_GENESIS_HASH =
'4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY';

export type CompressionPrograms = {
logWrapper: PublicKey;
compressionProgram: PublicKey;
};

export async function getCompressionPrograms(
context: Pick<Context, 'programs' | 'eddsa' | 'rpc'>
): Promise<CompressionPrograms> {
const genesisHash = await context.rpc.call<string>('getGenesisHash');

// Determine if the genesis hash matches known clusters.
const isKnownCluster = [
SOLANA_MAINNET_GENESIS_HASH,
SOLANA_DEVNET_GENESIS_HASH,
SOLANA_TESTNET_GENESIS_HASH,
].includes(genesisHash);

// Return appropriate program IDs based on the cluster.
if (isKnownCluster) {
return {
logWrapper: SPL_NOOP_PROGRAM_ID,
compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
};
}
return {
logWrapper: MPL_NOOP_PROGRAM_ID,
compressionProgram: MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
};
}
1 change: 1 addition & 0 deletions clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './hooked';
export * from './leafAssetId';
export * from './merkle';
export * from './plugin';
export * from './getCompressionPrograms';
21 changes: 12 additions & 9 deletions clients/js/test/createTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
safeFetchTreeConfigFromSeeds,
getMerkleTreeSize,
SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
getCompressionPrograms,
MPL_NOOP_PROGRAM_ID,
MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
} from '../src';
import { createUmi } from './_setup';

Expand Down Expand Up @@ -133,15 +136,17 @@ test('it can create a Bubblegum tree using mpl-account-compression and mpl-noop'
const umi = await createUmi();
const merkleTree = generateSigner(umi);

// For these tests, make sure `getCompressionPrograms` doesn't return spl programs.
const { logWrapper, compressionProgram } = await getCompressionPrograms(umi);
t.is(logWrapper, MPL_NOOP_PROGRAM_ID);
t.is(compressionProgram, MPL_ACCOUNT_COMPRESSION_PROGRAM_ID);

// When we create a tree at this address.
const builder = await createTree(umi, {
merkleTree,
maxDepth: 14,
maxBufferSize: 64,
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
...(await getCompressionPrograms(umi)),
});
await builder.sendAndConfirm(umi);

Expand Down Expand Up @@ -200,9 +205,7 @@ test('it cannot create a Bubblegum tree using invalid logWrapper with mpl-accoun
maxDepth: 14,
maxBufferSize: 64,
logWrapper: generateSigner(umi).publicKey,
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
compressionProgram: MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
});

const promise = builder.sendAndConfirm(umi);
Expand Down Expand Up @@ -262,9 +265,9 @@ test('it cannot create a Bubblegum tree when compression program does not match
merkleTree,
maxDepth: 14,
maxBufferSize: 64,
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
logWrapper: MPL_NOOP_PROGRAM_ID,
compressionProgram: generateSigner(umi).publicKey,
merkleTreeOwner: publicKey('mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'),
merkleTreeOwner: MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
});

const promise = builder.sendAndConfirm(umi);
Expand Down
39 changes: 20 additions & 19 deletions clients/js/test/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
hashMetadataData,
transfer,
verifyLeaf,
getCompressionPrograms,
MPL_NOOP_PROGRAM_ID,
MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
} from '../src';
import { createTree, createUmi, mint } from './_setup';
import {
Expand Down Expand Up @@ -58,21 +61,21 @@ test('it can transfer a compressed NFT', async (t) => {
test('it can transfer a compressed NFT using mpl-account-compression and mpl-noop', async (t) => {
// Given a tree with a minted NFT owned by leafOwnerA.
const umi = await createUmi();

// For these tests, make sure `getCompressionPrograms` doesn't return spl programs.
const { logWrapper, compressionProgram } = await getCompressionPrograms(umi);
t.is(logWrapper, MPL_NOOP_PROGRAM_ID);
t.is(compressionProgram, MPL_ACCOUNT_COMPRESSION_PROGRAM_ID);

const merkleTree = await createTree(umi, {
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
...(await getCompressionPrograms(umi)),
});
let merkleTreeAccount = await fetchMerkleTree(umi, merkleTree);
const leafOwnerA = generateSigner(umi);
const { metadata, leafIndex } = await mint(umi, {
merkleTree,
leafOwner: leafOwnerA.publicKey,
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
...(await getCompressionPrograms(umi)),
});

// When leafOwnerA transfers the NFT to leafOwnerB.
Expand All @@ -87,10 +90,7 @@ test('it can transfer a compressed NFT using mpl-account-compression and mpl-noo
nonce: leafIndex,
index: leafIndex,
proof: [],
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
...(await getCompressionPrograms(umi)),
}).sendAndConfirm(umi);

// Then the leaf was updated in the merkle tree.
Expand All @@ -107,6 +107,12 @@ test('it can transfer a compressed NFT using mpl-account-compression and mpl-noo
test('it cannot transfer a compressed NFT owned by spl-account-compression using mpl programs', async (t) => {
// Given a tree with a minted NFT owned by leafOwnerA.
const umi = await createUmi();

// For these tests, make sure `getCompressionPrograms` doesn't return spl programs.
const { logWrapper, compressionProgram } = await getCompressionPrograms(umi);
t.is(logWrapper, MPL_NOOP_PROGRAM_ID);
t.is(compressionProgram, MPL_ACCOUNT_COMPRESSION_PROGRAM_ID);

const merkleTree = await createTree(umi);
let merkleTreeAccount = await fetchMerkleTree(umi, merkleTree);
const leafOwnerA = generateSigner(umi);
Expand All @@ -127,10 +133,7 @@ test('it cannot transfer a compressed NFT owned by spl-account-compression using
nonce: leafIndex,
index: leafIndex,
proof: [],
logWrapper: publicKey('mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3'),
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
...(await getCompressionPrograms(umi)),
}).sendAndConfirm(umi);

// Then we expect a program error.
Expand Down Expand Up @@ -170,9 +173,7 @@ test('it cannot transfer a compressed NFT owned by spl-account-compression using
nonce: leafIndex,
index: leafIndex,
proof: [],
compressionProgram: publicKey(
'mcmt6YrQEMKw8Mw43FmpRLmf7BqRnFMKmAcbxE3xkAW'
),
compressionProgram: MPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
}).sendAndConfirm(umi);

// Then we expect a program error.
Expand Down

0 comments on commit f1fb8f0

Please sign in to comment.