|
@@ -6,6 +6,7 @@ const {
|
|
|
TOKEN_PROGRAM_ID,
|
|
|
Token,
|
|
|
} = require("@solana/spl-token");
|
|
|
+const miscIdl = require("../target/idl/misc.json");
|
|
|
|
|
|
describe("misc", () => {
|
|
|
// Configure the client to use the local cluster.
|
|
@@ -640,4 +641,102 @@ describe("misc", () => {
|
|
|
assert.ok(account.owner.equals(program.provider.wallet.publicKey));
|
|
|
assert.ok(account.mint.equals(mint.publicKey));
|
|
|
});
|
|
|
+
|
|
|
+ it("Can fetch all accounts of a given type", async () => {
|
|
|
+ // Initialize the accounts.
|
|
|
+ const data1 = anchor.web3.Keypair.generate();
|
|
|
+ const data2 = anchor.web3.Keypair.generate();
|
|
|
+ const data3 = anchor.web3.Keypair.generate();
|
|
|
+ const data4 = anchor.web3.Keypair.generate();
|
|
|
+ // Initialize filterable data.
|
|
|
+ const filterable1 = anchor.web3.Keypair.generate().publicKey;
|
|
|
+ const filterable2 = anchor.web3.Keypair.generate().publicKey;
|
|
|
+ // Set up a secondary wallet and program.
|
|
|
+ const anotherProgram = new anchor.Program(
|
|
|
+ miscIdl,
|
|
|
+ program.programId,
|
|
|
+ new anchor.Provider(
|
|
|
+ program.provider.connection,
|
|
|
+ new anchor.Wallet(anchor.web3.Keypair.generate()),
|
|
|
+ { commitment: program.provider.connection.commitment }
|
|
|
+ )
|
|
|
+ );
|
|
|
+ // Request airdrop for secondary wallet.
|
|
|
+ const signature = await program.provider.connection.requestAirdrop(
|
|
|
+ anotherProgram.provider.wallet.publicKey,
|
|
|
+ anchor.web3.LAMPORTS_PER_SOL
|
|
|
+ );
|
|
|
+ await program.provider.connection.confirmTransaction(signature);
|
|
|
+ // Create all the accounts.
|
|
|
+ await Promise.all([
|
|
|
+ program.rpc.testFetchAll(filterable1, {
|
|
|
+ accounts: {
|
|
|
+ data: data1.publicKey,
|
|
|
+ authority: program.provider.wallet.publicKey,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ },
|
|
|
+ signers: [data1],
|
|
|
+ }),
|
|
|
+ program.rpc.testFetchAll(filterable1, {
|
|
|
+ accounts: {
|
|
|
+ data: data2.publicKey,
|
|
|
+ authority: program.provider.wallet.publicKey,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ },
|
|
|
+ signers: [data2],
|
|
|
+ }),
|
|
|
+ program.rpc.testFetchAll(filterable2, {
|
|
|
+ accounts: {
|
|
|
+ data: data3.publicKey,
|
|
|
+ authority: program.provider.wallet.publicKey,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ },
|
|
|
+ signers: [data3],
|
|
|
+ }),
|
|
|
+ anotherProgram.rpc.testFetchAll(filterable1, {
|
|
|
+ accounts: {
|
|
|
+ data: data4.publicKey,
|
|
|
+ authority: anotherProgram.provider.wallet.publicKey,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ },
|
|
|
+ signers: [data4],
|
|
|
+ }),
|
|
|
+ ]);
|
|
|
+ // Call for multiple kinds of .all.
|
|
|
+ const allAccounts = await program.account.dataWithFilter.all();
|
|
|
+ const allAccountsFilteredByBuffer =
|
|
|
+ await program.account.dataWithFilter.all(
|
|
|
+ program.provider.wallet.publicKey.toBuffer()
|
|
|
+ );
|
|
|
+ const allAccountsFilteredByProgramFilters1 =
|
|
|
+ await program.account.dataWithFilter.all([
|
|
|
+ {
|
|
|
+ memcmp: {
|
|
|
+ offset: 8,
|
|
|
+ bytes: program.provider.wallet.publicKey.toBase58(),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
|
|
|
+ ]);
|
|
|
+ const allAccountsFilteredByProgramFilters2 =
|
|
|
+ await program.account.dataWithFilter.all([
|
|
|
+ {
|
|
|
+ memcmp: {
|
|
|
+ offset: 8,
|
|
|
+ bytes: program.provider.wallet.publicKey.toBase58(),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
|
|
|
+ ]);
|
|
|
+ // Without filters there should be 4 accounts.
|
|
|
+ assert.equal(allAccounts.length, 4);
|
|
|
+ // Filtering by main wallet there should be 3 accounts.
|
|
|
+ assert.equal(allAccountsFilteredByBuffer.length, 3);
|
|
|
+ // Filtering all the main wallet accounts and matching the filterable1 value
|
|
|
+ // results in a 2 accounts.
|
|
|
+ assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
|
|
|
+ // Filtering all the main wallet accounts and matching the filterable2 value
|
|
|
+ // results in 1 account.
|
|
|
+ assert.equal(allAccountsFilteredByProgramFilters2.length, 1);
|
|
|
+ });
|
|
|
});
|