UNPKG

6.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.VAccountService = void 0;
4const tslib_1 = require("tslib");
5const antelope_1 = require("@wharfkit/antelope");
6let VAddress = class VAddress extends antelope_1.Variant {
7};
8VAddress = tslib_1.__decorate([
9 antelope_1.Variant.type('vaddress', [antelope_1.Checksum160, antelope_1.Name])
10], VAddress);
11let ExtendedSymbol = class ExtendedSymbol extends antelope_1.Struct {
12 constructor(sym, contract) {
13 super({ 'sym': sym, 'contract': contract });
14 }
15};
16ExtendedSymbol.abiName = 'extended_symbol';
17ExtendedSymbol.abiFields = [{ name: 'sym', type: 'symbol' }, { name: 'contract', type: 'name' }];
18ExtendedSymbol = tslib_1.__decorate([
19 antelope_1.Struct.type('extended_symbol')
20], ExtendedSymbol);
21class VAccountService {
22 constructor(client) {
23 this.client = client;
24 /**
25 * TODO: Figure out return type
26 * get pending balance
27 * @param accountId ID of the given acccount
28 * @returns the payment rows of the given `accountId`
29 */
30 this.getPendingPayout = async (accountId) => {
31 const response = await this.client.eos.v1.chain.get_table_rows({
32 code: this.client.config.tasksContract,
33 scope: this.client.config.tasksContract,
34 table: 'payment',
35 index_position: 'tertiary',
36 key_type: 'i64',
37 lower_bound: antelope_1.UInt128.from(accountId),
38 upper_bound: antelope_1.UInt128.from(accountId)
39 });
40 console.debug(response);
41 return response;
42 };
43 }
44 async vtransfer(from_id, to_id, quantity) {
45 const transferAction = {
46 account: this.client.config.vaccountContract,
47 name: "vtransfer",
48 authorization: [this.client.session.permissionLevel],
49 data: {
50 from_id: from_id,
51 to_id: to_id,
52 quantity: {
53 quantity: quantity,
54 contract: this.client.config.tokenContract,
55 },
56 memo: "",
57 payer: this.client.session.actor,
58 sig: null,
59 fee: null,
60 },
61 };
62 return await this.client.session.transact({ action: transferAction });
63 }
64 async open() {
65 const conf = this.client.config;
66 const action = {
67 account: conf.vaccountContract,
68 name: "open",
69 authorization: [this.client.session.permissionLevel],
70 data: {
71 acc: VAddress.from(antelope_1.Name.from(this.client.session.actor.toString())),
72 symbol: new ExtendedSymbol('4,EFX', conf.tokenContract),
73 payer: this.client.session.actor,
74 },
75 };
76 return await this.client.session.transact({ action: action });
77 }
78 /**
79 * Get vAccount row of the configured account and token contract
80 * @returns {Promise<VAccount>}
81 */
82 async get() {
83 const { conf, keycs } = this.generateCheckSumForVAccount();
84 const response = await this.client.eos.v1.chain.get_table_rows({
85 code: conf.vaccountContract,
86 table: 'account',
87 scope: conf.vaccountContract,
88 upper_bound: keycs,
89 lower_bound: keycs,
90 index_position: 'secondary',
91 key_type: 'sha256',
92 });
93 return response.rows.find((row) => row.balance.contract === conf.tokenContract);
94 }
95 /**
96 * Get all VAccount rows of the configured account and token contract
97 */
98 async getAll() {
99 const { conf, keycs } = this.generateCheckSumForVAccount();
100 const response = await this.client.eos.v1.chain.get_table_rows({
101 code: conf.vaccountContract,
102 table: 'account',
103 scope: conf.vaccountContract,
104 upper_bound: keycs,
105 lower_bound: keycs,
106 index_position: 'secondary',
107 key_type: 'sha256',
108 });
109 return response.rows;
110 }
111 /**
112 * Retrieve the avatar asset for the given account
113 * @param account
114 */
115 async getAvatarAsset(account) {
116 const avatar = await this.client.dao.getAvatar(account);
117 const asset = await this.client.atomic.getAsset(account, avatar.asset_id);
118 return asset;
119 }
120 /**
121 * TODO: Define tests for this method
122 * Receive tokens from completed tasks.
123 * @param paymentId
124 * @returns
125 */
126 async payout() {
127 this.client.requireSession();
128 const actions = [];
129 const vacc = await this.get();
130 const settings = await this.client.tasks.getForceSettings();
131 const payments = await this.getPendingPayout(vacc.id);
132 if (payments) {
133 for (const payment of payments.rows) {
134 // payout is only possible after x amount of days have passed since the last_submission_time
135 if (((new Date(new Date(payment.last_submission_time) + 'UTC').getTime() / 1000) + settings.payout_delay_sec < ((Date.now() / 1000)))) {
136 actions.push({
137 account: this.client.config.tasksContract,
138 name: 'payout',
139 authorization: [{
140 actor: this.client.session.actor,
141 permission: this.client.session.permission
142 }],
143 data: {
144 payment_id: payment.id
145 }
146 });
147 }
148 }
149 }
150 else {
151 throw new Error('No pending payouts found');
152 }
153 return await this.client.session.transact({ actions: actions });
154 }
155 /**
156 * Generate checkSum for vaccount
157 */
158 generateCheckSumForVAccount() {
159 const conf = this.client.config;
160 let enc = new antelope_1.ABIEncoder(32);
161 antelope_1.Name.from(conf.tokenContract).toABI(enc);
162 const vaddr = VAddress.from(antelope_1.Name.from(this.client.session.actor.toString()));
163 enc.writeByte(vaddr.variantIdx);
164 vaddr.value.toABI(enc);
165 const key = enc.getBytes().hexString;
166 let arr = new Uint8Array(32);
167 arr.set(enc.getData(), 0);
168 const keycs = antelope_1.Checksum256.from(arr);
169 return { conf, keycs };
170 }
171}
172exports.VAccountService = VAccountService;
173;
174//# sourceMappingURL=vaccount.js.map
\No newline at end of file