@nosana/kit
Version:
Nosana KIT
91 lines (90 loc) • 3.67 kB
JavaScript
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/
import { combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, transformEncoder, } from '@solana/kit';
import { NOSANA_JOBS_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory } from '../shared';
export const WORK_DISCRIMINATOR = new Uint8Array([
15, 67, 45, 195, 215, 137, 229, 47,
]);
export function getWorkDiscriminatorBytes() {
return fixEncoderSize(getBytesEncoder(), 8).encode(WORK_DISCRIMINATOR);
}
export function getWorkInstructionDataEncoder() {
return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)]]), (value) => ({ ...value, discriminator: WORK_DISCRIMINATOR }));
}
export function getWorkInstructionDataDecoder() {
return getStructDecoder([
['discriminator', fixDecoderSize(getBytesDecoder(), 8)],
]);
}
export function getWorkInstructionDataCodec() {
return combineCodec(getWorkInstructionDataEncoder(), getWorkInstructionDataDecoder());
}
export function getWorkInstruction(input, config) {
// Program address.
const programAddress = config?.programAddress ?? NOSANA_JOBS_PROGRAM_ADDRESS;
// Original accounts.
const originalAccounts = {
run: { value: input.run ?? null, isWritable: true },
market: { value: input.market ?? null, isWritable: true },
payer: { value: input.payer ?? null, isWritable: true },
stake: { value: input.stake ?? null, isWritable: false },
nft: { value: input.nft ?? null, isWritable: false },
metadata: { value: input.metadata ?? null, isWritable: false },
authority: { value: input.authority ?? null, isWritable: false },
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
};
const accounts = originalAccounts;
// Resolve default values.
if (!accounts.systemProgram.value) {
accounts.systemProgram.value =
'11111111111111111111111111111111';
}
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [
getAccountMeta(accounts.run),
getAccountMeta(accounts.market),
getAccountMeta(accounts.payer),
getAccountMeta(accounts.stake),
getAccountMeta(accounts.nft),
getAccountMeta(accounts.metadata),
getAccountMeta(accounts.authority),
getAccountMeta(accounts.systemProgram),
],
programAddress,
data: getWorkInstructionDataEncoder().encode({}),
};
return instruction;
}
export function parseWorkInstruction(instruction) {
if (instruction.accounts.length < 8) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = instruction.accounts[accountIndex];
accountIndex += 1;
return accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
run: getNextAccount(),
market: getNextAccount(),
payer: getNextAccount(),
stake: getNextAccount(),
nft: getNextAccount(),
metadata: getNextAccount(),
authority: getNextAccount(),
systemProgram: getNextAccount(),
},
data: getWorkInstructionDataDecoder().decode(instruction.data),
};
}