From af49097cbce8f48543d6f2fee043009a0437e67c Mon Sep 17 00:00:00 2001 From: Conner Gallagher Date: Tue, 16 Aug 2022 13:10:26 -0600 Subject: [PATCH] job init changes job init changes job init changes --- libraries/ts/src/sbv2.ts | 102 +++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 32 deletions(-) diff --git a/libraries/ts/src/sbv2.ts b/libraries/ts/src/sbv2.ts index cf392e1..2b0797c 100644 --- a/libraries/ts/src/sbv2.ts +++ b/libraries/ts/src/sbv2.ts @@ -1653,45 +1653,83 @@ export class JobAccount { program: SwitchboardProgram, params: JobInitParams ): Promise { + const CHUNK_SIZE = 800; const payerKeypair = programWallet(program); const jobAccount = params.keypair ?? anchor.web3.Keypair.generate(); - const size = - 280 + params.data.length + (params.variables?.join("")?.length ?? 0); const [stateAccount, stateBump] = await ProgramStateAccount.getOrCreate( program, {} ); const state = await stateAccount.loadData(); - await program.methods - .jobInit({ - name: params.name ?? Buffer.from(""), - expiration: params.expiration ?? new anchor.BN(0), - data: params.data, - variables: - params.variables?.map((item) => Buffer.from("")) ?? - new Array(), - stateBump, - }) - .accounts({ - job: jobAccount.publicKey, - authorWallet: params.authority, - authority: params.authority, - programState: stateAccount.publicKey, - }) - .signers([jobAccount]) - .preInstructions([ - anchor.web3.SystemProgram.createAccount({ - fromPubkey: programWallet(program).publicKey, - newAccountPubkey: jobAccount.publicKey, - space: size, - lamports: - await program.provider.connection.getMinimumBalanceForRentExemption( - size - ), - programId: program.programId, - }), - ]) - .rpc(); + + if (params.data.byteLength <= CHUNK_SIZE) { + await program.methods + .jobInit({ + name: params.name ?? Buffer.from(""), + expiration: params.expiration ?? new anchor.BN(0), + stateBump, + data: params.data, + size: null, + }) + .accounts({ + job: jobAccount.publicKey, + authorWallet: params.authority, + authority: params.authority, + programState: stateAccount.publicKey, + }) + .signers([jobAccount]) + .rpc(); + } else { + const chunks: Buffer[] = []; + for (let i = 0; i < params.data.byteLength; ) { + const end = + i + CHUNK_SIZE >= params.data.byteLength + ? params.data.byteLength + : i + CHUNK_SIZE; + // console.log(`[ ${i}, ${end} ]`); + chunks.push(params.data.slice(i, end)); + i = end; + } + + const txns: string[] = []; + + txns.push( + await program.methods + .jobInit({ + name: [], + expiration: new anchor.BN(0), + stateBump, + data: Buffer.from(""), + size: params.data.byteLength, + }) + .accounts({ + job: jobAccount.publicKey, + authority: payerKeypair.publicKey, + programState: stateAccount.publicKey, + payer: payerKeypair.publicKey, + systemProgram: anchor.web3.SystemProgram.programId, + }) + .signers([jobAccount]) + .rpc() + ); + + for await (const [n, chunk] of chunks.entries()) { + txns.push( + await program.methods + .jobSetData({ + data: chunk, + size: params.data.byteLength, + chunkIdx: n, + }) + .accounts({ + job: jobAccount.publicKey, + authority: payerKeypair.publicKey, + }) + .rpc() + ); + } + } + return new JobAccount({ program, keypair: jobAccount }); }