123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- * This file is part of the storage node for the Joystream project.
- * Copyright (C) 2019 Joystream Contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- 'use strict';
- const debug = require('debug')('joystream:runtime:roles');
- const { Null, u64 } = require('@polkadot/types');
- const { _ } = require('lodash');
- /*
- * Add role related functionality to the substrate API.
- */
- class RolesApi
- {
- static async create(base)
- {
- const ret = new RolesApi();
- ret.base = base;
- await ret.init();
- return ret;
- }
- async init()
- {
- debug('Init');
- // Constants
- this.ROLE_STORAGE = 'StorageProvider'; // new u64(0x00);
- }
- /*
- * Raises errors if the given account ID is not valid for staking as the given
- * role. The role should be one of the ROLE_* constants above.
- */
- async checkAccountForStaking(accountId, role)
- {
- role = role || this.ROLE_STORAGE;
- if (!await this.base.identities.isMember(accountId)) {
- const msg = `Account with id "${accountId}" is not a member!`;
- debug(msg);
- throw new Error(msg);
- }
- if (!await this.hasBalanceForRoleStaking(accountId, role)) {
- const msg = `Account with id "${accountId}" does not have sufficient free balance for role staking!`;
- debug(msg);
- throw new Error(msg);
- }
- debug(`Account with id "${accountId}" is a member with sufficient free balance, able to proceed.`);
- return true;
- }
- /*
- * Returns the required balance for staking for a role.
- */
- async requiredBalanceForRoleStaking(role)
- {
- const params = await this.base.api.query.actors.parameters(role);
- if (params.isNone) {
- throw new Error(`Role ${role} is not defined!`);
- }
- const result = params.raw.min_stake
- .add(params.raw.entry_request_fee)
- .add(await this.base.balances.baseTransactionFee());
- return result;
- }
- /*
- * Returns true/false if the given account has the balance required for
- * staking for the given role.
- */
- async hasBalanceForRoleStaking(accountId, role)
- {
- const required = await this.requiredBalanceForRoleStaking(role);
- return await this.base.balances.hasMinimumBalanceOf(accountId, required);
- }
- /*
- * Transfer enough funds to allow the recipient to stake for the given role.
- */
- async transferForStaking(from, to, role)
- {
- const required = await this.requiredBalanceForRoleStaking(role);
- return await this.base.balances.transfer(from, to, required);
- }
- /*
- * Return current accounts holding a role.
- */
- async accountIdsByRole(role)
- {
- const ids = await this.base.api.query.actors.accountIdsByRole(role);
- return ids.map(id => id.toString());
- }
- /*
- * Returns the number of slots available for a role
- */
- async availableSlotsForRole(role)
- {
- let params = await this.base.api.query.actors.parameters(role);
- if (params.isNone) {
- throw new Error(`Role ${role} is not defined!`);
- }
- params = params.unwrap();
- const slots = params.max_actors;
- const active = await this.accountIdsByRole(role);
- return (slots.subn(active.length)).toNumber();
- }
- /*
- * Send a role application.
- * - The role account must not be a member, but have sufficient funds for
- * staking.
- * - The member account must be a member.
- *
- * After sending this application, the member account will have role request
- * in the 'My Requests' tab of the app.
- */
- async applyForRole(roleAccountId, role, memberAccountId)
- {
- const memberId = await this.base.identities.firstMemberIdOf(memberAccountId);
- if (memberId == undefined) {
- throw new Error('Account is not a member!');
- }
- const tx = this.base.api.tx.actors.roleEntryRequest(role, memberId);
- return await this.base.signAndSend(roleAccountId, tx);
- }
- /*
- * Check whether the given role is occupying the given role.
- */
- async checkForRole(roleAccountId, role)
- {
- const actor = await this.base.api.query.actors.actorByAccountId(roleAccountId);
- return !_.isEqual(actor.raw, new Null());
- }
- /*
- * Same as checkForRole(), but if the account is not currently occupying the
- * role, wait for the appropriate `actors.Staked` event to be emitted.
- */
- async waitForRole(roleAccountId, role)
- {
- if (await this.checkForRole(roleAccountId, role)) {
- return true;
- }
- return new Promise((resolve, reject) => {
- this.base.waitForEvent('actors', 'Staked').then((values) => {
- const name = values[0][0];
- const payload = values[0][1];
- if (payload.AccountId == roleAccountId) {
- resolve(true);
- } else {
- // reject() ?
- }
- });
- });
- }
- }
- module.exports = {
- RolesApi: RolesApi,
- }
|