Skip to content

Commit

Permalink
[SDK] add two new utilities toEther and toWei (#421)
Browse files Browse the repository at this point in the history
Co-authored-by: Nacho Iacovino <50103937+nachoiacovino@users.noreply.github.com>
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
  • Loading branch information
3 people committed Dec 8, 2022
1 parent 699a2b1 commit ea95c5f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-toys-warn.md
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

Add new utilities to parse/format currencies. `toEther` & `toWei` as default 18 decimal and `toUnits` & `toDisplayValue` in case of different numbers of decimals.
16 changes: 16 additions & 0 deletions packages/sdk/src/evm/common/currency.ts
Expand Up @@ -192,3 +192,19 @@ export async function normalizeAmount(
const decimals = await contractWrapper.readContract.decimals();
return utils.parseUnits(AmountSchema.parse(amount), decimals);
}

export function toEther(amount: BigNumberish): string {
return utils.formatEther(amount);
}

export function toWei(amount: Amount): BigNumber {
return utils.parseEther(AmountSchema.parse(amount));
}

export function toUnits(amount: Amount, decimals: BigNumberish): BigNumber {
return utils.parseUnits(AmountSchema.parse(amount), decimals);
}

export function toDisplayValue(amount: BigNumberish, decimals: BigNumberish): string {
return utils.formatUnits(amount, decimals);
}

0 comments on commit ea95c5f

Please sign in to comment.