Skip to content

Commit

Permalink
Add fromSatoshi and toSatoshi to utils function
Browse files Browse the repository at this point in the history
Add `fromSatoshi` fn that converts an amount from `1e8` to `1e18` token precision. Also here we remove optional `fromPrecision` param in `toSatoshi` fn - we assume we always convert from `1e18` to `1e8` and in `fromSatoshi` we always convert from `1e8` to `1e18`.
  • Loading branch information
kkosiorowska committed Apr 9, 2024
1 parent 88f96d8 commit d7884f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./hex"
export * from "./ethereum-signer"
export * from "./backoff"
export * from "./satoshi-converter"
15 changes: 15 additions & 0 deletions sdk/src/lib/utils/satoshi-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Multiplier to convert satoshi to 1e18 precision.
*/
const SATOSHI_MULTIPLIER = 10n ** 10n

export function toSatoshi(amount: bigint) {
const remainder = amount % SATOSHI_MULTIPLIER
const satoshis = (amount - remainder) / SATOSHI_MULTIPLIER

return satoshis
}

export function fromSatoshi(amount: bigint) {
return amount * SATOSHI_MULTIPLIER
}

0 comments on commit d7884f8

Please sign in to comment.