mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
use estimateGas() function in Router
This commit is contained in:
parent
00fc5d21e9
commit
c936c47260
@ -3,7 +3,12 @@ import Web3 from 'web3'
|
|||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from 'web3-core'
|
||||||
import { AbiItem } from 'web3-utils'
|
import { AbiItem } from 'web3-utils'
|
||||||
import defaultRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
|
import defaultRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
|
||||||
import { getFairGasPrice, setContractDefaults, configHelperNetworks } from '../utils'
|
import {
|
||||||
|
getFairGasPrice,
|
||||||
|
setContractDefaults,
|
||||||
|
configHelperNetworks,
|
||||||
|
estimateGas
|
||||||
|
} from '../utils'
|
||||||
import { Operation } from '../@types/Router'
|
import { Operation } from '../@types/Router'
|
||||||
import { Config } from '../models/index.js'
|
import { Config } from '../models/index.js'
|
||||||
|
|
||||||
@ -47,16 +52,7 @@ export class Router {
|
|||||||
* @return {Promise<TransactionReceipt>} Transaction receipt
|
* @return {Promise<TransactionReceipt>} Transaction receipt
|
||||||
*/
|
*/
|
||||||
public async estGasBuyDTBatch(address: string, operations: Operation[]): Promise<any> {
|
public async estGasBuyDTBatch(address: string, operations: Operation[]): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.buyDTBatch, operations)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.buyDTBatch(operations)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,7 +65,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
operations: Operation[]
|
operations: Operation[]
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const estGas = await this.estGasBuyDTBatch(address, operations)
|
const estGas = await estimateGas(address, this.router.methods.buyDTBatch, operations)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.buyDTBatch(operations).send({
|
const trxReceipt = await this.router.methods.buyDTBatch(operations).send({
|
||||||
@ -134,19 +130,8 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string,
|
tokenAddress: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
) {
|
): Promise<any> {
|
||||||
const routerContract = contractInstance || this.router
|
return estimateGas(address, this.router.methods.addApprovedToken, tokenAddress)
|
||||||
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await routerContract.methods
|
|
||||||
.addApprovedToken(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -163,7 +148,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasAddApprovedToken(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.addApprovedToken,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.addApprovedToken(tokenAddress).send({
|
const trxReceipt = await this.router.methods.addApprovedToken(tokenAddress).send({
|
||||||
@ -186,19 +175,8 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string,
|
tokenAddress: string,
|
||||||
contractInstance?: Contract
|
contractInstance?: Contract
|
||||||
) {
|
): Promise<any> {
|
||||||
const routerContract = contractInstance || this.router
|
return estimateGas(address, this.router.methods.removeApprovedToken, tokenAddress)
|
||||||
|
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await routerContract.methods
|
|
||||||
.removeApprovedToken(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -215,7 +193,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasRemoveApprovedToken(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.removeApprovedToken,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.removeApprovedToken(tokenAddress).send({
|
const trxReceipt = await this.router.methods.removeApprovedToken(tokenAddress).send({
|
||||||
@ -234,17 +216,7 @@ export class Router {
|
|||||||
* @return {Promise<TransactionReceipt>}
|
* @return {Promise<TransactionReceipt>}
|
||||||
*/
|
*/
|
||||||
public async estGasAddSSContract(address: string, tokenAddress: string): Promise<any> {
|
public async estGasAddSSContract(address: string, tokenAddress: string): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.addSSContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.addSSContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -261,7 +233,12 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasAddSSContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.addSSContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.addSSContract(tokenAddress).send({
|
const trxReceipt = await this.router.methods.addSSContract(tokenAddress).send({
|
||||||
from: address,
|
from: address,
|
||||||
@ -282,17 +259,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.removeSSContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.removeSSContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -309,7 +276,12 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasRemoveSSContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.removeSSContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.removeSSContract(tokenAddress).send({
|
const trxReceipt = await this.router.methods.removeSSContract(tokenAddress).send({
|
||||||
from: address,
|
from: address,
|
||||||
@ -330,17 +302,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.addFixedRateContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.addFixedRateContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -357,7 +319,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasAddFixedRateContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.addFixedRateContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.addFixedRateContract(tokenAddress).send({
|
const trxReceipt = await this.router.methods.addFixedRateContract(tokenAddress).send({
|
||||||
@ -379,17 +345,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.removeFixedRateContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.removeFixedRateContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -406,7 +362,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasRemoveFixedRateContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.removeFixedRateContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke removeFixedRateContract function of the contract
|
// Invoke removeFixedRateContract function of the contract
|
||||||
const trxReceipt = await this.router.methods
|
const trxReceipt = await this.router.methods
|
||||||
@ -430,17 +390,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.addDispenserContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.addDispenserContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -457,7 +407,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasAddDispenserContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.addDispenserContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.addDispenserContract(tokenAddress).send({
|
const trxReceipt = await this.router.methods.addDispenserContract(tokenAddress).send({
|
||||||
@ -479,17 +433,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.removeDispenserContract, tokenAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.removeDispenserContract(tokenAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -506,7 +450,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasRemoveDispenserContract(address, tokenAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.removeDispenserContract,
|
||||||
|
tokenAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods
|
const trxReceipt = await this.router.methods
|
||||||
@ -547,17 +495,14 @@ export class Router {
|
|||||||
newConsumeFee: number,
|
newConsumeFee: number,
|
||||||
newProviderFee: number
|
newProviderFee: number
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(
|
||||||
let estGas
|
address,
|
||||||
try {
|
this.router.methods.updateOPCFee,
|
||||||
estGas = await this.router.methods
|
newSwapOceanFee,
|
||||||
.updateOPCFee(newSwapOceanFee, newSwapNonOceanFee, newConsumeFee, newProviderFee)
|
newSwapNonOceanFee,
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
newConsumeFee,
|
||||||
} catch (e) {
|
newProviderFee
|
||||||
estGas = gasLimitDefault
|
)
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -580,8 +525,9 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasUpdateOPCFee(
|
const estGas = await estimateGas(
|
||||||
address,
|
address,
|
||||||
|
this.router.methods.updateOPCFee,
|
||||||
newSwapOceanFee,
|
newSwapOceanFee,
|
||||||
newSwapNonOceanFee,
|
newSwapNonOceanFee,
|
||||||
newConsumeFee,
|
newConsumeFee,
|
||||||
@ -610,17 +556,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
templateAddress: string
|
templateAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.addPoolTemplate, templateAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.addPoolTemplate(templateAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -637,7 +573,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasAddPoolTemplate(address, templateAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.addPoolTemplate,
|
||||||
|
templateAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods.addPoolTemplate(templateAddress).send({
|
const trxReceipt = await this.router.methods.addPoolTemplate(templateAddress).send({
|
||||||
@ -659,16 +599,7 @@ export class Router {
|
|||||||
address: string,
|
address: string,
|
||||||
templateAddress: string
|
templateAddress: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
return estimateGas(address, this.router.methods.removePoolTemplate, templateAddress)
|
||||||
let estGas
|
|
||||||
try {
|
|
||||||
estGas = await this.router.methods
|
|
||||||
.removePoolTemplate(templateAddress)
|
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
return estGas
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -685,7 +616,11 @@ export class Router {
|
|||||||
throw new Error(`Caller is not Router Owner`)
|
throw new Error(`Caller is not Router Owner`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const estGas = await this.estGasRemovePoolTemplate(address, templateAddress)
|
const estGas = await estimateGas(
|
||||||
|
address,
|
||||||
|
this.router.methods.removePoolTemplate,
|
||||||
|
templateAddress
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke createToken function of the contract
|
// Invoke createToken function of the contract
|
||||||
const trxReceipt = await this.router.methods
|
const trxReceipt = await this.router.methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user