From 3d608e4c4a3f25469882dd44a906ca704d2a3817 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/account.ts | 10 +++---- src/kit.ts | 60 ++++++++++++++++++++++++++++------------ src/types.ts | 27 ++++++++++++++++++ test/tests/account.ts | 4 +-- test/tests/kit.ts | 24 +++++++++++----- test/tests/permission.ts | 7 ++--- 6 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 src/types.ts diff --git a/src/account.ts b/src/account.ts index 6420468..e16a9ff 100644 --- a/src/account.ts +++ b/src/account.ts @@ -15,10 +15,10 @@ import {Permission} from './permission' import * as SystemContract from './contracts/eosio' import {Resource, ResourceType} from './resource' -export interface AccountArgs { +export interface AccountArgs { client: APIClient contract?: Contract - data: API.v1.AccountObject + data: Data } export interface BuyramOptions { @@ -40,12 +40,12 @@ export interface UndelegateOptions { net?: AssetType } -export class Account { - readonly data: API.v1.AccountObject +export class Account { + readonly data: Data readonly systemContract: SystemContract.Contract readonly client: APIClient - constructor(args: AccountArgs) { + constructor(args: AccountArgs) { this.data = args.data if (args.contract) { this.systemContract = args.contract diff --git a/src/kit.ts b/src/kit.ts index 93bffca..5e19441 100644 --- a/src/kit.ts +++ b/src/kit.ts @@ -1,33 +1,57 @@ -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: ChainDefinition = { + id: '1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f', + url: 'https://telos.greymass.com', + } + export const TelosTestnet: ChainDefinition = { + id: '1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f', + url: 'https://telos.greymass.com' + } + export const WAX: ChainDefinition = { + id: '1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4', + url: 'https://wax.greymass.com', + } + export const WAXTestnet: ChainDefinition = { + id: 'f16b1833c747c43682f4386fca9cbb327929334a762755ebec17f6f23c9b8a12', + url: 'https://testnet.waxsweden.org', + } } -export class AccountKit { +export class AccountKit { + readonly chain: ChainDefinition 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: ChainDefinition, contract?: Contract) { + this.chain = chain + this.contract = contract + this.client = new APIClient({ url: this.chain.url }) } - async load(accountName: NameType): Promise { - return new Account({ + async load(accountName: NameType): Promise> { + return new Account({ client: this.client, contract: this.contract, - data: await this.client.v1.chain.get_account(accountName), + data: await this.client.v1.chain.get_account(accountName) as DataType, }) } } 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, diff --git a/test/tests/kit.ts b/test/tests/kit.ts index 82a13a6..8a811ab 100644 --- a/test/tests/kit.ts +++ b/test/tests/kit.ts @@ -1,15 +1,16 @@ import {assert, expect} from 'chai' -import {Account, AccountKit, SystemContract} from '../../src' +import {Account, AccountKit, Chains, SystemContract} from '../../src' import {makeClient} from '@wharfkit/mock-data' +import { API } from '@wharfkit/antelope' +import { TelosAccountObject, WAXAccountObject } from 'src/types' -const client = makeClient('https://jungle4.greymass.com') suite('AccountKit', function () { let accountKit: AccountKit this.beforeAll(function () { - accountKit = new AccountKit({client}) + accountKit = new AccountKit(Chains.Jungle4) }) suite('constructor', function () { @@ -29,10 +30,10 @@ suite('AccountKit', function () { }) test('allow overriding of default contract', function () { - const kit = new AccountKit({ - client, - contract: new SystemContract.Contract({client: makeClient()}), - }) + const kit = new AccountKit( + Chains.Jungle4, + new SystemContract.Contract({client: makeClient()}), + ) }) }) @@ -51,4 +52,13 @@ suite('AccountKit', function () { expect(account).to.be.instanceOf(Account) }) }) + + test('returns telos account type', async function () { + const kit = new AccountKit(Chains.Telos) + const account = await kit.load('teamgreymass') + expect(account.data).to.be.instanceOf(API.v1.AccountObject) + expect(account.data).to.be.instanceOf(TelosAccountObject) + expect(account.data).not.to.be.instanceOf(WAXAccountObject) + assert.isDefined(account.data.voter_info?.last_stake) + }) }) diff --git a/test/tests/permission.ts b/test/tests/permission.ts index 4794755..b82bca6 100644 --- a/test/tests/permission.ts +++ b/test/tests/permission.ts @@ -9,12 +9,11 @@ import { UInt16, WaitWeight, } from '@wharfkit/antelope' -import {makeClient, mockAccountName} from '@wharfkit/mock-data' +import {mockAccountName} from '@wharfkit/mock-data' -import {Account, AccountKit, Permission} from '../../src' +import {Account, AccountKit, Chains, Permission} from '../../src' -const client = makeClient('https://jungle4.greymass.com') -const accountKit = new AccountKit({client}) +const accountKit = new AccountKit(Chains.Jungle4) suite('Permission', function () { let testAccount: Account