index.ts
path: /src/utils/index.ts
exponentToBigDecimal()
Params:
- decimals (BigInt): The power of ten to return.
ReturnType: BigDecimal
Returns the number 1
followed by decimals
number of 0s
with type BigDecimal.
It uses a for loop to iterate between 0
and decimals
and multiplies the previous result by 10
. Thus, only positive values are possible.
Dependencies:
Invoked at:
- tokenAmountToDecimal()
- priceToDecimal()
- convertTokenToDecimal()
- convertEthToDecimal
- sqrtPriceX96ToTokenPrices
safeDiv()
Params:
- amount0 (BigDecimal): Numerator for the division
- amount1 (BigDecimal): Denominator for the division
ReturnType: BigDecimal
Return 0
if parameter amount1
is equal to ZERO_BD
.
Else returns the result of dividing amount0
by amount1
using BigDecimal
's div()
method.
Dependencies:
Invoked at:
bigDecimalExponated()
Params:
- value (BigDecimal): value to be raised to a certain power
- power (BigInt): the exponent of the value to be calculated
ReturnType: BigDecimal
- Other Chains
- Arbitrum-One
If power
is ZERO_BI
, ONE_BD
is returned. value
is multipled by itself in a simple for loop executed abs(power)
number of times. If the power
is negative, uses safeDiv
to divide ONE_BD
with the result of the previous calculation. Returns the result in BigDecimal.
Dependencies:
Invoked at:
- Differs in logic to compute the exponent from other chains.
- Instead of looping and multiplying
value
throughpower
loop iterations, performs simple exponentiation by squaring
Additional Dependencies:
tokenAmountToDecimal()
Params:
- tokenAmount (BigDecimal): The amount of tokens to be divided (numerator)
- exchangeDecimals (BigInt): The power of 10 to divide the amount with
ReturnType: BigDecimal
If exchangeDecimals is ZERO_BI
, returns tokenAmount after converting to BigDecimal. Else divides the BigDecimal tokenAmount using 10 raised to exchangeDecimals
as the denominator.
Dependencies:
Invoked at:
- []
priceToDecimal()
Params:
- amount (BigDecimal): The amount to be divided (numerator)
- exchangeDecimals (BigInt): The power of 10 to divide the amount with
ReturnType: BigDecimal
If exchangeDecimals
is equal to ZERO_BI
returns the amount as it is. Otherwise uses safeDiv
to divide amount
with 10^exchangeDecimals
in BigDecimals type.
Dependencies:
Invoked at:
equalToZero()
Params:
- value (BigDecimal): Value to check whether zero
ReturnType: boolean
Converts value to string and then to float. Compares it against ZERO_BD after converting to String and then parsing as float. Returns boolean value from comparing the equality of the two float values.
Dependencies:
Invoked at:
isNullEthValue()
Params:
- value (String) - Hex String to check for Null Eth value
ReturnType: boolean
Returns boolean value. True is value == '0x0000000000000000000000000000000000000000000000000000000000000001', else false.
Invoked at:
bigDecimalExp18()
ReturnType: BigDecimal
Value: 10^18
Invoked at:
convertTokenToDecimal()
Params:
- tokenAmount (BigInt) - The amount of token value to be converted
- exchangeDecimals (BigInt) - The positive power of the exponent to divide the tokenAmount with
ReturnType: BigDecimal
- Other Chains
- Optimism
convertEthToDecimal()
Params:
- eth (BigInt) - Int value representing ether amount in wei
ReturnType: BigDecimal
Converts the value of ether in wei from integer to big decimal representing amount in ether. It converts the eth parameter to BigDecimal and then divides it with 10^18 BigDecimal value.
Dependencies:
Invoked at:
loadTransaction()
Params:
- event (ethereum.Event) - Ethereum event emitted from the transaction to return.
ReturnType: Transaction
Returns a Transaction instance for the specified event. If a transaction instance doesn't already exit for the event, it's created and then returned. Uses event.transaction.hash.toHexString()
to find the relevant transaction or to create a new transaction instance.
Uses event
parameters block.blockNumber
, block.timestamp
, transaction.gasUsed
and transaction.gasPrice
to populate transaction
's fields.
Entites:
- Transaction - Read/Create & Write