Skip to content

Instantly share code, notes, and snippets.

@maxidev
Created March 17, 2025 13:59
Show Gist options
  • Select an option

  • Save maxidev/350df179b7bd19c48532cc4e82a21a4f to your computer and use it in GitHub Desktop.

Select an option

Save maxidev/350df179b7bd19c48532cc4e82a21a4f to your computer and use it in GitHub Desktop.
unsigned_tx_hash_native_transfer.js
import { ethers } from "ethers";
// Conexión al proveedor de Arbitrum (no necesitamos wallet ni clave privada)
const provider = new ethers.JsonRpcProvider("https://arb1.arbitrum.io/rpc");
// Función para crear una transacción raw sin firmar
async function createUnsignedRawTransaction() {
try {
// 1. Definir los parámetros de la transacción
const tx = {
to: "0x..", // Dirección de destino
value: ethers.parseEther("0.01"), // Cantidad en ETH (0.01 ETH)
gasLimit: 21000, // Límite de gas
gasPrice: ethers.parseUnits("0.1", "gwei"), // Precio del gas (para tipo 0 o 1)
nonce: 1, // Nonce ficticio (ajústalo según el caso real)
chainId: 42161, // Chain ID de Arbitrum Mainnet
data: "0x", // Datos opcionales (vacío aquí)
// Opcional: Si usas EIP-1559 (tipo 2), reemplaza gasPrice por:
// type: 2,
// maxFeePerGas: ethers.parseUnits("0.1", "gwei"),
// maxPriorityFeePerGas: ethers.parseUnits("0.01", "gwei"),
};
// 2. Crear la transacción sin firmar
const unsignedTx = ethers.Transaction.from(tx);
// 3. Serializar la transacción en formato raw (RLP)
const serializedUnsignedTx = unsignedTx.unsignedSerialized;
// 4. Calcular el hash de la transacción sin firmar
const txHash = ethers.keccak256(serializedUnsignedTx);
// Output final: La transacción raw sin firmar y su hash
return {
rawUnsignedTx: serializedUnsignedTx,
unsignedHash: unsignedTx.unsignedHash,
};
} catch (error) {
console.error("Error:", error);
}
}
// Ejecutar la función
createUnsignedRawTransaction().then((result) => {
console.log("Resultado final:", result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment