deth
Version:
Ethereum node focused on Developer Experience
69 lines (68 loc) • 1.94 kB
JavaScript
import { makeQuantity, makeAddress, makeHexData } from '../primitives';
import { FakeTransaction } from 'ethereumjs-tx';
import { utils } from 'ethers';
export function toFakeTransaction(tx) {
var _a;
return new FakeTransaction({
from: (_a = tx.from, (_a !== null && _a !== void 0 ? _a : '0x0000000000000000000000000000000000000000')),
to: tx.to,
data: tx.data,
gasLimit: tx.gas,
gasPrice: tx.gasPrice,
nonce: tx.nonce,
value: tx.value,
});
}
const toQuantity = (value) => makeQuantity(utils.hexStripZeros(utils.hexlify(value)));
const toAddress = (value) => makeAddress(utils.hexlify(value));
const toHexData = (value) => makeHexData(utils.hexlify(value));
export function toRpcTransactionRequest(transaction) {
const result = {};
if (transaction.gasLimit) {
result.gas = toQuantity(transaction.gasLimit);
}
if (transaction.gasPrice) {
result.gasPrice = toQuantity(transaction.gasPrice);
}
if (transaction.nonce) {
result.nonce = toQuantity(transaction.nonce);
}
if (transaction.value) {
result.value = toQuantity(transaction.value);
}
if (transaction.from) {
result.from = toAddress(transaction.from);
}
if (transaction.to) {
result.to = toAddress(transaction.to);
}
if (transaction.data) {
result.data = toHexData(transaction.data);
}
return result;
}
export function toEthersTransaction(tx) {
const result = {};
if (tx.data) {
result.data = tx.data;
}
if (tx.from) {
result.from = tx.from;
}
if (tx.gas) {
result.gasLimit = tx.gas;
}
if (tx.gasPrice) {
result.gasPrice = tx.gasPrice;
}
if (tx.nonce) {
result.nonce = tx.nonce;
}
if (tx.to) {
result.to = tx.to;
}
if (tx.value) {
result.value = tx.value;
}
return result;
}