aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/web3/index.ts
blob: f986efc59303387095cf0904b395415ac75ceff6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { BigNumber } from '@0x/utils';
import { BlockWithoutTransactionData, Transaction as EthTransaction } from 'ethereum-types';

import { Block, Transaction } from '../../entities';

const MILLISECONDS_PER_SECOND = 1000;

/**
 * Parses a raw block and returns a Block entity.
 * @param rawBlock a raw block (e.g. returned from web3-wrapper).
 */
export function parseBlock(rawBlock: BlockWithoutTransactionData): Block {
    if (rawBlock.hash == null) {
        throw new Error('Tried to parse raw block but hash was null');
    }
    if (rawBlock.number == null) {
        throw new Error('Tried to parse raw block but number was null');
    }

    const block = new Block();
    block.hash = rawBlock.hash;
    block.number = rawBlock.number;
    // Block timestamps are in seconds, but we use milliseconds everywhere else.
    block.timestamp = rawBlock.timestamp * MILLISECONDS_PER_SECOND;
    return block;
}

/**
 * Parses a raw transaction and returns a Transaction entity.
 * @param rawBlock a raw transaction (e.g. returned from web3-wrapper).
 */
export function parseTransaction(rawTransaction: EthTransaction): Transaction {
    if (rawTransaction.blockHash == null) {
        throw new Error('Tried to parse raw transaction but blockHash was null');
    }
    if (rawTransaction.blockNumber == null) {
        throw new Error('Tried to parse raw transaction but blockNumber was null');
    }

    const tx = new Transaction();
    tx.transactionHash = rawTransaction.hash;
    tx.blockHash = rawTransaction.blockHash;
    tx.blockNumber = rawTransaction.blockNumber;

    tx.gasUsed = new BigNumber(rawTransaction.gas);
    tx.gasPrice = rawTransaction.gasPrice;

    return tx;
}