Skip to content

Commit

Permalink
js: add register favorite binding
Browse files Browse the repository at this point in the history
  • Loading branch information
dr497 committed Mar 15, 2024
1 parent e930b83 commit 969b5e7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
21 changes: 21 additions & 0 deletions js/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createInstructionV3,
burnInstruction,
createWithNftInstruction,
registerFavoriteInstruction,
} from "./instructions";
import { NameRegistryState } from "./state";
import { Numberu64, Numberu32 } from "./int";
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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];
};
50 changes: 50 additions & 0 deletions js/src/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
16 changes: 15 additions & 1 deletion js/tests/favorite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});

0 comments on commit 969b5e7

Please sign in to comment.