Skip to content

Commit

Permalink
Add ledger version arguments for view and resource functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ committed Sep 30, 2023
1 parent c61d01a commit dcf4eb0
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 75 deletions.
3 changes: 3 additions & 0 deletions src/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ export class Client<TABITable extends ABITable> {
return this.client.getAccountResource(
args[0].account,
`${abi.address}::${abi.name}::${structName}`,
{
ledgerVersion: args[0].ledger_version,
}
);

// TODO: decode the u64, u128, u256 to bigint
Expand Down
68 changes: 54 additions & 14 deletions src/core/__tests__/accountResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ describe('get account resource', () => {
});

// Act before assertions
beforeAll(async () => { });
beforeAll(async () => {});

// Teardown (cleanup) after assertions
afterAll(() => { });
afterAll(() => {});

it('get CoinStore', async () => {
const result = await client.useABI(COIN_ABI).resource.CoinStore({
Expand All @@ -30,7 +30,9 @@ describe('get account resource', () => {
expect(result.data.withdraw_events).toBeDefined();

// can inference nested struct
expect(result.data.deposit_events.guid.id.creation_num.startsWith).toBeDefined();
expect(
result.data.deposit_events.guid.id.creation_num.startsWith,
).toBeDefined();

// @ts-expect-error field not exist
expect(result.data.deposit_events.guid.id.abc).toBeUndefined();
Expand All @@ -39,10 +41,48 @@ describe('get account resource', () => {
expect(result.abc).toBeUndefined();
}, 60000);

it('get CoinStore with ledger version', async () => {
const result = await client.useABI(COIN_ABI).resource.CoinStore({
type_arguments: ['0x1::aptos_coin::AptosCoin'],
account: '0x1',
ledger_version: '562606728',
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"coin": {
"value": "50000358",
},
"deposit_events": {
"counter": "61",
"guid": {
"id": {
"addr": "0x1",
"creation_num": "12",
},
},
},
"frozen": false,
"withdraw_events": {
"counter": "0",
"guid": {
"id": {
"addr": "0x1",
"creation_num": "13",
},
},
},
},
"type": "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
}
`);
}, 60000);

it('use customized ABITable', async () => {
async () => {
type ABITAble = DefaultABITable & {
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64': typeof FIXED_POINT64_ABI,
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64': typeof FIXED_POINT64_ABI;
};

const client = createClient<ABITAble>({
Expand All @@ -60,7 +100,7 @@ describe('get account resource', () => {

// @ts-expect-error field not exist
result.data.ratio.abc;
}
};
}, 60000);

it('vector struct type', async () => {
Expand All @@ -71,20 +111,20 @@ describe('get account resource', () => {
});

result.data.coins[0]?.value.startsWith;
}
};
}, 60000);

it('option type', async () => {
const { data } = await client.useABI(COIN_ABI).resource.CoinInfo({
type_arguments: ["0x1::aptos_coin::AptosCoin"],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
account: '0x1',
});

expect(data.supply.vec[0]?.aggregator.vec).toBeDefined();
expect(data.supply.vec[0]?.integer.vec).toBeDefined();

// @ts-expect-error out of index, option only has 0 or 1 item
expect(data.supply.vec[1]).toBeUndefined();
expect(data.supply.vec[1]).toBeUndefined();
}, 60000);
});

Expand All @@ -101,12 +141,12 @@ const TEST_ABI = {
generic_type_params: [],
fields: [
{
"name": "ratio",
"type": "0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64::FixedPoint64"
name: 'ratio',
type: '0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64::FixedPoint64',
},
{
"name": "coin",
"type": "0x1::coin::Coin<T0>"
name: 'coin',
type: '0x1::coin::Coin<T0>',
},
],
},
Expand All @@ -117,8 +157,8 @@ const TEST_ABI = {
generic_type_params: [],
fields: [
{
"name": "coins",
"type": "vector<0x1::coin::Coin<T0>>"
name: 'coins',
type: 'vector<0x1::coin::Coin<T0>>',
},
],
},
Expand Down
136 changes: 76 additions & 60 deletions src/core/__tests__/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,100 @@ import { createClient } from '../Client';
import { createViewPayload } from '../createViewPayload';

describe('call view functions', () => {
const client = createClient({
nodeUrl: 'https://fullnode.testnet.aptoslabs.com/v1',
});
const client = createClient({
nodeUrl: 'https://fullnode.testnet.aptoslabs.com/v1',
});

const clientMain = createClient({
nodeUrl: 'https://fullnode.mainnet.aptoslabs.com/v1',
});
const clientMain = createClient({
nodeUrl: 'https://fullnode.mainnet.aptoslabs.com/v1',
});

// Act before assertions
beforeAll(async () => { });
// Act before assertions
beforeAll(async () => {});

// Teardown (cleanup) after assertions
afterAll(() => { });
// Teardown (cleanup) after assertions
afterAll(() => {});

it('basic', async () => {
const viewPayload = createViewPayload(COIN_ABI, {
function: 'name',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
const result = await client.view(viewPayload);
expect(result).toMatchInlineSnapshot(`
it('basic', async () => {
const viewPayload = createViewPayload(COIN_ABI, {
function: 'name',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
const result = await client.view(viewPayload);
expect(result).toMatchInlineSnapshot(`
[
"Aptos Coin",
]
`);

const viewPayload2 = createViewPayload(COIN_ABI, {
function: 'decimals',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
const result2 = await client.view(viewPayload2);
expect(result2).toMatchInlineSnapshot(`
const viewPayload2 = createViewPayload(COIN_ABI, {
function: 'decimals',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
const result2 = await client.view(viewPayload2);
expect(result2).toMatchInlineSnapshot(`
[
8,
]
`);
}, 60000);
}, 60000);

it('return struct', async () => {
const viewPayload = createViewPayload(TIERED_ORACLE_ABI, {
function: 'get_last_price',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
it('ledger version', async () => {
const viewPayload = createViewPayload(COIN_ABI, {
function: 'balance',
arguments: ['0x1'],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});
const result = await client.view(viewPayload, {
ledger_version: '562606728',
});
expect(result).toMatchInlineSnapshot(`
[
50000358n,
]
`);
}, 60000);

it('return struct', async () => {
const viewPayload = createViewPayload(TIERED_ORACLE_ABI, {
function: 'get_last_price',
arguments: [],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
});

// The declaration in Move:
// struct FixedPoint64 has copy, drop, store { value: u128 }
const result = await clientMain.view(viewPayload);
expect(result.length).toBe(1);
expect((result[0] as any).v).toBeDefined();
expect(typeof (result[0] as any).v).toEqual('string');
}, 60000);
// The declaration in Move:
// struct FixedPoint64 has copy, drop, store { value: u128 }
const result = await clientMain.view(viewPayload);
expect(result.length).toBe(1);
expect((result[0] as any).v).toBeDefined();
expect(typeof (result[0] as any).v).toEqual('string');
}, 60000);
});

const TIERED_ORACLE_ABI = {
address: '0x92e95ed77b5ac815d3fbc2227e76db238339e9ca43ace45031ec2589bea5b8c',
name: 'tiered_oracle',
friends: [
'0x92e95ed77b5ac815d3fbc2227e76db238339e9ca43ace45031ec2589bea5b8c::oracle',
],
exposed_functions: [
address: '0x92e95ed77b5ac815d3fbc2227e76db238339e9ca43ace45031ec2589bea5b8c',
name: 'tiered_oracle',
friends: [
'0x92e95ed77b5ac815d3fbc2227e76db238339e9ca43ace45031ec2589bea5b8c::oracle',
],
exposed_functions: [
{
name: 'get_last_price',
visibility: 'public',
is_entry: false,
is_view: true,
generic_type_params: [
{
name: 'get_last_price',
visibility: 'public',
is_entry: false,
is_view: true,
generic_type_params: [
{
constraints: [],
},
],
params: [],
return: [
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64::FixedPoint64',
],
constraints: [],
},
],
structs: [],
],
params: [],
return: [
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64::FixedPoint64',
],
},
],
structs: [],
} as const;
1 change: 1 addition & 0 deletions src/types/client/abiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type ABIResourceClient<TABITable extends ABITable, T extends ABIRoot> = {
[TStructName in ResourceStructName<T>]: (payload: {
type_arguments: ExtractStructGenericArgsType<T, TStructName>;
account: `0x${string}`;
ledger_version?: string;
}) => Promise<{
data: ExtractStructType<TABITable, T, TStructName>;
type: string;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2021",
"module": "ESNext",
"module": "NodeNext",
"lib": [
"ES2022"
],
Expand Down

0 comments on commit dcf4eb0

Please sign in to comment.