Skip to content

Commit

Permalink
Merge pull request #10 from wharfkit/using-contract-kit
Browse files Browse the repository at this point in the history
Using ContractKit
  • Loading branch information
aaroncox authored Sep 3, 2023
2 parents c27744e + 44635e4 commit ee7a965
Show file tree
Hide file tree
Showing 47 changed files with 4,268 additions and 2,282 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SHELL := /bin/bash
SRC_FILES := $(shell find src -name '*.ts')
TEST_FILES := $(wildcard test/*.ts)
TEST_FILES := $(wildcard test/tests/*.ts)
BIN := ./node_modules/.bin
MOCHA_OPTS := -u tdd -r ts-node/register -r tsconfig-paths/register --extension ts
NYC_OPTS := --temp-dir build/nyc_output --report-dir build/coverage
Expand All @@ -10,23 +10,23 @@ lib: ${SRC_FILES} package.json tsconfig.json node_modules rollup.config.mjs

.PHONY: test
test: node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
${BIN}/mocha ${MOCHA_OPTS} test/*.ts --grep '$(grep)'
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/mocha ${MOCHA_OPTS} test/tests/*.ts --grep '$(grep)'

build/coverage: ${SRC_FILES} ${TEST_FILES} node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc ${NYC_OPTS} --reporter=html \
${BIN}/mocha ${MOCHA_OPTS} -R nyan test/*.ts
${BIN}/mocha ${MOCHA_OPTS} -R nyan test/tests/*.ts

.PHONY: coverage
coverage: build/coverage
@open build/coverage/index.html

.PHONY: ci-test
ci-test: node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc ${NYC_OPTS} --reporter=text \
${BIN}/mocha ${MOCHA_OPTS} -R list test/*.ts
${BIN}/mocha ${MOCHA_OPTS} -R list test/tests/*.ts

.PHONY: check
check: node_modules
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
"prepare": "make"
},
"dependencies": {
"@greymass/eosio": "^0.6.4",
"@wharfkit/session": "^0.2.0",
"@wharfkit/antelope": "^0.7.3",
"@wharfkit/contract": "0.4.2",
"@wharfkit/resources": "^1.0.0",
"git": "^0.1.5",
"tslib": "^2.1.0"
},
"resolutions": {
"@wharfkit/antelope": "^0.7.3"
},
"devDependencies": {
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-commonjs": "^23.0.2",
Expand All @@ -33,6 +37,8 @@
"@types/node": "^18.11.9",
"@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
201 changes: 201 additions & 0 deletions src/account.ts
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),
})
}
}
Loading

0 comments on commit ee7a965

Please sign in to comment.