1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Replace toWei/fromWei when base token conversion (#1318)

* replaced toWei inside FRE

* added amountToUnits for maxPrice inside pool

* replaced fromWei

* add web3 param to getPoolCreationParams

* added await before amountToUnits inside swapExactAmount

* swapExactAmount update

* add await to unitsToAmount inside setRate

* convert newRate to string

* replaced unitToAmounts with amountToUnits inside setRate

* replace unitToAmounts with amountToUnits

* added await

* replaced String() with toSting()

* replaced String() in Provider

* transform newRate into wei inside estimateSetRate

* replace toWei inside getFreCreationParams

* added web3 param to getFreCreationParams

* add amountToUnits inside getFreOrderParams

* added logs

* removed unitsToAmount from fixed rate

* removed amountToUnits inside marketFee

* removed logs
This commit is contained in:
Norbi 2022-03-16 12:44:53 +02:00 committed by GitHub
parent a4a3e8abb8
commit a0e43235dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 81 deletions

View File

@ -705,7 +705,7 @@ export class NftFactory {
let estGas
try {
const ercCreateData = getErcCreationParams(ercParams)
const poolData = getPoolCreationParams(poolParams)
const poolData = await getPoolCreationParams(this.web3, poolParams)
estGas = await this.factory721.methods
.createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
@ -738,7 +738,7 @@ export class NftFactory {
poolParams
)
const ercCreateData = getErcCreationParams(ercParams)
const poolData = getPoolCreationParams(poolParams)
const poolData = await getPoolCreationParams(this.web3, poolParams)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
@ -769,8 +769,7 @@ export class NftFactory {
let estGas
const ercCreateData = getErcCreationParams(ercParams)
const fixedData = getFreCreationParams(freParams)
const fixedData = await getFreCreationParams(freParams)
try {
estGas = await this.factory721.methods

View File

@ -802,8 +802,24 @@ export class Pool {
this.config
)
const tokenAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn
)
const minAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.minAmountOut
)
const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256
const gasLimitDefault = this.GASLIMIT_DEFAULT
@ -817,8 +833,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
@ -859,20 +875,6 @@ export class Pool {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
}
amountsInOutMaxFee.tokenAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn
)
amountsInOutMaxFee.minAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.minAmountOut
)
let result = null
const estGas = await this.estSwapExactAmountIn(
address,
poolAddress,
@ -880,8 +882,26 @@ export class Pool {
amountsInOutMaxFee
)
const tokenAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn
)
const minAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.minAmountOut
)
let result = null
const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? await amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256
try {
@ -893,8 +913,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
@ -936,8 +956,24 @@ export class Pool {
const gasLimitDefault = this.GASLIMIT_DEFAULT
const maxAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn
)
const tokenAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.tokenAmountOut
)
const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? await amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256
let estGas
@ -950,8 +986,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
@ -988,18 +1024,6 @@ export class Pool {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}
amountsInOutMaxFee.maxAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn
)
amountsInOutMaxFee.tokenAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.tokenAmountOut
)
const estGas = await this.estSwapExactAmountOut(
account,
poolAddress,
@ -1007,8 +1031,24 @@ export class Pool {
amountsInOutMaxFee
)
const maxAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn
)
const tokenAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.tokenAmountOut
)
const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256
try {
@ -1020,8 +1060,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]

View File

