1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Datokens: check cap before mint (#531)

* compared cap with amount entered to mint
if exceeds an error is thrown

* refactor mint method
created method for estimate mint gas

* Revert "refactor mint method"

This reverts commit 4b370958f267fa3f24074bc40d3d9014c4139e48.

* mint method refactor to pass codeclimate test

* added new Datatokens unit test
created a unit to check if mint method throws error when cap amount exceeds cap
This commit is contained in:
bogdanfazakas 2021-01-13 14:42:29 +02:00 committed by GitHub
parent 34f8b4a9e0
commit 5c4ead9359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 18 deletions

View File

@ -170,28 +170,31 @@ export class DataTokens {
amount: string,
toAddress?: string
): Promise<TransactionReceipt> {
const destAddress = toAddress || address
const datatoken = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, {
from: address
})
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await datatoken.methods
.mint(destAddress, this.web3.utils.toWei(amount))
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
const capAvailble = await this.getCap(dataTokenAddress)
if (parseFloat(capAvailble) >= parseFloat(amount)) {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await datatoken.methods
.mint(toAddress || address, this.web3.utils.toWei(amount))
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
const trxReceipt = await datatoken.methods
.mint(toAddress || address, this.web3.utils.toWei(amount))
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
} else {
throw new Error(`Mint amount exceeds cap available`)
}
const trxReceipt = await datatoken.methods
.mint(destAddress, this.web3.utils.toWei(amount))
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**

View File

@ -65,6 +65,16 @@ describe('DataTokens', () => {
assert(balance === tokenAmount)
})
it('should fail when mint amount exceeds cap', async () => {
const availableCap = await datatoken.getCap(tokenAddress)
const amountExceedingCap = availableCap + 1
try {
await datatoken.mint(tokenAddress, minter, amountExceedingCap)
} catch (e) {
assert(e !== null)
}
})
it('should transfer datatokens', async () => {
await datatoken.transfer(tokenAddress, spender, tokenAmount, minter)
balance = await datatoken.balance(tokenAddress, spender)