diff --git a/src/@types/FixedPrice.ts b/src/@types/FixedPrice.ts index 5e37148a..67690fa0 100644 --- a/src/@types/FixedPrice.ts +++ b/src/@types/FixedPrice.ts @@ -6,7 +6,7 @@ export interface FreCreationParams { baseTokenDecimals: number datatokenDecimals: number fixedRate: string - marketFee: number + marketFee: string withMint?: boolean // add FixedPriced contract as minter if withMint == true allowedConsumer?: string // only account that consume the exhchange } @@ -18,3 +18,10 @@ export interface FreOrderParams { swapMarketFee: string marketFeeAddress: string } + +export interface PriceAndFees { + baseTokenAmount: string + baseTokenAmountBeforeFee: string + oceanFeeAmount: string + marketFeeAmount: string +} diff --git a/src/@types/Pool.ts b/src/@types/Pool.ts index b089acef..a01269d6 100644 --- a/src/@types/Pool.ts +++ b/src/@types/Pool.ts @@ -10,8 +10,8 @@ export interface PoolCreationParams { vestingAmount: string vestedBlocks: number initialBaseTokenLiquidity: string - swapFeeLiquidityProvider: number - swapFeeMarketRunner: number + swapFeeLiquidityProvider: string + swapFeeMarketRunner: string } export interface CurrentFees { diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index b6c23dae..a4ea7d08 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -12,6 +12,7 @@ import { setContractDefaults } from '../../utils' import { Config } from '../../models/index.js' +import { PriceAndFees } from '../..' export interface FixedPriceExchange { active: boolean @@ -554,15 +555,15 @@ export class FixedRateExchange { } /** - * getBTNeeded - returns amount in baseToken that user will pay for datatokenAmount + * calcBaseInGivenOutDT - Calculates how many base tokens are needed to get specified amount of datatokens * @param {String} exchangeId ExchangeId - * @param {Number} datatokenAmount Amount of datatokens user wants to buy - * @return {Promise} Amount of baseToken needed for buying + * @param {string} datatokenAmount Amount of datatokens user wants to buy + * @return {Promise} how many base tokens are needed and fees */ - public async getAmountBTIn( + public async calcBaseInGivenOutDT( exchangeId: string, datatokenAmount: string - ): Promise { + ): Promise { const result = await this.contract.methods .calcBaseInGivenOutDT( exchangeId, @@ -575,12 +576,33 @@ export class FixedRateExchange { ) .call() - return await this.unitsToAmount( - ( - await this.getExchange(exchangeId) - ).baseToken, - result.baseTokenAmount - ) + const priceAndFees = { + baseTokenAmount: await this.unitsToAmount( + ( + await this.getExchange(exchangeId) + ).baseToken, + result.baseTokenAmount + ), + baseTokenAmountBeforeFee: await this.unitsToAmount( + ( + await this.getExchange(exchangeId) + ).baseToken, + result.baseTokenAmountBeforeFee + ), + marketFeeAmount: await this.unitsToAmount( + ( + await this.getExchange(exchangeId) + ).baseToken, + result.marketFeeAmount + ), + oceanFeeAmount: await this.unitsToAmount( + ( + await this.getExchange(exchangeId) + ).baseToken, + result.oceanFeeAmount + ) + } as PriceAndFees + return priceAndFees } /** diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index b14a30b7..d213cf92 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -9,7 +9,8 @@ import { LoggerInstance, getFairGasPrice, setContractDefaults, - configHelperNetworks + configHelperNetworks, + getFreOrderParams } from '../utils' import { FreOrderParams, FreCreationParams, ProviderFees } from '../@types' import { Nft } from './NFT' @@ -1033,16 +1034,18 @@ export class Datatoken { ): Promise { const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) try { + const freContractParams = getFreOrderParams(freParams) + const estGas = await this.estGasBuyFromFreAndOrder( dtAddress, address, orderParams, - freParams, + freContractParams, dtContract ) const trxReceipt = await dtContract.methods - .buyFromFreAndOrder(orderParams, freParams) + .buyFromFreAndOrder(orderParams, freContractParams) .send({ from: address, gas: estGas + 1, diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 544d57ad..5d2880be 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -2,7 +2,12 @@ import Web3 from 'web3' import BigNumber from 'bignumber.js' import { Contract } from 'web3-eth-contract' import { generateDtName } from './DatatokenName' -import { Erc20CreateParams, FreCreationParams, PoolCreationParams } from '../@types' +import { + Erc20CreateParams, + FreCreationParams, + FreOrderParams, + PoolCreationParams +} from '../@types' import { Config } from '../models' import { minAbi } from './minAbi' import LoggerInstance from './Logger' @@ -49,6 +54,16 @@ export function getErcCreationParams(ercParams: Erc20CreateParams): any { } } +export function getFreOrderParams(freParams: FreOrderParams): any { + return { + exchangeContract: freParams.exchangeContract, + exchangeId: freParams.exchangeId, + maxBaseTokenAmount: freParams.maxBaseTokenAmount, + swapMarketFee: Web3.utils.toWei(freParams.swapMarketFee), + marketFeeAddress: freParams.marketFeeAddress + } +} + export function getFreCreationParams(freParams: FreCreationParams): any { if (!freParams.allowedConsumer) freParams.allowedConsumer = '0x0000000000000000000000000000000000000000' @@ -66,7 +81,7 @@ export function getFreCreationParams(freParams: FreCreationParams): any { freParams.baseTokenDecimals, freParams.datatokenDecimals, freParams.fixedRate, - freParams.marketFee, + Web3.utils.toWei(freParams.marketFee), withMint ] } @@ -89,7 +104,10 @@ export function getPoolCreationParams(poolParams: PoolCreationParams): any { poolParams.vestedBlocks, Web3.utils.toWei(poolParams.initialBaseTokenLiquidity) ], - swapFees: [poolParams.swapFeeLiquidityProvider, poolParams.swapFeeMarketRunner] + swapFees: [ + Web3.utils.toWei(poolParams.swapFeeLiquidityProvider), + Web3.utils.toWei(poolParams.swapFeeMarketRunner) + ] } } export async function unitsToAmount( diff --git a/test/integration/PublishFlows.test.ts b/test/integration/PublishFlows.test.ts index a98c3c40..dcfb42a4 100644 --- a/test/integration/PublishFlows.test.ts +++ b/test/integration/PublishFlows.test.ts @@ -116,8 +116,8 @@ describe('Publish tests', async () => { vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const bundleNFT = await factory.createNftErc20WithPool( accounts[0], @@ -192,7 +192,7 @@ describe('Publish tests', async () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', - marketFee: 1e15, + marketFee: '0', allowedConsumer: accounts[0], withMint: false } diff --git a/test/unit/NftFactory.test.ts b/test/unit/NftFactory.test.ts index 29aa3fa3..4282b97d 100644 --- a/test/unit/NftFactory.test.ts +++ b/test/unit/NftFactory.test.ts @@ -229,8 +229,8 @@ describe('Nft Factory test', () => { vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const txReceipt = await nftFactory.createNftErc20WithPool( @@ -275,7 +275,7 @@ describe('Nft Factory test', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', - marketFee: 1e15, + marketFee: '0.001', allowedConsumer: contracts.accounts[0], withMint: false } diff --git a/test/unit/pools/Router.test.ts b/test/unit/pools/Router.test.ts index 128d1cf8..c969bf3c 100644 --- a/test/unit/pools/Router.test.ts +++ b/test/unit/pools/Router.test.ts @@ -208,8 +208,8 @@ describe('Router unit test', () => { vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const nftFactory = new NftFactory( @@ -261,8 +261,8 @@ describe('Router unit test', () => { vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const txReceipt2 = await nftFactory.createNftErc20WithPool( diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 6f16f0a9..52a0e754 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -160,8 +160,8 @@ describe('Pool unit test', () => { vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const nftFactory = new NftFactory( @@ -606,8 +606,8 @@ describe('Pool unit test', () => { initialBaseTokenLiquidity: web3.utils.fromWei( await amountToUnits(web3, contracts.usdcAddress, '2000') ), - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const nftFactory = new NftFactory( diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 17edbe88..e39811e6 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -122,7 +122,7 @@ describe('Fixed Rate unit test', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: web3.utils.toWei('1'), - marketFee: 1e15, + marketFee: '0.001', allowedConsumer: ADDRESS_ZERO, withMint: false } @@ -225,9 +225,13 @@ describe('Fixed Rate unit test', () => { // no baseToken at the beginning expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0') }) - it('#getAmountBTIn - should get bt amount in for a specific dt amount', async () => { + it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => { // 100.2 DAI for 100 DT (0.1% market fee and 0.1% ocean fee) - expect(await fixedRate.getAmountBTIn(exchangeId, '100')).to.equal('100.2') + expect( + await ( + await fixedRate.calcBaseInGivenOutDT(exchangeId, '100') + ).baseTokenAmount + ).to.equal('100.2') }) it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => { // 99.8 DAI for 100 DT (0.1% market fee and 0.1% ocean fee) @@ -450,7 +454,7 @@ describe('Fixed Rate unit test', () => { baseTokenDecimals: 6, datatokenDecimals: 18, fixedRate: web3.utils.toWei('1'), - marketFee: 1e15, + marketFee: '0.001', allowedConsumer: ADDRESS_ZERO, withMint: false } @@ -557,9 +561,13 @@ describe('Fixed Rate unit test', () => { // no baseToken at the beginning expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0') }) - it('#getAmountBTIn - should get bt amount in for a specific dt amount', async () => { + it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => { // 100.2 USDC for 100 DT (0.1% market fee and 0.1% ocean fee) - expect(await fixedRate.getAmountBTIn(exchangeId, '100')).to.equal('100.2') + expect( + await ( + await fixedRate.calcBaseInGivenOutDT(exchangeId, '100') + ).baseTokenAmount + ).to.equal('100.2') }) it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => { // 99.8 USDC for 100 DT (0.1% market fee and 0.1% ocean fee) diff --git a/test/unit/pools/ssContracts/SideStaking.test.ts b/test/unit/pools/ssContracts/SideStaking.test.ts index 9218ee2f..0ecb0f94 100644 --- a/test/unit/pools/ssContracts/SideStaking.test.ts +++ b/test/unit/pools/ssContracts/SideStaking.test.ts @@ -173,8 +173,8 @@ describe('SideStaking unit test', () => { vestingAmount: '10000', vestedBlocks: vestedBlocks, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const txReceipt = await nftFactory.createNftErc20WithPool( @@ -425,8 +425,8 @@ describe('SideStaking unit test', () => { initialBaseTokenLiquidity: web3.utils.fromWei( await amountToUnits(web3, contracts.usdcAddress, '2000') ), - swapFeeLiquidityProvider: 1e15, - swapFeeMarketRunner: 1e15 + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const txReceipt = await nftFactory.createNftErc20WithPool( diff --git a/test/unit/tokens/Datatoken.test.ts b/test/unit/tokens/Datatoken.test.ts index 1d7066a2..b96144c9 100644 --- a/test/unit/tokens/Datatoken.test.ts +++ b/test/unit/tokens/Datatoken.test.ts @@ -166,7 +166,7 @@ describe('Datatoken', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: web3.utils.toWei('1'), - marketFee: 1e15 + marketFee: '0' } const fre = await datatoken.createFixedRate(datatokenAddress, nftOwner, freParams) assert(fre !== null) @@ -184,7 +184,7 @@ describe('Datatoken', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: web3.utils.toWei('1'), - marketFee: 1e15 + marketFee: '0' } try { await datatoken.createFixedRate(datatokenAddress, user3, freParams) @@ -426,7 +426,6 @@ describe('Datatoken', () => { const providerData = JSON.stringify({ timeout: 0 }) const providerFeeToken = ZERO_ADDRESS const providerFeeAmount = '0' - const dtAmount = web3.utils.toWei('1') const message = web3.utils.soliditySha3( { t: 'bytes', v: web3.utils.toHex(web3.utils.asciiToHex(providerData)) }, { t: 'address', v: user3 }, @@ -455,7 +454,7 @@ describe('Datatoken', () => { exchangeContract: fixedRateAddress, exchangeId: exchangeId, maxBaseTokenAmount: '1', - swapMarketFee: web3.utils.toWei('0.1'), + swapMarketFee: '0.1', marketFeeAddress: '0x0000000000000000000000000000000000000000' }