Skip to content

Commit

Permalink
refactor(experimental): graphql: add tests for programAccounts filters
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Aug 13, 2024
1 parent 5d122a0 commit 436094e
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions packages/rpc-graphql/src/__tests__/program-accounts-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Address } from '@solana/addresses';
import {
GetAccountInfoApi,
GetBlockApi,
Expand Down Expand Up @@ -617,4 +618,164 @@ describe('programAccounts', () => {
});
});
});
describe('when called with a data size filter', () => {
const programAddress =
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;

describe('when using memcmp filter', () => {
it('returns the matching accounts', async () => {
expect.assertions(1);
const variableValues = {
dataSizeFilters: [
{
dataSize: 165n, // Token account size
},
],
programAddress,
};
const source = /* GraphQL */ `
query testQuery($programAddress: Address!, $dataSizeFilters: [ProgramAccountsDataSizeFilter!]!) {
programAccounts(
programAddress: $programAddress
commitment: null
dataSizeFilters: $dataSizeFilters
) {
... on TokenAccount {
mint {
address
}
}
}
}
`;
const result = await rpcGraphQL.query(source, variableValues);
console.log(result);
expect(result).toMatchObject({
data: {
programAccounts: expect.arrayContaining([
{
mint: {
address: expect.any(String),
},
},
]),
},
});
});
});
});
describe('when called with a memcmp filter', () => {
// See scripts/fixtures/spl-token-mint-account.json
const mintAddress =
'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr' as Address<'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'>;
const programAddress =
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;

describe('when using memcmp filter', () => {
it('returns the matching accounts', async () => {
expect.assertions(1);
const variableValues = {
memcmpFilters: [
{
bytes: mintAddress, // Mint address in data.
encoding: 'BASE_58', // Base58-encoded address.
offset: 0, // Offset 0 for mint address.
},
],
programAddress,
};
const source = /* GraphQL */ `
query testQuery($programAddress: Address!, $memcmpFilters: [ProgramAccountsMemcmpFilter!]!) {
programAccounts(
programAddress: $programAddress
commitment: null
dataSizeFilters: null
memcmpFilters: $memcmpFilters
) {
... on TokenAccount {
mint {
address
}
}
}
}
`;
const result = await rpcGraphQL.query(source, variableValues);
console.log(result);
expect(result).toMatchObject({
data: {
programAccounts: expect.arrayContaining([
{
mint: {
address: mintAddress,
},
},
]),
},
});
});
});
});

describe('when called with both a data size and a memcmpy filter', () => {
// See scripts/fixtures/spl-token-mint-account.json
const mintAddress =
'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr' as Address<'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'>;
const programAddress =
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;

describe('when using memcmp filter', () => {
it('returns the matching accounts', async () => {
expect.assertions(1);
const variableValues = {
dataSizeFilters: [
{
dataSize: 165n, // Token account size
},
],
memcmpFilters: [
{
bytes: mintAddress, // Mint address in data.
encoding: 'BASE_58', // Base58-encoded address.
offset: 0, // Offset 0 for mint address.
},
],
programAddress,
};
const source = /* GraphQL */ `
query testQuery(
$programAddress: Address!
$dataSizeFilters: [ProgramAccountsDataSizeFilter!]!
$memcmpFilters: [ProgramAccountsMemcmpFilter!]!
) {
programAccounts(
programAddress: $programAddress
commitment: null
dataSizeFilters: $dataSizeFilters
memcmpFilters: $memcmpFilters
) {
... on TokenAccount {
mint {
address
}
}
}
}
`;
const result = await rpcGraphQL.query(source, variableValues);
console.log(result);
expect(result).toMatchObject({
data: {
programAccounts: expect.arrayContaining([
{
mint: {
address: mintAddress,
},
},
]),
},
});
});
});
});
});

0 comments on commit 436094e

Please sign in to comment.