Skip to content

Commit

Permalink
Merge pull request Joystream#24 from jfinkhaeuser/storage-role-fixes
Browse files Browse the repository at this point in the history
Storage role fixes
  • Loading branch information
jfinkhaeuser committed Apr 9, 2019
2 parents 8b956cb + c0f76a0 commit 661754d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
10 changes: 6 additions & 4 deletions bin/cli.js
Expand Up @@ -238,6 +238,8 @@ async function run_signup(account_file)
const member_address = api.key.address();

// Check if account works
const min = await api.requiredBalanceForRoleStaking(ROLE_STORAGE);
console.log(`Account needs to be a member and have a minimum balance of ${min.toString()}`);
const check = await api.checkAccountForStaking(member_address);
if (check) {
console.log('Account is working for staking, proceeding.');
Expand All @@ -246,14 +248,14 @@ async function run_signup(account_file)
// Create a role key
const role_key = await api.createRoleKey(member_address);
const role_address = role_key.address();
console.log('Generated ', role_address, '- this is going to be exported to a',
'JSON file. You can provide an empty passphrase to make starting the server',
'easier, but you must keep the file very safe, then.');
console.log('Generated', role_address, '- this is going to be exported to a JSON file.\n',
' You can provide an empty passphrase to make starting the server easier,\n',
' but you must keep the file very safe, then.');
const filename = await api.writeKeyPairExport(role_address);
console.log('Identity stored in', filename);

// Ok, transfer for staking.
await api.transferForStaking(member_address, role_address);
await api.transferForStaking(member_address, role_address, ROLE_STORAGE);
console.log('Funds transferred.');

// Now apply for the role
Expand Down
20 changes: 6 additions & 14 deletions lib/joystream/substrate/balances.js
Expand Up @@ -4,9 +4,6 @@ const debug = require('debug')('joystream:substrate:balances');

const { IdentitiesApi } = require('joystream/substrate/identities');

// TODO replace this with the one defined in the role
const DEFAULT_STAKING_AMOUNT = 3000;

class BalancesApi extends IdentitiesApi
{
static async create(account_file)
Expand All @@ -24,15 +21,15 @@ class BalancesApi extends IdentitiesApi
await super.init(account_file);
}

async hasBalanceForRoleStaking(accountId)
{
return await this.hasMinimumBalanceOf(accountId, DEFAULT_STAKING_AMOUNT);
}

async hasMinimumBalanceOf(accountId, min)
{
const balance = await this.freeBalance(accountId);
return balance.cmpn(min) >= 0;
if (typeof min === 'number') {
return balance.cmpn(min) >= 0;
}
else {
return balance.cmp(min) >= 0;
}
}

async freeBalance(accountId)
Expand All @@ -54,11 +51,6 @@ class BalancesApi extends IdentitiesApi
return await this.api.tx.balances.transfer(to_decoded, amount)
.signAndSend(from_key);
}

async transferForStaking(from, to)
{
return await this.transfer(from, to, DEFAULT_STAKING_AMOUNT + 100 /*FIXME */);
}
}

module.exports = {
Expand Down
29 changes: 27 additions & 2 deletions lib/joystream/substrate/roles.js
Expand Up @@ -27,15 +27,17 @@ class RolesApi extends BalancesApi
await super.init(account_file);
}

async checkAccountForStaking(accountId)
async checkAccountForStaking(accountId, role)
{
role = role || ROLE_STORAGE;

if (!await this.isMember(accountId)) {
const msg = `Account with id "${accountId}" is not a member!`;
debug(msg);
throw new Error(msg);
}

if (!await this.hasBalanceForRoleStaking(accountId)) {
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);
Expand All @@ -45,6 +47,29 @@ class RolesApi extends BalancesApi
return true;
}

async requiredBalanceForRoleStaking(role)
{
const params = await this.api.query.actors.parameters(role);
if (_.isEqual(params.raw, new Null())) {
throw new Error(`Role ${role} is not defined!`);
}
const result = params.raw.min_stake.add(params.raw.entry_request_fee);
return result;
}

async hasBalanceForRoleStaking(accountId, role)
{
const required = await this.requiredBalanceForRoleStaking(role);
return await this.hasMinimumBalanceOf(accountId, required);
}

async transferForStaking(from, to, role)
{
const required = await this.requiredBalanceForRoleStaking(role);
return await this.transfer(from, to, required);
}


async applyForRole(roleAccountId, role, memberAccountId)
{
const memberId = await this.memberIdOf(memberAccountId);
Expand Down

0 comments on commit 661754d

Please sign in to comment.