Merge pull request #54 from switchboard-xyz/job-init-batching
job init changes
This commit is contained in:
commit
21fba9b97d
|
@ -1652,45 +1652,83 @@ export class JobAccount {
|
||||||
program: SwitchboardProgram,
|
program: SwitchboardProgram,
|
||||||
params: JobInitParams
|
params: JobInitParams
|
||||||
): Promise<JobAccount> {
|
): Promise<JobAccount> {
|
||||||
|
const CHUNK_SIZE = 800;
|
||||||
const payerKeypair = programWallet(program);
|
const payerKeypair = programWallet(program);
|
||||||
const jobAccount = params.keypair ?? anchor.web3.Keypair.generate();
|
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(
|
const [stateAccount, stateBump] = await ProgramStateAccount.getOrCreate(
|
||||||
program,
|
program,
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
const state = await stateAccount.loadData();
|
const state = await stateAccount.loadData();
|
||||||
await program.methods
|
|
||||||
.jobInit({
|
if (params.data.byteLength <= CHUNK_SIZE) {
|
||||||
name: params.name ?? Buffer.from(""),
|
await program.methods
|
||||||
expiration: params.expiration ?? new anchor.BN(0),
|
.jobInit({
|
||||||
data: params.data,
|
name: params.name ?? Buffer.from(""),
|
||||||
variables:
|
expiration: params.expiration ?? new anchor.BN(0),
|
||||||
params.variables?.map((item) => Buffer.from("")) ??
|
stateBump,
|
||||||
new Array<Buffer>(),
|
data: params.data,
|
||||||
stateBump,
|
size: null,
|
||||||
})
|
})
|
||||||
.accounts({
|
.accounts({
|
||||||
job: jobAccount.publicKey,
|
job: jobAccount.publicKey,
|
||||||
authorWallet: params.authority,
|
authority: params.authority,
|
||||||
authority: params.authority,
|
programState: stateAccount.publicKey,
|
||||||
programState: stateAccount.publicKey,
|
payer: payerKeypair.publicKey,
|
||||||
})
|
systemProgram: SystemProgram.programId,
|
||||||
.signers([jobAccount])
|
})
|
||||||
.preInstructions([
|
.signers([jobAccount])
|
||||||
anchor.web3.SystemProgram.createAccount({
|
.rpc();
|
||||||
fromPubkey: programWallet(program).publicKey,
|
} else {
|
||||||
newAccountPubkey: jobAccount.publicKey,
|
const chunks: Buffer[] = [];
|
||||||
space: size,
|
for (let i = 0; i < params.data.byteLength; ) {
|
||||||
lamports:
|
const end =
|
||||||
await program.provider.connection.getMinimumBalanceForRentExemption(
|
i + CHUNK_SIZE >= params.data.byteLength
|
||||||
size
|
? params.data.byteLength
|
||||||
),
|
: i + CHUNK_SIZE;
|
||||||
programId: program.programId,
|
chunks.push(params.data.slice(i, end));
|
||||||
}),
|
i = end;
|
||||||
])
|
}
|
||||||
.rpc();
|
|
||||||
|
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: 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 });
|
return new JobAccount({ program, keypair: jobAccount });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue