diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 79d036ac6..20100d128 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -9,7 +9,9 @@ import { generateDtName, getFreCreationParams, getErcCreationParams, - getPoolCreationParams + getPoolCreationParams, + configHelperNetworks, + setContractDefaults } from '../utils' import { FreCreationParams, @@ -17,6 +19,7 @@ import { PoolCreationParams, DispenserCreationParams } from '../interfaces' +import { Config } from '../models/index.js' import { ProviderFees } from '../@types/index.js' interface Template { @@ -47,7 +50,7 @@ export class NftFactory { public factory721Address: string public factory721Abi: AbiItem | AbiItem[] public web3: Web3 - public startBlock: number + public config: Config public factory721: Contract /** @@ -60,15 +63,15 @@ export class NftFactory { factory721Address: string, web3: Web3, factory721Abi?: AbiItem | AbiItem[], - startBlock?: number + config?: Config ) { this.factory721Address = factory721Address this.factory721Abi = factory721Abi || (defaultFactory721Abi.abi as AbiItem[]) this.web3 = web3 - this.startBlock = startBlock || 0 - this.factory721 = new this.web3.eth.Contract( - this.factory721Abi, - this.factory721Address + this.config = config || configHelperNetworks[0] + this.factory721 = setContractDefaults( + new this.web3.eth.Contract(this.factory721Abi, this.factory721Address), + this.config ) } @@ -135,7 +138,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) let tokenAddress = null @@ -277,7 +280,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -333,7 +336,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -390,7 +393,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -444,7 +447,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -503,7 +506,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -563,7 +566,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -618,7 +621,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -674,7 +677,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -739,7 +742,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -807,7 +810,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -873,7 +876,7 @@ export class NftFactory { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt diff --git a/src/models/Config.ts b/src/models/Config.ts index faf4d559f..7b6c12bbb 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -170,6 +170,30 @@ export class Config { * @type {string} */ oceanTokenSymbol: string + + /** + * Specify the transaction Block Timeout + * @type {number} + */ + transactionBlockTimeout: number + + /** + * Specify the transaction Confirmation Blocks + * @type {number} + */ + transactionConfirmationBlocks: number + + /** + * Specify the transaction Polling Blocks Timeout + * @type {number} + */ + transactionPollingTimeout: number + + /** + * Specify the multiplier for the gas fee + * @type {number} + */ + gasFeeMultiplier: number } export default Config diff --git a/src/pools/Router.ts b/src/pools/Router.ts index 37f4108d7..a365d6e43 100644 --- a/src/pools/Router.ts +++ b/src/pools/Router.ts @@ -3,8 +3,9 @@ import Web3 from 'web3' import { TransactionReceipt } from 'web3-core' import { AbiItem } from 'web3-utils' import defaultRouter from '../artifacts/pools/FactoryRouter.sol/FactoryRouter.json' -import { getFairGasPrice } from '../utils' +import { getFairGasPrice, setContractDefaults, configHelperNetworks } from '../utils' import { Operation } from '../interfaces/RouterInterface' +import { Config } from '../models/index.js' /** * Provides an interface for FactoryRouter contract @@ -14,7 +15,7 @@ export class Router { public routerAddress: string public RouterAbi: AbiItem | AbiItem[] public web3: Web3 - public startBlock: number + public config: Config public router: Contract /** @@ -27,13 +28,16 @@ export class Router { routerAddress: string, web3: Web3, RouterAbi?: AbiItem | AbiItem[], - startBlock?: number + config?: Config ) { this.routerAddress = routerAddress this.RouterAbi = RouterAbi || (defaultRouter.abi as AbiItem[]) this.web3 = web3 - this.startBlock = startBlock || 0 - this.router = new this.web3.eth.Contract(this.RouterAbi, this.routerAddress) + this.config = config || configHelperNetworks[0] + this.router = setContractDefaults( + new this.web3.eth.Contract(this.RouterAbi, this.routerAddress), + this.config + ) } /** @@ -71,7 +75,7 @@ export class Router { const trxReceipt = await this.router.methods.buyDTBatch(operations).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -165,7 +169,7 @@ export class Router { const trxReceipt = await this.router.methods.addOceanToken(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -217,7 +221,7 @@ export class Router { const trxReceipt = await this.router.methods.removeOceanToken(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -262,7 +266,7 @@ export class Router { const trxReceipt = await this.router.methods.addSSContract(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -310,7 +314,7 @@ export class Router { const trxReceipt = await this.router.methods.removeSSContract(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -359,7 +363,7 @@ export class Router { const trxReceipt = await this.router.methods.addFixedRateContract(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -410,7 +414,7 @@ export class Router { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -459,7 +463,7 @@ export class Router { const trxReceipt = await this.router.methods.addDispenserContract(tokenAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -510,7 +514,7 @@ export class Router { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -571,7 +575,7 @@ export class Router { const trxReceipt = await this.router.methods.updateOPFFee(newFee, newFee).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -620,7 +624,7 @@ export class Router { const trxReceipt = await this.router.methods.addPoolTemplate(templateAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -670,7 +674,7 @@ export class Router { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 0e1499d73..c5950060d 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -2,7 +2,13 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Contract } from 'web3-eth-contract' -import { Logger, getFairGasPrice, LoggerInstance } from '../../utils' +import { + Logger, + getFairGasPrice, + LoggerInstance, + configHelperNetworks, + setContractDefaults +} from '../../utils' import BigNumber from 'bignumber.js' import PoolTemplate from '../../artifacts/pools/balancer/BPool.sol/BPool.json' import defaultErc20Abi from '../../artifacts/templates/ERC20Template.sol/ERC20Template.json' @@ -13,24 +19,31 @@ import { AmountsInMaxFee, AmountsOutMaxFee } from '../../interfaces' +import { Config } from '../../models' const MaxUint256 = '115792089237316195423570985008687907853269984665640564039457584007913129639934' /** * Provides an interface to Ocean friendly fork from Balancer BPool */ -// TODO: Add decimals handling export class Pool { public poolAbi: AbiItem | AbiItem[] public web3: Web3 public GASLIMIT_DEFAULT = 1000000 private logger: Logger - - constructor(web3: Web3, logger: Logger, poolAbi: AbiItem | AbiItem[] = null) { + private config: Config + + constructor( + web3: Web3, + logger: Logger, + poolAbi: AbiItem | AbiItem[] = null, + config?: Config + ) { if (poolAbi) this.poolAbi = poolAbi else this.poolAbi = PoolTemplate.abi as AbiItem[] this.web3 = web3 this.logger = logger + this.config = config || configHelperNetworks[0] } /** @@ -52,7 +65,10 @@ export class Pool { ): Promise { const tokenContract = contractInstance || - new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], tokenAddress) + setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], tokenAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -79,7 +95,10 @@ export class Pool { spender: string ): Promise { const tokenAbi = defaultErc20Abi.abi as AbiItem[] - const datatoken = new this.web3.eth.Contract(tokenAbi, tokenAddress) + const datatoken = setContractDefaults( + new this.web3.eth.Contract(tokenAbi, tokenAddress), + this.config + ) const trxReceipt = await datatoken.methods.allowance(owner, spender).call() return await this.unitsToAmount(tokenAddress, trxReceipt) @@ -125,7 +144,10 @@ export class Pool { type: 'function' } ] as AbiItem[] - const token = new this.web3.eth.Contract(minABI, tokenAddress) + const token = setContractDefaults( + new this.web3.eth.Contract(minABI, tokenAddress), + this.config + ) if (!force) { const currentAllowence = await this.allowance(tokenAddress, account, spender) if (new Decimal(currentAllowence).greaterThanOrEqualTo(new Decimal(amount))) { @@ -140,7 +162,7 @@ export class Pool { result = await token.methods.approve(spender, amountFormatted).send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`) @@ -157,7 +179,10 @@ export class Pool { async sharesBalance(account: string, poolAddress: string): Promise { let result = null try { - const token = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const token = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const balance = await token.methods.balanceOf(account).call() result = this.web3.utils.fromWei(balance) } catch (e) { @@ -184,7 +209,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -209,9 +237,12 @@ export class Pool { poolAddress: string, fee: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress, { - from: account - }) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress, { + from: account + }), + this.config + ) let result = null const estGas = await this.estSetSwapFee(account, poolAddress, fee) @@ -219,7 +250,7 @@ export class Pool { result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({ from: account, gas: estGas, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to set pool swap fee: ${e.message}`) @@ -233,7 +264,10 @@ export class Pool { * @return {String} */ async getNumTokens(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getNumTokens().call() @@ -249,7 +283,10 @@ export class Pool { * @return {String} */ async getPoolSharesTotalSupply(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let amount = null try { const result = await pool.methods.totalSupply().call() @@ -266,7 +303,10 @@ export class Pool { * @return {String[]} */ async getCurrentTokens(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getCurrentTokens().call() @@ -282,7 +322,10 @@ export class Pool { * @return {String[]} */ async getFinalTokens(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getFinalTokens().call() @@ -298,7 +341,10 @@ export class Pool { * @return {String} */ async getController(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getController().call() @@ -314,7 +360,10 @@ export class Pool { * @return {String} */ async getBaseToken(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getBaseTokenAddress().call() @@ -330,7 +379,10 @@ export class Pool { * @return {String} */ async getDatatoken(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.getDatatokenAddress().call() @@ -346,7 +398,10 @@ export class Pool { * @return {String} */ async getMarketFeeCollector(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods._publishMarketCollector().call() @@ -362,7 +417,10 @@ export class Pool { * @return {String} */ async getOPFCollector(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods._opfCollector().call() @@ -379,7 +437,10 @@ export class Pool { * @return {Boolean} */ async isBound(poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.isBound(token).call() @@ -399,7 +460,10 @@ export class Pool { async getReserve(poolAddress: string, token: string): Promise { let amount = null try { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const result = await pool.methods.getBalance(token).call() amount = await this.unitsToAmount(token, result) } catch (e) { @@ -415,7 +479,10 @@ export class Pool { * @return {Boolean} */ async isFinalized(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null try { result = await pool.methods.isFinalized().call() @@ -431,7 +498,10 @@ export class Pool { * @return {String} Swap fee. To get the percentage value, substract by 100. E.g. `0.1` represents a 10% swap fee. */ async getSwapFee(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let fee = null try { const result = await pool.methods.getSwapFee().call() @@ -449,7 +519,10 @@ export class Pool { * @return {String} */ async getNormalizedWeight(poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let weight = null try { const result = await pool.methods.getNormalizedWeight(token).call() @@ -467,7 +540,10 @@ export class Pool { * @return {String} */ async getDenormalizedWeight(poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let weight = null try { const result = await pool.methods.getDenormalizedWeight(token).call() @@ -484,7 +560,10 @@ export class Pool { * @return {String} */ async getTotalDenormalizedWeight(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let weight = null try { const result = await pool.methods.getTotalDenormalizedWeight().call() @@ -502,7 +581,10 @@ export class Pool { * @return {String} */ async getMarketFees(poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let weight = null try { const result = await pool.methods.publishMarketFees(token).call() @@ -518,7 +600,10 @@ export class Pool { * @return {CurrentFees} */ async getCurrentMarketFees(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) try { const currentMarketFees = await pool.methods.getCurrentOPFFees().call() return currentMarketFees @@ -532,7 +617,10 @@ export class Pool { * @return {CurrentFees} */ async getCurrentOPFFees(poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) try { const currentMarketFees = await pool.methods.getCurrentOPFFees().call() return currentMarketFees @@ -548,7 +636,10 @@ export class Pool { * @return {String} */ async getCommunityFees(poolAddress: string, token: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let weight = null try { const result = await pool.methods.communityFees(token).call() @@ -573,7 +664,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -594,7 +688,10 @@ export class Pool { * @return {TransactionReceipt} */ async collectOPF(address: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const estGas = await this.estCollectOPF(address, poolAddress) @@ -602,7 +699,7 @@ export class Pool { result = await pool.methods.collectOPF().send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) @@ -625,7 +722,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -653,7 +753,10 @@ export class Pool { if ((await this.getMarketFeeCollector(poolAddress)) !== address) { throw new Error(`Caller is not MarketFeeCollector`) } - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const estGas = await this.estCollectMarketFee(address, poolAddress) @@ -661,7 +764,7 @@ export class Pool { result = await pool.methods.collectMarketFee().send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) @@ -685,7 +788,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -714,7 +820,10 @@ export class Pool { if ((await this.getMarketFeeCollector(poolAddress)) !== address) { throw new Error(`Caller is not MarketFeeCollector`) } - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const estGas = await this.estUpdateMarketFeeCollector( address, @@ -726,7 +835,7 @@ export class Pool { result = await pool.methods.updateMarketFeeCollector(newCollector).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) @@ -736,9 +845,9 @@ export class Pool { async amountToUnits(token: string, amount: string): Promise { try { - const tokenContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - token + const tokenContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), + this.config ) let decimals = await tokenContract.methods.decimals().call() if (decimals === '0') { @@ -754,9 +863,9 @@ export class Pool { async unitsToAmount(token: string, amount: string): Promise { try { - const tokenContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - token + const tokenContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), + this.config ) let decimals = await tokenContract.methods.decimals().call() if (decimals === '0') { @@ -788,7 +897,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const maxPrice = amountsInOutMaxFee.maxPrice ? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice) @@ -836,7 +948,10 @@ export class Pool { tokenInOutMarket: TokenInOutMarket, amountsInOutMaxFee: AmountsInMaxFee ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) amountsInOutMaxFee.tokenAmountIn = await this.amountToUnits( tokenInOutMarket.tokenIn, @@ -879,7 +994,7 @@ export class Pool { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) @@ -906,7 +1021,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -951,7 +1069,10 @@ export class Pool { tokenInOutMarket: TokenInOutMarket, amountsInOutMaxFee: AmountsOutMaxFee ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null amountsInOutMaxFee.maxAmountIn = await this.amountToUnits( @@ -993,7 +1114,7 @@ export class Pool { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to swap exact amount out: ${e.message}`) @@ -1019,7 +1140,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1047,7 +1171,10 @@ export class Pool { poolAmountOut: string, maxAmountsIn: string[] ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const weiMaxAmountsIn = [] const tokens = await this.getFinalTokens(poolAddress) @@ -1071,7 +1198,7 @@ export class Pool { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to join pool: ${e.message}`) @@ -1097,7 +1224,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1125,7 +1255,10 @@ export class Pool { poolAmountIn: string, minAmountsOut: string[] ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const weiMinAmountsOut = [] const tokens = await this.getFinalTokens(poolAddress) @@ -1144,7 +1277,11 @@ export class Pool { try { result = await pool.methods .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) - .send({ from: account, gas: estGas, gasPrice: await getFairGasPrice(this.web3) }) + .send({ + from: account, + gas: estGas, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) } catch (e) { this.logger.error(`ERROR: Failed to exit pool: ${e.message}`) } @@ -1171,7 +1308,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1201,7 +1341,10 @@ export class Pool { tokenAmountIn: string, minPoolAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn) @@ -1223,7 +1366,7 @@ export class Pool { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to pay tokens in order to \ @@ -1252,7 +1395,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1282,7 +1428,10 @@ export class Pool { poolAmountOut: string, maxAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn) @@ -1304,7 +1453,7 @@ export class Pool { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error('ERROR: Failed to join swap pool amount out') @@ -1332,7 +1481,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1362,7 +1514,10 @@ export class Pool { poolAmountIn: string, minTokenAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const minTokenOutFormatted = await this.amountToUnits(tokenOut, minTokenAmountOut) @@ -1384,7 +1539,7 @@ export class Pool { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${e.message}`) @@ -1412,7 +1567,10 @@ export class Pool { ): Promise { const poolContract = contractInstance || - new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress) + setContractDefaults( + new this.web3.eth.Contract(this.poolAbi as AbiItem[], poolAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1442,7 +1600,10 @@ export class Pool { tokenAmountOut: string, maxPoolAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let result = null const estGas = await this.estExitswapExternAmountOut( @@ -1463,7 +1624,7 @@ export class Pool { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { this.logger.error('ERROR: Failed to exitswapExternAmountOut') @@ -1485,17 +1646,20 @@ export class Pool { tokenOut: string, swapMarketFee: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let decimalsTokenIn = 18 let decimalsTokenOut = 18 - const tokenInContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - tokenIn + const tokenInContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], tokenIn), + this.config ) - const tokenOutContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - tokenOut + const tokenOutContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], tokenOut), + this.config ) try { decimalsTokenIn = await tokenInContract.methods.decimals().call() @@ -1539,7 +1703,10 @@ export class Pool { tokenAmountOut: string, swapMarketFee: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const amountOutFormatted = await this.amountToUnits(tokenOut, tokenAmountOut) @@ -1568,7 +1735,10 @@ export class Pool { tokenAmountIn: string, swapMarketFee: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn) @@ -1596,7 +1766,10 @@ export class Pool { tokenIn: string, tokenAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let amount = null try { @@ -1616,7 +1789,10 @@ export class Pool { tokenIn: string, poolAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let amount = null const amountFormatted = await this.amountToUnits(poolAddress, poolAmountOut) try { @@ -1637,7 +1813,10 @@ export class Pool { tokenOut: string, poolAmountIn: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let amount = null try { @@ -1659,7 +1838,10 @@ export class Pool { tokenOut: string, tokenAmountOut: string ): Promise { - const pool = new this.web3.eth.Contract(this.poolAbi, poolAddress) + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) let amount = null try { diff --git a/src/pools/dispenser/Dispenser.ts b/src/pools/dispenser/Dispenser.ts index 339cde40b..393596176 100644 --- a/src/pools/dispenser/Dispenser.ts +++ b/src/pools/dispenser/Dispenser.ts @@ -4,8 +4,14 @@ import { Contract } from 'web3-eth-contract' import { TransactionReceipt } from 'web3-eth' import Decimal from 'decimal.js' import defaultDispenserAbi from '../../artifacts/pools/dispenser/Dispenser.sol/Dispenser.json' -import { LoggerInstance as logger, getFairGasPrice } from '../../utils/' +import { + LoggerInstance as logger, + getFairGasPrice, + configHelperNetworks, + setContractDefaults +} from '../../utils/' import { Datatoken } from '../../tokens' +import { Config } from '../../models/index.js' export interface DispenserToken { active: boolean @@ -21,7 +27,7 @@ export class Dispenser { public GASLIMIT_DEFAULT = 1000000 public web3: Web3 = null public dispenserAddress: string - public startBlock: number + public config: Config public dispenserAbi: AbiItem | AbiItem[] public dispenserContract: Contract @@ -35,17 +41,16 @@ export class Dispenser { web3: Web3, dispenserAddress: string = null, dispenserAbi: AbiItem | AbiItem[] = null, - startBlock?: number + config?: Config ) { this.web3 = web3 this.dispenserAddress = dispenserAddress - if (startBlock) this.startBlock = startBlock - else this.startBlock = 0 this.dispenserAbi = dispenserAbi || (defaultDispenserAbi.abi as AbiItem[]) + this.config = config || configHelperNetworks[0] if (web3) - this.dispenserContract = new this.web3.eth.Contract( - this.dispenserAbi, - this.dispenserAddress + this.dispenserContract = setContractDefaults( + new this.web3.eth.Contract(this.dispenserAbi, this.dispenserAddress), + this.config ) } @@ -140,7 +145,7 @@ export class Dispenser { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -200,7 +205,7 @@ export class Dispenser { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -243,7 +248,7 @@ export class Dispenser { const trxReceipt = await this.dispenserContract.methods.deactivate(dtAddress).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -299,7 +304,7 @@ export class Dispenser { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -356,7 +361,7 @@ export class Dispenser { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -402,7 +407,7 @@ export class Dispenser { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index e63fd9612..871e584ac 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -5,7 +5,13 @@ import { TransactionReceipt } from 'web3-core' import { Contract } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' -import { LoggerInstance, getFairGasPrice } from '../../utils' +import { + LoggerInstance, + getFairGasPrice, + configHelperNetworks, + setContractDefaults +} from '../../utils' +import { Config } from '../../models/index.js' export interface FixedPriceExchange { active: boolean @@ -54,7 +60,7 @@ export class FixedRateExchange { public web3: Web3 public contract: Contract = null - public startBlock: number + public config: Config public ssAbi: AbiItem | AbiItem[] /** @@ -67,27 +73,25 @@ export class FixedRateExchange { fixedRateAddress: string, fixedRateExchangeAbi: AbiItem | AbiItem[] = null, oceanAddress: string = null, - startBlock?: number + config?: Config ) { this.web3 = web3 - - if (startBlock) this.startBlock = startBlock - else this.startBlock = 0 + this.config = config || configHelperNetworks[0] this.fixedRateExchangeAbi = fixedRateExchangeAbi || (defaultFixedRateExchangeAbi.abi as AbiItem[]) this.oceanAddress = oceanAddress this.fixedRateAddress = fixedRateAddress - this.contract = new this.web3.eth.Contract( - this.fixedRateExchangeAbi, - this.fixedRateAddress + this.contract = setContractDefaults( + new this.web3.eth.Contract(this.fixedRateExchangeAbi, this.fixedRateAddress), + this.config ) } async amountToUnits(token: string, amount: string): Promise { let decimals = 18 - const tokenContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - token + const tokenContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), + this.config ) try { @@ -103,9 +107,9 @@ export class FixedRateExchange { async unitsToAmount(token: string, amount: string): Promise { let decimals = 18 - const tokenContract = new this.web3.eth.Contract( - defaultErc20Abi.abi as AbiItem[], - token + const tokenContract = setContractDefaults( + new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), + this.config ) try { decimals = await tokenContract.methods.decimals().call() @@ -202,7 +206,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -277,7 +281,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -346,7 +350,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -396,7 +400,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -445,7 +449,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -494,7 +498,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -721,7 +725,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -772,7 +776,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -820,7 +824,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.collectBT(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -867,7 +871,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.collectDT(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -914,7 +918,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -961,7 +965,7 @@ export class FixedRateExchange { const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -1058,7 +1062,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -1112,7 +1116,7 @@ export class FixedRateExchange { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } diff --git a/src/pools/ssContracts/SideStaking.ts b/src/pools/ssContracts/SideStaking.ts index 62b6705d4..df4415d02 100644 --- a/src/pools/ssContracts/SideStaking.ts +++ b/src/pools/ssContracts/SideStaking.ts @@ -2,20 +2,28 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Contract } from 'web3-eth-contract' -import { LoggerInstance, getFairGasPrice } from '../../utils' +import { + LoggerInstance, + getFairGasPrice, + ConfigHelper, + configHelperNetworks +} from '../../utils' import BigNumber from 'bignumber.js' import SideStakingTemplate from '../../artifacts/pools/ssContracts/SideStaking.sol/SideStaking.json' import defaultErc20Abi from '../../artifacts/templates/ERC20Template.sol/ERC20Template.json' +import { Config } from '../../models' export class SideStaking { public ssAbi: AbiItem | AbiItem[] public web3: Web3 public GASLIMIT_DEFAULT = 1000000 + public config: Config - constructor(web3: Web3, ssAbi: AbiItem | AbiItem[] = null) { + constructor(web3: Web3, ssAbi: AbiItem | AbiItem[] = null, config?: Config) { if (ssAbi) this.ssAbi = ssAbi else this.ssAbi = SideStakingTemplate.abi as AbiItem[] this.web3 = web3 + this.config = config || configHelperNetworks[0] } async amountToUnits(token: string, amount: string): Promise { @@ -322,7 +330,7 @@ export class SideStaking { result = await sideStaking.methods.getVesting(datatokenAddress).send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { LoggerInstance.error('ERROR: Failed to join swap pool amount out') @@ -392,7 +400,7 @@ export class SideStaking { .send({ from: account, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) } catch (e) { LoggerInstance.error('ERROR: Failed to join swap pool amount out') diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index 15e68f550..7cb5543f6 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -5,9 +5,15 @@ import { Contract } from 'web3-eth-contract' import Decimal from 'decimal.js' import defaultDatatokensAbi from '../artifacts/templates/ERC20Template.sol/ERC20Template.json' import defaultDatatokensEnterpriseAbi from '../artifacts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' -import { LoggerInstance, getFairGasPrice } from '../utils' +import { + LoggerInstance, + getFairGasPrice, + setContractDefaults, + configHelperNetworks +} from '../utils' import { FreOrderParams, FreCreationParams } from '../interfaces' import { Nft } from './NFT' +import { Config } from '../models/index.js' import { ProviderFees } from '../@types/Provider.js' /** * ERC20 ROLES @@ -37,7 +43,7 @@ export class Datatoken { public datatokensAbi: AbiItem | AbiItem[] public datatokensEnterpriseAbi: AbiItem | AbiItem[] public web3: Web3 - public startBlock: number + public config: Config public nft: Nft /** @@ -49,13 +55,13 @@ export class Datatoken { web3: Web3, datatokensAbi?: AbiItem | AbiItem[], datatokensEnterpriseAbi?: AbiItem | AbiItem[], - startBlock?: number + config?: Config ) { this.web3 = web3 this.datatokensAbi = datatokensAbi || (defaultDatatokensAbi.abi as AbiItem[]) this.datatokensEnterpriseAbi = datatokensEnterpriseAbi || (defaultDatatokensEnterpriseAbi.abi as AbiItem[]) - this.startBlock = startBlock || 0 + this.config = config || configHelperNetworks[0] this.nft = new Nft(this.web3) } @@ -76,7 +82,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) // Estimate gas cost for mint method const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -105,7 +115,10 @@ export class Datatoken { amount: string, address: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const estGas = await this.estGasApprove( dtAddress, @@ -121,7 +134,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -143,7 +156,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -174,7 +191,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -222,7 +243,10 @@ export class Datatoken { address: string, fixedRateParams: FreCreationParams ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if (!(await this.isERC20Deployer(dtAddress, address))) { throw new Error(`User is not ERC20 Deployer`) } @@ -261,7 +285,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -283,7 +307,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if (!dispenserParams.allowedSwapper) dispenserParams.allowedSwapper = '0x0000000000000000000000000000000000000000' @@ -327,7 +355,10 @@ export class Datatoken { throw new Error(`User is not ERC20 Deployer`) } - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if (!dispenserParams.allowedSwapper) dispenserParams.allowedSwapper = '0x0000000000000000000000000000000000000000' @@ -356,7 +387,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -375,7 +406,10 @@ export class Datatoken { amount: string, toAddress?: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.getDTPermissions(dtAddress, address)).minter !== true) { throw new Error(`Caller is not Minter`) @@ -397,7 +431,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } else { @@ -420,7 +454,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) // Estimate gas cost for addMinter method const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -448,7 +486,10 @@ export class Datatoken { address: string, minter: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.isERC20Deployer(dtAddress, address)) !== true) { throw new Error(`Caller is not ERC20Deployer`) @@ -460,7 +501,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.addMinter(minter).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -481,7 +522,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) // should check ERC20Deployer role using erc721 level .. @@ -513,7 +558,10 @@ export class Datatoken { address: string, minter: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.isERC20Deployer(dtAddress, address)) !== true) { throw new Error(`Caller is not ERC20Deployer`) @@ -525,7 +573,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.removeMinter(minter).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -546,7 +594,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) // Estimate gas for addFeeManager method const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -575,7 +627,10 @@ export class Datatoken { address: string, paymentManager: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.isERC20Deployer(dtAddress, address)) !== true) { throw new Error(`Caller is not ERC20Deployer`) @@ -592,7 +647,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.addPaymentManager(paymentManager).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -613,7 +668,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -640,7 +699,10 @@ export class Datatoken { address: string, paymentManager: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.isERC20Deployer(dtAddress, address)) !== true) { throw new Error(`Caller is not ERC20Deployer`) @@ -659,7 +721,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -680,7 +742,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -708,7 +774,10 @@ export class Datatoken { address: string, paymentCollector: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) if ((await this.getDTPermissions(dtAddress, address)).paymentManager !== true) { throw new Error(`Caller is not Fee Manager`) } @@ -726,7 +795,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -737,7 +806,10 @@ export class Datatoken { * @return {Promise} */ public async getPaymentCollector(dtAddress: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const paymentCollector = await dtContract.methods.getPaymentCollector().call() return paymentCollector } @@ -777,7 +849,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -805,7 +881,10 @@ export class Datatoken { amount: string, address: string ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) try { const estGas = await this.estGasTransfer( dtAddress, @@ -818,7 +897,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.transfer(toAddress, amount).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -845,7 +924,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) // Estimate gas for startOrder method const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -875,8 +958,11 @@ export class Datatoken { serviceIndex: number, providerFees: ProviderFees ): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) - + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) + try { const estGas = await this.estGasStartOrder( dtAddress, @@ -892,7 +978,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -961,7 +1047,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -1030,7 +1116,7 @@ export class Datatoken { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } catch (e) { @@ -1053,7 +1139,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1084,7 +1174,10 @@ export class Datatoken { throw new Error(`User is not ERC20 Deployer`) } - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const estGas = await this.estGasSetData(dtAddress, address, value, dtContract) @@ -1092,7 +1185,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.setData(value).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -1110,7 +1203,11 @@ export class Datatoken { contractInstance?: Contract ): Promise { const dtContract = - contractInstance || new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1139,7 +1236,10 @@ export class Datatoken { if ((await this.nft.getNftOwner(await this.getNFTAddress(dtAddress))) !== address) { throw new Error('Caller is NOT Nft Owner') } - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const estGas = await this.estGasCleanPermissions(dtAddress, address, dtContract) @@ -1147,7 +1247,7 @@ export class Datatoken { const trxReceipt = await dtContract.methods.cleanPermissions().send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -1159,7 +1259,10 @@ export class Datatoken { * @return {Promise} */ public async getDTPermissions(dtAddress: string, address: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const roles = await dtContract.methods.permissions(address).call() return roles } @@ -1169,7 +1272,10 @@ export class Datatoken { * @return {Promise} */ public async getCap(dtAddress: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const cap = await dtContract.methods.cap().call() return this.web3.utils.fromWei(cap) } @@ -1179,7 +1285,10 @@ export class Datatoken { * @return {Promise} */ public async getDecimals(dtAddress: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const decimals = await dtContract.methods.decimals().call() return decimals } @@ -1189,7 +1298,10 @@ export class Datatoken { * @return {Promise} */ public async getNFTAddress(dtAddress: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const nftAddress = await dtContract.methods.getERC721Address().call() return nftAddress } @@ -1200,7 +1312,10 @@ export class Datatoken { * @return {Promise} */ public async isERC20Deployer(dtAddress: string, address: string): Promise { - const dtContract = new this.web3.eth.Contract(this.datatokensAbi, dtAddress) + const dtContract = setContractDefaults( + new this.web3.eth.Contract(this.datatokensAbi, dtAddress), + this.config + ) const isERC20Deployer = await dtContract.methods.isERC20Deployer(address).call() return isERC20Deployer } diff --git a/src/tokens/NFT.ts b/src/tokens/NFT.ts index 8fc33c270..beaf3dda4 100644 --- a/src/tokens/NFT.ts +++ b/src/tokens/NFT.ts @@ -2,9 +2,16 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils' import { TransactionReceipt } from 'web3-eth' import defaultNftAbi from '../artifacts/templates/ERC721Template.sol/ERC721Template.json' -import { LoggerInstance, getFairGasPrice, generateDtName } from '../utils' +import { + LoggerInstance, + getFairGasPrice, + generateDtName, + setContractDefaults, + configHelperNetworks +} from '../utils' import { Contract } from 'web3-eth-contract' import { MetadataProof } from '../../src/@types' +import { Config } from '../models/index.js' /** * ERC721 ROLES @@ -23,11 +30,12 @@ export class Nft { public nftAbi: AbiItem | AbiItem[] public web3: Web3 public startBlock: number + public config: Config - constructor(web3: Web3, nftAbi?: AbiItem | AbiItem[], startBlock?: number) { + constructor(web3: Web3, nftAbi?: AbiItem | AbiItem[], config?: Config) { this.nftAbi = nftAbi || (defaultNftAbi.abi as AbiItem[]) this.web3 = web3 - this.startBlock = startBlock || 0 + this.config = config || configHelperNetworks[0] } /** @@ -61,7 +69,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { @@ -119,7 +131,10 @@ export class Nft { } // Create 721contract object - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const estGas = await this.estGasCreateErc20( nftAddress, @@ -148,7 +163,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) let tokenAddress = null @@ -175,7 +190,11 @@ export class Nft { contractInstance?: Contract ) { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -197,7 +216,10 @@ export class Nft { * @return {Promise} trxReceipt */ public async addManager(nftAddress: string, address: string, manager: string) { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftOwner(nftAddress)) !== address) { throw new Error(`Caller is not NFT Owner`) @@ -209,7 +231,7 @@ export class Nft { const trxReceipt = await nftContract.methods.addManager(manager).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -230,7 +252,11 @@ export class Nft { contractInstance?: Contract ) { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { @@ -251,7 +277,10 @@ export class Nft { * @return {Promise} trxReceipt */ public async removeManager(nftAddress: string, address: string, manager: string) { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftOwner(nftAddress)) !== address) { throw new Error(`Caller is not NFT Owner`) @@ -268,7 +297,7 @@ export class Nft { const trxReceipt = await nftContract.methods.removeManager(manager).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -289,7 +318,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { @@ -315,7 +348,10 @@ export class Nft { address: string, erc20Deployer: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftPermissions(nftAddress, address)).manager !== true) { throw new Error(`Caller is not Manager`) @@ -335,7 +371,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -356,7 +392,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -383,7 +423,10 @@ export class Nft { address: string, erc20Deployer: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ( (await this.getNftPermissions(nftAddress, address)).manager !== true || @@ -405,7 +448,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -426,7 +469,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -452,7 +499,10 @@ export class Nft { address: string, metadataUpdater: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftPermissions(nftAddress, address)).manager !== true) { throw new Error(`Caller is not Manager`) @@ -469,7 +519,7 @@ export class Nft { const trxReceipt = await nftContract.methods.addToMetadataList(metadataUpdater).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -490,7 +540,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -517,7 +571,10 @@ export class Nft { address: string, metadataUpdater: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ( (await this.getNftPermissions(nftAddress, address)).manager !== true || @@ -540,7 +597,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -561,7 +618,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -587,7 +648,10 @@ export class Nft { address: string, storeUpdater: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftPermissions(nftAddress, address)).manager !== true) { throw new Error(`Caller is not Manager`) @@ -604,7 +668,7 @@ export class Nft { const trxReceipt = await nftContract.methods.addTo725StoreList(storeUpdater).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -625,7 +689,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -651,7 +719,10 @@ export class Nft { address: string, storeUpdater: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ( (await this.getNftPermissions(nftAddress, address)).manager !== true || @@ -674,7 +745,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -693,7 +764,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -721,7 +796,10 @@ export class Nft { nftAddress: string, address: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftOwner(nftAddress)) !== address) { throw new Error(`Caller is not NFT Owner`) @@ -733,7 +811,7 @@ export class Nft { const trxReceipt = await nftContract.methods.cleanPermissions().send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -756,7 +834,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -788,7 +870,10 @@ export class Nft { nftReceiver: string, tokenId?: number ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftOwner(nftAddress)) !== nftOwner) { throw new Error(`Caller is not NFT Owner`) @@ -810,7 +895,7 @@ export class Nft { .send({ from: nftOwner, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -833,7 +918,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -865,7 +954,10 @@ export class Nft { nftReceiver: string, tokenId?: number ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if ((await this.getNftOwner(nftAddress)) !== nftOwner) { throw new Error(`Caller is not NFT Owner`) @@ -887,7 +979,7 @@ export class Nft { .send({ from: nftOwner, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -917,7 +1009,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -960,7 +1056,10 @@ export class Nft { metadataHash: string, metadataProofs?: MetadataProof[] ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if (!metadataProofs) metadataProofs = [] if (!(await this.getNftPermissions(nftAddress, address)).updateMetadata) { throw new Error(`Caller is not Metadata updater`) @@ -990,7 +1089,7 @@ export class Nft { .send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -1011,7 +1110,11 @@ export class Nft { contractInstance?: Contract ): Promise { const nftContract = - contractInstance || new this.web3.eth.Contract(this.nftAbi, nftAddress) + contractInstance || + setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1040,7 +1143,10 @@ export class Nft { address: string, metadataState: number ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) if (!(await this.getNftPermissions(nftAddress, address)).updateMetadata) { throw new Error(`Caller is not Metadata updater`) @@ -1052,7 +1158,7 @@ export class Nft { const trxReceipt = await nftContract.methods.setMetaDataState(metadataState).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt @@ -1069,7 +1175,10 @@ export class Nft { address: string, data: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas @@ -1095,13 +1204,16 @@ export class Nft { address: string, data: string ): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const estGas = await this.estSetTokenURI(nftAddress, address, data) const trxReceipt = await nftContract.methods.setTokenURI('1', data).send({ from: address, gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3) + gasPrice: await getFairGasPrice(this.web3, this.config) }) return trxReceipt } @@ -1111,7 +1223,10 @@ export class Nft { * @return {Promise} string */ public async getNftOwner(nftAddress: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const trxReceipt = await nftContract.methods.ownerOf(1).call() return trxReceipt } @@ -1122,7 +1237,10 @@ export class Nft { * @return {Promise} */ public async getNftPermissions(nftAddress: string, address: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const roles = await nftContract.methods.getPermissions(address).call() return roles } @@ -1132,7 +1250,10 @@ export class Nft { * @return {Promise} */ public async getMetadata(nftAddress: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) return await nftContract.methods.getMetaData().call() } @@ -1142,7 +1263,10 @@ export class Nft { * @return {Promise} */ public async isErc20Deployer(nftAddress: string, address: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const isERC20Deployer = await nftContract.methods.isERC20Deployer(address).call() return isERC20Deployer } @@ -1153,7 +1277,10 @@ export class Nft { * @return {Promise} The data stored at the key */ public async getData(nftAddress: string, key: string): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const data = await nftContract.methods.getData(key).call() return data } @@ -1164,7 +1291,10 @@ export class Nft { * @return {Promise} The data stored at the key */ public async getTokenURI(nftAddress: string, id: number): Promise { - const nftContract = new this.web3.eth.Contract(this.nftAbi, nftAddress) + const nftContract = setContractDefaults( + new this.web3.eth.Contract(this.nftAbi, nftAddress), + this.config + ) const data = await nftContract.methods.tokenURI(id).call() return data } diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 98278a6f7..0ef433868 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -17,7 +17,11 @@ const configHelperNetworksBase: Config = { poolTemplateAddress: null, fixedRateExchangeAddress: null, dispenserAddress: null, - startBlock: 0 + startBlock: 0, + transactionBlockTimeout: 50, + transactionConfirmationBlocks: 1, + transactionPollingTimeout: 750, + gasFeeMultiplier: 1 } export const configHelperNetworks: Config[] = [ @@ -59,7 +63,11 @@ export const configHelperNetworks: Config[] = [ providerUri: 'https://provider.mainnet.oceanprotocol.com', subgraphUri: 'https://subgraph.mainnet.oceanprotocol.com', explorerUri: 'https://etherscan.io', - startBlock: 11105459 + startBlock: 11105459, + transactionBlockTimeout: 150, + transactionConfirmationBlocks: 5, + transactionPollingTimeout: 1750, + gasFeeMultiplier: 1.05 }, { ...configHelperNetworksBase, @@ -117,7 +125,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://bsc-dataseed.binance.org', providerUri: 'https://provider.bsc.oceanprotocol.com', subgraphUri: 'https://subgraph.bsc.oceanprotocol.com', - explorerUri: 'https://bscscan.com/' + explorerUri: 'https://bscscan.com/', + gasFeeMultiplier: 1.05 }, { ...configHelperNetworksBase, @@ -135,7 +144,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://rpc.energyweb.org', providerUri: 'https://provider.energyweb.oceanprotocol.com', subgraphUri: 'https://subgraph.energyweb.oceanprotocol.com', - explorerUri: 'https://explorer.energyweb.org' + explorerUri: 'https://explorer.energyweb.org', + gasFeeMultiplier: 1.05 }, { ...configHelperNetworksBase, @@ -144,7 +154,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://moonriver.api.onfinality.io/public', providerUri: 'https://provider.moonriver.oceanprotocol.com', subgraphUri: 'https://subgraph.moonriver.oceanprotocol.com', - explorerUri: 'https://blockscout.moonriver.moonbeam.network' + explorerUri: 'https://blockscout.moonriver.moonbeam.network', + gasFeeMultiplier: 1.05 } ] diff --git a/src/utils/ContractParams.ts b/src/utils/ContractUtils.ts similarity index 68% rename from src/utils/ContractParams.ts rename to src/utils/ContractUtils.ts index 909d6ef2a..419ddabac 100644 --- a/src/utils/ContractParams.ts +++ b/src/utils/ContractUtils.ts @@ -1,6 +1,31 @@ -import { Erc20CreateParams, FreCreationParams, PoolCreationParams } from '../interfaces' import Web3 from 'web3' +import BigNumber from 'bignumber.js' +import { Contract } from 'web3-eth-contract' import { generateDtName } from './DatatokenName' +import { Erc20CreateParams, FreCreationParams, PoolCreationParams } from '../interfaces' +import { Config } from '../models' + +export function setContractDefaults(contract: Contract, config: Config): Contract { + if (config) { + if (config.transactionBlockTimeout) + contract.transactionBlockTimeout = config.transactionBlockTimeout + if (config.transactionConfirmationBlocks) + contract.transactionConfirmationBlocks = config.transactionConfirmationBlocks + if (config.transactionPollingTimeout) + contract.transactionPollingTimeout = config.transactionPollingTimeout + } + return contract +} + +export async function getFairGasPrice(web3: Web3, config: Config): Promise { + const x = new BigNumber(await web3.eth.getGasPrice()) + if (config && config.gasFeeMultiplier) + return x + .multipliedBy(config.gasFeeMultiplier) + .integerValue(BigNumber.ROUND_DOWN) + .toString(10) + else return x.toString(10) +} export function getErcCreationParams(ercParams: Erc20CreateParams): any { let name: string, symbol: string diff --git a/src/utils/GasUtils.ts b/src/utils/GasUtils.ts deleted file mode 100644 index 90b907db6..000000000 --- a/src/utils/GasUtils.ts +++ /dev/null @@ -1,7 +0,0 @@ -import BigNumber from 'bignumber.js' -import Web3 from 'web3' - -export async function getFairGasPrice(web3: Web3): Promise { - const x = new BigNumber(await web3.eth.getGasPrice()) - return x.multipliedBy(1.05).integerValue(BigNumber.ROUND_DOWN).toString(10) -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 53e07b209..ceffbf1d0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,6 @@ export * from './Logger' -export * from './GasUtils' export * from './DatatokenName' -export * from './ContractParams' +export * from './ContractUtils' export * from './FetchHelper' export * from './ConfigHelper' export * from './DdoHelpers'