From 2c8832e22c05021bb6935ced46629cfeb1d72057 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 6 Jun 2022 17:43:39 +0200 Subject: [PATCH] inherit FixedRateExchange from SmartContract --- src/contracts/pools/FixedRateExchange.ts | 249 +++++++----------- test/integration/CodeExamples.test.ts | 4 +- .../pools/fixedRate/FixedRateExchange.test.ts | 4 +- 3 files changed, 94 insertions(+), 163 deletions(-) diff --git a/src/contracts/pools/FixedRateExchange.ts b/src/contracts/pools/FixedRateExchange.ts index ef1217b7..5e298831 100644 --- a/src/contracts/pools/FixedRateExchange.ts +++ b/src/contracts/pools/FixedRateExchange.ts @@ -1,67 +1,14 @@ -import defaultFixedRateExchangeAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' +import FixedRateExchangeAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import { TransactionReceipt } from 'web3-core' import { Contract } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' -import Web3 from 'web3' -import { - LoggerInstance, - getFairGasPrice, - setContractDefaults, - amountToUnits, - unitsToAmount, - estimateGas, - ZERO_ADDRESS -} from '../../utils' -import { Config, ConfigHelper } from '../../config' +import { LoggerInstance, getFairGasPrice, estimateGas, ZERO_ADDRESS } from '../../utils' import { PriceAndFees, FeesInfo, FixedPriceExchange } from '../../@types' +import { SmartContractWithAddress } from '..' -export class FixedRateExchange { - /** Ocean related functions */ - public fixedRateAddress: string - public fixedRateExchangeAbi: AbiItem | AbiItem[] - public web3: Web3 - public fixedRateContract: Contract = null - - public config: Config - public ssAbi: AbiItem | AbiItem[] - - /** - * Instantiate FixedRateExchange - * @param {any} web3 - * @param {any} fixedRateExchangeAbi - */ - constructor( - web3: Web3, - fixedRateAddress: string, - network?: string | number, - fixedRateExchangeAbi: AbiItem | AbiItem[] = null, - config?: Config - ) { - this.web3 = web3 - this.config = config || new ConfigHelper().getConfig(network || 'unknown') - this.fixedRateExchangeAbi = - fixedRateExchangeAbi || (defaultFixedRateExchangeAbi.abi as AbiItem[]) - this.fixedRateAddress = fixedRateAddress - this.fixedRateContract = setContractDefaults( - new this.web3.eth.Contract(this.fixedRateExchangeAbi, this.fixedRateAddress), - this.config - ) - } - - async amountToUnits( - token: string, - amount: string, - tokenDecimals: number - ): Promise { - return amountToUnits(this.web3, token, amount, tokenDecimals) - } - - async unitsToAmount( - token: string, - amount: string, - tokenDecimals: number - ): Promise { - return unitsToAmount(this.web3, token, amount, tokenDecimals) +export class FixedRateExchange extends SmartContractWithAddress { + getDefaultAbi(): AbiItem | AbiItem[] { + return FixedRateExchangeAbi.abi as AbiItem[] } /** @@ -71,7 +18,7 @@ export class FixedRateExchange { * @return {Promise} exchangeId */ public async generateExchangeId(baseToken: string, datatoken: string): Promise { - const exchangeId = await this.fixedRateContract.methods + const exchangeId = await this.contract.methods .generateExchangeId(baseToken, datatoken) .call() return exchangeId @@ -96,7 +43,7 @@ export class FixedRateExchange { consumeMarketFee: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -142,7 +89,7 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.buyDT, + this.contract.methods.buyDT, exchangeId, dtAmountFormatted, maxBtFormatted, @@ -150,7 +97,7 @@ export class FixedRateExchange { consumeMarketFeeFormatted ) try { - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .buyDT( exchangeId, dtAmountFormatted, @@ -189,7 +136,7 @@ export class FixedRateExchange { consumeMarketFee: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -234,7 +181,7 @@ export class FixedRateExchange { ) const estGas = await estimateGas( address, - this.fixedRateContract.methods.sellDT, + this.contract.methods.sellDT, exchangeId, dtAmountFormatted, minBtFormatted, @@ -242,7 +189,7 @@ export class FixedRateExchange { consumeMarketFeeFormatted ) try { - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .sellDT( exchangeId, dtAmountFormatted, @@ -269,9 +216,7 @@ export class FixedRateExchange { * @return {Promise} no of available exchanges */ public async getNumberOfExchanges(): Promise { - const numExchanges = await this.fixedRateContract.methods - .getNumberOfExchanges() - .call() + const numExchanges = await this.contract.methods.getNumberOfExchanges().call() return numExchanges } @@ -289,7 +234,7 @@ export class FixedRateExchange { newRate: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -313,11 +258,11 @@ export class FixedRateExchange { ): Promise { const estGas = await estimateGas( address, - this.fixedRateContract.methods.setRate, + this.contract.methods.setRate, exchangeId, this.web3.utils.toWei(newRate) ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(newRate)) .send({ from: address, @@ -341,7 +286,7 @@ export class FixedRateExchange { newAllowedSwapper: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -365,11 +310,11 @@ export class FixedRateExchange { ): Promise { const estGas = await estimateGas( address, - this.fixedRateContract.methods.setAllowedSwapper, + this.contract.methods.setAllowedSwapper, exchangeId, newAllowedSwapper ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .setAllowedSwapper(exchangeId, newAllowedSwapper) .send({ from: address, @@ -391,7 +336,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } @@ -411,16 +356,14 @@ export class FixedRateExchange { if (exchange.active === true) return null const estGas = await estimateGas( address, - this.fixedRateContract.methods.toggleExchangeState, + this.contract.methods.toggleExchangeState, exchangeId ) - const trxReceipt = await this.fixedRateContract.methods - .toggleExchangeState(exchangeId) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -436,7 +379,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } @@ -457,17 +400,15 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.toggleExchangeState, + this.contract.methods.toggleExchangeState, exchangeId ) - const trxReceipt = await this.fixedRateContract.methods - .toggleExchangeState(exchangeId) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -478,7 +419,7 @@ export class FixedRateExchange { * @return {Promise} Rate (converted from wei) */ public async getRate(exchangeId: string): Promise { - const weiRate = await this.fixedRateContract.methods.getRate(exchangeId).call() + const weiRate = await this.contract.methods.getRate(exchangeId).call() const rate = await this.web3.utils.fromWei(weiRate) return rate } @@ -489,7 +430,7 @@ export class FixedRateExchange { * @return {Promise} dt supply formatted */ public async getDTSupply(exchangeId: string): Promise { - const dtSupply = await this.fixedRateContract.methods.getDTSupply(exchangeId).call() + const dtSupply = await this.contract.methods.getDTSupply(exchangeId).call() const exchange = await this.getExchange(exchangeId) return await this.unitsToAmount(exchange.datatoken, dtSupply, +exchange.dtDecimals) } @@ -500,7 +441,7 @@ export class FixedRateExchange { * @return {Promise} dt supply formatted */ public async getBTSupply(exchangeId: string): Promise { - const btSupply = await this.fixedRateContract.methods.getBTSupply(exchangeId).call() + const btSupply = await this.contract.methods.getBTSupply(exchangeId).call() const exchange = await this.getExchange(exchangeId) return await this.unitsToAmount(exchange.baseToken, btSupply, +exchange.btDecimals) } @@ -511,7 +452,7 @@ export class FixedRateExchange { * @return {Promise} address of allowedSwapper */ public async getAllowedSwapper(exchangeId: string): Promise { - return await this.fixedRateContract.methods.getAllowedSwapper(exchangeId).call() + return await this.contract.methods.getAllowedSwapper(exchangeId).call() } /** @@ -527,7 +468,7 @@ export class FixedRateExchange { consumeMarketFee: string = '0' ): Promise { const fixedRateExchange = await this.getExchange(exchangeId) - const result = await this.fixedRateContract.methods + const result = await this.contract.methods .calcBaseInGivenOutDT( exchangeId, await this.amountToUnits( @@ -577,7 +518,7 @@ export class FixedRateExchange { consumeMarketFee: string = '0' ): Promise { const exchange = await this.getExchange(exchangeId) - const result = await this.fixedRateContract.methods + const result = await this.contract.methods .calcBaseOutGivenInDT( exchangeId, await this.amountToUnits( @@ -598,7 +539,7 @@ export class FixedRateExchange { * @return {Promise} Exchange details */ public async getExchange(exchangeId: string): Promise { - const result: FixedPriceExchange = await this.fixedRateContract.methods + const result: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() result.dtDecimals = result.dtDecimals.toString() @@ -634,9 +575,7 @@ export class FixedRateExchange { * @return {Promise} Exchange details */ public async getFeesInfo(exchangeId: string): Promise { - const result: FeesInfo = await this.fixedRateContract.methods - .getFeesInfo(exchangeId) - .call() + const result: FeesInfo = await this.contract.methods.getFeesInfo(exchangeId).call() result.opcFee = this.web3.utils.fromWei(result.opcFee.toString()) result.marketFee = this.web3.utils.fromWei(result.marketFee.toString()) @@ -662,7 +601,7 @@ export class FixedRateExchange { * @return {Promise} Exchanges list */ public async getExchanges(): Promise { - return await this.fixedRateContract.methods.getExchanges().call() + return await this.contract.methods.getExchanges().call() } /** @@ -671,7 +610,7 @@ export class FixedRateExchange { * @return {Promise} Result */ public async isActive(exchangeId: string): Promise { - const result = await this.fixedRateContract.methods.isActive(exchangeId).call() + const result = await this.contract.methods.isActive(exchangeId).call() return result } @@ -687,7 +626,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas(account, fixedRate.methods.toggleMintState, exchangeId, true) } @@ -708,11 +647,11 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.toggleMintState, + this.contract.methods.toggleMintState, exchangeId, true ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, true) .send({ from: address, @@ -734,7 +673,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -760,12 +699,12 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.toggleMintState, + this.contract.methods.toggleMintState, exchangeId, false ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, false) .send({ from: address, @@ -790,8 +729,8 @@ export class FixedRateExchange { amount: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract - const fixedrate: FixedPriceExchange = await this.fixedRateContract.methods + const fixedRate = contractInstance || this.contract + const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits( @@ -817,7 +756,7 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const fixedrate: FixedPriceExchange = await this.fixedRateContract.methods + const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits( @@ -828,18 +767,16 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.collectBT, + this.contract.methods.collectBT, exchangeId, amountWei ) - const trxReceipt = await this.fixedRateContract.methods - .collectBT(exchangeId, amountWei) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -857,8 +794,8 @@ export class FixedRateExchange { amount: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract - const fixedrate: FixedPriceExchange = await this.fixedRateContract.methods + const fixedRate = contractInstance || this.contract + const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() @@ -885,7 +822,7 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const fixedrate: FixedPriceExchange = await this.fixedRateContract.methods + const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits( @@ -896,18 +833,16 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.collectDT, + this.contract.methods.collectDT, exchangeId, amountWei ) - const trxReceipt = await this.fixedRateContract.methods - .collectDT(exchangeId, amountWei) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -923,7 +858,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } @@ -943,16 +878,14 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.collectMarketFee, + this.contract.methods.collectMarketFee, exchangeId ) - const trxReceipt = await this.fixedRateContract.methods - .collectMarketFee(exchangeId) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -968,7 +901,7 @@ export class FixedRateExchange { exchangeId: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } @@ -988,16 +921,14 @@ export class FixedRateExchange { const estGas = await estimateGas( address, - this.fixedRateContract.methods.collectOceanFee, + this.contract.methods.collectOceanFee, exchangeId ) - const trxReceipt = await this.fixedRateContract.methods - .collectOceanFee(exchangeId) - .send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) return trxReceipt } @@ -1008,7 +939,7 @@ export class FixedRateExchange { async getOPCCollector(): Promise { let result = null try { - result = await this.fixedRateContract.methods.opcCollector().call() + result = await this.contract.methods.opcCollector().call() } catch (e) { LoggerInstance.error(`ERROR: Failed to get OPC Collector address: ${e.message}`) } @@ -1022,7 +953,7 @@ export class FixedRateExchange { async getRouter(): Promise { let result = null try { - result = await this.fixedRateContract.methods.router().call() + result = await this.contract.methods.router().call() } catch (e) { LoggerInstance.error(`ERROR: Failed to get Router address: ${e.message}`) } @@ -1058,7 +989,7 @@ export class FixedRateExchange { newMarketFee: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -1082,11 +1013,11 @@ export class FixedRateExchange { ): Promise { const estGas = await estimateGas( address, - this.fixedRateContract.methods.updateMarketFee, + this.contract.methods.updateMarketFee, exchangeId, this.web3.utils.toWei(newMarketFee) ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .updateMarketFee(exchangeId, this.web3.utils.toWei(newMarketFee)) .send({ from: address, @@ -1110,7 +1041,7 @@ export class FixedRateExchange { newMarketFeeCollector: string, contractInstance?: Contract ): Promise { - const fixedRate = contractInstance || this.fixedRateContract + const fixedRate = contractInstance || this.contract return estimateGas( account, @@ -1134,11 +1065,11 @@ export class FixedRateExchange { ): Promise { const estGas = await estimateGas( address, - this.fixedRateContract.methods.updateMarketFeeCollector, + this.contract.methods.updateMarketFeeCollector, exchangeId, newMarketFeeCollector ) - const trxReceipt = await this.fixedRateContract.methods + const trxReceipt = await this.contract.methods .updateMarketFeeCollector(exchangeId, newMarketFeeCollector) .send({ from: address, diff --git a/test/integration/CodeExamples.test.ts b/test/integration/CodeExamples.test.ts index 76c1b70f..c22c50f4 100644 --- a/test/integration/CodeExamples.test.ts +++ b/test/integration/CodeExamples.test.ts @@ -614,7 +614,7 @@ describe('Marketplace flow tests', async () => { it('7.3 Marketplace displays fixed rate asset for sale', async () => { /// ```Typescript - const fixedRate = new FixedRateExchange(web3, freAddress) + const fixedRate = new FixedRateExchange(freAddress, web3) const oceanAmount = await ( await fixedRate.calcBaseInGivenOutDT(freId, '1') ).baseTokenAmount @@ -655,7 +655,7 @@ describe('Marketplace flow tests', async () => { DATATOKEN_AMOUNT ) - const fixedRate = new FixedRateExchange(web3, freAddress) + const fixedRate = new FixedRateExchange(freAddress, web3) /// ``` /// Now we can make the contract call /// ```Typescript diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 6d09be46..cbaa4181 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -100,7 +100,7 @@ describe('Fixed Rate unit test', () => { // user1 has no dt1 expect(await balance(web3, dtAddress, user1)).to.equal('0') - fixedRate = new FixedRateExchange(web3, contracts.fixedRateAddress, 8996) + fixedRate = new FixedRateExchange(contracts.fixedRateAddress, web3, 8996) assert(fixedRate != null) }) @@ -409,7 +409,7 @@ describe('Fixed Rate unit test', () => { // user1 has no dt1 expect(await balance(web3, dtAddress, user1)).to.equal('0') - fixedRate = new FixedRateExchange(web3, contracts.fixedRateAddress, 8996) + fixedRate = new FixedRateExchange(contracts.fixedRateAddress, web3, 8996) assert(fixedRate != null) })