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

add estimateGas parameter to Dispenser

This commit is contained in:
Miquel A. Cabot 2022-06-13 10:41:54 +02:00
parent a2f2a31289
commit 95d50bdd61

View File

@ -38,13 +38,14 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} allowedSwapper only account that can ask tokens. set address(0) if not required * @param {String} allowedSwapper only account that can ask tokens. set address(0) if not required
* @return {Promise<TransactionReceipt>} transactionId * @return {Promise<TransactionReceipt>} transactionId
*/ */
public async create( public async create<G extends boolean = false>(
dtAddress: string, dtAddress: string,
address: string, address: string,
maxTokens: string, maxTokens: string,
maxBalance: string, maxBalance: string,
allowedSwapper: string allowedSwapper: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.create, this.contract.methods.create,
@ -54,6 +55,7 @@ export class Dispenser extends SmartContractWithAddress {
address, address,
allowedSwapper allowedSwapper
) )
if (estimateGas) return estGas
// Call createFixedRate contract method // Call createFixedRate contract method
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
@ -80,12 +82,13 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} address User address (must be owner of the datatoken) * @param {String} address User address (must be owner of the datatoken)
* @return {Promise<TransactionReceipt>} TransactionReceipt * @return {Promise<TransactionReceipt>} TransactionReceipt
*/ */
public async activate( public async activate<G extends boolean = false>(
dtAddress: string, dtAddress: string,
maxTokens: string, maxTokens: string,
maxBalance: string, maxBalance: string,
address: string address: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
try { try {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
@ -94,6 +97,7 @@ export class Dispenser extends SmartContractWithAddress {
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance) this.web3.utils.toWei(maxBalance)
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.activate( .activate(
@ -119,16 +123,18 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} address User address (must be owner of the datatoken) * @param {String} address User address (must be owner of the datatoken)
* @return {Promise<TransactionReceipt>} TransactionReceipt * @return {Promise<TransactionReceipt>} TransactionReceipt
*/ */
public async deactivate( public async deactivate<G extends boolean = false>(
dtAddress: string, dtAddress: string,
address: string address: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
try { 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
const trxReceipt = await this.contract.methods.deactivate(dtAddress).send({ const trxReceipt = await this.contract.methods.deactivate(dtAddress).send({
from: address, from: address,
@ -149,11 +155,12 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} newAllowedSwapper refers to the new allowedSwapper * @param {String} newAllowedSwapper refers to the new allowedSwapper
* @return {Promise<TransactionReceipt>} TransactionReceipt * @return {Promise<TransactionReceipt>} TransactionReceipt
*/ */
public async setAllowedSwapper( public async setAllowedSwapper<G extends boolean = false>(
dtAddress: string, dtAddress: string,
address: string, address: string,
newAllowedSwapper: string newAllowedSwapper: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
try { try {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
@ -161,6 +168,7 @@ export class Dispenser extends SmartContractWithAddress {
dtAddress, dtAddress,
newAllowedSwapper newAllowedSwapper
) )
if (estimateGas) return estGas
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setAllowedSwapper(dtAddress, newAllowedSwapper) .setAllowedSwapper(dtAddress, newAllowedSwapper)
@ -186,12 +194,13 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} destination who will receive the tokens * @param {String} destination who will receive the tokens
* @return {Promise<TransactionReceipt>} TransactionReceipt * @return {Promise<TransactionReceipt>} TransactionReceipt
*/ */
public async dispense( public async dispense<G extends boolean = false>(
dtAddress: string, dtAddress: string,
address: string, address: string,
amount: string = '1', amount: string = '1',
destination: string destination: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.dispense, this.contract.methods.dispense,
@ -199,6 +208,7 @@ export class Dispenser extends SmartContractWithAddress {
this.web3.utils.toWei(amount), this.web3.utils.toWei(amount),
destination destination
) )
if (estimateGas) return estGas
try { try {
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
@ -221,15 +231,17 @@ export class Dispenser extends SmartContractWithAddress {
* @param {String} address User address (must be owner of the dispenser) * @param {String} address User address (must be owner of the dispenser)
* @return {Promise<TransactionReceipt>} TransactionReceipt * @return {Promise<TransactionReceipt>} TransactionReceipt
*/ */
public async ownerWithdraw( public async ownerWithdraw<G extends boolean = false>(
dtAddress: string, dtAddress: string,
address: string address: string,
): Promise<TransactionReceipt> { estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> {
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
this.contract.methods.ownerWithdraw, this.contract.methods.ownerWithdraw,
dtAddress dtAddress
) )
if (estimateGas) return estGas
try { try {
const trxReceipt = await this.contract.methods.ownerWithdraw(dtAddress).send({ const trxReceipt = await this.contract.methods.ownerWithdraw(dtAddress).send({