Use shared decodeCalldata in approval view
Replace the local decodeCalldata function in approval.js with an import from the new shared module.
This commit is contained in:
@@ -3,6 +3,7 @@ const { state, saveState } = require("../../shared/state");
|
|||||||
const { formatEther, formatUnits, Interface, toUtf8String } = require("ethers");
|
const { formatEther, formatUnits, Interface, toUtf8String } = require("ethers");
|
||||||
const { ERC20_ABI } = require("../../shared/constants");
|
const { ERC20_ABI } = require("../../shared/constants");
|
||||||
const { TOKEN_BY_ADDRESS } = require("../../shared/tokenList");
|
const { TOKEN_BY_ADDRESS } = require("../../shared/tokenList");
|
||||||
|
const { decodeCalldata } = require("../../shared/decodeCalldata");
|
||||||
const txStatus = require("./txStatus");
|
const txStatus = require("./txStatus");
|
||||||
const uniswap = require("../../shared/uniswap");
|
const uniswap = require("../../shared/uniswap");
|
||||||
|
|
||||||
@@ -41,91 +42,7 @@ function etherscanTokenLink(address) {
|
|||||||
return `https://etherscan.io/token/${address}`;
|
return `https://etherscan.io/token/${address}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to decode calldata using known ABIs.
|
// decodeCalldata is now in ../../shared/decodeCalldata.js
|
||||||
// Returns { name, description, details } or null.
|
|
||||||
function decodeCalldata(data, toAddress) {
|
|
||||||
if (!data || data === "0x" || data.length < 10) return null;
|
|
||||||
|
|
||||||
// Try ERC-20 (approve / transfer)
|
|
||||||
try {
|
|
||||||
const parsed = erc20Iface.parseTransaction({ data });
|
|
||||||
if (parsed) {
|
|
||||||
const token = TOKEN_BY_ADDRESS.get(toAddress.toLowerCase());
|
|
||||||
const tokenSymbol = token ? token.symbol : null;
|
|
||||||
const tokenDecimals = token ? token.decimals : 18;
|
|
||||||
const contractLabel = tokenSymbol
|
|
||||||
? tokenSymbol + " (" + toAddress + ")"
|
|
||||||
: toAddress;
|
|
||||||
|
|
||||||
if (parsed.name === "approve") {
|
|
||||||
const spender = parsed.args[0];
|
|
||||||
const rawAmount = parsed.args[1];
|
|
||||||
const maxUint = BigInt(
|
|
||||||
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
||||||
);
|
|
||||||
const isUnlimited = rawAmount === maxUint;
|
|
||||||
const amountStr = isUnlimited
|
|
||||||
? "Unlimited"
|
|
||||||
: formatTxValue(formatUnits(rawAmount, tokenDecimals)) +
|
|
||||||
(tokenSymbol ? " " + tokenSymbol : "");
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: "Token Approval",
|
|
||||||
description: tokenSymbol
|
|
||||||
? "Approve spending of your " + tokenSymbol
|
|
||||||
: "Approve spending of an ERC-20 token",
|
|
||||||
details: [
|
|
||||||
{
|
|
||||||
label: "Token",
|
|
||||||
value: contractLabel,
|
|
||||||
address: toAddress,
|
|
||||||
isToken: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: "Spender",
|
|
||||||
value: spender,
|
|
||||||
address: spender,
|
|
||||||
},
|
|
||||||
{ label: "Amount", value: amountStr },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.name === "transfer") {
|
|
||||||
const to = parsed.args[0];
|
|
||||||
const rawAmount = parsed.args[1];
|
|
||||||
const amountStr =
|
|
||||||
formatTxValue(formatUnits(rawAmount, tokenDecimals)) +
|
|
||||||
(tokenSymbol ? " " + tokenSymbol : "");
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: "Token Transfer",
|
|
||||||
description: tokenSymbol
|
|
||||||
? "Transfer " + tokenSymbol
|
|
||||||
: "Transfer ERC-20 token",
|
|
||||||
details: [
|
|
||||||
{
|
|
||||||
label: "Token",
|
|
||||||
value: contractLabel,
|
|
||||||
address: toAddress,
|
|
||||||
isToken: true,
|
|
||||||
},
|
|
||||||
{ label: "Recipient", value: to, address: to },
|
|
||||||
{ label: "Amount", value: amountStr },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Not ERC-20 — fall through
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try Uniswap Universal Router
|
|
||||||
const routerResult = uniswap.decode(data, toAddress);
|
|
||||||
if (routerResult) return routerResult;
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function showTxApproval(details) {
|
function showTxApproval(details) {
|
||||||
const toAddr = details.txParams.to;
|
const toAddr = details.txParams.to;
|
||||||
|
|||||||
Reference in New Issue
Block a user