All files / src createbridge.js

100% Statements 41/41
47.62% Branches 10/21
100% Functions 6/6
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116  9x               1x 1x   1x 1x 1x   1x                         1x 1x 1x           1x 1x 1x   1x               2x 2x   2x 2x 2x 2x   2x             1x 1x   1x 1x 1x   1x                   1x 1x 1x   1x 1x 1x   1x               2x 2x   2x 2x 2x   2x     9x                
/* Public */
const { ChainAction, composeAction } = require('./compose');
// Initializes createbridge with the following details:
// Only the createbridge account can call this action
// symbol               = the core token of the chain or the token used to pay for new user accounts of the chain
// precision            = precision of the core token of the chain
// newAccountContract   = the contract to call for new account action
// minimumRAM           = minimum bytes of RAM to put in a new account created on the chain
function init(symbol, precision, newAccountContract, newAccountAction, minimumRAM, options) {
  const { contractName = 'createbridge', permission = 'active', broadcast = true } = options;
  const chainSymbol = `${precision},${symbol}`;
 
  const args = { contractName, chainSymbol, newAccountContract, newAccountAction, minimumRAM, permission };
  const action = composeAction(ChainAction.CreateBridge_init, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
// Registers an app with the createbridge contract. Called with the following parameter:
// authorizingAccount       = an object with account name and permission to be registered as the owner of the app
// appName:                 = the string/account name representing the app
// ram:                     = bytes of ram to put in the new user account created for the app (defaults to 4kb)
// net                      = amount to be staked for net
// cpu                      = amount to be staked for cpu
// airdropContract          = name of the airdrop contract
// airdropToken             = total number of tokens to be airdropped
// airdroplimit             = number of tokens to be airdropped to the newly created account
function define(authorizingAccount, appName, ram = 4096, net, cpu, pricekey, options) {
  const { airdropContract, airdropToken, airdropLimit, contractName = 'createbridge', broadcast = true } = options;
  const { accountName, permission = 'active' } = authorizingAccount;
  const airdrop = {
    contract: airdropContract,
    tokens: airdropToken,
    limit: airdropLimit
  };
 
  const args = { accountName, airdrop, appName, contractName, cpu, permission, net, pricekey, ram };
  const action = composeAction(ChainAction.CreateBridge_Define, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
// Creates a new user account. It also airdrops custom dapp tokens to the new user account if an app owner has opted for airdrops
// authorizingAccount = an object with account name and permission of the account paying for the balance left after getting the donation from the app contributors
// keys               = owner key and active key for the new account
// origin             = the string representing the app to create the new user account for. For ex- everipedia.org, lumeos
function createNewAccount(authorizingAccount, keys, options) {
  const { accountName, permission } = authorizingAccount;
  const { origin, oreAccountName, contractName = 'createbridge', broadcast = true, referral = '' } = options;
 
  const { active: activekey, owner: ownerkey } = keys.publicKeys;
  const args = { accountName, activekey, contractName, oreAccountName, origin, ownerkey, permission, referral };
  const action = composeAction(ChainAction.CreateBridge_Create, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
// Owner account of an app can whitelist other accounts.
// authorizingAccount   = an object with account name and permission contributing towards an app
// whitelistAccount     = account name to be whitelisted to create accounts on behalf of the app
function whitelist(authorizingAccount, whitelistAccount, appName, options) {
  const { contractName = 'createbridge', broadcast = true } = options;
  const { accountName, permission = 'active' } = authorizingAccount;
 
  const args = { accountName, appName, contractName, permission, whitelistAccount };
  const action = composeAction(ChainAction.CreateBridge_Whitelist, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
// Contributes to account creation for an app by transferring the amount to createbridge with the app name in the memo field
// authorizingAccount   = an object with account name and permission contributing towards an app
// appName       = name of the app to contribute
// amount        = amount to contribute
// ramPercentage = RAM% per account the contributor wants to subsidize
// totalAccounts = max accounts that can be created with the provided contribution (optional)
function transfer(authorizingAccount, appName, amount, ramPercentage, totalAccounts = -1, options) {
  const { contractName = 'eosio.token', createbridgeAccountName = 'createbridge', broadcast = true } = options;
  const { accountName, permission = 'active' } = authorizingAccount;
  const memo = `${appName},${ramPercentage},${totalAccounts}`;
 
  const args = { accountName, amount, contractName, createbridgeAccountName, memo, permission };
  const action = composeAction(ChainAction.CreateBridge_Transfer, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
// Transfers the remaining balance of a contributor from createbridge back to the contributor
// authorizingAccount = an object with account name and permission trying to reclaim the balance
// appName    = the app name for which the account is trying to reclaim the balance
// symbol     = symbol of the tokens to be reclaimed.
function reclaim(authorizingAccount, appName, symbol, options) {
  const { contractName = 'createbridge', broadcast = true } = options;
  const { accountName, permission = 'active' } = authorizingAccount;
 
  const args = { accountName, appName, contractName, permission, symbol };
  const action = composeAction(ChainAction.CreateBridge_Reclaim, args);
  const actions = [action];
 
  return this.transact(actions, broadcast);
}
 
module.exports = {
  init,
  createNewAccount,
  define,
  whitelist,
  transfer,
  reclaim
};