Get anchor account sizes by name instead of IDL index (#95)
* Adding function to fetch account sizes and using that instead of static export * Adding a test for getAccountSize * Updating test to explicitly check account sizes. Also adding reserved bytes calculations
This commit is contained in:
parent
44021b15a7
commit
e843b7591c
|
@ -10,10 +10,9 @@ import {
|
|||
TickArrayData,
|
||||
WhirlpoolData,
|
||||
WhirlpoolsConfigData,
|
||||
WHIRLPOOL_ACCOUNT_SIZE,
|
||||
WHIRLPOOL_CODER,
|
||||
} from "../..";
|
||||
import { FeeTierData } from "../../types/public";
|
||||
import { FeeTierData, getAccountSize } from "../../types/public";
|
||||
import {
|
||||
ParsableEntity,
|
||||
ParsableFeeTier,
|
||||
|
@ -221,7 +220,7 @@ export class AccountFetcher {
|
|||
configId,
|
||||
}: ListWhirlpoolParams): Promise<WhirlpoolAccount[]> {
|
||||
const filters = [
|
||||
{ dataSize: WHIRLPOOL_ACCOUNT_SIZE },
|
||||
{ dataSize: getAccountSize(AccountName.Whirlpool) },
|
||||
{
|
||||
memcmp: WHIRLPOOL_CODER.memcmp(
|
||||
AccountName.Whirlpool,
|
||||
|
|
|
@ -31,11 +31,38 @@ const IDL = WhirlpoolIDL as Idl;
|
|||
*/
|
||||
export const WHIRLPOOL_CODER = new BorshAccountsCoder(IDL);
|
||||
|
||||
/**
|
||||
* Get the size of an account owned by the Whirlpool program in bytes.
|
||||
* @param accountName Whirlpool account name
|
||||
* @returns Size in bytes of the account
|
||||
*/
|
||||
export function getAccountSize(accountName: AccountName) {
|
||||
const size = WHIRLPOOL_CODER.size(IDL.accounts!.find((account) => account.name === accountName)!);
|
||||
return size + RESERVED_BYTES[accountName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserved bytes for each account used for calculating the account size.
|
||||
*/
|
||||
const RESERVED_BYTES: ReservedBytes = {
|
||||
[AccountName.WhirlpoolsConfig]: 2,
|
||||
[AccountName.Position]: 0,
|
||||
[AccountName.TickArray]: 0,
|
||||
[AccountName.Whirlpool]: 0,
|
||||
[AccountName.FeeTier]: 0,
|
||||
[AccountName.PositionBundle]: 64,
|
||||
};
|
||||
|
||||
type ReservedBytes = {
|
||||
[name in AccountName]: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Size of the Whirlpool account in bytes.
|
||||
* @deprecated Please use {@link getAccountSize} instead.
|
||||
* @category Solana Accounts
|
||||
*/
|
||||
export const WHIRLPOOL_ACCOUNT_SIZE = WHIRLPOOL_CODER.size(IDL.accounts![4]);
|
||||
export const WHIRLPOOL_ACCOUNT_SIZE = getAccountSize(AccountName.Whirlpool);
|
||||
|
||||
/**
|
||||
* @category Solana Accounts
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import * as assert from "assert";
|
||||
import { AccountName, getAccountSize } from "../../../src";
|
||||
|
||||
describe("anchor-types", () => {
|
||||
it("all whirlpool account names exist in IDL", async () => {
|
||||
type ExpectedSize = { [a in AccountName]: number };
|
||||
const expectedSizes: ExpectedSize = {
|
||||
[AccountName.WhirlpoolsConfig]: 108,
|
||||
[AccountName.Position]: 216,
|
||||
[AccountName.TickArray]: 9988,
|
||||
[AccountName.Whirlpool]: 653,
|
||||
[AccountName.FeeTier]: 44,
|
||||
[AccountName.PositionBundle]: 136,
|
||||
};
|
||||
Object.values(AccountName).forEach((name) => {
|
||||
let actualSize;
|
||||
try {
|
||||
assert.equal(
|
||||
getAccountSize(name),
|
||||
expectedSizes[name],
|
||||
`For ${name} expected ${expectedSizes[name]} but got ${actualSize}`
|
||||
);
|
||||
} catch (e) {
|
||||
assert.fail(`Error fetching size for ${name}: ${e}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue