UNPKG

@colony/colony-js-client

Version:

Reference implementation of the Colony API

972 lines (743 loc) 29 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); require('isomorphic-fetch'); var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert); var _bn = require('bn.js'); var _bn2 = _interopRequireDefault(_bn); var _colonyJsContractClient = require('@colony/colony-js-contract-client'); var _colonyJsContractClient2 = _interopRequireDefault(_colonyJsContractClient); var _colonyJsUtils = require('@colony/colony-js-utils'); var _index = require('../ColonyClient/index'); var _index2 = _interopRequireDefault(_index); var _index3 = require('../TokenLockingClient/index'); var _index4 = _interopRequireDefault(_index3); var _LookupRegisteredENSDomain = require('./callers/LookupRegisteredENSDomain'); var _LookupRegisteredENSDomain2 = _interopRequireDefault(_LookupRegisteredENSDomain); var _CreateToken = require('./senders/CreateToken'); var _CreateToken2 = _interopRequireDefault(_CreateToken); var _CreateColony = require('./senders/CreateColony'); var _CreateColony2 = _interopRequireDefault(_CreateColony); var _addRecoveryMethods = require('../addRecoveryMethods'); var _addRecoveryMethods2 = _interopRequireDefault(_addRecoveryMethods); var _constants = require('../constants'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* eslint-disable import/no-cycle */ var ColonyNetworkClient = function (_ContractClient) { (0, _inherits3.default)(ColonyNetworkClient, _ContractClient); function ColonyNetworkClient() { (0, _classCallCheck3.default)(this, ColonyNetworkClient); return (0, _possibleConstructorReturn3.default)(this, (ColonyNetworkClient.__proto__ || Object.getPrototypeOf(ColonyNetworkClient)).apply(this, arguments)); } (0, _createClass3.default)(ColonyNetworkClient, [{ key: 'getColonyAddress', /* Get the address of the `Colony` contract with the given `id` */ value: function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(id) { var _ref2, address; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: (0, _assert2.default)(Number.isFinite(id), 'An ID parameter must be provided'); _context.next = 3; return this.getColony.call({ id: id }); case 3: _ref2 = _context.sent; address = _ref2.address; if ((0, _colonyJsUtils.isValidAddress)(address)) { _context.next = 7; break; } throw new Error('Colony contract with ID ' + id + ' could not be found'); case 7: return _context.abrupt('return', address); case 8: case 'end': return _context.stop(); } } }, _callee, this); })); function getColonyAddress(_x) { return _ref.apply(this, arguments); } return getColonyAddress; }() /* Get an initialized ColonyClient for the `Colony` contract with the given `id`. */ }, { key: 'getColonyClient', value: function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(id) { var address; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: (0, _assert2.default)(Number.isFinite(id), 'An ID parameter must be provided'); _context2.next = 3; return this.getColonyAddress(id); case 3: address = _context2.sent; return _context2.abrupt('return', this.getColonyClientByAddress(address)); case 5: case 'end': return _context2.stop(); } } }, _callee2, this); })); function getColonyClient(_x2) { return _ref3.apply(this, arguments); } return getColonyClient; }() /* Get an initialized ColonyClient for the `Colony` contract with the given `contractAddress`. */ }, { key: 'getColonyClientByAddress', value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(contractAddress) { var tokenLockingClient, colonyClient; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.getTokenLockingClient(); case 2: tokenLockingClient = _context3.sent; colonyClient = new this.constructor.ColonyClient({ adapter: this.adapter, networkClient: this, query: { contractAddress: contractAddress }, tokenLockingClient: tokenLockingClient, verbose: this.verbose }); _context3.next = 6; return colonyClient.init(); case 6: return _context3.abrupt('return', colonyClient); case 7: case 'end': return _context3.stop(); } } }, _callee3, this); })); function getColonyClientByAddress(_x3) { return _ref4.apply(this, arguments); } return getColonyClientByAddress; }() /* Get an initialized ColonyClient for the Meta Colony. */ }, { key: 'getMetaColonyClient', value: function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() { var _ref6, address; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return this.getMetaColonyAddress.call(); case 2: _ref6 = _context4.sent; address = _ref6.address; if ((0, _colonyJsUtils.isValidAddress)(address)) { _context4.next = 6; break; } throw new Error('MetaColony contract could not be found'); case 6: return _context4.abrupt('return', this.getMetaColonyClientByAddress(address)); case 7: case 'end': return _context4.stop(); } } }, _callee4, this); })); function getMetaColonyClient() { return _ref5.apply(this, arguments); } return getMetaColonyClient; }() /* Get an initialized ColonyClient for the `Colony` contract with the given `contractAddress`. */ }, { key: 'getMetaColonyClientByAddress', value: function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(contractAddress) { var tokenLockingClient, metaColonyClient; return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.getTokenLockingClient(); case 2: tokenLockingClient = _context5.sent; metaColonyClient = new this.constructor.ColonyClient({ adapter: this.adapter, networkClient: this, query: { contractAddress: contractAddress, contractName: 'IMetaColony' }, tokenLockingClient: tokenLockingClient, verbose: this.verbose }); _context5.next = 6; return metaColonyClient.init(); case 6: return _context5.abrupt('return', metaColonyClient); case 7: case 'end': return _context5.stop(); } } }, _callee5, this); })); function getMetaColonyClientByAddress(_x4) { return _ref7.apply(this, arguments); } return getMetaColonyClientByAddress; }() /* Get an initialized TokenLockingClient. */ }, { key: 'getTokenLockingClient', value: function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() { var _ref9, address; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return this.getTokenLockingAddress.call(); case 2: _ref9 = _context6.sent; address = _ref9.address; if ((0, _colonyJsUtils.isValidAddress)(address)) { _context6.next = 6; break; } throw new Error('TokenLocking contract could not be found'); case 6: return _context6.abrupt('return', this.getTokenLockingClientByAddress(address)); case 7: case 'end': return _context6.stop(); } } }, _callee6, this); })); function getTokenLockingClient() { return _ref8.apply(this, arguments); } return getTokenLockingClient; }() /* Get an initialized TokenLockingClient for the `TokenLocking` contract with the given `contractAddress`. */ }, { key: 'getTokenLockingClientByAddress', value: function () { var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(contractAddress) { var tokenLockingClient; return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: tokenLockingClient = new this.constructor.TokenLockingClient({ adapter: this.adapter, networkClient: this, query: { contractAddress: contractAddress }, verbose: this.verbose }); _context7.next = 3; return tokenLockingClient.init(); case 3: return _context7.abrupt('return', tokenLockingClient); case 4: case 'end': return _context7.stop(); } } }, _callee7, this); })); function getTokenLockingClientByAddress(_x5) { return _ref10.apply(this, arguments); } return getTokenLockingClientByAddress; }() }, { key: 'initializeContractMethods', value: function initializeContractMethods() { var _this2 = this; (0, _addRecoveryMethods2.default)(this); // Custom callers this.lookupRegisteredENSDomain = new _LookupRegisteredENSDomain2.default({ client: this, name: 'lookupRegisteredENSDomain', functionName: 'lookupRegisteredENSDomain', input: [['ensAddress', 'address']], output: [['domain', 'string']] }); // Custom senders this.createToken = new _CreateToken2.default({ client: this }); this.createColonyWithOptions = new _CreateColony2.default({ client: this, name: 'createColonyWithOptions', functionName: 'createColony(address,uint256,string,string,bool)', input: [['tokenAddress', 'address'], ['version', 'number'], ['colonyName', 'string'], ['orbitdb', 'string'], ['useExtensionManager', 'boolean']], defaultValues: { version: _constants.MAX_VERSION, colonyName: '', orbitdb: '', useExtensionManager: false } }); // Events this.addEvent('AuctionCreated', [['auction', 'address'], ['token', 'anyAddress'], ['quantity', 'bigNumber']]); this.addEvent('ColonyAdded', [['colonyId', 'number'], ['colonyAddress', 'address'], ['tokenAddress', 'anyAddress']]); this.addEvent('ColonyLabelRegistered', [['colony', 'address'], ['label', 'string']]); this.addEvent('ColonyNetworkInitialised', [['resolver', 'address']]); this.addEvent('ColonyVersionAdded', [['version', 'number'], ['resolver', 'address']]); this.addEvent('MetaColonyCreated', [['metaColony', 'address'], ['token', 'anyAddress'], ['rootSkillId', 'number']]); this.addEvent('MiningCycleResolverSet', [['miningCycleResolver', 'address']]); this.addEvent('NetworkFeeInverseSet', [['feeInverse', 'number']]); this.addEvent('RecoveryRoleSet', [['address', 'anyAddress'], ['setTo', 'boolean']]); this.addEvent('ReputationMiningCycleComplete', [['hash', 'hexString'], ['nNodes', 'number']]); this.addEvent('ReputationMiningInitialised', [['inactiveReputationMiningCycle', 'address']]); this.addEvent('ReputationRootHashSet', [['newHash', 'hexString'], ['newNNodes', 'number'], ['stakers', 'address'], ['reward', 'bigNumber']]); this.addEvent('SkillAdded', [['skillId', 'number'], ['parentSkillId', 'number']]); this.addEvent('TokenLockingAddressSet', [['tokenLocking', 'address']]); this.addEvent('UserLabelRegistered', [['user', 'address'], ['label', 'string']]); // Callers this.addCaller('calculateMinerWeight', { input: [['timeStaked', 'number'], ['submissionIndexed', 'number']], output: [['minerWeight', 'number']] }); this.addCaller('ensSupportsInterface', { functionName: 'supportsInterface', input: [['interfaceId', 'hexString']], output: [['isSupported', 'boolean']] }); this.addCaller('getAddressForENSHash', { functionName: 'addr', input: [['nameHash', 'hexString']], output: [['ensAddress', 'address']] }); this.addCaller('getChildSkillId', { input: [['skillId', 'number'], ['childSkillIndex', 'number']], output: [['childSkillId', 'number']] }); this.addCaller('getColony', { input: [['id', 'number']], output: [['address', 'address']] }); this.addCaller('getColonyCount', { output: [['count', 'number']] }); this.addCaller('getColonyVersionResolver', { input: [['version', 'number']], output: [['address', 'address']] }); this.addCaller('getCurrentColonyVersion', { output: [['version', 'number']] }); this.addCaller('getENSRegistrarAddress', { functionName: 'getENSRegistrar', output: [['address', 'address']] }); this.addCaller('getFeeInverse', { output: [['feeInverse', 'bigNumber']] }); this.addCaller('getMetaColonyAddress', { functionName: 'getMetaColony', output: [['address', 'address']] }); this.addCaller('getMiningResolverAddress', { functionName: 'getMiningResolver', output: [['address', 'address']] }); this.addCaller('getParentSkillId', { input: [['skillId', 'number'], ['parentSkillIndex', 'number']], output: [['parentSkillId', 'number']] }); this.addCaller('getProfileDBAddress', { input: [['nameHash', 'hexString']], output: [['orbitDBAddress', 'string']] }); this.addCaller('getReplacementReputationUpdateLogEntry', { input: [['reputationMiningCycle', 'address'], ['id', 'number']], output: [['user', 'address'], ['amount', 'bigNumber'], ['skillId', 'number'], ['colony', 'address'], ['nUpdates', 'number'], ['nPreviousUpdates', 'number']] }); this.addCaller('getReplacementReputationUpdateLogsExist', { input: [['reputationMiningCycle', 'address']], output: [['logsExist', 'boolean']] }); this.addCaller('getReputationMiningCycle', { input: [['active', 'boolean']], output: [['address', 'address']] }); this.addCaller('getReputationMiningSkillId', { output: [['skillId', 'number']] }); this.addCaller('getReputationRootHash', { output: [['rootHash', 'string']] }); this.addCaller('getReputationRootHashNodeCount', { functionName: 'getReputationRootHashNNodes', output: [['nNodes', 'number']] }); this.addCaller('getSkill', { input: [['skillId', 'number']], output: [['nParents', 'number'], ['nChildren', 'number'], ['parents', '[number]'], ['children', '[number]'], ['isGlobalSkill', 'boolean']], validateEmpty: function () { var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(_ref11) { var skillId = _ref11.skillId; var _ref13, count; return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return _this2.getSkillCount.call(); case 2: _ref13 = _context8.sent; count = _ref13.count; if (!(skillId > count)) { _context8.next = 6; break; } throw new Error('Skill ID ' + skillId + ' not found'); case 6: return _context8.abrupt('return', true); case 7: case 'end': return _context8.stop(); } } }, _callee8, _this2); })); function validateEmpty(_x6) { return _ref12.apply(this, arguments); } return validateEmpty; }() }); this.addCaller('getSkillCount', { output: [['count', 'number']] }); this.addCaller('getTokenLockingAddress', { functionName: 'getTokenLocking', output: [['address', 'address']] }); this.addCaller('isColony', { input: [['colony', 'address']], output: [['isColony', 'boolean']] }); // Senders this.addSender('addColonyVersion', { input: [['version', 'number'], ['resolver', 'address']] }); this.addSender('addSkill', { input: [['parentSkillId', 'number']] }); this.addSender('appendReputationUpdateLog', { input: [['user', 'address'], ['amount', 'bigNumber'], ['skillId', 'number']] }); this.addSender('createColony', { input: [['tokenAddress', 'address']] }); this.addSender('createMetaColony', { input: [['tokenAddress', 'address']] }); this.addSender('deprecateSkill', { input: [['skillId', 'number']] }); this.addSender('initialiseReputationMining', {}); this.addSender('registerUserLabel', { input: [['username', 'string'], ['orbitDBPath', 'string']], defaultValues: { orbitDBPath: '' } }); this.addSender('setMiningResolver', { input: [['miningResolverAddress', 'address']] }); this.addSender('setTokenLocking', { input: [['tokenLockingAddress', 'address']] }); this.addSender('setFeeInverse', { input: [['feeInverse', 'bigNumber']] }); this.addSender('setReplacementReputationUpdateLogEntry', { input: [['reputationMiningCycle', 'address'], ['id', 'number'], ['user', 'address'], ['amount', 'bigNumber'], ['skillId', 'number'], ['colony', 'address'], ['nUpdates', 'number'], ['nPreviousUpdates', 'number']] }); this.addSender('setReputationRootHash', { input: [['newHash', 'string'], ['newNNodes', 'number'], ['stakers', '[address]'], ['reward', 'number']] }); this.addSender('setupRegistrar', { input: [['ens', 'address'], ['rootNode', 'string']] }); this.addSender('startNextMiningCycle', {}); this.addSender('startTokenAuction', { input: [['tokenAddress', 'anyAddress']] }); } /* Get the reputation of an address within a colony for the given `skillId`. */ }, { key: 'getReputation', value: function () { var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9() { var _ref15 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, colonyAddress = _ref15.colonyAddress, skillId = _ref15.skillId, address = _ref15.address; var _ref16, rootHash, response; return _regenerator2.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: (0, _assert2.default)((0, _colonyJsUtils.isValidAddress)(colonyAddress), 'colonyAddress must be an address'); (0, _assert2.default)(Number.isFinite(skillId), 'skillId must be a number'); (0, _assert2.default)((0, _colonyJsUtils.isValidAddress)(address), 'address must be an address'); // Throw error if private network if (!(typeof this.network === 'undefined')) { _context9.next = 5; break; } throw new Error('This method is not supported on a private network'); case 5: if (!(this.network !== 'goerli' && this.network !== 'mainnet' && this.network !== 'homestead')) { _context9.next = 7; break; } throw new Error('This method is only supported on goerli or mainnet'); case 7: _context9.next = 9; return this.getReputationRootHash.call(); case 9: _ref16 = _context9.sent; rootHash = _ref16.rootHash; _context9.next = 13; return fetch('https://colony.io/reputation/' + this.network + '/' + rootHash + '/' + colonyAddress + '/' + skillId + '/' + address); case 13: response = _context9.sent; return _context9.abrupt('return', response.json()); case 15: case 'end': return _context9.stop(); } } }, _callee9, this); })); function getReputation() { return _ref14.apply(this, arguments); } return getReputation; }() }], [{ key: 'defaultQuery', /* Start the next reputation mining cycle. */ /* Set the token locking address. */ /* Set the root hash of the reputation mining cycle. */ /* Assign a `RECOVERY` role to an address. This function can only be called by an address assigned the `ROOT` role. */ /* Set the inverse amount of the network fee. If the fee is 1% (or 0.01), the inverse amount will be 100. */ /* Register an ENS label for a user. */ /* Check whether or not the network is in recovery mode. */ /* Create an initial inactive reputation mining cycle. */ /* Get the total number of global and local skills in the network. */ /* Get the number of nodes in the current reputation state tree. */ /* Get the ID of the skill associated with reputation mining. */ /* Get whether any replacement log entries have been set for the supplied reputation mining cycle. */ /* Get the total number of addresses that are assigned a `RECOVERY` role. */ /* Get the ID of a parent skill. */ /* Get the Meta Colony contract address. */ /* Get the address of the ENS registrar. */ /* Get the address of the resolver contract for a specific colony version. */ /* Get the colony contract address for a colony. */ /* Get the address of a registered ENS label. This function will return an empty address if an ENS label has not been registered. */ /* Enter network recovery mode. This function can only be called by an address assigned the `RECOVERY` role. */ /* Deprecate a skill. */ /* Create the Meta Colony. */ /* Create a new colony on the network. */ /* Indicate approval to exit network recovery mode. This function can only be called by an address assigned the `RECOVERY` role. */ /* Add a new global or local skill to the skills tree. */ get: function get() { return { contractName: 'IColonyNetwork', routerName: 'EtherRouter' }; } /* Create and start an auction for a token owned by the Colony Network. The auction will be for the total amount of the specificed tokens that are owned by the Colony Network. */ /* Set up the registrar. */ /* Set the value for a storage slot while in recovery mode. This can only be called by an address assigned the `RECOVERY` role. */ /* Set replacement log entry when the network is in recovery mode. */ /* Set the address for the mining resolver. */ /* Remove the `RECOVERY` role from an address. This function can only be called by an address assigned the `ROOT` role. */ /* Lookup the registed ENS label for an address. This function will return an empty string if the address does not have a registered ENS label. */ /* Check whether or not an address is a colony contract. */ /* Get the token locking contract address. */ /* Get information about a skill. */ /* Get the root hash of the current reputation state tree. */ /* Get the address of either the active or inactive reputation mining cycle. The active reputation mining cycle is the one currently under consideration by reputation miners. The inactive reputation cycle is the one with the log that is being appended. */ /* Get replacement log entry that was set when the network was in recovery mode. */ /* Get the address of the OrbitDB database associaated with a user profile. */ /* Get the address of the mining resolver. */ /* Get the inverse amount of the network fee. If the fee is 1% (or 0.01), the inverse amount will be 100. */ /* Get the latest colony contract version. This is the version used to create all new colonies. */ /* Get the total number of colonies on the network. The return value is also the ID of the last colony created. */ /* Get the ID of a child skill. */ /* Exit network recovery mode. This function can be called by anyone if enough whitelist approvals are given. */ /* Check whether an ENS interface is supported. */ /* Create a new ERC20 token contract. */ /* Create a new colony on the network. */ /* Calculate the raw miner weight in WADs. */ /* Add a reputation update entry to the log. */ /* Add a new colony contract version and set the address of the resolver contract. */ }, { key: 'ColonyClient', get: function get() { return _index2.default; } }, { key: 'TokenLockingClient', get: function get() { return _index4.default; } }]); return ColonyNetworkClient; }(_colonyJsContractClient2.default); exports.default = ColonyNetworkClient; //# sourceMappingURL=index.js.map