near-safe
Version:
An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.
70 lines (69 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeTransactionSerializable = decodeTransactionSerializable;
exports.decodeRlpHex = decodeRlpHex;
exports.decodeTypedData = decodeTypedData;
exports.decodeUserOperation = decodeUserOperation;
const viem_1 = require("viem");
const deployments_1 = require("../_gen/deployments");
const multisend_1 = require("../lib/multisend");
function decodeTransactionSerializable(chainId, tx) {
const { gas, maxFeePerGas, maxPriorityFeePerGas, to } = tx;
if (chainId !== tx.chainId) {
throw Error(`Transaction chainId mismatch ${chainId} != ${tx.chainId}`);
}
if (!gas || !maxFeePerGas || !maxPriorityFeePerGas) {
throw Error(`Insufficient feeData for ${(0, viem_1.serializeTransaction)(tx)}. Check https://rawtxdecode.in/`);
}
if (!to) {
throw Error(`Transaction is missing the 'to' in ${(0, viem_1.serializeTransaction)(tx)}. Check https://rawtxdecode.in/`);
}
return {
chainId,
// This is an upper bound on the gas fees (could be lower)
costEstimate: (0, viem_1.formatEther)(gas * (maxFeePerGas + maxPriorityFeePerGas)),
transactions: [
{
to,
value: (tx.value || 0n).toString(),
data: tx.data || "0x",
},
],
};
}
function decodeRlpHex(chainId, tx) {
return decodeTransactionSerializable(chainId, (0, viem_1.parseTransaction)(tx));
}
function decodeTypedData(chainId, data) {
return {
chainId,
costEstimate: "0",
transactions: [],
message: data,
};
}
function decodeUserOperation(chainId, userOp) {
const { callGasLimit, maxFeePerGas, maxPriorityFeePerGas } = userOp;
const maxGasPrice = BigInt(maxFeePerGas) + BigInt(maxPriorityFeePerGas);
const { args } = (0, viem_1.decodeFunctionData)({
abi: deployments_1.SAFE_DEPLOYMENTS.m4337.abi,
data: userOp.callData,
});
// Determine if singular or double!
const transactions = (0, multisend_1.isMultisendTx)(args)
? (0, multisend_1.decodeMulti)(args[2])
: [
{
to: args[0],
value: args[1],
data: args[2],
operation: args[3],
},
];
return {
chainId,
// This is an upper bound on the gas fees (could be lower)
costEstimate: (0, viem_1.formatEther)(BigInt(callGasLimit) * maxGasPrice),
transactions,
};
}