diff --git a/src/datatokens/Datatoken.ts b/src/datatokens/Datatoken.ts index 7182a96b..b9df0474 100644 --- a/src/datatokens/Datatoken.ts +++ b/src/datatokens/Datatoken.ts @@ -20,9 +20,6 @@ export interface OrderParams { consumer: string amount: string serviceIndex: number - consumeFeeAddress: string - consumeFeeToken: string - consumeFeeAmount: string } export interface DispenserParams { @@ -831,9 +828,6 @@ export class Datatoken { consumer: string, amount: string, serviceIndex: number, - mpFeeAddress: string, - feeToken: string, - feeAmount: string, contractInstance?: Contract ): Promise { const dtContract = @@ -844,14 +838,7 @@ export class Datatoken { let estGas try { estGas = await dtContract.methods - .startOrder( - consumer, - this.web3.utils.toWei(amount), - serviceIndex, - mpFeeAddress, - feeToken, - this.web3.utils.toWei(feeAmount) - ) + .startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -875,13 +862,9 @@ export class Datatoken { address: string, consumer: string, amount: string, - serviceIndex: number, - mpFeeAddress: string, - feeToken: string, - feeAmount: string + serviceIndex: number ): Promise { const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress) - if (!mpFeeAddress) mpFeeAddress = '0x0000000000000000000000000000000000000000' try { const estGas = await this.estGasStartOrder( dtAddress, @@ -889,21 +872,11 @@ export class Datatoken { consumer, amount, serviceIndex, - mpFeeAddress, - feeToken, - feeAmount, dtContract ) const trxReceipt = await dtContract.methods - .startOrder( - consumer, - this.web3.utils.toWei(amount), - serviceIndex, - mpFeeAddress, - feeToken, - this.web3.utils.toWei(feeAmount) - ) + .startOrder(consumer, this.web3.utils.toWei(amount), serviceIndex) .send({ from: address, gas: estGas + 1, diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 89d57bea..c0642424 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -23,9 +23,6 @@ export interface TokenOrder { consumer: string amount: string | number serviceIndex: number - consumeFeeAddress: string - consumeFeeToken: string // address of the token marketplace wants to add fee on top - consumeFeeAmount: number } export interface NFTCreateData { diff --git a/src/interfaces/FixedRateInterface.ts b/src/interfaces/FixedRateInterface.ts index 7e3c5364..bc60d067 100644 --- a/src/interfaces/FixedRateInterface.ts +++ b/src/interfaces/FixedRateInterface.ts @@ -15,4 +15,6 @@ export interface FreOrderParams { exchangeContract: string exchangeId: string maxBaseTokenAmount: string + swapMarketFee: number + marketFeeAddress: string } diff --git a/src/interfaces/PoolInterface.ts b/src/interfaces/PoolInterface.ts index 2fda2dfc..29c32ad9 100644 --- a/src/interfaces/PoolInterface.ts +++ b/src/interfaces/PoolInterface.ts @@ -18,3 +18,23 @@ export interface CurrentFees { tokens: string[] amounts: string[] } + +export interface TokenInOutMarket { + tokenIn: string + tokenOut: string + marketFeeAddress: string +} + +export interface AmountsInMaxFee { + tokenAmountIn: string + minAmountOut: string + maxPrice: string + swapMarketFee: string +} + +export interface AmountsOutMaxFee { + tokenAmountOut: string + maxAmountIn: string + maxPrice: string + swapMarketFee: string +} diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index b21bfd04..ce20bb0c 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -8,7 +8,13 @@ import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/bal import defaultPool from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import Decimal from 'decimal.js' -import { CurrentFees } from '../../interfaces' +import { + CurrentFees, + TokenInOutMarket, + AmountsInOutMaxFee, + AmountsInMaxFee, + AmountsOutMaxFee +} from '../../interfaces' const BN = require('bn.js') const MaxUint256 = @@ -639,7 +645,7 @@ export class Pool { } /** - * collectOPF - collect market fees - can be called by the marketFeeCollector + * collectOPF - collect market fees - can be called by the publishMarketCollector * @param {String} address * @param {String} poolAddress * @param {String} to address that will receive fees @@ -733,50 +739,6 @@ export class Pool { return result } - /** - * Estimate gas cost for swapExactAmountIn - * @param {String} address - * @param {String} poolAddress - * @param {String} tokenIn - * @param {String} tokenAmountIn will be converted to wei - * @param {String} tokenOut - * @param {String} minAmountOut will be converted to wei - * @param {String} maxPrice will be converted to wei - * @param {Contract} contractInstance optional contract instance - * @return {Promise} - */ - public async estSwapExactAmountIn( - address: string, - poolAddress: string, - tokenIn: string, - tokenAmountIn: string, - tokenOut: string, - minAmountOut: string, - maxPrice: string, - contractInstance?: Contract - ): Promise { - const poolContract = - contractInstance || - new this.web3.eth.Contract(this.poolABI as AbiItem[], poolAddress) - - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .swapExactAmountIn( - tokenIn, - tokenAmountIn, - tokenOut, - minAmountOut, - maxPrice || MaxUint256 - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas - } - async amountToUnits(token: string, amount: string): Promise { try { const tokenContract = new this.web3.eth.Contract( @@ -812,6 +774,53 @@ export class Pool { } } + /** + * Estimate gas cost for swapExactAmountIn + * @param {String} address + * @param {String} poolAddress + * @param {String} tokenIn + * @param {String} tokenAmountIn will be converted to wei + * @param {String} tokenOut + * @param {String} minAmountOut will be converted to wei + * @param {String} maxPrice will be converted to wei + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ + public async estSwapExactAmountIn( + address: string, + poolAddress: string, + tokenInOutMarket: TokenInOutMarket, + amountsInOutMaxFee: AmountsInMaxFee, + contractInstance?: Contract + ): Promise { + const poolContract = + contractInstance || + new this.web3.eth.Contract(this.poolABI as AbiItem[], poolAddress) + + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await poolContract.methods + .swapExactAmountIn( + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + amountsInOutMaxFee.tokenAmountIn, + amountsInOutMaxFee.minAmountOut, + this.web3.utils.toWei(amountsInOutMaxFee.maxPrice), + amountsInOutMaxFee.swapMarketFee + ] + ) + .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + } + return estGas + } + /** * swapExactAmountIn - Trades an exact tokenAmountIn of tokenIn taken from the caller by the pool, in exchange for at least minAmountOut of tokenOut given to the caller from the pool, with a maximum marginal price of maxPrice. Returns (tokenAmountOut, spotPriceAfter), where tokenAmountOut is the amount of token that came out of the pool, and spotPriceAfter is the new marginal spot price, ie, the result of getSpotPrice after the call. (These values are what are limited by the arguments; you are guaranteed tokenAmountOut >= minAmountOut and spotPriceAfter <= maxPrice). * @param {String} address @@ -826,39 +835,44 @@ export class Pool { async swapExactAmountIn( address: string, poolAddress: string, - tokenIn: string, - tokenAmountIn: string, - tokenOut: string, - minAmountOut: string, - maxPrice?: string + tokenInOutMarket: TokenInOutMarket, + amountsInOutMaxFee: AmountsInOutMaxFee ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) - const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn) + amountsInOutMaxFee.tokenAmountIn = await this.amountToUnits( + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.tokenAmountIn + ) - const minAmountOutFormatted = await this.amountToUnits(tokenOut, minAmountOut) + amountsInOutMaxFee.minAmountOut = await this.amountToUnits( + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.minAmountOut + ) let result = null const estGas = await this.estSwapExactAmountIn( address, poolAddress, - tokenIn, - amountInFormatted, - tokenOut, - minAmountOutFormatted.toString(), - maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 + tokenInOutMarket, + amountsInOutMaxFee ) - // console.log(minAmountOutFormatted, 'minamoutnoutformatted') try { result = await pool.methods .swapExactAmountIn( - tokenIn, - amountInFormatted, - tokenOut, - minAmountOutFormatted, - maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + amountsInOutMaxFee.tokenAmountIn, + amountsInOutMaxFee.minAmountOut, + this.web3.utils.toWei(amountsInOutMaxFee.maxPrice), + amountsInOutMaxFee.swapMarketFee + ] ) .send({ from: address, @@ -887,11 +901,8 @@ export class Pool { public async estSwapExactAmountOut( address: string, poolAddress: string, - tokenIn: string, - maxAmountIn: string, - tokenOut: string, - amountOut: string, - maxPrice?: string, + tokenInOutMarket: TokenInOutMarket, + amountsInOutMaxFee: AmountsOutMaxFee, contractInstance?: Contract ): Promise { const poolContract = @@ -903,11 +914,17 @@ export class Pool { try { estGas = await poolContract.methods .swapExactAmountOut( - tokenIn, - maxAmountIn, - tokenOut, - amountOut, - maxPrice || MaxUint256 + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + amountsInOutMaxFee.maxAmountIn, + amountsInOutMaxFee.tokenAmountOut, + this.web3.utils.toWei(amountsInOutMaxFee.maxPrice), + amountsInOutMaxFee.swapMarketFee + ] ) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { @@ -930,35 +947,41 @@ export class Pool { async swapExactAmountOut( account: string, poolAddress: string, - tokenIn: string, - maxAmountIn: string, - tokenOut: string, - amountOut: string, - maxPrice?: string + tokenInOutMarket: TokenInOutMarket, + amountsInOutMaxFee: AmountsOutMaxFee ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null - const maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn) - const amountOutFormatted = await this.amountToUnits(tokenOut, amountOut) + amountsInOutMaxFee.maxAmountIn = await this.amountToUnits( + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.maxAmountIn + ) + amountsInOutMaxFee.tokenAmountOut = await this.amountToUnits( + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.tokenAmountOut + ) const estGas = await this.estSwapExactAmountOut( account, poolAddress, - tokenIn, - maxAmountInFormatted, - tokenOut, - amountOutFormatted, - maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 + tokenInOutMarket, + amountsInOutMaxFee ) try { result = await pool.methods .swapExactAmountOut( - tokenIn, - maxAmountInFormatted, - tokenOut, - amountOutFormatted, - maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + amountsInOutMaxFee.maxAmountIn, + amountsInOutMaxFee.tokenAmountOut, + this.web3.utils.toWei(amountsInOutMaxFee.maxPrice), + amountsInOutMaxFee.swapMarketFee + ] ) .send({ from: account, @@ -1452,7 +1475,8 @@ export class Pool { async getSpotPrice( poolAddress: string, tokenIn: string, - tokenOut: string + tokenOut: string, + swapMarketFe: number ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let decimalsTokenIn = 18 @@ -1479,7 +1503,7 @@ export class Pool { let price = null try { - price = await pool.methods.getSpotPrice(tokenIn, tokenOut).call() + price = await pool.methods.getSpotPrice(tokenIn, tokenOut, swapMarketFe).call() price = new BigNumber(price.toString()) } catch (e) { this.logger.error('ERROR: Failed to get spot price of swapping tokenIn to tokenOut') @@ -1506,7 +1530,8 @@ export class Pool { poolAddress: string, tokenIn: string, tokenOut: string, - tokenAmountOut: string + tokenAmountOut: string, + swapMarketFee: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) @@ -1516,7 +1541,7 @@ export class Pool { try { const result = await pool.methods - .getAmountInExactOut(tokenIn, tokenOut, amountOutFormatted) + .getAmountInExactOut(tokenIn, tokenOut, amountOutFormatted, swapMarketFee) .call() amount = await this.unitsToAmount(tokenIn, result) } catch (e) { @@ -1529,7 +1554,8 @@ export class Pool { poolAddress: string, tokenIn: string, tokenOut: string, - tokenAmountIn: string + tokenAmountIn: string, + swapMarketFee: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) @@ -1539,7 +1565,7 @@ export class Pool { try { const result = await pool.methods - .getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted) + .getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted, swapMarketFee) .call() amount = await this.unitsToAmount(tokenOut, result)