From 2be0bf7444c5f3a90514cb2e03f51fed49a24310 Mon Sep 17 00:00:00 2001 From: Noah Gundotra Date: Wed, 25 May 2022 14:37:05 -0400 Subject: [PATCH] fix nits --- contracts/tests/gumdrop-test.ts | 37 ++++++++------------------------- contracts/tests/gumdropTree.ts | 1 + contracts/tests/utils.ts | 9 +++++++- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/contracts/tests/gumdrop-test.ts b/contracts/tests/gumdrop-test.ts index 988727ba..3cb001f5 100644 --- a/contracts/tests/gumdrop-test.ts +++ b/contracts/tests/gumdrop-test.ts @@ -4,26 +4,8 @@ import NodeWallet from '@project-serum/anchor/dist/cjs/nodewallet'; import { Gumdrop } from "../target/types/gumdrop"; import { MerkleTree } from './gumdropTree'; import { BinaryWriter } from 'borsh' - -async function delay(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -async function succeedOrThrow(txId: string, connection: Connection) { - const err = (await connection.confirmTransaction(txId, "confirmed")).value.err - if (err) { - throw new Error(`${txId} failed: \n${JSON.stringify(err)}\n`); - } -} - - -function getMerkleRollAccountSize(maxDepth: number, maxBufferSize: number): number { - let headerSize = 8 + 32 + 32; - let changeLogSize = (maxDepth * 32 + 32 + 4 + 4) * maxBufferSize; - let rightMostPathSize = maxDepth * 32 + 32 + 4 + 4; - let merkleRollSize = 8 + 8 + 16 + changeLogSize + rightMostPathSize; - return merkleRollSize + headerSize; -} +import { getMerkleRollAccountSize } from './merkle-roll-serde'; +import { succeedOrThrow } from './utils'; async function initMerkleTreeInstruction( maxDepth: number, @@ -76,7 +58,7 @@ async function getBubblegumTreeAuthority(tree: PublicKey, bubblegumProgramId: Pu } type GumdropLeaf = { - metadata: Metadata, + metadata: TokenMetadata, publicKey: PublicKey, }; @@ -84,7 +66,7 @@ type Creator = { creator: PublicKey, share: number, }; -type Metadata = { +type TokenMetadata = { name: string, symbol: string, uri: string, @@ -101,7 +83,7 @@ type Metadata = { creators: Creator[], }; -const METADATA = [ +const TOKEN_METADATA = [ { name: "A", symbol: "A", @@ -184,7 +166,7 @@ const METADATA = [ }, ]; -function serializeMetadata(metadata: Metadata): Buffer { +function serializeMetadata(metadata: TokenMetadata): Buffer { let writer = new BinaryWriter(); writer.writeString(metadata.name) writer.writeString(metadata.symbol) @@ -223,7 +205,7 @@ function hashLeaf(leaf: GumdropLeaf, index: number, bubblegumTree: PublicKey): B function buildGumdropTree(bubblegumTree: PublicKey, claimer: PublicKey): MerkleTree { let leaves: Buffer[] = []; - METADATA.forEach((metadata, index) => { + TOKEN_METADATA.forEach((metadata, index) => { leaves.push(hashLeaf({ metadata, publicKey: claimer @@ -328,14 +310,13 @@ describe('Airdropping compressed NFTs with Gumdrop', () => { const txId = await gumdrop.provider.send(tx, [merkleRollKeypair, payer], { skipPreflight: true }); - console.log(`${txId} sent`); await succeedOrThrow(txId, connection); console.log("Compressed tree init succeeded 😎"); // Get nonce key for all compressed NFTs let index = 0; - while (index < METADATA.length) { - const nftMetadata = METADATA[index]; + while (index < TOKEN_METADATA.length) { + const nftMetadata = TOKEN_METADATA[index]; const proof = gumdropTree.getProof(index); console.log("\nVerified proof:", gumdropTree.verifyProof(index, proof, gumdropTree.getRoot())); const leafHash = gumdropTree.layers[0][index].buffer; diff --git a/contracts/tests/gumdropTree.ts b/contracts/tests/gumdropTree.ts index 039d6eb3..83aef6d0 100644 --- a/contracts/tests/gumdropTree.ts +++ b/contracts/tests/gumdropTree.ts @@ -1,3 +1,4 @@ +/// Stolen from https://github.com/metaplex-foundation/gumdrop/blob/main/packages/gumdrop/src/utils/merkleTree.ts import { keccak_256 } from 'js-sha3'; export class MerkleTree { diff --git a/contracts/tests/utils.ts b/contracts/tests/utils.ts index e7c4e5ee..596777d7 100644 --- a/contracts/tests/utils.ts +++ b/contracts/tests/utils.ts @@ -1,3 +1,4 @@ +import { Connection } from '@solana/web3.js'; import { Provider } from "@project-serum/anchor"; import { TransactionInstruction, Transaction, Signer } from "@solana/web3.js"; @@ -11,7 +12,6 @@ export async function logTx(provider: Provider, txId: string, verbose: boolean = } }; - export async function execute( provider: Provider, instructions: TransactionInstruction[], @@ -27,3 +27,10 @@ export async function execute( await logTx(provider, txid, false); return txid; } + +export async function succeedOrThrow(txId: string, connection: Connection) { + const err = (await connection.confirmTransaction(txId, "confirmed")).value.err + if (err) { + throw new Error(`${txId} failed: \n${JSON.stringify(err)}\n`); + } +}