71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import {
|
|
Account,
|
|
bigIntToHex,
|
|
setLengthLeft,
|
|
toBuffer,
|
|
} from "@ethereumjs/util";
|
|
import { BlockData, HeaderData } from "@ethereumjs/block";
|
|
import {
|
|
AccessListEIP2930TxData,
|
|
FeeMarketEIP1559TxData,
|
|
TxData,
|
|
} from "@ethereumjs/tx";
|
|
|
|
const isTruthy = (val: any) => !!val;
|
|
|
|
// TODO: fix blockInfo type
|
|
export function headerDataFromWeb3Response(blockInfo: any): HeaderData {
|
|
return {
|
|
parentHash: blockInfo.parentHash,
|
|
uncleHash: blockInfo.sha3Uncles,
|
|
coinbase: blockInfo.miner,
|
|
stateRoot: blockInfo.stateRoot,
|
|
transactionsTrie: blockInfo.transactionsRoot,
|
|
receiptTrie: blockInfo.receiptsRoot,
|
|
logsBloom: blockInfo.logsBloom,
|
|
difficulty: BigInt(blockInfo.difficulty),
|
|
number: BigInt(blockInfo.number),
|
|
gasLimit: BigInt(blockInfo.gasLimit),
|
|
gasUsed: BigInt(blockInfo.gasUsed),
|
|
timestamp: BigInt(blockInfo.timestamp),
|
|
extraData: blockInfo.extraData,
|
|
mixHash: (blockInfo as any).mixHash, // some reason the types are not up to date :(
|
|
nonce: blockInfo.nonce,
|
|
baseFeePerGas: blockInfo.baseFeePerGas
|
|
? BigInt(blockInfo.baseFeePerGas)
|
|
: undefined,
|
|
};
|
|
}
|
|
|
|
export function txDataFromWeb3Response(
|
|
txInfo: any,
|
|
): TxData | AccessListEIP2930TxData | FeeMarketEIP1559TxData {
|
|
return {
|
|
...txInfo,
|
|
data: txInfo.input,
|
|
gasPrice: BigInt(txInfo.gasPrice),
|
|
gasLimit: txInfo.gas,
|
|
to: isTruthy(txInfo.to)
|
|
? setLengthLeft(toBuffer(txInfo.to), 20)
|
|
: undefined,
|
|
value: BigInt(txInfo.value),
|
|
maxFeePerGas: isTruthy(txInfo.maxFeePerGas)
|
|
? BigInt(txInfo.maxFeePerGas)
|
|
: undefined,
|
|
maxPriorityFeePerGas: isTruthy(txInfo.maxPriorityFeePerGas)
|
|
? BigInt(txInfo.maxPriorityFeePerGas)
|
|
: undefined,
|
|
};
|
|
}
|
|
|
|
export function blockDataFromWeb3Response(blockInfo: any): BlockData {
|
|
return {
|
|
header: headerDataFromWeb3Response(blockInfo),
|
|
transactions: blockInfo.transactions.map(txDataFromWeb3Response),
|
|
};
|
|
}
|
|
|
|
export { bigIntToHex };
|
|
|
|
export const emptyAccountSerialize = new Account().serialize();
|