diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index cb6fef07..065c2e14 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -498,4 +498,82 @@ export class DataTokens { const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any) return topic } + + /** + * Purpose a new minter + * @param {String} dataTokenAddress + * @param {String} newMinter + * @param {String} address - only current minter can call this + * @return {Promise} transactionId + */ + public async proposeMinter( + dataTokenAddress: string, + newMinterAddress: string, + address: string + ): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { + from: address + }) + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await datatoken.methods + .proposeMinter(newMinterAddress) + .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + } + try { + const trxReceipt = await datatoken.methods.proposeMinter(newMinterAddress).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) + }) + return trxReceipt + } catch (e) { + return null + } + } + + /** + * Approve minter role + * @param {String} dataTokenAddress + * @param {String} address - only proposad minter can call this + * @return {Promise} transactionId + */ + public async approveMinter(dataTokenAddress: string, address: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, { + from: address + }) + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await datatoken.methods + .approveMinter() + .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + } + try { + const trxReceipt = await datatoken.methods.approveMinter().send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) + }) + return trxReceipt + } catch (e) { + return null + } + } + + /** Check if an address has the minter role + * @param {String} dataTokenAddress + * * @param {String} address + * @return {Promise} string + */ + public async isMinter(dataTokenAddress: string, address: string): Promise { + const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress) + const trxReceipt = await datatoken.methods.isMinter(address).call() + return trxReceipt + } } diff --git a/test/unit/Datatokens.test.ts b/test/unit/Datatokens.test.ts index a70bf743..4a4bc957 100644 --- a/test/unit/Datatokens.test.ts +++ b/test/unit/Datatokens.test.ts @@ -11,6 +11,7 @@ const web3 = new Web3('http://127.0.0.1:8545') describe('DataTokens', () => { let minter: string + let newMinter: string let spender: string let balance: string let contracts: TestContractHandler @@ -30,6 +31,7 @@ describe('DataTokens', () => { await contracts.getAccounts() minter = contracts.accounts[0] spender = contracts.accounts[1] + newMinter = contracts.accounts[2] await contracts.deployContracts(minter) }) @@ -90,4 +92,28 @@ describe('DataTokens', () => { balance = await datatoken.balance(tokenAddress, minter) assert(balance === tokenAmount) }) + + it('should check if it has the minter role', async () => { + const isMinter = await datatoken.isMinter(tokenAddress, minter) + assert(isMinter === true) + }) + + it('should propose a new minter', async () => { + const tx = await datatoken.proposeMinter(tokenAddress, newMinter, minter) + assert(tx !== null) + }) + + it('should new minter accept the new role', async () => { + const tx = await datatoken.approveMinter(tokenAddress, newMinter) + assert(tx !== null) + }) + + it('should check if it does not have the minter role any more', async () => { + const isMinter = await datatoken.isMinter(tokenAddress, minter) + assert(isMinter === false) + }) + it('newMinter should check if it has the minter role', async () => { + const isMinter = await datatoken.isMinter(tokenAddress, newMinter) + assert(isMinter === true) + }) })