aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/web3/index.ts
diff options
context:
space:
mode:
authorAlex Browne <stephenalexbrowne@gmail.com>2018-12-06 03:08:19 +0800
committerGitHub <noreply@github.com>2018-12-06 03:08:19 +0800
commitb2dd5495bcf13a9ea71498b5def12c75589b0156 (patch)
tree0e0d728d540e747c32a083d604d7916a35ea95cf /packages/pipeline/src/parsers/web3/index.ts
parent72a30260d88e722a6b076134693360c573f6c70f (diff)
parente0348f9c044b4909260e4864398b4f50232da620 (diff)
downloaddexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar.gz
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar.bz2
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar.lz
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar.xz
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.tar.zst
dexon-sol-tools-b2dd5495bcf13a9ea71498b5def12c75589b0156.zip
Merge pull request #1377 from 0xProject/feature/pipeline-cleanup-mega-rebase
Merge all pipeline code into development
Diffstat (limited to 'packages/pipeline/src/parsers/web3/index.ts')
-rw-r--r--packages/pipeline/src/parsers/web3/index.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/pipeline/src/parsers/web3/index.ts b/packages/pipeline/src/parsers/web3/index.ts
new file mode 100644
index 000000000..f986efc59
--- /dev/null
+++ b/packages/pipeline/src/parsers/web3/index.ts
@@ -0,0 +1,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;
+}