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

use estimateGas() function in Dispenser

This commit is contained in:
Miquel A. Cabot 2022-04-05 17:41:00 +02:00
parent 2c0f3ae108
commit 09d7683986

View File

@ -8,7 +8,8 @@ import {
LoggerInstance as logger, LoggerInstance as logger,
getFairGasPrice, getFairGasPrice,
configHelperNetworks, configHelperNetworks,
setContractDefaults setContractDefaults,
estimateGas
} from '../../utils/' } from '../../utils/'
import { Datatoken } from '../../tokens' import { Datatoken } from '../../tokens'
import { Config } from '../../models/index.js' import { Config } from '../../models/index.js'
@ -90,23 +91,15 @@ export class Dispenser {
maxBalance: string, maxBalance: string,
allowedSwapper: string allowedSwapper: string
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.dispenserContract.methods.create,
estGas = await this.dispenserContract.methods dtAddress,
.create( this.web3.utils.toWei(maxTokens),
dtAddress, this.web3.utils.toWei(maxBalance),
this.web3.utils.toWei(maxTokens), address,
this.web3.utils.toWei(maxBalance), allowedSwapper
address, )
allowedSwapper
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -125,11 +118,13 @@ export class Dispenser {
maxBalance: string, maxBalance: string,
allowedSwapper: string allowedSwapper: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await this.estGasCreate( const estGas = await estimateGas(
dtAddress, address,
this.dispenserContract.methods.create,
dtAddress,
this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance),
address, address,
maxTokens,
maxBalance,
allowedSwapper allowedSwapper
) )
@ -164,20 +159,13 @@ export class Dispenser {
maxBalance: string, maxBalance: string,
address: string address: string
): Promise<any> { ): Promise<any> {
let estGas return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
try { this.dispenserContract.methods.activate,
estGas = await this.dispenserContract.methods dtAddress,
.activate( this.web3.utils.toWei(maxTokens),
dtAddress, this.web3.utils.toWei(maxBalance)
this.web3.utils.toWei(maxTokens), )
this.web3.utils.toWei(maxBalance)
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -195,7 +183,14 @@ export class Dispenser {
address: string address: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
try { try {
const estGas = await this.estGasActivate(dtAddress, maxTokens, maxBalance, address) const estGas = await estimateGas(
address,
this.dispenserContract.methods.activate,
dtAddress,
this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance)
)
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.dispenserContract.methods
.activate( .activate(
dtAddress, dtAddress,
@ -221,16 +216,7 @@ export class Dispenser {
* @return {Promise<any>} * @return {Promise<any>}
*/ */
public async estGasDeactivate(dtAddress: string, address: string): Promise<any> { public async estGasDeactivate(dtAddress: string, address: string): Promise<any> {
let estGas return estimateGas(address, this.dispenserContract.methods.deactivate, dtAddress)
const gasLimitDefault = this.GASLIMIT_DEFAULT
try {
estGas = await this.dispenserContract.methods
.deactivate(dtAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -244,7 +230,12 @@ export class Dispenser {
address: string address: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
try { try {
const estGas = await this.estGasDeactivate(dtAddress, address) const estGas = await estimateGas(
address,
this.dispenserContract.methods.deactivate,
dtAddress
)
const trxReceipt = await this.dispenserContract.methods.deactivate(dtAddress).send({ const trxReceipt = await this.dispenserContract.methods.deactivate(dtAddress).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
@ -269,16 +260,12 @@ export class Dispenser {
address: string, address: string,
newAllowedSwapper: string newAllowedSwapper: string
): Promise<any> { ): Promise<any> {
let estGas return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
try { this.dispenserContract.methods.setAllowedSwapper,
estGas = await this.dispenserContract.methods dtAddress,
.setAllowedSwapper(dtAddress, newAllowedSwapper) newAllowedSwapper
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -294,11 +281,13 @@ export class Dispenser {
newAllowedSwapper: string newAllowedSwapper: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
try { try {
const estGas = await this.estGasSetAllowedSwapper( const estGas = await estimateGas(
dtAddress,
address, address,
this.dispenserContract.methods.setAllowedSwapper,
dtAddress,
newAllowedSwapper newAllowedSwapper
) )
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.dispenserContract.methods
.setAllowedSwapper(dtAddress, newAllowedSwapper) .setAllowedSwapper(dtAddress, newAllowedSwapper)
.send({ .send({
@ -326,16 +315,13 @@ export class Dispenser {
amount: string = '1', amount: string = '1',
destination: string destination: string
): Promise<any> { ): Promise<any> {
let estGas return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
try { this.dispenserContract.methods.dispense,
estGas = await this.dispenserContract.methods dtAddress,
.dispense(dtAddress, this.web3.utils.toWei(amount), destination) this.web3.utils.toWei(amount),
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) destination
} catch (e) { )
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -354,7 +340,14 @@ export class Dispenser {
amount: string = '1', amount: string = '1',
destination: string destination: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await this.estGasDispense(dtAddress, address, amount, destination) const estGas = await estimateGas(
address,
this.dispenserContract.methods.dispense,
dtAddress,
this.web3.utils.toWei(amount),
destination
)
try { try {
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.dispenserContract.methods
.dispense(dtAddress, this.web3.utils.toWei(amount), destination) .dispense(dtAddress, this.web3.utils.toWei(amount), destination)
@ -378,16 +371,7 @@ export class Dispenser {
* @return {Promise<any>} * @return {Promise<any>}
*/ */
public async estGasOwnerWithdraw(dtAddress: string, address: string): Promise<any> { public async estGasOwnerWithdraw(dtAddress: string, address: string): Promise<any> {
let estGas return estimateGas(address, this.dispenserContract.methods.ownerWithdraw, dtAddress)
const gasLimitDefault = this.GASLIMIT_DEFAULT
try {
estGas = await this.dispenserContract.methods
.ownerWithdraw(dtAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -400,7 +384,12 @@ export class Dispenser {
dtAddress: string, dtAddress: string,
address: string address: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await this.estGasOwnerWithdraw(dtAddress, address) const estGas = await estimateGas(
address,
this.dispenserContract.methods.ownerWithdraw,
dtAddress
)
try { try {
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.dispenserContract.methods
.ownerWithdraw(dtAddress) .ownerWithdraw(dtAddress)