diff --git a/src/pools/Router.ts b/src/pools/Router.ts index 5aa2258e..7187a142 100644 --- a/src/pools/Router.ts +++ b/src/pools/Router.ts @@ -3,7 +3,7 @@ import Web3 from 'web3' import { TransactionReceipt } from 'web3-core' import { AbiItem } from 'web3-utils' 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 { exchangeIds: string @@ -24,7 +24,6 @@ export class Router { public routerAddress: string public RouterABI: AbiItem | AbiItem[] public web3: Web3 - private logger: Logger public startBlock: number public router: Contract @@ -37,18 +36,35 @@ export class Router { constructor( routerAddress: string, web3: Web3, - logger: Logger, RouterABI?: AbiItem | AbiItem[], startBlock?: number ) { this.routerAddress = routerAddress this.RouterABI = RouterABI || (defaultRouter.abi as AbiItem[]) this.web3 = web3 - this.logger = logger this.startBlock = startBlock || 0 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} Transaction receipt + */ + public async estGasBuyDTBatch(address: string, operations: Operations[]): Promise { + 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 * @param {String} address @@ -59,16 +75,7 @@ export class Router { address: string, operations: Operations[] ): Promise { - // Get estimated gas value - 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 - } + const estGas = await this.estGasBuyDTBatch(address, operations) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.buyDTBatch(operations).send({ @@ -122,6 +129,25 @@ export class Router { 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} + */ + public async estGasAddOceanToken(address: string, tokenAddress: string): Promise { + 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 * @param {String} address @@ -136,15 +162,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } + const estGas = await this.estGasAddOceanToken(address, tokenAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addOceanToken(tokenAddress).send({ @@ -156,6 +174,29 @@ export class Router { return trxReceipt } + /** + * Estimate gas cost for removeOceanToken method + * @param {String} address + * @param {String} tokenAddress address to remove + * @return {Promise} + */ + public async estGasRemoveOceanToken( + address: string, + tokenAddress: string + ): Promise { + 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 * @param {String} address @@ -170,15 +211,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } + const estGas = await this.estGasRemoveOceanToken(address, tokenAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.removeOceanToken(tokenAddress).send({ @@ -190,6 +223,26 @@ export class Router { return trxReceipt } + /** + * Estimate gas cost for addSSContract method + * @param {String} address + * @param {String} tokenAddress contract address to add + * @return {Promise} + */ + public async estGasAddSSContract(address: string, tokenAddress: string): Promise { + 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 * @param {String} address @@ -204,16 +257,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } - + const estGas = await this.estGasAddSSContract(address, tokenAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addSSContract(tokenAddress).send({ from: address, @@ -224,6 +268,29 @@ export class Router { return trxReceipt } + /** + * Estimate gas cost for addFixedRateContract method + * @param {String} address + * @param {String} tokenAddress contract address to add + * @return {Promise} + */ + public async estGasAddFixedRateContract( + address: string, + tokenAddress: string + ): Promise { + 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 * @param {String} address @@ -238,15 +305,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } + const estGas = await this.estGasAddFixedRateContract(address, tokenAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addFixedRateContract(tokenAddress).send({ @@ -272,6 +331,26 @@ export class Router { return await this.router.methods.swapOceanFee().call() } + /** + * Estimate gas cost for updateOPFFee method + * @param {String} address + * @param {String} newFee new OPF Fee + * @return {Promise} + */ + public async estGasUpdateOPFFee(address: string, newFee: number): Promise { + 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 * @param {String} address @@ -287,14 +366,7 @@ export class Router { } 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 - } + const estGas = await this.estGasUpdateOPFFee(address, newFee) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.updateOPFFee(newFee).send({ @@ -306,6 +378,29 @@ export class Router { return trxReceipt } + /** + * Estimate gas cost for addPoolTemplate method + * @param {String} address + * @param {String} templateAddress template address to add + * @return {Promise} + */ + public async estGasAddPoolTemplate( + address: string, + templateAddress: string + ): Promise { + 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 * @param {String} address @@ -320,15 +415,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } + const estGas = await this.estGasAddPoolTemplate(address, templateAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addPoolTemplate(templateAddress).send({ @@ -340,6 +427,28 @@ export class Router { return trxReceipt } + /** + * Estimate gas cost for removePoolTemplate method + * @param {String} address + * @param {String} templateAddress template address to remove + * @return {Promise} + */ + public async estGasRemovePoolTemplate( + address: string, + templateAddress: string + ): Promise { + 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 * @param {String} address @@ -354,15 +463,7 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - 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 - } + const estGas = await this.estGasRemovePoolTemplate(address, templateAddress) // Invoke createToken function of the contract const trxReceipt = await this.router.methods diff --git a/test/unit/pools/Router.test.ts b/test/unit/pools/Router.test.ts index 0d9314be..21169aad 100644 --- a/test/unit/pools/Router.test.ts +++ b/test/unit/pools/Router.test.ts @@ -74,7 +74,7 @@ describe('Router unit test', () => { }) 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 () => {