@nosana/kit
Version:
Nosana KIT
62 lines • 2.91 kB
JavaScript
import { ipfsHashToSolBytesArray } from '@nosana/ipfs';
export async function finish({ job, ipfsResultsHash }, { config, deps, client, get, getRuns, getRequiredWallet, getStaticAccounts, getNosATA, }) {
try {
const wallet = getRequiredWallet();
// Get required accounts in parallel
const [jobAccount, [run], { jobsProgram, ...staticAccounts }] = await Promise.all([
get(job, false),
getRuns({ job }),
getStaticAccounts(),
]);
if (!run) {
throw new Error('No job run account found for the specified job');
}
// Derive vault PDA and get user ATA in parallel
const [vault, nodeATA] = await Promise.all([
deps.solana.pda([jobAccount.market, config.nosTokenAddress], jobsProgram),
getNosATA(run.node), // ATA for the node (user)
]);
// Determine deposit account: if job.price > 0, use job payer's ATA, otherwise use vault
const deposit = jobAccount.price > 0 ? await getNosATA(jobAccount.payer) : vault;
// Convert IPFS result hash to Solana bytes array
const ipfsResult = new Uint8Array(ipfsHashToSolBytesArray(ipfsResultsHash));
// Create the finish instruction
const finishInstruction = client.getFinishInstruction({
job,
run: run.address,
market: jobAccount.market,
vault,
deposit,
user: nodeATA,
payerJob: jobAccount.payer,
payerRun: run.payer,
authority: wallet,
ipfsResult,
...staticAccounts,
}, {
programAddress: jobsProgram,
});
// Check if ATA accounts exist and create instructions if needed
const preInstructions = [];
// Check if node ATA exists, if not create it
const createNodeATAInstruction = await deps.solana.getCreateATAInstructionIfNeeded(nodeATA, config.nosTokenAddress, run.node, wallet);
if (createNodeATAInstruction) {
preInstructions.push(createNodeATAInstruction);
}
// Check if deposit ATA exists (only if job.price > 0)
if (jobAccount.price > 0) {
const createDepositATAInstruction = await deps.solana.getCreateATAInstructionIfNeeded(deposit, config.nosTokenAddress, jobAccount.payer, wallet);
if (createDepositATAInstruction) {
preInstructions.push(createDepositATAInstruction);
}
}
// Return tuple with pre-instructions (if any) followed by finish instruction
return [...preInstructions, finishInstruction];
}
catch (err) {
const errorMessage = `Failed to create finish instruction: ${err instanceof Error ? err.message : String(err)}`;
deps.logger.error(errorMessage);
throw new Error(errorMessage);
}
}
//# sourceMappingURL=finish.js.map