-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: passing ChainDefinition as AcountKit arg
- Loading branch information
Showing
6 changed files
with
96 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters