mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add minter roles (#600)
This commit is contained in:
parent
2c32205ede
commit
8cc1ef3e37
@ -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<string>} transactionId
|
||||
*/
|
||||
public async proposeMinter(
|
||||
dataTokenAddress: string,
|
||||
newMinterAddress: string,
|
||||
address: string
|
||||
): Promise<string> {
|
||||
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<string>} transactionId
|
||||
*/
|
||||
public async approveMinter(dataTokenAddress: string, address: string): Promise<string> {
|
||||
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>} string
|
||||
*/
|
||||
public async isMinter(dataTokenAddress: string, address: string): Promise<boolean> {
|
||||
const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress)
|
||||
const trxReceipt = await datatoken.methods.isMinter(address).call()
|
||||
return trxReceipt
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user