From 9bd6fe7b2f0cbee3bc723f01bd3f922168bbaa19 Mon Sep 17 00:00:00 2001 From: Jarry Xiao <61092285+jarry-xiao@users.noreply.github.com> Date: Thu, 30 Jun 2022 10:24:04 -0400 Subject: [PATCH] Jxiao/fix log truncation (#121) * Initial implementation of log truncation fix * Add new files * refactor bubblegum pda * amend me * update basic ts sdk * better bgum convenience * addProof modifies instructions to add proof fluently * better bg init * fix rebase issues * fix sugar shack tests * use convenience initializer * revert maxSeq change, update smokeTest Co-authored-by: Noah Gundotra --- contracts/sdk/gummyroll/convenience.ts | 66 +++++++++++++++++++ contracts/sdk/gummyroll/index.ts | 1 + contracts/sdk/gummyroll/instructions/index.ts | 46 +++++++------ 3 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 contracts/sdk/gummyroll/convenience.ts diff --git a/contracts/sdk/gummyroll/convenience.ts b/contracts/sdk/gummyroll/convenience.ts new file mode 100644 index 00000000000..5629cb27455 --- /dev/null +++ b/contracts/sdk/gummyroll/convenience.ts @@ -0,0 +1,66 @@ +import { PublicKey, Keypair, TransactionInstruction, SystemProgram, Connection } from "@solana/web3.js"; +import { PROGRAM_ID } from "."; +import { getMerkleRollAccountSize } from "./accounts"; +import * as anchor from '@project-serum/anchor'; +import { Gummyroll } from "./types"; +import { CANDY_WRAPPER_PROGRAM_ID } from "../utils"; + +export async function createAllocTreeIx( + connection: Connection, + maxBufferSize: number, + maxDepth: number, + canopyDepth: number, + payer: PublicKey, + merkleRoll: PublicKey, +): Promise { + const requiredSpace = getMerkleRollAccountSize( + maxDepth, + maxBufferSize, + canopyDepth ?? 0 + ); + return SystemProgram.createAccount({ + fromPubkey: payer, + newAccountPubkey: merkleRoll, + lamports: + await connection.getMinimumBalanceForRentExemption( + requiredSpace + ), + space: requiredSpace, + programId: PROGRAM_ID + }); +} + +export async function getCreateTreeIxs( + gummyroll: anchor.Program, + maxBufferSize: number, + maxDepth: number, + canopyDepth: number, + payer: PublicKey, + merkleRoll: PublicKey, + authority: Keypair, + appendAuthority: PublicKey, +): Promise { + const allocAccountIx = await createAllocTreeIx( + gummyroll.provider.connection, + maxBufferSize, + maxDepth, + canopyDepth, + payer, + merkleRoll, + ); + const initIx = gummyroll.instruction.initEmptyGummyroll( + maxDepth, + maxBufferSize, + { + accounts: { + merkleRoll, + authority, + appendAuthority, + candyWrapper: CANDY_WRAPPER_PROGRAM_ID + }, + signers: [authority], + }, + ) + + return [allocAccountIx, initIx]; +} diff --git a/contracts/sdk/gummyroll/index.ts b/contracts/sdk/gummyroll/index.ts index 0528b4b5dba..ee19704be5c 100644 --- a/contracts/sdk/gummyroll/index.ts +++ b/contracts/sdk/gummyroll/index.ts @@ -3,6 +3,7 @@ export * from './instructions'; export * from './accounts'; export * from './types'; export * from './utils'; +export * from './convenience'; /** * Program address diff --git a/contracts/sdk/gummyroll/instructions/index.ts b/contracts/sdk/gummyroll/instructions/index.ts index 74a1c887871..41aba3c6de4 100644 --- a/contracts/sdk/gummyroll/instructions/index.ts +++ b/contracts/sdk/gummyroll/instructions/index.ts @@ -1,6 +1,26 @@ import { Program } from '@project-serum/anchor'; import { Gummyroll } from "../types"; import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js'; +import { CANDY_WRAPPER_PROGRAM_ID } from '../../utils' + +/** + * Modifies given instruction + */ +export function addProof( + instruction: TransactionInstruction, + nodeProof: Buffer[], +): TransactionInstruction { + instruction.keys = instruction.keys.concat( + nodeProof.map((node) => { + return { + pubkey: new PublicKey(node), + isSigner: false, + isWritable: false, + }; + }) + ) + return instruction; +} export function createReplaceIx( gummyroll: Program, @@ -12,14 +32,7 @@ export function createReplaceIx( index: number, proof: Buffer[] ): TransactionInstruction { - const nodeProof = proof.map((node) => { - return { - pubkey: new PublicKey(node), - isSigner: false, - isWritable: false, - }; - }); - return gummyroll.instruction.replaceLeaf( + return addProof(gummyroll.instruction.replaceLeaf( Array.from(treeRoot), Array.from(previousLeaf), Array.from(newLeaf), @@ -28,11 +41,11 @@ export function createReplaceIx( accounts: { merkleRoll, authority: authority.publicKey, + candyWrapper: CANDY_WRAPPER_PROGRAM_ID, }, signers: [authority], - remainingAccounts: nodeProof, } - ); + ), proof); } export function createAppendIx( @@ -49,6 +62,7 @@ export function createAppendIx( merkleRoll, authority: authority.publicKey, appendAuthority: appendAuthority.publicKey, + candyWrapper: CANDY_WRAPPER_PROGRAM_ID, }, signers: [authority, appendAuthority], } @@ -83,14 +97,7 @@ export function createVerifyLeafIx( index: number, proof: Buffer[], ): TransactionInstruction { - const nodeProof = proof.map((node) => { - return { - pubkey: new PublicKey(node), - isSigner: false, - isWritable: false, - }; - }); - return gummyroll.instruction.verifyLeaf( + return addProof(gummyroll.instruction.verifyLeaf( Array.from(root), Array.from(leaf), index, @@ -99,7 +106,6 @@ export function createVerifyLeafIx( merkleRoll }, signers: [], - remainingAccounts: nodeProof, } - ); + ), proof); }