WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d35ea09

Browse files
fix: remove hex amount leading zeros (#419)
1 parent c9cd0a2 commit d35ea09

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/components/transactions/swapComparison.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ const V4_BASE_ACTIONS_ABI_DEFINITION = {
4343
};
4444

4545
// Helper functions for input validation and parsing
46+
function stripLeadingZeros(hexString) {
47+
// Handle 0x0 or 0x00...0 -> return 0x0
48+
if (hexString === '0x' || /^0x0+$/u.test(hexString)) {
49+
return '0x0';
50+
}
51+
// Remove leading zeros: 0x0110d... -> 0x110d...
52+
return hexString.replace(/^0x0+/u, '0x');
53+
}
54+
4655
function isValidAddress(address) {
4756
return ethers.utils.isAddress(address);
4857
}
@@ -58,8 +67,10 @@ function parseEthAmount(input) {
5867
if (isNaN(parsed) || parsed < 0) {
5968
return null;
6069
}
61-
// Convert ETH to wei and then to hex
62-
return ethers.utils.parseEther(value.toString()).toHexString();
70+
// Convert ETH to wei and then to hex, stripping leading zeros
71+
return stripLeadingZeros(
72+
ethers.utils.parseEther(value.toString()).toHexString(),
73+
);
6374
} catch {
6475
return null;
6576
}
@@ -104,7 +115,9 @@ function getConfigValues() {
104115

105116
const ethAmountHex =
106117
parseEthAmount(ethAmountInput) ||
107-
ethers.utils.parseEther(DEFAULT_ETH_AMOUNT).toHexString();
118+
stripLeadingZeros(
119+
ethers.utils.parseEther(DEFAULT_ETH_AMOUNT).toHexString(),
120+
);
108121

109122
const feeBips =
110123
parseFeePercentage(feePercentageInput) || DEFAULT_FEE_PERCENTAGE * 100;

0 commit comments

Comments
 (0)