From a80326d7150ab8229b7858334441309d2469f852 Mon Sep 17 00:00:00 2001 From: DR497 <47689875+dr497@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:51:22 +0800 Subject: [PATCH] js: add register favorite binding --- js/src/bindings.ts | 21 ++++++++++++++++ js/src/instructions.ts | 50 +++++++++++++++++++++++++++++++++++++++ js/tests/favorite.test.ts | 16 ++++++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/js/src/bindings.ts b/js/src/bindings.ts index dd7ce07..4165a3d 100644 --- a/js/src/bindings.ts +++ b/js/src/bindings.ts @@ -15,6 +15,7 @@ import { createInstructionV3, burnInstruction, createWithNftInstruction, + registerFavoriteInstruction, } from "./instructions"; import { NameRegistryState } from "./state"; import { Numberu64, Numberu32 } from "./int"; @@ -58,6 +59,7 @@ import { Validation, writeRoa, } from "@bonfida/sns-records"; +import { FavouriteDomain, NAME_OFFERS_ID } from "./favorite-domain"; /** * Creates a name account with the given rent budget, allocated space, owner and class. @@ -971,3 +973,22 @@ export const transferSubdomain = async ( return ix; }; + +/** + * This function can be used to register a domain name as favorite + * @param nameAccount The name account being registered as favorite + * @param owner The owner of the name account + * @param programId The name offer program ID + * @returns + */ +export const registerFavorite = (nameAccount: PublicKey, owner: PublicKey) => { + const [favKey] = FavouriteDomain.getKeySync(NAME_OFFERS_ID, owner); + const ix = new registerFavoriteInstruction().getInstruction( + NAME_OFFERS_ID, + nameAccount, + favKey, + owner, + SystemProgram.programId, + ); + return [ix]; +}; diff --git a/js/src/instructions.ts b/js/src/instructions.ts index 94aaac4..0262c30 100644 --- a/js/src/instructions.ts +++ b/js/src/instructions.ts @@ -809,3 +809,53 @@ export function reallocInstruction( data, }); } + +export class registerFavoriteInstruction { + tag: number; + static schema = { + struct: { + tag: "u8", + }, + }; + constructor() { + this.tag = 6; + } + serialize(): Uint8Array { + return serialize(registerFavoriteInstruction.schema, this); + } + getInstruction( + programId: PublicKey, + nameAccount: PublicKey, + favouriteAccount: PublicKey, + owner: PublicKey, + systemProgram: PublicKey, + ): TransactionInstruction { + const data = Buffer.from(this.serialize()); + let keys: AccountKey[] = []; + keys.push({ + pubkey: nameAccount, + isSigner: false, + isWritable: false, + }); + keys.push({ + pubkey: favouriteAccount, + isSigner: false, + isWritable: true, + }); + keys.push({ + pubkey: owner, + isSigner: true, + isWritable: true, + }); + keys.push({ + pubkey: systemProgram, + isSigner: false, + isWritable: false, + }); + return new TransactionInstruction({ + keys, + programId, + data, + }); + } +} diff --git a/js/tests/favorite.test.ts b/js/tests/favorite.test.ts index 9dea0a7..5c0f785 100644 --- a/js/tests/favorite.test.ts +++ b/js/tests/favorite.test.ts @@ -4,7 +4,9 @@ import { getFavoriteDomain, getMultipleFavoriteDomains, } from "../src/favorite-domain"; -import { PublicKey, Connection, Keypair } from "@solana/web3.js"; +import { PublicKey, Connection, Keypair, Transaction } from "@solana/web3.js"; +import { registerFavorite } from "../src/bindings"; +import { getDomainKeySync } from "../src/utils"; jest.setTimeout(10_000); @@ -64,3 +66,15 @@ test("Multiple favorite domains", async () => { ); result.forEach((x, idx) => expect(x).toBe(items[idx].domain)); }); + +test("Register fav", async () => { + const owner = new PublicKey("Fxuoy3gFjfJALhwkRcuKjRdechcgffUApeYAfMWck6w8"); + const tx = new Transaction(); + const ix = registerFavorite(getDomainKeySync("wallet-guide-3").pubkey, owner); + tx.add(...ix); + const { blockhash } = await connection.getLatestBlockhash(); + tx.recentBlockhash = blockhash; + tx.feePayer = owner; + const res = await connection.simulateTransaction(tx); + expect(res.value.err).toBe(null); +});