Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace toWei/fromWei when base token conversion #1318

Merged
merged 21 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/factories/NFTFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
100 changes: 70 additions & 30 deletions src/pools/balancer/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -817,8 +833,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
Expand Down Expand Up @@ -859,29 +875,33 @@ export class Pool {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
}

amountsInOutMaxFee.tokenAmountIn = await amountToUnits(
const estGas = await this.estSwapExactAmountIn(
address,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const tokenAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn
)

amountsInOutMaxFee.minAmountOut = await amountToUnits(
const minAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.minAmountOut
)

let result = null

const estGas = await this.estSwapExactAmountIn(
address,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? await amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256

try {
Expand All @@ -893,8 +913,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.tokenAmountIn,
amountsInOutMaxFee.minAmountOut,
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
Expand Down Expand Up @@ -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
Expand All @@ -950,8 +986,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
Expand Down Expand Up @@ -988,27 +1024,31 @@ export class Pool {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

amountsInOutMaxFee.maxAmountIn = await amountToUnits(
const estGas = await this.estSwapExactAmountOut(
account,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const maxAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn
)

amountsInOutMaxFee.tokenAmountOut = await amountToUnits(
const tokenAmountOut = await amountToUnits(
this.web3,
tokenInOutMarket.tokenOut,
amountsInOutMaxFee.tokenAmountOut
)

const estGas = await this.estSwapExactAmountOut(
account,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const maxPrice = amountsInOutMaxFee.maxPrice
? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice)
? amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: MaxUint256

try {
Expand All @@ -1020,8 +1060,8 @@ export class Pool {
tokenInOutMarket.marketFeeAddress
],
[
amountsInOutMaxFee.maxAmountIn,
amountsInOutMaxFee.tokenAmountOut,
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
Expand Down
50 changes: 24 additions & 26 deletions src/pools/fixedRate/FixedRateExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -362,13 +362,14 @@ export class FixedRateExchange {
exchangeId: string,
newRate: string
): Promise<TransactionReceipt> {
const exchange = await this.getExchange(exchangeId)
const estGas = await this.estSetRate(
address,
exchangeId,
this.web3.utils.toWei(String(newRate))
await this.amountToUnits(exchange.baseToken, String(newRate))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why noy toString()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what was used above so I used to be consistent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange, we always use toString, don't know how that stayed there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to update all String(value) in other places if you find more of this 😃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will

)
const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(newRate))
.setRate(exchangeId, await this.amountToUnits(exchange.baseToken, String(newRate)))
.send({
from: address,
gas: estGas + 1,
Expand Down Expand Up @@ -592,7 +593,7 @@ export class FixedRateExchange {
.calcBaseInGivenOutDT(
exchangeId,
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount),
this.web3.utils.toWei(consumeMarketFee)
await this.amountToUnits(fixedRateExchange.baseToken, consumeMarketFee)
)
.call()

Expand Down Expand Up @@ -629,16 +630,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),
this.unitsToAmount(exchange.baseToken, consumeMarketFee)
)
.call()

Expand Down Expand Up @@ -1089,13 +1086,14 @@ export class FixedRateExchange {
exchangeId: string,
newMarketFee: string
): Promise<TransactionReceipt> {
const exchange = await this.getExchange(exchangeId)
const estGas = await this.estSetRate(
address,
exchangeId,
this.web3.utils.toWei(newMarketFee)
await this.unitsToAmount(exchange.baseToken, newMarketFee)
)
const trxReceipt = await this.contract.methods
.updateMarketFee(exchangeId, this.web3.utils.toWei(newMarketFee))
.updateMarketFee(exchangeId, this.unitsToAmount(exchange.baseToken, newMarketFee))
.send({
from: address,
gas: estGas + 1,
Expand Down
11 changes: 9 additions & 2 deletions src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
12 changes: 10 additions & 2 deletions test/unit/pools/balancer/Pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 10 additions & 2 deletions test/unit/pools/ssContracts/SideStaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand Down