优化钱包兼容性:移除 value:0x0 以兼容更多钱包、增强币安检测和网络切换提示
This commit is contained in:
@@ -365,6 +365,7 @@ function detectWalletLabel(
|
|||||||
if (!provider && !detail) return "EVM 钱包";
|
if (!provider && !detail) return "EVM 钱包";
|
||||||
const announcedName = String(detail?.info?.name || "").trim();
|
const announcedName = String(detail?.info?.name || "").trim();
|
||||||
const announcedRdns = String(detail?.info?.rdns || "").toLowerCase();
|
const announcedRdns = String(detail?.info?.rdns || "").toLowerCase();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
provider?.isOkxWallet ||
|
provider?.isOkxWallet ||
|
||||||
announcedName.toLowerCase().includes("okx") ||
|
announcedName.toLowerCase().includes("okx") ||
|
||||||
@@ -372,13 +373,6 @@ function detectWalletLabel(
|
|||||||
) {
|
) {
|
||||||
return "OKX Wallet";
|
return "OKX Wallet";
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
provider?.isMetaMask ||
|
|
||||||
announcedName.toLowerCase().includes("metamask") ||
|
|
||||||
announcedRdns.includes("metamask")
|
|
||||||
) {
|
|
||||||
return "MetaMask";
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
provider?.isRabby ||
|
provider?.isRabby ||
|
||||||
announcedName.toLowerCase().includes("rabby") ||
|
announcedName.toLowerCase().includes("rabby") ||
|
||||||
@@ -396,11 +390,19 @@ function detectWalletLabel(
|
|||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
(provider as any)?.isBinance ||
|
(provider as any)?.isBinance ||
|
||||||
|
(provider as any)?.bnbSign ||
|
||||||
announcedName.toLowerCase().includes("binance") ||
|
announcedName.toLowerCase().includes("binance") ||
|
||||||
announcedRdns.includes("binance")
|
announcedRdns.includes("binance")
|
||||||
) {
|
) {
|
||||||
return "Binance Web3 Wallet";
|
return "Binance Web3 Wallet";
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
provider?.isMetaMask ||
|
||||||
|
announcedName.toLowerCase().includes("metamask") ||
|
||||||
|
announcedRdns.includes("metamask")
|
||||||
|
) {
|
||||||
|
return "MetaMask";
|
||||||
|
}
|
||||||
if (announcedName) return announcedName;
|
if (announcedName) return announcedName;
|
||||||
return "EVM 钱包";
|
return "EVM 钱包";
|
||||||
}
|
}
|
||||||
@@ -1948,19 +1950,29 @@ export function AccountCenter() {
|
|||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const code = Number(err?.code);
|
const code = Number(err?.code);
|
||||||
if (code !== 4902 || targetChainId !== 137) throw err;
|
const msg = String(err?.message || "").toLowerCase();
|
||||||
await eth.request({
|
|
||||||
method: "wallet_addEthereumChain",
|
// If the error code indicates the chain is not added (4902), or it's Polygon (137)
|
||||||
params: [
|
if (code === 4902 || targetChainId === 137) {
|
||||||
{
|
try {
|
||||||
chainId: "0x89",
|
await eth.request({
|
||||||
chainName: "Polygon Mainnet",
|
method: "wallet_addEthereumChain",
|
||||||
nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
|
params: [
|
||||||
rpcUrls: ["https://polygon-rpc.com"],
|
{
|
||||||
blockExplorerUrls: ["https://polygonscan.com"],
|
chainId: "0x89",
|
||||||
},
|
chainName: "Polygon Mainnet",
|
||||||
],
|
nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
|
||||||
});
|
rpcUrls: ["https://polygon-rpc.com"],
|
||||||
|
blockExplorerUrls: ["https://polygonscan.com"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} catch (addErr: any) {
|
||||||
|
err = addErr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error(`请在钱包中手动切换到 Polygon (Matic) 网络后再试。(${err?.message || "网络切换失败"})`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2351,16 +2363,14 @@ export function AccountCenter() {
|
|||||||
|
|
||||||
if (allowance < amountUnits) {
|
if (allowance < amountUnits) {
|
||||||
setPaymentInfo(`检测到授权不足,正在发起 ${tokenSymbol} 授权...`);
|
setPaymentInfo(`检测到授权不足,正在发起 ${tokenSymbol} 授权...`);
|
||||||
|
const approveParams: Record<string, any> = {
|
||||||
|
from: payingWallet,
|
||||||
|
to: tokenAddress,
|
||||||
|
data: buildApproveCalldata(txPayload.to, amountUnits),
|
||||||
|
};
|
||||||
const approveHash = (await eth.request({
|
const approveHash = (await eth.request({
|
||||||
method: "eth_sendTransaction",
|
method: "eth_sendTransaction",
|
||||||
params: [
|
params: [approveParams],
|
||||||
{
|
|
||||||
from: payingWallet,
|
|
||||||
to: tokenAddress,
|
|
||||||
data: buildApproveCalldata(txPayload.to, amountUnits),
|
|
||||||
value: "0x0",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})) as string;
|
})) as string;
|
||||||
await waitForReceipt(String(approveHash || ""));
|
await waitForReceipt(String(approveHash || ""));
|
||||||
approvedInThisRun = true;
|
approvedInThisRun = true;
|
||||||
@@ -2369,16 +2379,18 @@ export function AccountCenter() {
|
|||||||
setPaymentInfo("授权额度充足,正在发起支付...");
|
setPaymentInfo("授权额度充足,正在发起支付...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const payParams: Record<string, any> = {
|
||||||
|
from: payingWallet,
|
||||||
|
to: txPayload.to,
|
||||||
|
data: txPayload.data,
|
||||||
|
};
|
||||||
|
if (txPayload.value && txPayload.value !== "0x0" && txPayload.value !== "0") {
|
||||||
|
payParams.value = txPayload.value;
|
||||||
|
}
|
||||||
|
|
||||||
const txHash = (await eth.request({
|
const txHash = (await eth.request({
|
||||||
method: "eth_sendTransaction",
|
method: "eth_sendTransaction",
|
||||||
params: [
|
params: [payParams],
|
||||||
{
|
|
||||||
from: payingWallet,
|
|
||||||
to: txPayload.to,
|
|
||||||
data: txPayload.data,
|
|
||||||
value: txPayload.value || "0x0",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})) as string;
|
})) as string;
|
||||||
const txHashNorm = String(txHash || "").toLowerCase();
|
const txHashNorm = String(txHash || "").toLowerCase();
|
||||||
setLastTxHash(txHashNorm);
|
setLastTxHash(txHashNorm);
|
||||||
|
|||||||
Reference in New Issue
Block a user