Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

refactor(experimental): provide account encoding on the data field #2017

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 31 additions & 44 deletions packages/rpc-graphql/src/__tests__/account-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,12 @@ describe('account', () => {
// See scripts/fixtures/gpa1.json
const variableValues = {
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
encoding: 'BASE_58',
};
const source = /* GraphQL */ `
query testQuery($address: String!, $encoding: AccountEncoding) {
account(address: $address, encoding: $encoding) {
query testQuery($address: String!) {
account(address: $address) {
address
... on AccountBase58 {
data
}
dataBase58: data(encoding: BASE_58)
}
}
`;
Expand All @@ -245,7 +242,7 @@ describe('account', () => {
data: {
account: {
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
data: '2Uw1bpnsXxu3e',
dataBase58: '2Uw1bpnsXxu3e',
},
},
});
Expand All @@ -255,15 +252,12 @@ describe('account', () => {
// See scripts/fixtures/gpa1.json
const variableValues = {
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
encoding: 'BASE_64',
};
const source = /* GraphQL */ `
query testQuery($address: String!, $encoding: AccountEncoding) {
account(address: $address, encoding: $encoding) {
query testQuery($address: String!) {
account(address: $address) {
address
... on AccountBase64 {
data
}
dataBase64: data(encoding: BASE_64)
}
}
`;
Expand All @@ -272,7 +266,7 @@ describe('account', () => {
data: {
account: {
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
data: 'dGVzdCBkYXRh',
dataBase64: 'dGVzdCBkYXRh',
},
},
});
Expand All @@ -285,12 +279,10 @@ describe('account', () => {
encoding: 'BASE_64_ZSTD',
};
const source = /* GraphQL */ `
query testQuery($address: String!, $encoding: AccountEncoding) {
account(address: $address, encoding: $encoding) {
query testQuery($address: String!) {
account(address: $address) {
address
... on AccountBase64Zstd {
data
}
dataBase64Zstd: data(encoding: BASE_64_ZSTD)
}
}
`;
Expand All @@ -299,50 +291,36 @@ describe('account', () => {
data: {
account: {
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
data: 'KLUv/QBYSQAAdGVzdCBkYXRh',
dataBase64Zstd: 'KLUv/QBYSQAAdGVzdCBkYXRh',
},
},
});
});
it('can get account data as jsonParsed', async () => {
expect.assertions(2);
it('defaults to jsonParsed if possible', async () => {
expect.assertions(1);
// See scripts/fixtures/spl-token-mint-account.json
const variableValues = {
address: 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr',
};
const source = /* GraphQL */ `
query testQuery($address: String!) {
account(address: $address, encoding: PARSED) {
... on AccountBase64 {
data
}
account(address: $address) {
... on MintAccount {
supply
}
}
}
`;
const resultParsed = await rpcGraphQL.query(source, {
// See scripts/fixtures/spl-token-mint-account.json
address: 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr',
});
expect(resultParsed).toMatchObject({
const result = await rpcGraphQL.query(source, variableValues);
expect(result).toMatchObject({
data: {
account: {
supply: expect.any(String),
},
},
});
// Defaults to base64 if can't be parsed
const resultBase64 = await rpcGraphQL.query(source, {
// See scripts/fixtures/gpa1.json
address: 'CcYNb7WqpjaMrNr7B1mapaNfWctZRH7LyAjWRLBGt1Fk',
});
expect(resultBase64).toMatchObject({
data: {
account: {
data: 'dGVzdCBkYXRh',
},
},
});
});
it('defaults to jsonParsed if possible', async () => {
it('can get account data as base58, base64, base64+zstd, and jsonParsed at the same time', async () => {
expect.assertions(1);
// See scripts/fixtures/spl-token-mint-account.json
const variableValues = {
Expand All @@ -351,6 +329,9 @@ describe('account', () => {
const source = /* GraphQL */ `
query testQuery($address: String!) {
account(address: $address) {
dataBase58: data(encoding: BASE_58)
dataBase64: data(encoding: BASE_64)
dataBase64Zstd: data(encoding: BASE_64_ZSTD)
... on MintAccount {
supply
}
Expand All @@ -361,6 +342,12 @@ describe('account', () => {
expect(result).toMatchObject({
data: {
account: {
dataBase58:
'DK9N6ppsTv6i3RBVVA7PCUXGnybDuNQKwFKKpmcEJ5RqfZ4HzeWRbdV32h1Z1QPshdhDChwWSYH4EaBZSS7EJC48fBkv3Lh6R8YdSCUxsSpEWQX',
dataBase64:
'AQAAAOkoOVUJZf/U1krKr0bUXfcxjltPV8kMSH1gYl2Cm4N78eLlNFwmdhcGAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==',
dataBase64Zstd:
'KLUv/QBYtQEA9AIBAAAA6Sg5VQll/9TWSsqvRtRd9zGOW09XyQxIfWBiXYKbg3vx4uU0XCZ2FwYBAAEAR4Iy',
supply: expect.any(String),
},
},
Expand Down
81 changes: 23 additions & 58 deletions packages/rpc-graphql/src/__tests__/program-accounts-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,14 @@ describe('programAccounts', () => {
// See scripts/fixtures/gpa2-1.json, scripts/fixtures/gpa2-2.json,
const variableValues = {
commitment: 'confirmed',
encoding: 'BASE_58',
programAddress: 'AmtpVzo6H6qQCP9dH9wfu5hfa8kKaAFpTJ4aamPYR6V6',
};
const source = /* GraphQL */ `
query testQuery($programAddress: String!, $commitment: Commitment, $encoding: AccountEncoding) {
programAccounts(programAddress: $programAddress, commitment: $commitment, encoding: $encoding) {
query testQuery($programAddress: String!, $commitment: Commitment) {
programAccounts(programAddress: $programAddress, commitment: $commitment) {
address
dataBase58: data(encoding: BASE_58)
executable
... on AccountBase58 {
data
}
}
}
`;
Expand All @@ -106,12 +103,12 @@ describe('programAccounts', () => {
programAccounts: expect.arrayContaining([
{
address: 'C5q1p5UiCVrt6vcLJDGcS4AZ98fahKyb9XkDRdqATK17',
data: '2Uw1bpnsXxu3e',
dataBase58: '2Uw1bpnsXxu3e',
executable: false,
},
{
address: 'Hhsoev7Apk5dMbktzLUrsTHuMq9e9GSYBaLcnN2PfdKS',
data: '2Uw1bpnsXxu3e',
dataBase58: '2Uw1bpnsXxu3e',
executable: false,
},
]),
Expand All @@ -123,17 +120,14 @@ describe('programAccounts', () => {
// See scripts/fixtures/gpa2-1.json, scripts/fixtures/gpa2-2.json,
const variableValues = {
commitment: 'confirmed',
encoding: 'BASE_64',
programAddress: 'AmtpVzo6H6qQCP9dH9wfu5hfa8kKaAFpTJ4aamPYR6V6',
};
const source = /* GraphQL */ `
query testQuery($programAddress: String!, $commitment: Commitment, $encoding: AccountEncoding) {
programAccounts(programAddress: $programAddress, commitment: $commitment, encoding: $encoding) {
query testQuery($programAddress: String!, $commitment: Commitment) {
programAccounts(programAddress: $programAddress, commitment: $commitment) {
address
dataBase64: data(encoding: BASE_64)
executable
... on AccountBase64 {
data
}
}
}
`;
Expand All @@ -143,12 +137,12 @@ describe('programAccounts', () => {
programAccounts: expect.arrayContaining([
{
address: 'C5q1p5UiCVrt6vcLJDGcS4AZ98fahKyb9XkDRdqATK17',
data: 'dGVzdCBkYXRh',
dataBase64: 'dGVzdCBkYXRh',
executable: false,
},
{
address: 'Hhsoev7Apk5dMbktzLUrsTHuMq9e9GSYBaLcnN2PfdKS',
data: 'dGVzdCBkYXRh',
dataBase64: 'dGVzdCBkYXRh',
executable: false,
},
]),
Expand All @@ -160,17 +154,14 @@ describe('programAccounts', () => {
// See scripts/fixtures/gpa2-1.json, scripts/fixtures/gpa2-2.json,
const variableValues = {
commitment: 'confirmed',
encoding: 'BASE_64_ZSTD',
programAddress: 'AmtpVzo6H6qQCP9dH9wfu5hfa8kKaAFpTJ4aamPYR6V6',
};
const source = /* GraphQL */ `
query testQuery($programAddress: String!, $commitment: Commitment, $encoding: AccountEncoding) {
programAccounts(programAddress: $programAddress, commitment: $commitment, encoding: $encoding) {
query testQuery($programAddress: String!, $commitment: Commitment) {
programAccounts(programAddress: $programAddress, commitment: $commitment) {
address
dataBase64Zstd: data(encoding: BASE_64_ZSTD)
executable
... on AccountBase64Zstd {
data
}
}
}
`;
Expand All @@ -180,12 +171,12 @@ describe('programAccounts', () => {
programAccounts: expect.arrayContaining([
{
address: 'C5q1p5UiCVrt6vcLJDGcS4AZ98fahKyb9XkDRdqATK17',
data: 'KLUv/QBYSQAAdGVzdCBkYXRh',
dataBase64Zstd: 'KLUv/QBYSQAAdGVzdCBkYXRh',
executable: false,
},
{
address: 'Hhsoev7Apk5dMbktzLUrsTHuMq9e9GSYBaLcnN2PfdKS',
data: 'KLUv/QBYSQAAdGVzdCBkYXRh',
dataBase64Zstd: 'KLUv/QBYSQAAdGVzdCBkYXRh',
executable: false,
},
]),
Expand Down Expand Up @@ -569,25 +560,12 @@ describe('programAccounts', () => {
length: 5,
offset: 0,
},
encoding: 'BASE_58',
programAddress: 'DXngmJfjurhnAwbMPgpUGPH6qNvetCKRJ6PiD4ag4PTj',
};
const source = /* GraphQL */ `
query testQuery(
$programAddress: String!
$commitment: Commitment
$dataSlice: DataSlice
$encoding: AccountEncoding
) {
programAccounts(
programAddress: $programAddress
commitment: $commitment
dataSlice: $dataSlice
encoding: $encoding
) {
... on AccountBase58 {
data
}
query testQuery($programAddress: String!, $commitment: Commitment, $dataSlice: DataSlice) {
programAccounts(programAddress: $programAddress, commitment: $commitment) {
dataBase58: data(encoding: BASE_58, dataSlice: $dataSlice)
}
}
`;
Expand All @@ -596,7 +574,7 @@ describe('programAccounts', () => {
data: {
programAccounts: expect.arrayContaining([
{
data: 'E8f4pET',
dataBase58: 'E8f4pET',
},
]),
},
Expand All @@ -613,25 +591,12 @@ describe('programAccounts', () => {
length: 5,
offset: 0,
},
encoding: 'BASE_64',
programAddress: 'DXngmJfjurhnAwbMPgpUGPH6qNvetCKRJ6PiD4ag4PTj',
};
const source = /* GraphQL */ `
query testQuery(
$programAddress: String!
$commitment: Commitment
$dataSlice: DataSlice
$encoding: AccountEncoding
) {
programAccounts(
programAddress: $programAddress
commitment: $commitment
dataSlice: $dataSlice
encoding: $encoding
) {
... on AccountBase64 {
data
}
query testQuery($programAddress: String!, $commitment: Commitment, $dataSlice: DataSlice) {
programAccounts(programAddress: $programAddress, commitment: $commitment) {
dataBase64: data(encoding: BASE_64, dataSlice: $dataSlice)
}
}
`;
Expand All @@ -640,7 +605,7 @@ describe('programAccounts', () => {
data: {
programAccounts: expect.arrayContaining([
{
data: 'dGVzdCA=',
dataBase64: 'dGVzdCA=',
},
]),
},
Expand Down
16 changes: 16 additions & 0 deletions packages/rpc-graphql/src/resolvers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ export const resolveAccount = (fieldName: string) => {
) =>
parent[fieldName] === null ? null : context.loaders.account.load({ ...args, address: parent[fieldName] }, info);
Copy link
Contributor

@steveluscher steveluscher Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is what you mean by ‘This is remedied in the next commit in the stack,’ but what should happen here is:

  1. The account resolver introspects the query itself, and looks into its own subselection for direct child data fields.
    a. If there are no data fields, fetch the account with whatever encoding is lightest on the RPC.
    b. If there are data fields, fetch the account with one of those encodings (to ensure a cache hit between the fetch for the account and the fetch for at least one of the data fields).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was going to tackle this in the next commit, but I guess now that I think of it there's no reason for this one then. 😅

};

export const resolveAccountData = () => {
return (
parent: { address: Address },
args: { encoding: AccountLoaderArgs['encoding']; dataSlice?: AccountLoaderArgs['dataSlice'] },
context: RpcGraphQLContext,
info: GraphQLResolveInfo | undefined,
) => {
return (
context.loaders.account
.load({ ...args, address: parent.address }, info)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.then((account: any) => account.data)
);
};
};
Loading
Loading