Skip to content

Commit

Permalink
Big update to mirror styles from other libs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Sep 3, 2023
1 parent 47a8299 commit 8a9b982
Show file tree
Hide file tree
Showing 32 changed files with 1,839 additions and 970 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@wharfkit/antelope": "^0.7.3",
"@wharfkit/contract": "0.4.2",
"@wharfkit/resources": "^1.0.0",
"git": "^0.1.5",
"tslib": "^2.1.0"
},
Expand All @@ -37,6 +38,7 @@
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.0.2",
"@wharfkit/session": "^1.0.0",
"chai": "^4.3.4",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.1.0",
Expand Down
234 changes: 139 additions & 95 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,154 +4,198 @@ import {
APIClient,
Asset,
AssetType,
Authority,
Name,
NameType,
UInt32Type,
} from '@wharfkit/antelope'
import {Contract} from '@wharfkit/contract'
import {Resources} from '@wharfkit/resources'

import {Permission} from './permission'
import {Eosio} from './contracts/eosio'
import {SystemContract} from './contracts/eosio'
import {Resource, ResourceType} from './resource'

export interface AccountArgs {
accountData: API.v1.AccountObject
client: APIClient
contract?: Contract
data: API.v1.AccountObject
}

export interface Resources {
cpu_available: number
cpu_used: number
net_available: number
net_used: number
ram_quota: number
ram_usage: number
export interface BuyramOptions {
receiver?: NameType
}

export interface DelegateOptions {
from?: NameType
receiver?: NameType
cpu?: AssetType
net?: AssetType
transfer?: boolean
}

export interface UndelegateOptions {
from?: NameType
receiver?: NameType
cpu?: AssetType
net?: AssetType
}

export class Account {
readonly account_data: API.v1.AccountObject
readonly eosioContract: Eosio.Contract
readonly data: API.v1.AccountObject
readonly systemContract: Contract
readonly client: APIClient

constructor({accountData, client}: AccountArgs) {
this.account_data = accountData
this.eosioContract = new Eosio.Contract({client})
this.client = client
constructor(args: AccountArgs) {
this.data = args.data
if (args.contract) {
this.systemContract = args.contract
} else {
this.systemContract = new SystemContract.Contract({client: args.client})
}
this.client = args.client
}

get accountName() {
return Name.from(this.account_data.account_name)
return Name.from(this.data.account_name)
}

getPermission(permissionName: NameType): Permission {
const permissionObject = this.account_data.permissions.find((permission) =>
get systemToken() {
return Asset.Symbol.from(this.data.total_resources.cpu_weight.symbol)
}

balance(contract: NameType = 'eosio.token', symbol?: Asset.SymbolType): Promise<Asset> {
return new Promise((resolve, reject) => {
this.client.v1.chain
.get_currency_balance(contract, String(this.accountName), symbol && String(symbol))
.then((balances) => {
const balance = (balances as any)[0]

if (!balance) {
reject(
new Error(
`No balance found for ${symbol} token of ${contract} contract.`
)
)
}

resolve(balance)
})
.catch((err) => {
if (
err.message.includes('No data') ||
err.message.includes('Account Query Exception')
) {
reject(new Error(`Token contract ${contract} does not exist.`))
}
reject(err)
})
})
}

permission(permissionName: NameType): Permission {
const permission = this.data.permissions.find((permission) =>
permission.perm_name.equals(permissionName)
)

if (!permissionObject) {
if (!permission) {
throw new Error(
`Permission ${permissionName} does not exist on account ${this.accountName}.`
)
}

return new Permission(permissionName, {
account: this.accountName,
parent: permissionObject.parent,
permission: permissionObject.perm_name,
auth: Authority.from(permissionObject.required_auth),
authorized_by: '............1',
})
return Permission.from(permission)
}

updatePermission(permission: Permission): Action {
return this.eosioContract.action('updateauth', permission.actionData)
resource(resourceType: ResourceType): Resource {
return new Resource(resourceType, this.data)
}

removePermission(permissionName: NameType): Action {
return this.eosioContract.action('deleteauth', {
account: '............1',
authorized_by: '............1',
permission: permissionName,
// TODO: Refactor once resources library is updated
resources(sampleAccount?: NameType) {
// Returns an instance of the @wharfkit/resources library
// configured for this blockchain/account
return new Resources({
api: this.client,
sampleAccount: sampleAccount ? String(sampleAccount) : undefined,
symbol: this.data.core_liquid_balance
? String(this.data.core_liquid_balance.symbol)
: undefined,
})
}

buyRam(amount: AssetType): Action {
return this.eosioContract.action('buyram', {
payer: '............1',
receiver: '............1',
quant: amount,
setPermission(permission: Permission): Action {
return this.systemContract.action('updateauth', {
account: this.accountName,
auth: permission.required_auth,
authorized_by: '',
parent: permission.parent,
permission: permission.perm_name,
})
}

buyRamBytes(bytes: number): Action {
return this.eosioContract.action('buyrambytes', {
payer: '............1',
receiver: '............1',
bytes,
removePermission(permissionName: NameType): Action {
return this.systemContract.action('deleteauth', {
account: this.accountName,
authorized_by: '',
permission: permissionName,
})
}

sellRam(bytes: number): Action {
return this.eosioContract.action('sellram', {
account: '............1',
bytes,
})
linkauth() {
// TODO: Implement `linkauth` action calls
}

delegateResources(cpu: AssetType, net: AssetType): Action {
return this.eosioContract.action('delegatebw', {
from: '............1',
receiver: '............1',
stake_cpu_quantity: cpu,
stake_net_quantity: net,
transfer: false,
})
unlinkauth() {
// TODO: Implement `unlinkauth` action calls
}

undelegateResources(cpu: AssetType, net: AssetType): Action {
return this.eosioContract.action('undelegatebw', {
from: '............1',
receiver: '............1',
unstake_cpu_quantity: cpu,
unstake_net_quantity: net,
buyRam(amount: AssetType, options?: BuyramOptions): Action {
let receiver = this.accountName
if (options && options.receiver) {
receiver = Name.from(options.receiver)
}
return this.systemContract.action('buyram', {
payer: this.accountName,
quant: amount,
receiver,
})
}

getResources(): Resources {
return {
net_available: Number(this.account_data.net_limit.available),
net_used: Number(this.account_data.net_limit.available),
cpu_available: Number(this.account_data.cpu_limit.available),
cpu_used: Number(this.account_data.cpu_limit.used),
ram_quota: Number(this.account_data.ram_quota),
ram_usage: Number(this.account_data.ram_usage),
buyRamBytes(bytes: UInt32Type, options?: BuyramOptions): Action {
let receiver = this.accountName
if (options && options.receiver) {
receiver = Name.from(options.receiver)
}
return this.systemContract.action('buyrambytes', {
bytes,
payer: this.accountName,
receiver,
})
}

getBalance(contract: NameType = 'eosio.token', symbol?: Asset.SymbolType): Promise<Asset> {
return new Promise((resolve, reject) => {
this.client.v1.chain
.get_currency_balance(contract, String(this.accountName), symbol && String(symbol))
.then((balances) => {
const balance = (balances as any)[0]
sellRam(bytes: UInt32Type): Action {
return this.systemContract.action('sellram', {
account: this.accountName,
bytes,
})
}

if (!balance) {
reject(
new Error(
`No balance found for ${symbol} token of ${contract} contract.`
)
)
}
delegate(value: DelegateOptions): Action {
return this.systemContract.action('delegatebw', {
from: value.from || this.accountName,
receiver: value.receiver || this.accountName,
stake_cpu_quantity: value.cpu || Asset.fromUnits(0, this.systemToken),
stake_net_quantity: value.net || Asset.fromUnits(0, this.systemToken),
transfer: value.transfer !== undefined ? value.transfer : false,
})
}

resolve(balance)
})
.catch((err) => {
if (
err.message.includes('No data') ||
err.message.includes('Account Query Exception')
) {
reject(new Error(`Token contract ${contract} does not exist.`))
}
reject(err)
})
undelegate(value: UndelegateOptions): Action {
return this.systemContract.action('undelegatebw', {
from: value.from || this.accountName,
receiver: value.receiver || this.accountName,
unstake_cpu_quantity: value.cpu || Asset.fromUnits(0, this.systemToken),
unstake_net_quantity: value.net || Asset.fromUnits(0, this.systemToken),
})
}
}
6 changes: 3 additions & 3 deletions src/contracts/eosio.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './account'
export * from './permission'
export * from './kit'
export * from './contracts/eosio'

import {AccountKit} from './kit'

Expand Down
26 changes: 9 additions & 17 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
import {APIClient, Name, NameType} from '@wharfkit/antelope'
import {Contract} from '@wharfkit/contract'

import {Account} from './account'

export interface AccountKitArgs {
client: APIClient
contract?: Contract
}

export class AccountKit {
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.')
}
}

/**
* Load an Account by name from an API endpoint
*
* @param account The name of the account to load
* @returns
*/
async load(accountName: NameType): Promise<Account> {
const account = Name.from(accountName)

let accountData

try {
accountData = await this.client.v1.chain.get_account(account)
} catch (error) {
throw new Error(`Account ${account} does not exist`)
}

return new Account({
accountData: accountData,
client: this.client,
contract: this.contract,
data: await this.client.v1.chain.get_account(accountName),
})
}
}
Loading

0 comments on commit 8a9b982

Please sign in to comment.