-
-
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.
Merge pull request #10 from wharfkit/using-contract-kit
Using ContractKit
- Loading branch information
Showing
47 changed files
with
4,268 additions
and
2,282 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
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,201 @@ | ||
import { | ||
Action, | ||
API, | ||
APIClient, | ||
Asset, | ||
AssetType, | ||
Name, | ||
NameType, | ||
UInt32Type, | ||
} from '@wharfkit/antelope' | ||
import {Contract} from '@wharfkit/contract' | ||
import {Resources} from '@wharfkit/resources' | ||
|
||
import {Permission} from './permission' | ||
import {SystemContract} from './contracts/eosio' | ||
import {Resource, ResourceType} from './resource' | ||
|
||
export interface AccountArgs { | ||
client: APIClient | ||
contract?: Contract | ||
data: API.v1.AccountObject | ||
} | ||
|
||
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 data: API.v1.AccountObject | ||
readonly systemContract: Contract | ||
readonly client: APIClient | ||
|
||
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.data.account_name) | ||
} | ||
|
||
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 (!permission) { | ||
throw new Error( | ||
`Permission ${permissionName} does not exist on account ${this.accountName}.` | ||
) | ||
} | ||
|
||
return Permission.from(permission) | ||
} | ||
|
||
resource(resourceType: ResourceType): Resource { | ||
return new Resource(resourceType, this.data) | ||
} | ||
|
||
// 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, | ||
}) | ||
} | ||
|
||
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, | ||
}) | ||
} | ||
|
||
removePermission(permissionName: NameType): Action { | ||
return this.systemContract.action('deleteauth', { | ||
account: this.accountName, | ||
authorized_by: '', | ||
permission: permissionName, | ||
}) | ||
} | ||
|
||
linkauth() { | ||
// TODO: Implement `linkauth` action calls | ||
} | ||
|
||
unlinkauth() { | ||
// TODO: Implement `unlinkauth` action calls | ||
} | ||
|
||
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, | ||
}) | ||
} | ||
|
||
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, | ||
}) | ||
} | ||
|
||
sellRam(bytes: UInt32Type): Action { | ||
return this.systemContract.action('sellram', { | ||
account: this.accountName, | ||
bytes, | ||
}) | ||
} | ||
|
||
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, | ||
}) | ||
} | ||
|
||
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), | ||
}) | ||
} | ||
} |
Oops, something went wrong.