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

Remove try...catch from Dispenser

This commit is contained in:
Miquel A. Cabot 2022-06-21 11:59:05 +02:00
parent e5cdeee702
commit bf1eab7df8

View File

@ -17,16 +17,11 @@ export class Dispenser extends SmartContractWithAddress {
* @return {Promise<FixedPricedExchange>} Exchange details * @return {Promise<FixedPricedExchange>} Exchange details
*/ */
public async status(dtAdress: string): Promise<DispenserToken> { public async status(dtAdress: string): Promise<DispenserToken> {
try { const status: DispenserToken = await this.contract.methods.status(dtAdress).call()
const status: DispenserToken = await this.contract.methods.status(dtAdress).call() status.maxTokens = this.web3.utils.fromWei(status.maxTokens)
status.maxTokens = this.web3.utils.fromWei(status.maxTokens) status.maxBalance = this.web3.utils.fromWei(status.maxBalance)
status.maxBalance = this.web3.utils.fromWei(status.maxBalance) status.balance = this.web3.utils.fromWei(status.balance)
status.balance = this.web3.utils.fromWei(status.balance) return status
return status
} catch (e) {
LoggerInstance.warn(`No dispenser available for datatoken: ${dtAdress}`)
}
return null
} }
/** /**
@ -89,32 +84,27 @@ export class Dispenser extends SmartContractWithAddress {
address: string, address: string,
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
try { const estGas = await calculateEstimatedGas(
const estGas = await calculateEstimatedGas( address,
address, this.contract.methods.activate,
this.contract.methods.activate, dtAddress,
this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance)
)
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods
.activate(
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance) this.web3.utils.toWei(maxBalance)
) )
if (estimateGas) return estGas .send({
from: address,
const trxReceipt = await this.contract.methods gas: estGas + 1,
.activate( gasPrice: await this.getFairGasPrice()
dtAddress, })
this.web3.utils.toWei(maxTokens), return trxReceipt
this.web3.utils.toWei(maxBalance)
)
.send({
from: address,
gas: estGas + 1,
gasPrice: await this.getFairGasPrice()
})
return trxReceipt
} catch (e) {
LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
}
return null
} }
/** /**
@ -128,24 +118,19 @@ export class Dispenser extends SmartContractWithAddress {
address: string, address: string,
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
try { const estGas = await calculateEstimatedGas(
const estGas = await calculateEstimatedGas( address,
address, this.contract.methods.deactivate,
this.contract.methods.deactivate, dtAddress
dtAddress )
) if (estimateGas) return estGas
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods.deactivate(dtAddress).send({ const trxReceipt = await this.contract.methods.deactivate(dtAddress).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
gasPrice: await this.getFairGasPrice() gasPrice: await this.getFairGasPrice()
}) })
return trxReceipt return trxReceipt
} catch (e) {
LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
}
return null
} }
/** /**
@ -161,27 +146,22 @@ export class Dispenser extends SmartContractWithAddress {
newAllowedSwapper: string, newAllowedSwapper: string,
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
try { const estGas = await calculateEstimatedGas(
const estGas = await calculateEstimatedGas( address,
address, this.contract.methods.setAllowedSwapper,
this.contract.methods.setAllowedSwapper, dtAddress,
dtAddress, newAllowedSwapper
newAllowedSwapper )
) if (estimateGas) return estGas
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setAllowedSwapper(dtAddress, newAllowedSwapper) .setAllowedSwapper(dtAddress, newAllowedSwapper)
.send({ .send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
gasPrice: await this.getFairGasPrice() gasPrice: await this.getFairGasPrice()
}) })
return trxReceipt return trxReceipt
} catch (e) {
LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
}
return null
} }
/** /**
@ -210,19 +190,14 @@ export class Dispenser extends SmartContractWithAddress {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await this.contract.methods
const trxReceipt = await this.contract.methods .dispense(dtAddress, this.web3.utils.toWei(amount), destination)
.dispense(dtAddress, this.web3.utils.toWei(amount), destination) .send({
.send({ from: address,
from: address, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
}) return trxReceipt
return trxReceipt
} catch (e) {
LoggerInstance.error(`ERROR: Failed to dispense tokens: ${e.message}`)
}
return null
} }
/** /**
@ -243,17 +218,12 @@ export class Dispenser extends SmartContractWithAddress {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await this.contract.methods.ownerWithdraw(dtAddress).send({
const trxReceipt = await this.contract.methods.ownerWithdraw(dtAddress).send({ from: address,
from: address, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
}) return trxReceipt
return trxReceipt
} catch (e) {
LoggerInstance.error(`ERROR: Failed to withdraw tokens: ${e.message}`)
}
return null
} }
/** /**