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 673e774
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
42 changes: 27 additions & 15 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -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<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 = 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<Chain extends ChainDefinition<ResponseType>> {
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<Account> {
Expand Down
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

0 comments on commit 673e774

Please sign in to comment.