Skip to content

Commit 9de6d78

Browse files
sirtimidclaude
andcommitted
fix(evm-wallet): fix funding precision and support MATIC alias on Polygon
- Use BigInt constants for smart account funding thresholds to avoid floating-point precision issues that could produce negative fund amounts - Accept both "MATIC" and "POL" as native token on Polygon (chain 137) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 15b6c22 commit 9de6d78

3 files changed

Lines changed: 26 additions & 22 deletions

File tree

packages/evm-wallet-experiment/openclaw-plugin/tools/swap.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { WalletCaller } from '../daemon.ts';
22
import type { OpenClawPluginApi } from '../types.ts';
33
import {
44
NATIVE_ETH,
5-
NATIVE_TOKEN_BY_CHAIN,
5+
NATIVE_TOKENS_BY_CHAIN,
66
errorMessage,
77
formatTxResult,
88
makeError,
@@ -29,9 +29,10 @@ async function resolveSwapToken(
2929
): Promise<
3030
{ address: string; decimals: number; symbol: string } | { error: string }
3131
> {
32-
const nativeSymbol = NATIVE_TOKEN_BY_CHAIN[chainId] ?? 'ETH';
33-
if (token === NATIVE_ETH || token.toUpperCase() === nativeSymbol) {
34-
return { address: NATIVE_ETH, decimals: 18, symbol: nativeSymbol };
32+
const nativeSymbols = NATIVE_TOKENS_BY_CHAIN[chainId] ?? ['ETH'];
33+
const primaryNative = nativeSymbols[0];
34+
if (token === NATIVE_ETH || nativeSymbols.includes(token.toUpperCase())) {
35+
return { address: NATIVE_ETH, decimals: 18, symbol: primaryNative };
3536
}
3637

3738
let resolved: { address: string; symbol?: string };

packages/evm-wallet-experiment/openclaw-plugin/utils.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ export const HEX_VALUE_RE = /^0x[\da-f]+$/iu;
77
export const NATIVE_ETH = '0x0000000000000000000000000000000000000000';
88

99
/**
10-
* Native token symbol for each supported chain.
11-
* Only the chain's own native token should resolve to the zero address.
10+
* Native token symbols for each supported chain.
11+
* Only the chain's own native token(s) should resolve to the zero address.
12+
* Polygon accepts both "POL" (current) and "MATIC" (legacy).
1213
*/
13-
export const NATIVE_TOKEN_BY_CHAIN: Record<number, string> = {
14-
1: 'ETH',
15-
10: 'ETH',
16-
56: 'BNB',
17-
137: 'POL',
18-
8453: 'ETH',
19-
42161: 'ETH',
20-
59144: 'ETH',
21-
11155111: 'ETH',
14+
export const NATIVE_TOKENS_BY_CHAIN: Record<number, string[]> = {
15+
1: ['ETH'],
16+
10: ['ETH'],
17+
56: ['BNB'],
18+
137: ['POL', 'MATIC'],
19+
8453: ['ETH'],
20+
42161: ['ETH'],
21+
59144: ['ETH'],
22+
11155111: ['ETH'],
2223
};
2324

2425
const BLOCK_EXPLORER_URLS: Record<number, string> = {

packages/evm-wallet-experiment/scripts/home-interactive.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,21 +581,21 @@ async function main() {
581581
// The hybrid smart account has a different address from the EOA,
582582
// so it needs its own ETH to execute value transfers.
583583
if (smartAccountAddress) {
584-
const MIN_BALANCE = 0.05;
585-
const TARGET_BALANCE = 0.1;
584+
const MIN_BALANCE_WEI = 50000000000000000n; // 0.05 ETH
585+
const TARGET_BALANCE_WEI = 100000000000000000n; // 0.1 ETH
586586

587587
info('Checking smart account balance...');
588588
const balanceHex = await signer.provider.request({
589589
method: 'eth_getBalance',
590590
params: [smartAccountAddress, 'latest'],
591591
});
592592
const balanceWei = BigInt(balanceHex);
593-
const balanceEth = Number(balanceWei) / 1e18;
594593

595-
if (balanceEth < MIN_BALANCE) {
596-
const fundWei = BigInt(Math.round(TARGET_BALANCE * 1e18)) - balanceWei;
594+
if (balanceWei < MIN_BALANCE_WEI) {
595+
const fundWei = TARGET_BALANCE_WEI - balanceWei;
596+
const balanceEthStr = (Number(balanceWei) / 1e18).toFixed(4);
597597
info(
598-
`Smart account has ${balanceEth.toFixed(4)} ETH — funding to ${TARGET_BALANCE} ETH via MetaMask...`,
598+
`Smart account has ${balanceEthStr} ETH — funding to 0.1 ETH via MetaMask...`,
599599
);
600600
try {
601601
await signer.provider.request({
@@ -618,7 +618,9 @@ async function main() {
618618
);
619619
}
620620
} else {
621-
ok(`Smart account balance: ${balanceEth.toFixed(4)} ETH`);
621+
ok(
622+
`Smart account balance: ${(Number(balanceWei) / 1e18).toFixed(4)} ETH`,
623+
);
622624
}
623625
}
624626
} else {

0 commit comments

Comments
 (0)