Skip to content

Commit

Permalink
Change ABITable to an array type instead of a map (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ authored May 31, 2024
1 parent 4093ace commit d7b62ed
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-goats-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@thalalabs/surf': minor
---

Change ABITable to an array type instead of a map
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-non-null-assertion": "off",

// TODO: remove these rules
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-empty-interface": "off",

"@typescript-eslint/no-unused-vars": ["error", { "varsIgnorePattern": "^_" }]
"@typescript-eslint/no-unused-vars": [
"error",
{ "varsIgnorePattern": "^_" }
]
}
}
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ When you input `client.useABI(COIN_ABI).view.` into your IDE, the auto-completio

### Installation


Edit or add a `.npmrc` file to including following lines:

```
//npm.pkg.github.com/:_authToken=_authToken
@thalalabs:registry=https://npm.pkg.github.com
```

Run command to login:

```
$ npm login --registry=https://npm.pkg.github.com
> Username: USERNAME
Expand All @@ -71,7 +72,6 @@ $ npm login --registry=https://npm.pkg.github.com

USERNAME is you github account username. Get the token from your github settings, see ["Managing your personal access tokens."](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens).


```shell
npm i @thalalabs/surf @aptos-labs/ts-sdk
```
Expand Down Expand Up @@ -214,9 +214,12 @@ import { DefaultABITable } from "@thalalabs/surf";
import { createSurfClient } from '@thalalabs/surf';
import { Aptos } from '@aptos-labs/ts-sdk';

type ABITAble = DefaultABITable & {
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64': typeof FIXED_POINT64_ABI,
};
type ABITAble = [
...DefaultABITable,
...[
typeof FIXED_POINT64_ABI
]
];

const client = createSurfClient<ABITAble>(new Aptos());
```
Expand Down
20 changes: 6 additions & 14 deletions src/core/__tests__/accountResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import { createSurfClient } from '../Client.js';

describe('get account resource', () => {
const client = createSurfClient(
new Aptos(
new AptosConfig({ network: Network.TESTNET })
)
new Aptos(new AptosConfig({ network: Network.TESTNET })),
);

// 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 @@ -33,9 +31,7 @@ describe('get account resource', () => {
expect(result.withdraw_events).toBeDefined();

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

// @ts-expect-error field not exist
expect(result.deposit_events.guid.id.abc).toBeUndefined();
Expand Down Expand Up @@ -81,14 +77,10 @@ describe('get account resource', () => {

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

const client = createSurfClient<ABITAble>(
new Aptos(
new AptosConfig({ network: Network.TESTNET })
)
new Aptos(new AptosConfig({ network: Network.TESTNET })),
);

const result = await client.useABI(TEST_ABI).resource.TestStruct({
Expand Down
2 changes: 1 addition & 1 deletion src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type UnknownStruct<_T extends string> = object;
export type UnknownStruct<_T extends string> = unknown;

export type AnyNumber = number | bigint | string;

Expand Down
66 changes: 35 additions & 31 deletions src/types/convertor/structConvertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ export type ConvertStructFieldType<
type ConvertPrimitiveStructField<T extends MovePrimitive> = T extends 'bool'
? boolean
: T extends 'u8'
? number
: T extends 'u16'
? number
: T extends 'u32'
? number
: T extends 'u64'
? string
: T extends 'u128'
? string
: T extends 'u256'
? string
: T extends 'address'
? `0x${string}`
: T extends '0x1::string::String'
? string
: never;
? number
: T extends 'u16'
? number
: T extends 'u32'
? number
: T extends 'u64'
? string
: T extends 'u128'
? string
: T extends 'u256'
? string
: T extends 'address'
? `0x${string}`
: T extends '0x1::string::String'
? string
: never;

// Convert a struct field non-struct Move type to a TypeScript type
type ConvertStructFieldNonStructType<
Expand All @@ -49,10 +49,10 @@ type ConvertStructFieldNonStructType<
> = TMoveType extends MovePrimitive
? ConvertPrimitiveStructField<TMoveType>
: TMoveType extends `vector<${infer TInner}>`
? ConvertStructFieldType<TABITable, TInner>[]
: TMoveType extends `0x1::option::Option<${infer TInner}>`
? ConvertStructFieldOptionType<TABITable, TInner>
: UnknownStruct<TMoveType>;
? ConvertStructFieldType<TABITable, TInner>[]
: TMoveType extends `0x1::option::Option<${infer TInner}>`
? ConvertStructFieldOptionType<TABITable, TInner>
: UnknownStruct<TMoveType>;

type ConvertStructFieldOptionType<
TABITable extends ABITable,
Expand All @@ -68,16 +68,20 @@ type ConvertStructFieldStructType<
> = TMoveType extends `${infer TAccountAddress}::${infer TModuleName}::${infer TStructName}${
| ''
| `<${infer _TInnerType}>`}`
? `${TAccountAddress}::${TModuleName}` extends keyof TABITable
? OmitInner<TStructName> extends ResourceStructName<
TABITable[`${TAccountAddress}::${TModuleName}`]
? OmitInner<TStructName> extends ResourceStructName<
Extract<
TABITable[number],
{ address: TAccountAddress; name: TModuleName }
>
>
? ExtractStructType<
TABITable,
Extract<
TABITable[number],
{ address: TAccountAddress; name: TModuleName }
>,
OmitInner<TStructName>
>
? ExtractStructType<
TABITable,
TABITable[`${TAccountAddress}::${TModuleName}`],
OmitInner<TStructName>
>
: // Unknown struct, use the default struct type
UnknownStruct<TMoveType>
: UnknownStruct<TMoveType>
: // Unknown struct, use the default struct type
UnknownStruct<TMoveType>
: UnknownStruct<TMoveType>;
20 changes: 9 additions & 11 deletions src/types/defaultABITable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { OPTIONAL_AGGREGATOR_ABI } from '../abi/optional_aggregator.js';
import { TABLE_ABI } from '../abi/table.js';
import { ABIRoot } from './abi.js';

export type ABITable = {
[TAddress in string]: ABIRoot;
};
export type ABITable = ABIRoot[];

export type DefaultABITable = {
'0x1::coin': typeof COIN_ABI;
'0x1::event': typeof EVENT_ABI;
'0x1::guid': typeof GUID_ABI;
'0x1::table': typeof TABLE_ABI;
'0x1::optional_aggregator': typeof OPTIONAL_AGGREGATOR_ABI;
'0x1::aggregator': typeof AGGREGATOR_ABI;
};
export type DefaultABITable = [
typeof COIN_ABI,
typeof EVENT_ABI,
typeof GUID_ABI,
typeof TABLE_ABI,
typeof OPTIONAL_AGGREGATOR_ABI,
typeof AGGREGATOR_ABI,
];

0 comments on commit d7b62ed

Please sign in to comment.