From 673e7746731958e825fa60535538098259fc3e9c Mon Sep 17 00:00:00 2001 From: dafuga Date: Wed, 11 Oct 2023 17:02:01 -0700 Subject: [PATCH] refactor: passing ChainDefinition as AcountKit arg --- src/kit.ts | 42 +++++++++++++++++++++++++++--------------- src/types.ts | 27 +++++++++++++++++++++++++++ test/tests/account.ts | 4 ++-- 3 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 src/types.ts diff --git a/src/kit.ts b/src/kit.ts index 93bffca..85d089f 100644 --- a/src/kit.ts +++ b/src/kit.ts @@ -1,26 +1,38 @@ -import {APIClient, Name, NameType} from '@wharfkit/antelope' +import {API, APIClient, NameType} from '@wharfkit/antelope' import {Contract} from '@wharfkit/contract' import {Account} from './account' - -export interface AccountKitArgs { - client: APIClient - contract?: Contract +import {TelosAccountObject, WAXAccountObject} from './types' +​ +export interface ChainDefinition { + id: string + url: string +} +​ +export namespace Chains { + export const EOS: ChainDefinition = { + id: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', + url: 'https://eos.greymass.com', + } + export const Jungle4: ChainDefinition = { + id: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d', + url: 'https://jungle4.greymass.com', + } + // export const Telos = new ChainDefinition('1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f', 'https://telos.greymass.com', TelosAccountObject) + // export const TelosTestnet = new ChainDefinition('4667b205c6838ef70ff7988f6e8257e8be0e1284a2f59699054a018f743b1d11', 'https://testnet.telos.caleos.io', TelosAccountObject) + // export const WAX = new ChainDefinition('1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4', 'https://wax.greymass.com', WAXAccountObject) + // export const WAXTestnet = new ChainDefinition('f16b1833c747c43682f4386fca9cbb327929334a762755ebec17f6f23c9b8a12', 'https://testnet.waxsweden.org', WAXAccountObject) } -export class AccountKit { +export class AccountKit> { + readonly chain: Chain readonly client: APIClient readonly contract?: Contract - constructor(args: AccountKitArgs) { - if (args.contract) { - this.contract = args.contract - } - if (args.client) { - this.client = args.client - } else { - throw new Error('A `client` must be passed when initializing the AccountKit.') - } + constructor(chain: Chain, contract?: Contract) { + this.chain = chain + this.contract = contract + this.client = new APIClient({ url: this.chain.url }) } async load(accountName: NameType): Promise { diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..0bbee46 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,27 @@ +import {API, Float64, Int64, Struct, TimePoint} from '@wharfkit/antelope' + +export type AccountData = API.v1.AccountObject | TelosAccountObject | WAXAccountObject + +@Struct.type('telos_account_voter_info') +export class TelosAccountVoterInfo extends API.v1.AccountVoterInfo { + @Struct.field(Int64) last_stake!: Int64 +} + +@Struct.type('telos_account_object') +export class TelosAccountObject extends API.v1.AccountObject { + @Struct.field(TelosAccountVoterInfo, {optional: true}) + declare voter_info?: TelosAccountVoterInfo +} + +@Struct.type('wax_account_voter_info') +export class WAXAccountVoterInfo extends API.v1.AccountVoterInfo { + @Struct.field(Float64) declare unpaid_voteshare: Float64 + @Struct.field(TimePoint) declare unpaid_voteshare_last_updated: TimePoint + @Struct.field(Float64) declare unpaid_voteshare_change_rate: Float64 + @Struct.field(TimePoint) declare last_claim_time: TimePoint +} + +@Struct.type('wax_account_object') +export class WAXAccountObject extends API.v1.AccountObject { + @Struct.field(WAXAccountVoterInfo, {optional: true}) declare voter_info?: WAXAccountVoterInfo +} \ No newline at end of file diff --git a/test/tests/account.ts b/test/tests/account.ts index bb6add3..4074948 100644 --- a/test/tests/account.ts +++ b/test/tests/account.ts @@ -4,12 +4,12 @@ import {makeClient, mockSessionArgs, mockSessionOptions} from '@wharfkit/mock-da import {Session} from '@wharfkit/session' import {PlaceholderAuth} from '@wharfkit/signing-request' -import {Account, AccountKit, Permission, SystemContract} from '../../src' +import {Account, AccountKit, Chains, Permission, SystemContract} from '../../src' const mockAccountName = 'wharfkit1133' const client = makeClient('https://jungle4.greymass.com') -const accountKit = new AccountKit({client}) +const accountKit = new AccountKit(Chains.Jungle4) const session = new Session( { ...mockSessionArgs,