Skip to content

Commit

Permalink
refactor: passing ChainDefinition as AcountKit arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 12, 2023
1 parent 47a291b commit 3d608e4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data extends API.v1.AccountObject = API.v1.AccountObject> {
client: APIClient
contract?: Contract
data: API.v1.AccountObject
data: Data
}

export interface BuyramOptions {
Expand All @@ -40,12 +40,12 @@ export interface UndelegateOptions {
net?: AssetType
}

export class Account {
readonly data: API.v1.AccountObject
export class Account<Data extends API.v1.AccountObject = API.v1.AccountObject> {
readonly data: Data
readonly systemContract: SystemContract.Contract
readonly client: APIClient

constructor(args: AccountArgs) {
constructor(args: AccountArgs<Data>) {
this.data = args.data
if (args.contract) {
this.systemContract = args.contract
Expand Down
60 changes: 42 additions & 18 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -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<ResponseType extends API.v1.AccountObject = any> {
id: string
url: string
}
export namespace Chains {
export const EOS: ChainDefinition<API.v1.AccountObject> = {
id: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
url: 'https://eos.greymass.com',
}
export const Jungle4: ChainDefinition<API.v1.AccountObject> = {
id: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d',
url: 'https://jungle4.greymass.com',
}
export const Telos: ChainDefinition<TelosAccountObject> = {
id: '1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f',
url: 'https://telos.greymass.com',
}
export const TelosTestnet: ChainDefinition<TelosAccountObject> = {
id: '1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f',
url: 'https://telos.greymass.com'
}
export const WAX: ChainDefinition<WAXAccountObject> = {
id: '1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4',
url: 'https://wax.greymass.com',
}
export const WAXTestnet: ChainDefinition<WAXAccountObject> = {
id: 'f16b1833c747c43682f4386fca9cbb327929334a762755ebec17f6f23c9b8a12',
url: 'https://testnet.waxsweden.org',
}
}

export class AccountKit {
export class AccountKit<DataType extends API.v1.AccountObject = API.v1.AccountObject> {
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<DataType>, contract?: Contract) {
this.chain = chain
this.contract = contract
this.client = new APIClient({ url: this.chain.url })
}

async load(accountName: NameType): Promise<Account> {
return new Account({
async load(accountName: NameType): Promise<Account<DataType>> {
return new Account<DataType>({
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,
})
}
}
27 changes: 27 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions test/tests/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 17 additions & 7 deletions test/tests/kit.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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()}),
)
})
})

Expand All @@ -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)
})
})
7 changes: 3 additions & 4 deletions test/tests/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d608e4

Please sign in to comment.