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); + } } /**