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

View File

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

View File

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

View File

@ -100,7 +100,7 @@ export class Provider {
}, },
signal: signal signal: signal
}) })
return String((await response.json()).nonce) return (await response.json()).nonce.toString()
} catch (e) { } catch (e) {
LoggerInstance.error(e) LoggerInstance.error(e)
throw new Error('HTTP request failed') throw new Error('HTTP request failed')
@ -623,7 +623,7 @@ export class Provider {
let signatureMessage = accountId let signatureMessage = accountId
signatureMessage += jobId signatureMessage += jobId
signatureMessage += String(index) signatureMessage += index.toString()
signatureMessage += nonce signatureMessage += nonce
const signature = await this.createHashSignature(web3, accountId, signatureMessage) 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 { return {
addresses: [ addresses: [
poolParams.ssContract, poolParams.ssContract,
@ -102,7 +105,11 @@ export function getPoolCreationParams(poolParams: PoolCreationParams): any {
poolParams.baseTokenDecimals, poolParams.baseTokenDecimals,
Web3.utils.toWei(poolParams.vestingAmount), Web3.utils.toWei(poolParams.vestingAmount),
poolParams.vestedBlocks, poolParams.vestedBlocks,
Web3.utils.toWei(poolParams.initialBaseTokenLiquidity) await amountToUnits(
web3,
poolParams.baseTokenAddress,
poolParams.initialBaseTokenLiquidity
)
], ],
swapFees: [ swapFees: [
Web3.utils.toWei(poolParams.swapFeeLiquidityProvider), 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 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 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 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 { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory'
import { Pool } from '../../../../src/pools/balancer/Pool' import { Pool } from '../../../../src/pools/balancer/Pool'
import { import {
@ -606,7 +612,9 @@ describe('Pool unit test', () => {
baseTokenDecimals: await usdcContract.methods.decimals().call(), baseTokenDecimals: await usdcContract.methods.decimals().call(),
vestingAmount: '10000', vestingAmount: '10000',
vestedBlocks: 2500000, vestedBlocks: 2500000,
initialBaseTokenLiquidity: web3.utils.fromWei( initialBaseTokenLiquidity: await unitsToAmount(
web3,
contracts.usdcAddress,
await amountToUnits(web3, contracts.usdcAddress, '2000') await amountToUnits(web3, contracts.usdcAddress, '2000')
), ),
swapFeeLiquidityProvider: '0.001', 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 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 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 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 { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory'
import { Pool } from '../../../../src/pools/balancer/Pool' import { Pool } from '../../../../src/pools/balancer/Pool'
import { SideStaking } from '../../../../src/pools/ssContracts/SideStaking' import { SideStaking } from '../../../../src/pools/ssContracts/SideStaking'
@ -410,7 +416,9 @@ describe('SideStaking unit test', () => {
baseTokenDecimals: await usdcContract.methods.decimals().call(), baseTokenDecimals: await usdcContract.methods.decimals().call(),
vestingAmount: '10000', vestingAmount: '10000',
vestedBlocks: 2500000, vestedBlocks: 2500000,
initialBaseTokenLiquidity: web3.utils.fromWei( initialBaseTokenLiquidity: await unitsToAmount(
web3,
contracts.usdcAddress,
await amountToUnits(web3, contracts.usdcAddress, '2000') await amountToUnits(web3, contracts.usdcAddress, '2000')
), ),
swapFeeLiquidityProvider: '0.001', swapFeeLiquidityProvider: '0.001',