import { KeyedAccountInfo, PublicKey, SystemProgram } from '@solana/web3.js'; import { Account, AccountStatic, AsyncSigner, DecodedAccountData, InstructionReturn, decodeAccount, staticImplements, } from '@staratlas/data-source'; import { isEqual } from 'lodash'; import { KeyIndexInput } from './common'; import { CraftingDomainAccount, CraftingIDL, CraftingIDLProgram, } from './constants'; /** * Checks equality between 2 Domain Accounts * @param data1 - First Domain Account * @param data2 - Second Domain Account * @returns boolean */ export function craftingDomainDataEquals( data1: CraftingDomainAccount, data2: CraftingDomainAccount, ): boolean { return ( data1.version === data2.version && data1.profile.equals(data2.profile) && isEqual(data1.namespace, data2.namespace) ); } @staticImplements>() export class CraftingDomain implements Account { static readonly ACCOUNT_NAME: NonNullable< CraftingIDL['accounts'] >[number]['name'] = 'domain'; static readonly MIN_DATA_SIZE: number = 8 + // discriminator 1 + // version 32 + // profile 32 + // cargoStatsDefinition 32; // namespace constructor(private _data: CraftingDomainAccount, private _key: PublicKey) {} get data(): Readonly { return this._data; } get key(): PublicKey { return this._key; } static decodeData( account: KeyedAccountInfo, program: CraftingIDLProgram, ): DecodedAccountData { return decodeAccount(account, program, CraftingDomain); } /** * Register a Crafting Domain * @param program - Crafting program * @param domain - the crafting domain * @param signer - The entity calling this instruction * @param profile - Profile Account that will hold required Crafting Permissions for this new domain * @param namespace - the name of the new crafting domain (use `stringToByteArray` to convert a string to a byte array) * @returns InstructionReturn */ static initializeDomain( program: CraftingIDLProgram, domain: AsyncSigner, signer: AsyncSigner, profile: PublicKey, namespace: number[], ): InstructionReturn { return async (funder) => [ { instruction: await program.methods .initializeDomain(namespace) .accountsStrict({ signer: signer.publicKey(), domain: domain.publicKey(), funder: funder.publicKey(), profile, systemProgram: SystemProgram.programId, }) .instruction(), signers: [signer, domain, funder], }, ]; } /** * Update a crafting domain * @param program - Crafting program * @param domain - the crafting domain * @param key - Key authorized to use this instruction * @param profile - Profile Account with required Crafting Permissions * @param newProfile - The new crafting permissions profile (replaces `profile`) * @param input - the input parameters * @returns InstructionReturn */ static updateDomain( program: CraftingIDLProgram, domain: PublicKey, key: AsyncSigner, profile: PublicKey, newProfile: PublicKey, input: KeyIndexInput, ): InstructionReturn { return async () => [ { instruction: await program.methods .updateDomain(input) .accountsStrict({ key: key.publicKey(), domain, profile, newProfile, }) .instruction(), signers: [key], }, ]; } }