diff --git a/package-lock.json b/package-lock.json index 7af9c97f..f04745fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0-next.19", "license": "Apache-2.0", "dependencies": { - "@oceanprotocol/contracts": "1.0.0-alpha.19", + "@oceanprotocol/contracts": "1.0.0-alpha.20", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", @@ -3033,9 +3033,9 @@ } }, "node_modules/@oceanprotocol/contracts": { - "version": "1.0.0-alpha.19", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz", - "integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==", + "version": "1.0.0-alpha.20", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.20.tgz", + "integrity": "sha512-Z+zP7XzYjZfd0+URupld2LLZ91E07EWM4wlxpRGOhXEZ7oL3iQAlRJGq6SZN7C65LSUaMQuhj+n5C8aYOXUdwQ==", "dependencies": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", @@ -27682,9 +27682,9 @@ } }, "@oceanprotocol/contracts": { - "version": "1.0.0-alpha.19", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz", - "integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==", + "version": "1.0.0-alpha.20", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.20.tgz", + "integrity": "sha512-Z+zP7XzYjZfd0+URupld2LLZ91E07EWM4wlxpRGOhXEZ7oL3iQAlRJGq6SZN7C65LSUaMQuhj+n5C8aYOXUdwQ==", "requires": { "@openzeppelin/contracts": "^4.3.3", "@openzeppelin/test-helpers": "^0.5.15", diff --git a/package.json b/package.json index 22ecd909..d34eea7e 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "web3": "^1.7.0" }, "dependencies": { - "@oceanprotocol/contracts": "1.0.0-alpha.19", + "@oceanprotocol/contracts": "1.0.0-alpha.20", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", diff --git a/src/@types/FixedPrice.ts b/src/@types/FixedPrice.ts index 67690fa0..3fe6ea43 100644 --- a/src/@types/FixedPrice.ts +++ b/src/@types/FixedPrice.ts @@ -21,7 +21,7 @@ export interface FreOrderParams { export interface PriceAndFees { baseTokenAmount: string - baseTokenAmountBeforeFee: string oceanFeeAmount: string marketFeeAmount: string + consumeMarketFeeAmount: string } diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index fb429beb..4c90af18 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -270,6 +270,25 @@ export class Pool { return result } + /** + * Get getMarketFee + * @param {String} poolAddress + * @return {String} + */ + async getMarketFee(poolAddress: string): Promise { + const pool = setContractDefaults( + new this.web3.eth.Contract(this.poolAbi, poolAddress), + this.config + ) + let result = null + try { + result = await pool.methods.getMarketFee().call() + } catch (e) { + LoggerInstance.error(`ERROR: Failed to get getMarketFee: ${e.message}`) + } + return this.web3.utils.fromWei(result).toString() + } + /** * Get marketFeeCollector of this pool * @param {String} poolAddress @@ -673,17 +692,19 @@ export class Pool { } /** - * Estimate gas cost for updateMarketFeeCollector + * Estimate gas cost for updatePublishMarketFee * @param {String} address * @param {String} poolAddress - * @param {String} newCollector new market fee collector address + * @param {String} newPublishMarketAddress new market address + * @param {String} newPublishMarketSwapFee new market swap fee * @param {Contract} contractInstance optional contract instance * @return {Promise} */ - public async estUpdateMarketFeeCollector( + public async estUpdatePublishMarketFee( address: string, poolAddress: string, - newCollector: string, + newPublishMarketAddress: string, + newPublishMarketSwapFee: string, contractInstance?: Contract ): Promise { const poolContract = @@ -697,7 +718,7 @@ export class Pool { let estGas try { estGas = await poolContract.methods - .updateMarketFeeCollector(newCollector) + .updatePublishMarketFee(newPublishMarketAddress, newPublishMarketSwapFee) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -706,16 +727,18 @@ export class Pool { } /** - * updateMarketFeeCollector - updates marketFeeCollector - can be called only by the marketFeeCollector + * updatePublishMarketFee - sets a new newPublishMarketAddress and new newPublishMarketSwapFee- can be called only by the marketFeeCollector * @param {String} address * @param {String} poolAddress - * @param {String} newCollector new market fee collector address + * @param {String} newPublishMarketAddress new market fee collector address + * @param {String} newPublishMarketSwapFee fee recieved by the publisher market when a dt is swaped from a pool, percent * @return {TransactionReceipt} */ - async updateMarketFeeCollector( + async updatePublishMarketFee( address: string, poolAddress: string, - newCollector: string + newPublishMarketAddress: string, + newPublishMarketSwapFee: string ): Promise { if ((await this.getMarketFeeCollector(poolAddress)) !== address) { throw new Error(`Caller is not MarketFeeCollector`) @@ -725,20 +748,26 @@ export class Pool { this.config ) let result = null - const estGas = await this.estUpdateMarketFeeCollector( + + const estGas = await this.estUpdatePublishMarketFee( address, poolAddress, - newCollector + newPublishMarketAddress, + this.web3.utils.toWei(newPublishMarketSwapFee) ) - try { - result = await pool.methods.updateMarketFeeCollector(newCollector).send({ - from: address, - gas: estGas + 1, - gasPrice: await getFairGasPrice(this.web3, this.config) - }) + result = await pool.methods + .updatePublishMarketFee( + newPublishMarketAddress, + this.web3.utils.toWei(newPublishMarketSwapFee) + ) + .send({ + from: address, + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3, this.config) + }) } catch (e) { - LoggerInstance.error(`ERROR: Failed to swap exact amount in : ${e.message}`) + LoggerInstance.error(`ERROR: Failed to updatePublishMarketFee : ${e.message}`) } return result } diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 5617f9ea..3c6f87ac 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -120,6 +120,8 @@ export class FixedRateExchange { * @param {String} account * @param {String} dtAmount datatoken amount we want to buy * @param {String} datatokenAddress datatokenAddress + * @param {String} consumeMarketAddress consumeMarketAddress + * @param {String} consumeMarketFee fee recieved by the consume market when a dt is bought from a fixed rate exchange, percent * @param {Contract} contractInstance optional contract instance * @return {Promise} */ @@ -128,6 +130,8 @@ export class FixedRateExchange { datatokenAddress: string, dtAmount: string, maxBaseTokenAmount: string, + consumeMarketAddress: string, + consumeMarketFee: string, contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract @@ -135,7 +139,13 @@ export class FixedRateExchange { let estGas try { estGas = await fixedRate.methods - .buyDT(datatokenAddress, dtAmount.toString(), maxBaseTokenAmount.toString()) + .buyDT( + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -149,14 +159,19 @@ export class FixedRateExchange { * @param {String} datatokenAmount Amount of datatokens * @param {String} maxBaseTokenAmount max amount of baseToken we want to pay for datatokenAmount * @param {String} address User address + * @param {String} consumeMarketAddress consumeMarketAddress + * @param {String} consumeMarketFee consumeMarketFee * @return {Promise} transaction receipt */ public async buyDT( address: string, exchangeId: string, datatokenAmount: string, - maxBaseTokenAmount: string + maxBaseTokenAmount: string, + consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', + consumeMarketFee: string = '0' ): Promise { + const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee) const dtAmountFormatted = await this.amountToUnits( ( await this.getExchange(exchangeId) @@ -174,11 +189,19 @@ export class FixedRateExchange { address, exchangeId, dtAmountFormatted, - maxBtFormatted + maxBtFormatted, + consumeMarketAddress, + consumeMarketFeeFormatted ) try { const trxReceipt = await this.contract.methods - .buyDT(exchangeId, dtAmountFormatted, maxBtFormatted) + .buyDT( + exchangeId, + dtAmountFormatted, + maxBtFormatted, + consumeMarketAddress, + consumeMarketFeeFormatted + ) .send({ from: address, gas: estGas + 1, @@ -196,6 +219,8 @@ export class FixedRateExchange { * @param {String} account * @param {String} dtAmount datatoken amount we want to sell * @param {String} datatokenAddress datatokenAddress + * @param {String} consumeMarketAddress consumeMarketAddress + * @param {String} consumeMarketFee consumeMarketFee * @param {Contract} contractInstance optional contract instance * @return {Promise} */ @@ -204,6 +229,8 @@ export class FixedRateExchange { datatokenAddress: string, dtAmount: string, maxBaseTokenAmount: string, + consumeMarketAddress: string, + consumeMarketFee: string, contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract @@ -211,7 +238,13 @@ export class FixedRateExchange { let estGas try { estGas = await fixedRate.methods - .sellDT(datatokenAddress, dtAmount, maxBaseTokenAmount) + .sellDT( + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -225,14 +258,19 @@ export class FixedRateExchange { * @param {String} datatokenAmount Amount of datatokens * @param {String} minBaseTokenAmount min amount of baseToken we want to receive back * @param {String} address User address + * @param {String} consumeMarketAddress consumeMarketAddress + * @param {String} consumeMarketFee consumeMarketFee * @return {Promise} transaction receipt */ public async sellDT( address: string, exchangeId: string, datatokenAmount: string, - minBaseTokenAmount: string + minBaseTokenAmount: string, + consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', + consumeMarketFee: string = '0' ): Promise { + const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee) const dtAmountFormatted = await this.amountToUnits( ( await this.getExchange(exchangeId) @@ -249,11 +287,19 @@ export class FixedRateExchange { address, exchangeId, dtAmountFormatted, - minBtFormatted + minBtFormatted, + consumeMarketAddress, + consumeMarketFeeFormatted ) try { const trxReceipt = await this.contract.methods - .sellDT(exchangeId, dtAmountFormatted, minBtFormatted) + .sellDT( + exchangeId, + dtAmountFormatted, + minBtFormatted, + consumeMarketAddress, + consumeMarketFeeFormatted + ) .send({ from: address, gas: estGas + 1, @@ -533,17 +579,20 @@ export class FixedRateExchange { * calcBaseInGivenOutDT - Calculates how many base tokens are needed to get specified amount of datatokens * @param {String} exchangeId ExchangeId * @param {string} datatokenAmount Amount of datatokens user wants to buy + * @param {String} consumeMarketFee consumeMarketFee * @return {Promise} how many base tokens are needed and fees */ public async calcBaseInGivenOutDT( exchangeId: string, - datatokenAmount: string + datatokenAmount: string, + consumeMarketFee: string = '0' ): Promise { const fixedRateExchange = await this.getExchange(exchangeId) const result = await this.contract.methods .calcBaseInGivenOutDT( exchangeId, - await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount) + await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount), + this.web3.utils.toWei(consumeMarketFee) ) .call() @@ -552,10 +601,6 @@ export class FixedRateExchange { fixedRateExchange.baseToken, result.baseTokenAmount ), - baseTokenAmountBeforeFee: await this.unitsToAmount( - fixedRateExchange.baseToken, - result.baseTokenAmountBeforeFee - ), marketFeeAmount: await this.unitsToAmount( fixedRateExchange.baseToken, result.marketFeeAmount @@ -563,6 +608,10 @@ export class FixedRateExchange { oceanFeeAmount: await this.unitsToAmount( fixedRateExchange.baseToken, result.oceanFeeAmount + ), + consumeMarketFeeAmount: await this.unitsToAmount( + fixedRateExchange.baseToken, + result.consumeMarketFeeAmount ) } as PriceAndFees return priceAndFees @@ -572,11 +621,13 @@ export class FixedRateExchange { * getBTOut - returns amount in baseToken that user will receive for datatokenAmount sold * @param {String} exchangeId ExchangeId * @param {Number} datatokenAmount Amount of datatokens + * @param {String} consumeMarketFee consumeMarketFee * @return {Promise} Amount of baseTokens user will receive */ public async getAmountBTOut( exchangeId: string, - datatokenAmount: string + datatokenAmount: string, + consumeMarketFee: string = '0' ): Promise { const result = await this.contract.methods .calcBaseOutGivenInDT( @@ -586,7 +637,8 @@ export class FixedRateExchange { await this.getExchange(exchangeId) ).datatoken, datatokenAmount - ) + ), + this.web3.utils.toWei(consumeMarketFee) ) .call() @@ -594,7 +646,7 @@ export class FixedRateExchange { ( await this.getExchange(exchangeId) ).baseToken, - result.baseTokenAmount + result[0] ) } diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 146d28ae..e77f433c 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -560,10 +560,14 @@ describe('Pool unit test', () => { it('#updateMarketFeeCollector- should update market fee collector', async () => { // contracts.accounts[0] is the marketFeeCollector + assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) - - await pool.updateMarketFeeCollector(contracts.accounts[0], poolAddress, user3) - + await pool.updatePublishMarketFee( + contracts.accounts[0], + poolAddress, + user3, + await pool.getMarketFee(poolAddress) + ) assert((await pool.getMarketFeeCollector(poolAddress)) === user3) }) }) @@ -1079,7 +1083,12 @@ describe('Pool unit test', () => { // contracts.accounts[0] is the marketFeeCollector assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) - await pool.updateMarketFeeCollector(contracts.accounts[0], poolAddress, user3) + await pool.updatePublishMarketFee( + contracts.accounts[0], + poolAddress, + user3, + await pool.getMarketFee(poolAddress) + ) assert((await pool.getMarketFeeCollector(poolAddress)) === user3) })