Back to All

The OPTIMISM network reports an error using the sendTransaction method

This is my code. It's not working.

import { Request, Response } from 'express';
import { Network, Alchemy, Wallet, Utils } from "alchemy-sdk";
const PRIVATE_KEY = "xxxxxx"; //hide
const API_KEY = "xxxx"; //hide

const settings = {
apiKey: API_KEY,
network: Network.OPT_MAINNET,
};
const alchemy = new Alchemy(settings);
let wallet = new Wallet(PRIVATE_KEY);

async function sendTx(req:Request,res:Response): Promise {
try {
const transaction = {
to: "0xe301Caf612F870E3E8E8d60C98877e0E234733fa",
value: Utils.parseEther("0.001"),
gasLimit: "21000",
maxPriorityFeePerGas: Utils.parseUnits("5", "gwei"),
maxFeePerGas: Utils.parseUnits("20", "gwei"),
nonce: await alchemy.core.getTransactionCount(wallet.getAddress()),
type: 2,
chainId: 10, // Corresponds to OPT_MAINNET
};
const rawTransaction = await wallet.signTransaction(transaction);
const tx = await alchemy.transact.sendTransaction(rawTransaction);
res.send(tx);
} catch (error:any) {
res.send(error.message);
}
}

export {
sendTx,
}


I checked PRIVATE_KEY and API_KEY were fine.
On the ETH main network, sendTransaction works normally, but an error is displayed on the OPTIMISM network.
This is an error message

"message":"processing response error (body = \ "{\ \ " jsonrpc \ \ \ ": \ \ " 2.0 \ \ \ ", \ \ \ "id \ \ " : 44, \ \ \ "the error \ \ " : {\ \ \ "code \ \ " : - 32000, \ \ \ "the message \ \ " : \ \ \ "RLP: expected input list for types.txdata\"}}", error={"code":-32000}, requestBody="{\"method\":\"eth_sendRawTransaction\",\"params\":[\"0x02f8730a0385012a05f2008504a817c800825 20894e301caf612f870e3e8e8d60c98877e0e234733fa87038d7ea4c6800080c080a0e3b6d8058ce1d8897b81137d30fe8801133d4f57d32d173034d 0b62b343df2d4a07d46f9876d71669c6d8f81ed0eeb47ef2aff8b865cec34b962361dcd5a45b64f\"],\"id\":44,\"jsonrpc\":\"2 .0\"}", requestMethod="POST", url="https://opt-mainnet.g.alchemy.com/v2/0bP2WTNFGVP74XTNPm-CIUR30xXK5ZQb\", code=SERVER_ERROR, version=web/5.7.1)"

ReadMe