Skip to content

Commit

Permalink
Add utils fn
Browse files Browse the repository at this point in the history
Add utils functions that help convert number to token precision.
  • Loading branch information
r-czajkowski committed Jul 14, 2023
1 parent fecc080 commit fe8b96e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/hooks/tbtc/useSubscribeToRedemptionRequestedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useBridgeContract } from "./useBridgeContract"
import { tbtcSlice } from "../../store/tbtc"
import { BigNumber, Event } from "ethers"
import { useThreshold } from "../../contexts/ThresholdContext"
import { fromSatoshiToTokenPrecision } from "../../threshold-ts/utils"

export const useSubscribeToRedemptionRequestedEvent = () => {
const contract = useBridgeContract()
Expand Down Expand Up @@ -37,7 +38,7 @@ export const useSubscribeToRedemptionRequestedEvent = () => {
tbtcSlice.actions.redemptionRequested({
// TODO: Take into account fees, see
// https://github.com/threshold-network/token-dashboard/pull/569
amount: requestedAmount.mul(BigNumber.from(10).pow(10)).toString(),
amount: fromSatoshiToTokenPrecision(requestedAmount).toString(),
txHash: event.transactionHash,
redemptionKey,
blockNumber: event.blockNumber,
Expand Down
3 changes: 2 additions & 1 deletion src/threshold-ts/tbtc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isSameETHAddress,
AddressZero,
isPayToScriptHashTypeAddress,
fromSatoshiToTokenPrecision,
} from "../utils"
import {
Client,
Expand Down Expand Up @@ -883,7 +884,7 @@ export class TBTC implements ITBTC {
// handled sucesfully the `getRedemptionRequest` returns `0`. The
// `amount` in event is in satoshi, so here we convert to token
// precision.
amount: this._satoshiMultiplier.mul(event.amount).toString(),
amount: fromSatoshiToTokenPrecision(event.amount).toString(),
activityKey: redemptionKey,
bridgeProcess: "unmint",
blockNumber: event.blockNumber,
Expand Down
2 changes: 2 additions & 0 deletions src/threshold-ts/utils/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
validate,
} from "bitcoin-address-validation"

export const BITCOIN_PRECISION = 8

export const isValidBtcAddress = (
address: string,
network: BitcoinNetwork = BitcoinNetwork.Mainnet
Expand Down
10 changes: 10 additions & 0 deletions src/threshold-ts/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BigNumberish } from "ethers"
import { defaultAbiCoder } from "ethers/lib/utils"
import { BITCOIN_PRECISION } from "./bitcoin"
import { to1ePrecision } from "./math"

export const isValidType = (paramType: string, value: string) => {
try {
Expand All @@ -8,3 +11,10 @@ export const isValidType = (paramType: string, value: string) => {
return false
}
}

export const fromSatoshiToTokenPrecision = (
value: BigNumberish,
tokenPrecision: number = 18
) => {
return to1ePrecision(value, tokenPrecision - BITCOIN_PRECISION)
}
9 changes: 9 additions & 0 deletions src/threshold-ts/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export const min = (a: BigNumberish, b: BigNumberish) => {
export const max = (a: BigNumberish, b: BigNumberish) => {
return compare(a, b, "gt")
}

export function to1ePrecision(n: BigNumberish, precision: number): BigNumber {
const decimalMultiplier = BigNumber.from(10).pow(precision)
return BigNumber.from(n).mul(decimalMultiplier)
}

export function to1e18(n: BigNumberish): BigNumber {
return to1ePrecision(n, 18)
}

0 comments on commit fe8b96e

Please sign in to comment.