job init changes

job init changes


job init changes
This commit is contained in:
Conner Gallagher 2022-08-16 13:10:26 -06:00
parent 82caa78153
commit af49097cbc
1 changed files with 70 additions and 32 deletions

View File

@ -1653,24 +1653,23 @@ export class JobAccount {
program: SwitchboardProgram,
params: JobInitParams
): Promise<JobAccount> {
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();
if (params.data.byteLength <= CHUNK_SIZE) {
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<Buffer>(),
stateBump,
data: params.data,
size: null,
})
.accounts({
job: jobAccount.publicKey,
@ -1679,19 +1678,58 @@ export class JobAccount {
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();
} 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 });
}