@ -171,17 +171,17 @@ export class FixedRateExchange {
consumeMarketAddress: string = '0x0000000000000000000000000000000000000000',
consumeMarketFee: string = '0'
): Promise<TransactionReceipt> {
const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee)
const exchange = await this.getExchange(exchangeId)
const consumeMarketFeeFormatted = await this.amountToUnits(
exchange.baseToken,
consumeMarketFee
)
const dtAmountFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
).datatoken,
exchange.datatoken,
datatokenAmount
)
const maxBtFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
).baseToken,
exchange.baseToken,
maxBaseTokenAmount
)
@ -270,17 +270,17 @@ export class FixedRateExchange {
consumeMarketAddress: string = '0x0000000000000000000000000000000000000000',
consumeMarketFee: string = '0'
): Promise<TransactionReceipt> {
const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee)
const exchange = await this.getExchange(exchangeId)
const consumeMarketFeeFormatted = await this.amountToUnits(
exchange.baseToken,
consumeMarketFee
)
const dtAmountFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
).datatoken,
exchange.datatoken,
datatokenAmount
)
const minBtFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
).baseToken,
exchange.baseToken,
minBaseTokenAmount
)
const estGas = await this.estBuyDT(
@ -327,7 +327,7 @@ export class FixedRateExchange {
* Estimate gas cost for setRate
* @param {String} account
* @param {String} exchangeId ExchangeId
* @param {Number} newRate New rate
* @param {String} newRate New rate
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
@ -342,7 +342,7 @@ export class FixedRateExchange {
let estGas
try {
estGas = await fixedRate.methods
.setRate(exchangeId, newRate)
.setRate(exchangeId, await this.web3.utils.toWei(newRate))
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
@ -353,7 +353,7 @@ export class FixedRateExchange {
/**
* Set new rate
* @param {String} exchangeId ExchangeId
* @param {Number} newRate New rate
* @param {String} newRate New rate
* @param {String} address User account
* @return {Promise<TransactionReceipt>} transaction receipt
*/
@ -362,11 +362,7 @@ export class FixedRateExchange {
exchangeId: string,
newRate: string
): Promise<TransactionReceipt> {
const estGas = await this.estSetRate(
address,
exchangeId,
this.web3.utils.toWei(String(newRate))
)
const estGas = await this.estSetRate(address, exchangeId, newRate)
const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(newRate))
.send({
@ -533,7 +529,8 @@ export class FixedRateExchange {
*/
public async getRate(exchangeId: string): Promise<string> {
const weiRate = await this.contract.methods.getRate(exchangeId).call()
return this.web3.utils.fromWei(weiRate)
const rate = await this.web3.utils.fromWei(weiRate)
return rate
}
/**
@ -592,7 +589,7 @@ export class FixedRateExchange {
.calcBaseInGivenOutDT(
exchangeId,
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount),
this.web3.utils.toWei(consumeMarketFee)
await this.amountToUnits(fixedRateExchange.baseToken, consumeMarketFee)
)
.call()
@ -629,16 +626,12 @@ export class FixedRateExchange {
datatokenAmount: string,
consumeMarketFee: string = '0'
): Promise<string> {
const exchange = await this.getExchange(exchangeId)
const result = await this.contract.methods
.calcBaseOutGivenInDT(
exchangeId,
await this.amountToUnits(
(
await this.getExchange(exchangeId)
).datatoken,
datatokenAmount
),
this.web3.utils.toWei(consumeMarketFee)
await this.amountToUnits(exchange.datatoken, datatokenAmount),
await this.amountToUnits(exchange.baseToken, consumeMarketFee)
)
.call()

View File

@ -100,7 +100,7 @@ export class Provider {
},
signal: signal
})
return String((await response.json()).nonce)
return (await response.json()).nonce.toString()
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed')
@ -623,7 +623,7 @@ export class Provider {
let signatureMessage = accountId
signatureMessage += jobId
signatureMessage += String(index)
signatureMessage += index.toString()
signatureMessage += nonce
const signature = await this.createHashSignature(web3, accountId, signatureMessage)

View File

@ -87,7 +87,10 @@ export function getFreCreationParams(freParams: FreCreationParams): any {
}
}
export function getPoolCreationParams(poolParams: PoolCreationParams): any {
export async function getPoolCreationParams(
web3: Web3,
poolParams: PoolCreationParams
): Promise<any> {
return {
addresses: [
poolParams.ssContract,
@ -102,7 +105,11 @@ export function getPoolCreationParams(poolParams: PoolCreationParams): any {
poolParams.baseTokenDecimals,
Web3.utils.toWei(poolParams.vestingAmount),
poolParams.vestedBlocks,
Web3.utils.toWei(poolParams.initialBaseTokenLiquidity)
await amountToUnits(
web3,
poolParams.baseTokenAddress,
poolParams.initialBaseTokenLiquidity
)
],
swapFees: [
Web3.utils.toWei(poolParams.swapFeeLiquidityProvider),

View File

@ -12,7 +12,13 @@ import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispen
import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json'
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
import { allowance, amountToUnits, approve, LoggerInstance } from '../../../../src/utils'
import {
allowance,
amountToUnits,
approve,
LoggerInstance,
unitsToAmount
} from '../../../../src/utils'
import { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory'
import { Pool } from '../../../../src/pools/balancer/Pool'
import {
@ -606,7 +612,9 @@ describe('Pool unit test', () => {
baseTokenDecimals: await usdcContract.methods.decimals().call(),
vestingAmount: '10000',
vestedBlocks: 2500000,
initialBaseTokenLiquidity: web3.utils.fromWei(
initialBaseTokenLiquidity: await unitsToAmount(
web3,
contracts.usdcAddress,
await amountToUnits(web3, contracts.usdcAddress, '2000')
),
swapFeeLiquidityProvider: '0.001',

View File

@ -12,7 +12,13 @@ import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispen
import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json'
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
import { allowance, amountToUnits, approve, LoggerInstance } from '../../../../src/utils'
import {
allowance,
amountToUnits,
approve,
LoggerInstance,
unitsToAmount
} from '../../../../src/utils'
import { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory'
import { Pool } from '../../../../src/pools/balancer/Pool'
import { SideStaking } from '../../../../src/pools/ssContracts/SideStaking'
@ -410,7 +416,9 @@ describe('SideStaking unit test', () => {
baseTokenDecimals: await usdcContract.methods.decimals().call(),
vestingAmount: '10000',
vestedBlocks: 2500000,
initialBaseTokenLiquidity: web3.utils.fromWei(
initialBaseTokenLiquidity: await unitsToAmount(
web3,
contracts.usdcAddress,
await amountToUnits(web3, contracts.usdcAddress, '2000')
),
swapFeeLiquidityProvider: '0.001',