Skip to content

Instantly share code, notes, and snippets.

@crypt0miester
Created August 15, 2024 15:59
Show Gist options
  • Select an option

  • Save crypt0miester/3cc888c01838b9f90e3eb8e7953a7054 to your computer and use it in GitHub Desktop.

Select an option

Save crypt0miester/3cc888c01838b9f90e3eb8e7953a7054 to your computer and use it in GitHub Desktop.
Set and close main domain instructions
import * as beet from "@metaplex-foundation/beet";
import * as web3 from "@solana/web3.js";
import { ANS_PROGRAM_ID, findMainDomain, getHashedName, getNameAccountKeyWithBump, TLD_HOUSE_PROGRAM_ID } from "@onsol/tldparser";
/**
* @category Instructions
* @category SetMainDomain
* @category generated
*/
export type SetMainDomainInstructionArgs = {
name: string;
hashedName: Uint8Array;
tld: string;
reverseAccHashedName: Uint8Array;
};
/**
* @category Instructions
* @category SetMainDomain
* @category generated
*/
export const setMainDomainStruct = new beet.FixableBeetArgsStruct<
SetMainDomainInstructionArgs & {
instructionDiscriminator: number[] /* size: 8 */;
}
>(
[
["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)],
["name", beet.utf8String],
["hashedName", beet.bytes],
["tld", beet.utf8String],
["reverseAccHashedName", beet.bytes],
],
"SetMainDomainInstructionArgs",
);
/**
* Accounts required by the _setMainDomain_ instruction
*
* @property [_writable_, **signer**] payer
* @property [] tldState
* @property [] tldHouse
* @property [_writable_] mainDomain
* @property [] nameClass
* @property [] nameAccount
* @property [] nameParent
* @property [_writable_] reverseNameAccount
* @property [] nameServiceProgram
* @category Instructions
* @category SetMainDomain
* @category generated
*/
export type SetMainDomainInstructionAccounts = {
payer: web3.PublicKey;
tldState: web3.PublicKey;
tldHouse: web3.PublicKey;
mainDomain: web3.PublicKey;
nameClass: web3.PublicKey;
nameAccount: web3.PublicKey;
nameParent: web3.PublicKey;
reverseNameAccount: web3.PublicKey;
systemProgram?: web3.PublicKey;
nameServiceProgram: web3.PublicKey;
anchorRemainingAccounts?: web3.AccountMeta[];
};
export const setMainDomainInstructionDiscriminator = [
135, 132, 229, 79, 45, 195, 204, 248,
];
/**
* Creates a _SetMainDomain_ instruction.
*
* @param accounts that will be accessed while the instruction is processed
* @param args to provide as instruction data to the program
*
* @category Instructions
* @category SetMainDomain
* @category generated
*/
export function createSetMainDomainInstruction(
accounts: SetMainDomainInstructionAccounts,
args: SetMainDomainInstructionArgs,
programId = new web3.PublicKey("TLDHkysf5pCnKsVA4gXpNvmy7psXLPEu4LAdDJthT9S"),
) {
const [data] = setMainDomainStruct.serialize({
instructionDiscriminator: setMainDomainInstructionDiscriminator,
...args,
});
const keys: web3.AccountMeta[] = [
{
pubkey: accounts.payer,
isWritable: true,
isSigner: true,
},
{
pubkey: accounts.tldState,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.tldHouse,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.mainDomain,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.nameClass,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.nameAccount,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.nameParent,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.reverseNameAccount,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.systemProgram ?? web3.SystemProgram.programId,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.nameServiceProgram,
isWritable: false,
isSigner: false,
},
];
if (accounts.anchorRemainingAccounts != null) {
for (const acc of accounts.anchorRemainingAccounts) {
keys.push(acc);
}
}
const ix = new web3.TransactionInstruction({
programId,
keys,
data,
});
return ix;
}
/**
* @category Instructions
* @category CloseMainDomain
* @category generated
*/
export const closeMainDomainStruct = new beet.BeetArgsStruct<{
instructionDiscriminator: number[] /* size: 8 */;
}>(
[["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)]],
"CloseMainDomainInstructionArgs",
);
/**
* Accounts required by the _closeMainDomain_ instruction
*
* @property [**signer**] authority
* @property [_writable_] owner
* @property [] tldState
* @property [_writable_] mainDomain
* @category Instructions
* @category CloseMainDomain
* @category generated
*/
export type CloseMainDomainInstructionAccounts = {
authority: web3.PublicKey;
owner: web3.PublicKey;
tldState: web3.PublicKey;
mainDomain: web3.PublicKey;
anchorRemainingAccounts?: web3.AccountMeta[];
};
export const closeMainDomainInstructionDiscriminator = [
30, 91, 225, 42, 139, 118, 160, 134,
];
/**
* Creates a _CloseMainDomain_ instruction.
*
* @param accounts that will be accessed while the instruction is processed
* @category Instructions
* @category CloseMainDomain
* @category generated
*/
export function createCloseMainDomainInstruction(
accounts: CloseMainDomainInstructionAccounts,
programId = new web3.PublicKey("TLDHkysf5pCnKsVA4gXpNvmy7psXLPEu4LAdDJthT9S"),
) {
const [data] = closeMainDomainStruct.serialize({
instructionDiscriminator: closeMainDomainInstructionDiscriminator,
});
const keys: web3.AccountMeta[] = [
{
pubkey: accounts.authority,
isWritable: false,
isSigner: true,
},
{
pubkey: accounts.owner,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.tldState,
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.mainDomain,
isWritable: true,
isSigner: false,
},
];
if (accounts.anchorRemainingAccounts != null) {
for (const acc of accounts.anchorRemainingAccounts) {
keys.push(acc);
}
}
const ix = new web3.TransactionInstruction({
programId,
keys,
data,
});
return ix;
}
export async function setMainDomain(
domain: string,
payer: web3.PublicKey,
) {
const name = domain.toLowerCase();
const tld = ".blink";
const tldState = new web3.PublicKey("VmmhRjr64KbpTZpgmeiVSWmR8H8RyqgigF1XQf8AvET");
const tldHouse = new web3.PublicKey("CD1viBNn8vu8sPiYuPf2tx9SVrpgqP6vEr2ivD4aJtu9");
const hashedName = await getHashedName(name);
const nameParent = new web3.PublicKey("GJ2H7fwUuvTvVW5pctHwQzrwPv9X25KAs6hqi9SzK4bU");
const [nameAccount] = getNameAccountKeyWithBump(
hashedName,
undefined,
nameParent,
);
const reverseAccHashedName = await getHashedName(nameAccount.toBase58());
const [reverseNameAccount] = getNameAccountKeyWithBump(
reverseAccHashedName,
tldHouse,
undefined,
);
const [mainDomain] = findMainDomain(payer);
const setMainDomainArgs: SetMainDomainInstructionArgs = {
name,
hashedName: Uint8Array.from(hashedName),
tld,
reverseAccHashedName: Uint8Array.from(reverseAccHashedName),
};
const setMainDomainIxAccounts: SetMainDomainInstructionAccounts = {
payer: payer,
tldState,
tldHouse,
mainDomain,
nameClass: web3.PublicKey.default,
nameAccount,
nameParent,
reverseNameAccount,
nameServiceProgram: ANS_PROGRAM_ID,
};
const setMainDomainIx = createSetMainDomainInstruction(
setMainDomainIxAccounts,
setMainDomainArgs,
TLD_HOUSE_PROGRAM_ID,
);
return setMainDomainIx;
}
export async function closeMainDomain(payer: web3.PublicKey) {
const tldState = new web3.PublicKey("VmmhRjr64KbpTZpgmeiVSWmR8H8RyqgigF1XQf8AvET")
const [mainDomain] = findMainDomain(payer);
const closeMainDomainIxAccounts: CloseMainDomainInstructionAccounts = {
authority: payer,
owner: payer,
tldState,
mainDomain,
};
const closeMainDomainIx = createCloseMainDomainInstruction(
closeMainDomainIxAccounts,
TLD_HOUSE_PROGRAM_ID,
);
return closeMainDomainIx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment