From 8110d8cd78e0ef9e310407e115c920d1724f258f Mon Sep 17 00:00:00 2001 From: gregfromstl Date: Thu, 21 Nov 2024 20:53:39 +0000 Subject: [PATCH] [SDK] Fix: Hedera toWei Conversion (#5466) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Relevant Slack discussion](https://thirdwebdev.slack.com/archives/C04P6J3K0ES/p1731616237108399?thread_ts=1730826933.868789&cid=C04P6J3K0ES) CNCT-2368 --- ## PR-Codex overview This PR focuses on adjusting the `toWei` function to accommodate different decimal values for various chains, particularly for Hedera, improving token handling in the `thirdweb` library. ### Detailed summary - Updated `toWei` function to accept a `chain` parameter for variable decimal handling. - Modified `useSendToken.ts` to pass `activeChain` to `toWei`. - Changed `TransferConfirmationScreen.tsx` to use `chain` for `toWei`. - Adjusted `zkDeployCreate2Factory.ts` to use `options.chain` in `toWei`. - Updated `deposit.ts` to consider `options.contract.chain` in `toWei`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .changeset/bright-mayflies-think.md | 5 +++++ .../zksync/zkDeployCreate2Factory.ts | 2 +- .../src/extensions/erc20/write/deposit.ts | 4 +++- .../react/core/hooks/wallets/useSendToken.ts | 2 +- .../Buy/swap/TransferConfirmationScreen.tsx | 2 +- packages/thirdweb/src/utils/units.ts | 18 ++++++++++++++++-- 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .changeset/bright-mayflies-think.md diff --git a/.changeset/bright-mayflies-think.md b/.changeset/bright-mayflies-think.md new file mode 100644 index 00000000000..7ef459d23ea --- /dev/null +++ b/.changeset/bright-mayflies-think.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Account for Hedera wei decimals difference diff --git a/packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts b/packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts index 0aebba6ff7b..863089968c3 100644 --- a/packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts +++ b/packages/thirdweb/src/contract/deployment/zksync/zkDeployCreate2Factory.ts @@ -41,7 +41,7 @@ export async function zkDeployCreate2Factory( privateKey: PUBLISHED_PRIVATE_KEY, }); - const valueToSend = toWei("0.01"); + const valueToSend = toWei("0.01", options.chain); const balance = await getWalletBalance({ address: create2Signer.address, chain: options.chain, diff --git a/packages/thirdweb/src/extensions/erc20/write/deposit.ts b/packages/thirdweb/src/extensions/erc20/write/deposit.ts index 91dc7c11985..404b649cc70 100644 --- a/packages/thirdweb/src/extensions/erc20/write/deposit.ts +++ b/packages/thirdweb/src/extensions/erc20/write/deposit.ts @@ -29,7 +29,9 @@ export type DepositParams = */ export function deposit(options: BaseTransactionOptions) { const value = - "amountWei" in options ? options.amountWei : toWei(options.amount); + "amountWei" in options + ? options.amountWei + : toWei(options.amount, options.contract.chain); return prepareContractCall({ contract: options.contract, method: [FN_SELECTOR, [], []] as const, diff --git a/packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts b/packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts index f8afaeb06fe..4dd424a52bb 100644 --- a/packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts +++ b/packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts @@ -79,7 +79,7 @@ export function useSendToken(client: ThirdwebClient) { chain: activeChain, client, to, - value: toWei(amount), + value: toWei(amount, activeChain), }); await sendTransaction({ diff --git a/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/TransferConfirmationScreen.tsx b/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/TransferConfirmationScreen.tsx index c675df5f686..630b10169a0 100644 --- a/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/TransferConfirmationScreen.tsx +++ b/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/TransferConfirmationScreen.tsx @@ -217,7 +217,7 @@ export function TransferConfirmationScreen( client, chain, to: receiverAddress, - value: toWei(tokenAmount), + value: toWei(tokenAmount, chain), }) : transfer({ contract: getContract({ diff --git a/packages/thirdweb/src/utils/units.ts b/packages/thirdweb/src/utils/units.ts index a9a2d56a999..3af1da0ebf7 100644 --- a/packages/thirdweb/src/utils/units.ts +++ b/packages/thirdweb/src/utils/units.ts @@ -1,3 +1,5 @@ +import type { Chain } from "../chains/types.js"; + /** * Converts a given number of units to a string representation with a specified number of decimal places. * @param units - The number of units to convert. @@ -125,10 +127,22 @@ export function toUnits(tokens: string, decimals: number): bigint { * toWei('1') * // 1000000000000000000n * ``` + * + * For chains with varying decimals, you can provide the chain. + * ```ts + * import { toWei } from "thirdweb/utils"; + * toWei('1', defineChain(295)) + * // 10000000n + * ``` * @utils */ -export function toWei(tokens: string) { - return toUnits(tokens, 18); +export function toWei(tokens: string, chain?: Chain) { + switch (chain?.id) { + case 295: + return toUnits(tokens, 8); + default: + return toUnits(tokens, 18); + } } /**