UNPKG

@effectai/effect-js

Version:

Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))

174 lines 6.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VAccountService = void 0; const tslib_1 = require("tslib"); const antelope_1 = require("@wharfkit/antelope"); let VAddress = class VAddress extends antelope_1.Variant { }; VAddress = tslib_1.__decorate([ antelope_1.Variant.type('vaddress', [antelope_1.Checksum160, antelope_1.Name]) ], VAddress); let ExtendedSymbol = class ExtendedSymbol extends antelope_1.Struct { constructor(sym, contract) { super({ 'sym': sym, 'contract': contract }); } }; ExtendedSymbol.abiName = 'extended_symbol'; ExtendedSymbol.abiFields = [{ name: 'sym', type: 'symbol' }, { name: 'contract', type: 'name' }]; ExtendedSymbol = tslib_1.__decorate([ antelope_1.Struct.type('extended_symbol') ], ExtendedSymbol); class VAccountService { constructor(client) { this.client = client; /** * TODO: Figure out return type * get pending balance * @param accountId ID of the given acccount * @returns the payment rows of the given `accountId` */ this.getPendingPayout = async (accountId) => { const response = await this.client.eos.v1.chain.get_table_rows({ code: this.client.config.tasksContract, scope: this.client.config.tasksContract, table: 'payment', index_position: 'tertiary', key_type: 'i64', lower_bound: antelope_1.UInt128.from(accountId), upper_bound: antelope_1.UInt128.from(accountId) }); console.debug(response); return response; }; } async vtransfer(from_id, to_id, quantity) { const transferAction = { account: this.client.config.vaccountContract, name: "vtransfer", authorization: [this.client.session.permissionLevel], data: { from_id: from_id, to_id: to_id, quantity: { quantity: quantity, contract: this.client.config.tokenContract, }, memo: "", payer: this.client.session.actor, sig: null, fee: null, }, }; return await this.client.session.transact({ action: transferAction }); } async open() { const conf = this.client.config; const action = { account: conf.vaccountContract, name: "open", authorization: [this.client.session.permissionLevel], data: { acc: VAddress.from(antelope_1.Name.from(this.client.session.actor.toString())), symbol: new ExtendedSymbol('4,EFX', conf.tokenContract), payer: this.client.session.actor, }, }; return await this.client.session.transact({ action: action }); } /** * Get vAccount row of the configured account and token contract * @returns {Promise<VAccount>} */ async get() { const { conf, keycs } = this.generateCheckSumForVAccount(); const response = await this.client.eos.v1.chain.get_table_rows({ code: conf.vaccountContract, table: 'account', scope: conf.vaccountContract, upper_bound: keycs, lower_bound: keycs, index_position: 'secondary', key_type: 'sha256', }); return response.rows.find((row) => row.balance.contract === conf.tokenContract); } /** * Get all VAccount rows of the configured account and token contract */ async getAll() { const { conf, keycs } = this.generateCheckSumForVAccount(); const response = await this.client.eos.v1.chain.get_table_rows({ code: conf.vaccountContract, table: 'account', scope: conf.vaccountContract, upper_bound: keycs, lower_bound: keycs, index_position: 'secondary', key_type: 'sha256', }); return response.rows; } /** * Retrieve the avatar asset for the given account * @param account */ async getAvatarAsset(account) { const avatar = await this.client.dao.getAvatar(account); const asset = await this.client.atomic.getAsset(account, avatar.asset_id); return asset; } /** * TODO: Define tests for this method * Receive tokens from completed tasks. * @param paymentId * @returns */ async payout() { this.client.requireSession(); const actions = []; const vacc = await this.get(); const settings = await this.client.tasks.getForceSettings(); const payments = await this.getPendingPayout(vacc.id); if (payments) { for (const payment of payments.rows) { // payout is only possible after x amount of days have passed since the last_submission_time if (((new Date(new Date(payment.last_submission_time) + 'UTC').getTime() / 1000) + settings.payout_delay_sec < ((Date.now() / 1000)))) { actions.push({ account: this.client.config.tasksContract, name: 'payout', authorization: [{ actor: this.client.session.actor, permission: this.client.session.permission }], data: { payment_id: payment.id } }); } } } else { throw new Error('No pending payouts found'); } return await this.client.session.transact({ actions: actions }); } /** * Generate checkSum for vaccount */ generateCheckSumForVAccount() { const conf = this.client.config; let enc = new antelope_1.ABIEncoder(32); antelope_1.Name.from(conf.tokenContract).toABI(enc); const vaddr = VAddress.from(antelope_1.Name.from(this.client.session.actor.toString())); enc.writeByte(vaddr.variantIdx); vaddr.value.toABI(enc); const key = enc.getBytes().hexString; let arr = new Uint8Array(32); arr.set(enc.getData(), 0); const keycs = antelope_1.Checksum256.from(arr); return { conf, keycs }; } } exports.VAccountService = VAccountService; ; //# sourceMappingURL=vaccount.js.map