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 all 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
7 changes: 3 additions & 4 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 Expand Up @@ -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
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
55 changes: 24 additions & 31 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 @@ -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>}
*/
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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({
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

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
Loading