mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
fixes (#1273)
* fixes * fix test * fix swap * fix test * fix test * more fixes * fixes * change swapMarketFee from wei to decimal * fix
This commit is contained in:
parent
5152bc2164
commit
3c315eb49d
@ -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
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ export interface PoolCreationParams {
|
||||
vestingAmount: string
|
||||
vestedBlocks: number
|
||||
initialBaseTokenLiquidity: string
|
||||
swapFeeLiquidityProvider: number
|
||||
swapFeeMarketRunner: number
|
||||
swapFeeLiquidityProvider: string
|
||||
swapFeeMarketRunner: string
|
||||
}
|
||||
|
||||
export interface CurrentFees {
|
||||
|
@ -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<string>} Amount of baseToken needed for buying
|
||||
* @param {string} datatokenAmount Amount of datatokens user wants to buy
|
||||
* @return {Promise<PriceAndFees>} how many base tokens are needed and fees
|
||||
*/
|
||||
public async getAmountBTIn(
|
||||
public async calcBaseInGivenOutDT(
|
||||
exchangeId: string,
|
||||
datatokenAmount: string
|
||||
): Promise<string> {
|
||||
): Promise<PriceAndFees> {
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<TransactionReceipt> {
|
||||
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,
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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)
|
||||
|
@ -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(
|
||||
|
@ -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'
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user