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

split estGas for the methods in Router class

This commit is contained in:
Bogdan Fazakas 2021-10-26 23:34:15 +03:00
parent 4fde8b29fd
commit 21081cc386
2 changed files with 179 additions and 78 deletions

View File

@ -3,7 +3,7 @@ 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 { Logger, getFairGasPrice, generateDtName } from '../utils' import { LoggerInstance, getFairGasPrice } from '../utils'
interface Operations { interface Operations {
exchangeIds: string exchangeIds: string
@ -24,7 +24,6 @@ export class Router {
public routerAddress: string public routerAddress: string
public RouterABI: AbiItem | AbiItem[] public RouterABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
private logger: Logger
public startBlock: number public startBlock: number
public router: Contract public router: Contract
@ -37,18 +36,35 @@ export class Router {
constructor( constructor(
routerAddress: string, routerAddress: string,
web3: Web3, web3: Web3,
logger: Logger,
RouterABI?: AbiItem | AbiItem[], RouterABI?: AbiItem | AbiItem[],
startBlock?: number startBlock?: number
) { ) {
this.routerAddress = routerAddress this.routerAddress = routerAddress
this.RouterABI = RouterABI || (defaultRouter.abi as AbiItem[]) this.RouterABI = RouterABI || (defaultRouter.abi as AbiItem[])
this.web3 = web3 this.web3 = web3
this.logger = logger
this.startBlock = startBlock || 0 this.startBlock = startBlock || 0
this.router = new this.web3.eth.Contract(this.RouterABI, this.routerAddress) this.router = new this.web3.eth.Contract(this.RouterABI, this.routerAddress)
} }
/**
* Estimate gas cost for buyDTBatch method
* @param {String} address
* @param {Operations} operations Operations objects array
* @return {Promise<TransactionReceipt>} Transaction receipt
*/
public async estGasBuyDTBatch(address: string, operations: Operations[]): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.buyDTBatch(operations)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* BuyDTBatch * BuyDTBatch
* @param {String} address * @param {String} address
@ -59,16 +75,7 @@ export class Router {
address: string, address: string,
operations: Operations[] operations: Operations[]
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
// Get estimated gas value const estGas = await this.estGasBuyDTBatch(address, operations)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.buyDTBatch(operations)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// 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({
@ -122,6 +129,25 @@ export class Router {
return await this.router.methods.isPoolTemplate(address).call() return await this.router.methods.isPoolTemplate(address).call()
} }
/**
* Estimate gas cost for addOceanToken method
* @param {String} address
* @param {String} tokenAddress template address to add
* @return {Promise<TransactionReceipt>}
*/
public async estGasAddOceanToken(address: string, tokenAddress: string): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.addOceanToken(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Add a new token to oceanTokens list, pools with basetoken in this list have NO opf Fee * Add a new token to oceanTokens list, pools with basetoken in this list have NO opf Fee
* @param {String} address * @param {String} address
@ -136,15 +162,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasAddOceanToken(address, tokenAddress)
let estGas
try {
estGas = await this.router.methods
.addOceanToken(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.router.methods.addOceanToken(tokenAddress).send({ const trxReceipt = await this.router.methods.addOceanToken(tokenAddress).send({
@ -156,6 +174,29 @@ export class Router {
return trxReceipt return trxReceipt
} }
/**
* Estimate gas cost for removeOceanToken method
* @param {String} address
* @param {String} tokenAddress address to remove
* @return {Promise<TransactionReceipt>}
*/
public async estGasRemoveOceanToken(
address: string,
tokenAddress: string
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.removeOceanToken(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Remove a token from oceanTokens list, pools without basetoken in this list have a opf Fee * Remove a token from oceanTokens list, pools without basetoken in this list have a opf Fee
* @param {String} address * @param {String} address
@ -170,15 +211,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasRemoveOceanToken(address, tokenAddress)
let estGas
try {
estGas = await this.router.methods
.removeOceanToken(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.router.methods.removeOceanToken(tokenAddress).send({ const trxReceipt = await this.router.methods.removeOceanToken(tokenAddress).send({
@ -190,6 +223,26 @@ export class Router {
return trxReceipt return trxReceipt
} }
/**
* Estimate gas cost for addSSContract method
* @param {String} address
* @param {String} tokenAddress contract address to add
* @return {Promise<TransactionReceipt>}
*/
public async estGasAddSSContract(address: string, tokenAddress: string): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.addSSContract(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Add a new contract to ssContract list, after is added, can be used when deploying a new pool * Add a new contract to ssContract list, after is added, can be used when deploying a new pool
* @param {String} address * @param {String} address
@ -204,16 +257,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasAddSSContract(address, tokenAddress)
let estGas
try {
estGas = await this.router.methods
.addSSContract(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// 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,
@ -224,6 +268,29 @@ export class Router {
return trxReceipt return trxReceipt
} }
/**
* Estimate gas cost for addFixedRateContract method
* @param {String} address
* @param {String} tokenAddress contract address to add
* @return {Promise<TransactionReceipt>}
*/
public async estGasAddFixedRateContract(
address: string,
tokenAddress: string
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.addFixedRateContract(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Add a new contract to fixedRate list, after is added, can be used when deploying a new pool * Add a new contract to fixedRate list, after is added, can be used when deploying a new pool
* @param {String} address * @param {String} address
@ -238,15 +305,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasAddFixedRateContract(address, tokenAddress)
let estGas
try {
estGas = await this.router.methods
.addFixedRateContract(tokenAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// 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({
@ -272,6 +331,26 @@ export class Router {
return await this.router.methods.swapOceanFee().call() return await this.router.methods.swapOceanFee().call()
} }
/**
* Estimate gas cost for updateOPFFee method
* @param {String} address
* @param {String} newFee new OPF Fee
* @return {Promise<TransactionReceipt>}
*/
public async estGasUpdateOPFFee(address: string, newFee: number): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.updateOPFFee(newFee)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Add a new contract to fixedRate list, after is added, can be used when deploying a new pool * Add a new contract to fixedRate list, after is added, can be used when deploying a new pool
* @param {String} address * @param {String} address
@ -287,14 +366,7 @@ export class Router {
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas const estGas = await this.estGasUpdateOPFFee(address, newFee)
try {
estGas = await this.router.methods
.updateOPFFee(newFee)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.router.methods.updateOPFFee(newFee).send({ const trxReceipt = await this.router.methods.updateOPFFee(newFee).send({
@ -306,6 +378,29 @@ export class Router {
return trxReceipt return trxReceipt
} }
/**
* Estimate gas cost for addPoolTemplate method
* @param {String} address
* @param {String} templateAddress template address to add
* @return {Promise<TransactionReceipt>}
*/
public async estGasAddPoolTemplate(
address: string,
templateAddress: string
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.addPoolTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Add a new template to poolTemplates mapping, after template is added,it can be used * Add a new template to poolTemplates mapping, after template is added,it can be used
* @param {String} address * @param {String} address
@ -320,15 +415,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasAddPoolTemplate(address, templateAddress)
let estGas
try {
estGas = await this.router.methods
.addPoolTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// 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({
@ -340,6 +427,28 @@ export class Router {
return trxReceipt return trxReceipt
} }
/**
* Estimate gas cost for removePoolTemplate method
* @param {String} address
* @param {String} templateAddress template address to remove
* @return {Promise<TransactionReceipt>}
*/
public async estGasRemovePoolTemplate(
address: string,
templateAddress: string
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.router.methods
.removePoolTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/** /**
* Remove template from poolTemplates mapping, after template is removed,it can be used anymore * Remove template from poolTemplates mapping, after template is removed,it can be used anymore
* @param {String} address * @param {String} address
@ -354,15 +463,7 @@ export class Router {
throw new Error(`Caller is not Router Owner`) throw new Error(`Caller is not Router Owner`)
} }
const gasLimitDefault = this.GASLIMIT_DEFAULT const estGas = await this.estGasRemovePoolTemplate(address, templateAddress)
let estGas
try {
estGas = await this.router.methods
.removePoolTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.router.methods const trxReceipt = await this.router.methods

View File

@ -74,7 +74,7 @@ describe('Router unit test', () => {
}) })
it('should initiate Router instance', async () => { it('should initiate Router instance', async () => {
router = new Router(contracts.routerAddress, web3, LoggerInstance) router = new Router(contracts.routerAddress, web3)
}) })
it('#getOwner - should return actual owner', async () => { it('#getOwner - should return actual owner', async () => {