From c01fb0756c89338f3f2fd59b61df310d8ae795a2 Mon Sep 17 00:00:00 2001 From: Alex Coseru Date: Mon, 21 Mar 2022 14:40:34 +0200 Subject: [PATCH] Feature/contracts alpha 25 (#1358) * bump to contracts alpha 26 * update gasFeeMultiplier --- package-lock.json | 16 ++--- package.json | 2 +- src/pools/fixedRate/FixedRateExchange.ts | 58 +++++++++++++------ src/utils/ConfigHelper.ts | 12 ++-- test/unit/pools/dispenser/Dispenser.test.ts | 5 -- .../pools/fixedRate/FixedRateExchange.test.ts | 25 ++++---- 6 files changed, 68 insertions(+), 50 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4f0d7ea8..88b5d7f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,10 +6,10 @@ "packages": { "": { "name": "@oceanprotocol/lib", - "version": "1.0.0-next.27", + "version": "1.0.0-next.28", "license": "Apache-2.0", "dependencies": { - "@oceanprotocol/contracts": "^1.0.0-alpha.22", + "@oceanprotocol/contracts": "^1.0.0-alpha.26", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", @@ -3071,9 +3071,9 @@ } }, "node_modules/@oceanprotocol/contracts": { - "version": "1.0.0-alpha.22", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.22.tgz", - "integrity": "sha512-+m0E2WgH9x66DCtoHB3aaJ69ep3sp/x+jjae50HVgvpw0I0fvatXn5jfHrBxRIBvu0ZIbj6Ct0En3N6h5Yi1vw==", + "version": "1.0.0-alpha.26", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.26.tgz", + "integrity": "sha512-PHmewip7ZnyMhdk/eLLaGGt4NB9j02haw6djyMB21jZVsqcXr5fLOfvAxra93H0ddkAFMK2valo5Ga+X9c3RMQ==", "dependencies": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", @@ -23457,9 +23457,9 @@ } }, "@oceanprotocol/contracts": { - "version": "1.0.0-alpha.22", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.22.tgz", - "integrity": "sha512-+m0E2WgH9x66DCtoHB3aaJ69ep3sp/x+jjae50HVgvpw0I0fvatXn5jfHrBxRIBvu0ZIbj6Ct0En3N6h5Yi1vw==", + "version": "1.0.0-alpha.26", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.26.tgz", + "integrity": "sha512-PHmewip7ZnyMhdk/eLLaGGt4NB9j02haw6djyMB21jZVsqcXr5fLOfvAxra93H0ddkAFMK2valo5Ga+X9c3RMQ==", "requires": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", diff --git a/package.json b/package.json index 477128a1..f280c3b9 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "web3": "^1.7.1" }, "dependencies": { - "@oceanprotocol/contracts": "1.0.0-alpha.22", + "@oceanprotocol/contracts": "1.0.0-alpha.26", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 3883ec96..b14eb349 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -100,17 +100,13 @@ export class FixedRateExchange { /** * Creates unique exchange identifier. + * @param {String} baseToken baseToken contract address * @param {String} datatoken Datatoken contract address - * @param {String} owner Owner of the exchange * @return {Promise} exchangeId */ - public async generateExchangeId( - baseToken: string, - datatoken: string, - owner: string - ): Promise { + public async generateExchangeId(baseToken: string, datatoken: string): Promise { const exchangeId = await this.contract.methods - .generateExchangeId(baseToken, datatoken, owner) + .generateExchangeId(baseToken, datatoken) .call() return exchangeId } @@ -816,20 +812,26 @@ export class FixedRateExchange { * Estimate gas cost for collectBT * @param {String} account * @param {String} exchangeId ExchangeId + * @param {String} amount amount to be collected * @param {Contract} contractInstance optional contract instance * @return {Promise} */ public async estCollectBT( account: string, exchangeId: string, + amount: string, contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas + const fixedrate: FixedPriceExchange = await this.contract.methods + .getExchange(exchangeId) + .call() + const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) try { estGas = await fixedRate.methods - .collectBT(exchangeId) + .collectBT(exchangeId, amountWei) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -838,20 +840,26 @@ export class FixedRateExchange { } /** - * Collect BaseTokens in the contract (only exchange owner) - * @param {String} exchangeId ExchangeId + * Collect BaseTokens in the contract (anyone can call this, funds are sent to erc20.paymentCollector) * @param {String} address User address + * @param {String} exchangeId ExchangeId + * @param {String} amount amount to be collected * @return {Promise} transaction receipt */ public async collectBT( address: string, - exchangeId: string + exchangeId: string, + amount: string ): Promise { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectBT(address, exchangeId) - const trxReceipt = await this.contract.methods.collectBT(exchangeId).send({ + const estGas = await this.estCollectBT(address, exchangeId, amount) + const fixedrate: FixedPriceExchange = await this.contract.methods + .getExchange(exchangeId) + .call() + const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) + const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, gasPrice: await getFairGasPrice(this.web3, this.config) @@ -863,20 +871,26 @@ export class FixedRateExchange { * Estimate gas cost for collecDT * @param {String} account * @param {String} exchangeId ExchangeId + * @param {String} amount amount to be collected * @param {Contract} contractInstance optional contract instance * @return {Promise} */ public async estCollectDT( account: string, exchangeId: string, + amount: string, contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas + const fixedrate: FixedPriceExchange = await this.contract.methods + .getExchange(exchangeId) + .call() + const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) try { estGas = await fixedRate.methods - .collectDT(exchangeId) + .collectDT(exchangeId, amountWei) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -885,20 +899,26 @@ export class FixedRateExchange { } /** - * Collect datatokens in the contract (only exchange owner) - * @param {String} exchangeId ExchangeId + * Collect datatokens in the contract (anyone can call this, funds are sent to erc20.paymentCollector) * @param {String} address User address + * @param {String} exchangeId ExchangeId + * @param {String} amount amount to be collected * @return {Promise} transaction receipt */ public async collectDT( address: string, - exchangeId: string + exchangeId: string, + amount: string ): Promise { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectDT(address, exchangeId) - const trxReceipt = await this.contract.methods.collectDT(exchangeId).send({ + const estGas = await this.estCollectDT(address, exchangeId, amount) + const fixedrate: FixedPriceExchange = await this.contract.methods + .getExchange(exchangeId) + .call() + const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) + const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, gasPrice: await getFairGasPrice(this.web3, this.config) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 49672be1..536d4363 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -41,7 +41,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://ropsten.infura.io/v3', providerUri: 'https://v4.provider.ropsten.oceanprotocol.com', subgraphUri: 'https://v4.subgraph.ropsten.oceanprotocol.com', - explorerUri: 'https://ropsten.etherscan.io' + explorerUri: 'https://ropsten.etherscan.io', + gasFeeMultiplier: 1.1 }, { ...configHelperNetworksBase, @@ -50,7 +51,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://rinkeby.infura.io/v3', providerUri: 'https://v4.provider.rinkeby.oceanprotocol.com', subgraphUri: 'https://v4.subgraph.rinkeby.oceanprotocol.com', - explorerUri: 'https://rinkeby.etherscan.io' + explorerUri: 'https://rinkeby.etherscan.io', + gasFeeMultiplier: 1.1 }, { ...configHelperNetworksBase, @@ -83,7 +85,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://rpc.api.moonbase.moonbeam.network', providerUri: 'https://v4.provider.moonbase.oceanprotocol.com', subgraphUri: 'https://v4.subgraph.moonbase.oceanprotocol.com', - explorerUri: 'https://moonbase.moonscan.io/' + explorerUri: 'https://moonbase.moonscan.io/', + gasFeeMultiplier: 1.1 }, { ...configHelperNetworksBase, @@ -101,7 +104,8 @@ export const configHelperNetworks: Config[] = [ nodeUri: 'https://polygon-mumbai.infura.io/v3', providerUri: 'https://v4.provider.mumbai.oceanprotocol.com', subgraphUri: 'https://v4.subgraph.mumbai.oceanprotocol.com', - explorerUri: 'https://mumbai.polygonscan.com' + explorerUri: 'https://mumbai.polygonscan.com', + gasFeeMultiplier: 1.1 }, { ...configHelperNetworksBase, diff --git a/test/unit/pools/dispenser/Dispenser.test.ts b/test/unit/pools/dispenser/Dispenser.test.ts index 6f4334fd..b163f970 100644 --- a/test/unit/pools/dispenser/Dispenser.test.ts +++ b/test/unit/pools/dispenser/Dispenser.test.ts @@ -173,11 +173,6 @@ describe('Dispenser flow', () => { assert(tx, 'user3 failed to get 1DT') }) - it('tries to withdraw all datatokens', async () => { - const tx = await DispenserClass.ownerWithdraw(dtAddress, user3) - assert(tx === null, 'Request should fail') - }) - it('user2 withdraws all datatokens', async () => { const tx = await DispenserClass.ownerWithdraw(dtAddress, contracts.accounts[0]) assert(tx, 'user2 failed to withdraw all her tokens') diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 9627391c..d2ca4876 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -16,6 +16,7 @@ import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/community import { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory' import { FixedRateExchange } from '../../../../src/pools/fixedRate/FixedRateExchange' import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' +import { amountToUnits, unitsToAmount } from '../../../../src/utils/ContractUtils' const web3 = new Web3('http://127.0.0.1:8545') describe('Fixed Rate unit test', () => { @@ -193,7 +194,7 @@ describe('Fixed Rate unit test', () => { it('#generate exchangeId - should generate a specific exchangeId', async () => { expect( - await fixedRate.generateExchangeId(contracts.daiAddress, dtAddress, exchangeOwner) + await fixedRate.generateExchangeId(contracts.daiAddress, dtAddress) ).to.equal(exchangeId) }) @@ -348,9 +349,10 @@ describe('Fixed Rate unit test', () => { // user2 buys 1 DT await fixedRate.buyDT(user2, exchangeId, '1', '2') // 1 DAI in the contract - expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('1') + const fixedRateDetails = await fixedRate.getExchange(exchangeId) + expect(fixedRateDetails.btBalance).to.equal('1') // owner collects BTs - await fixedRate.collectBT(exchangeOwner, exchangeId) + await fixedRate.collectBT(exchangeOwner, exchangeId, fixedRateDetails.btBalance) // btBalance is zero expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') }) @@ -359,7 +361,7 @@ describe('Fixed Rate unit test', () => { // 9 dts left expect(result.dtBalance).to.equal('9') // owner collects DTs - await fixedRate.collectDT(exchangeOwner, exchangeId) + await fixedRate.collectDT(exchangeOwner, exchangeId, result.dtBalance) // no more dts in the contract const result2 = await fixedRate.getExchange(exchangeId) expect(result2.dtBalance).to.equal('0') @@ -383,7 +385,7 @@ describe('Fixed Rate unit test', () => { expect(result.oceanFeeAvailable).to.equal('0.042') // user3 is the marketFeeCollector expect(await daiContract.methods.balanceOf(user3).call()).to.equal( - web3.utils.toWei('0.021') + web3.utils.toWei('1.021') ) }) @@ -508,11 +510,7 @@ describe('Fixed Rate unit test', () => { it('#generate exchangeId - should generate a specific exchangeId', async () => { expect( - await fixedRate.generateExchangeId( - contracts.usdcAddress, - dtAddress, - exchangeOwner - ) + await fixedRate.generateExchangeId(contracts.usdcAddress, dtAddress) ).to.equal(exchangeId) }) @@ -661,9 +659,10 @@ describe('Fixed Rate unit test', () => { // user2 buys 1 DT await fixedRate.buyDT(user2, exchangeId, '1', '2') // 1 DAI in the contract - expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('1') + const exchangeDetails = await fixedRate.getExchange(exchangeId) + expect(exchangeDetails.btBalance).to.equal('1') // owner collects BTs - await fixedRate.collectBT(exchangeOwner, exchangeId) + await fixedRate.collectBT(exchangeOwner, exchangeId, exchangeDetails.btBalance) // btBalance is zero expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') }) @@ -672,7 +671,7 @@ describe('Fixed Rate unit test', () => { // 9 dts left expect(result.dtBalance).to.equal('9') // owner collects DTs - await fixedRate.collectDT(exchangeOwner, exchangeId) + await fixedRate.collectDT(exchangeOwner, exchangeId, result.dtBalance) // no more dts in the contract const result2 = await fixedRate.getExchange(exchangeId) expect(result2.dtBalance).to.equal('0')