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

[SDK] add two new utilities toEther and toWei #421

Merged
merged 16 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-birds-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": major
---

jnsdls marked this conversation as resolved.
Show resolved Hide resolved
Add new utilies toEther, toWei, toUnits and formatUnits
19 changes: 19 additions & 0 deletions packages/sdk/src/evm/common/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ export async function normalizeAmount(
const decimals = await contractWrapper.readContract.decimals();
return utils.parseUnits(AmountSchema.parse(amount), decimals);
}

export function toEther(amount: Amount): string {
return utils.formatEther(AmountSchema.parse(amount));
}

export function toWei(amount: BigNumber | string): BigNumber {
yehia67 marked this conversation as resolved.
Show resolved Hide resolved
return utils.parseEther(AmountSchema.parse(amount));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you do y have to parse anything, just pass the BigNumberish value in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"pasreEther" params accept string only parseEther(ether: string)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right so it should be the opposite then:

toEther(amount: BigNumberish)
and
toWei(amount: Amount)

}

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

export function formatUnits(
jnsdls marked this conversation as resolved.
Show resolved Hide resolved
amount: BigNumber | string,
yehia67 marked this conversation as resolved.
Show resolved Hide resolved
decimals: number,
): string {
return utils.formatUnits(AmountSchema.parse(amount), decimals);
jnsdls marked this conversation as resolved.
Show resolved Hide resolved
}