Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
track gas_used and status of the tx
Browse files Browse the repository at this point in the history
  • Loading branch information
dekz committed May 22, 2020
1 parent ba3df90 commit fc9e4c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const META_TRANSACTION_DOCS_URL = 'https://0x.org/docs/api#meta_transacti
export const ETH_GAS_STATION_API_BASE_URL = 'https://ethgasstation.info';
export const UNSTICKING_TRANSACTION_GAS_MULTIPLIER = 1.1;
export const ETH_TRANSFER_GAS_LIMIT = 21000;
export const STUCK_TX_POLLING_INTERVAL_MS = ONE_SECOND_MS * 5;
export const TX_HASH_RESPONSE_WAIT_TIME_MS = ONE_SECOND_MS * 100;
export const SUBMITTED_TX_DB_POLLING_INTERVAL_MS = 200;
export const PUBLIC_ADDRESS_FOR_ETH_CALLS = '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B';
Expand Down
25 changes: 20 additions & 5 deletions src/entities/TransactionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ export class TransactionEntity {
@Column({ name: 'expected_mined_in_sec', type: 'int' })
public expectedMinedInSec?: number;

@Column({ name: 'nonce', type: 'bigint', nullable: true, transformer: BigIntTransformer })
public nonce?: number;

@Column({ name: 'gas_price', type: 'varchar', nullable: true, transformer: BigNumberTransformer })
public gasPrice?: BigNumber;

@Column({ name: 'value', type: 'varchar', nullable: true, transformer: BigNumberTransformer })
public value?: BigNumber;

@Column({ name: 'block_number', type: 'bigint', nullable: true, transformer: BigIntTransformer })
public blockNumber?: number;
@Column({ name: 'gas', type: 'int', nullable: true })
public gas?: number;

@Column({ name: 'from', type: 'varchar', nullable: true })
public from?: string;

@Column({ name: 'nonce', type: 'bigint', nullable: true, transformer: BigIntTransformer })
public nonce?: number;

@Column({ name: 'gas_used', type: 'int', nullable: true })
public gasUsed?: number;

@Column({ name: 'block_number', type: 'bigint', nullable: true, transformer: BigIntTransformer })
public blockNumber?: number;

@Column({ name: 'tx_status', type: 'int', nullable: true })
public txStatus?: number;

@CreateDateColumn({ name: 'created_at' })
public createdAt?: Date;

Expand Down Expand Up @@ -95,6 +104,9 @@ export class TransactionEntity {
gasPrice: ZERO,
value: ZERO,
from: '',
gas: null,
gasUsed: null,
txStatus: null,
},
) {
this.refHash = opts.refHash;
Expand All @@ -109,6 +121,9 @@ export class TransactionEntity {
this.value = opts.value;
this.blockNumber = opts.blockNumber;
this.from = opts.from;
this.gas = opts.gas;
this.gasUsed = opts.gasUsed;
this.txStatus = opts.txStatus;
const now = new Date();
this.expectedAt = new Date(now.getTime() + this.expectedMinedInSec * ONE_SECOND_MS);
}
Expand Down
14 changes: 9 additions & 5 deletions src/entities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { BigNumber } from '@0x/utils';

export interface TransactionEntityOpts {
refHash: string;
txHash?: string;
to: string;
data?: string;
takerAddress?: string;
status: string;
expectedMinedInSec: number;
to: string;
data?: string;
value?: BigNumber;
from?: string;
nonce?: number;
gasPrice?: BigNumber;
value?: BigNumber;
gas?: number;
txHash?: string;
gasUsed?: number;
blockNumber?: number;
from?: string;
// Ethereum tx status, 1 == success, 0 == failure
txStatus?: number;
}
9 changes: 9 additions & 0 deletions src/services/transaction_watcher_signer_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Connection, Not, Repository } from 'typeorm';
import { ENABLE_PROMETHEUS_METRICS } from '../config';
import {
ETH_DECIMALS,
ETH_TRANSFER_GAS_LIMIT,
GWEI_DECIMALS,
ONE_SECOND_MS,
SIGNER_STATUS_DB_KEY,
Expand Down Expand Up @@ -153,6 +154,7 @@ export class TransactionWatcherSignerService {
txEntity.txHash = ethereumTransactionHash;
txEntity.nonce = ethereumTxnParams.nonce;
txEntity.from = ethereumTxnParams.from;
txEntity.gas = ethereumTxnParams.gas;
if (ENABLE_PROMETHEUS_METRICS) {
this._gasPriceSummary.observe(
{ [SIGNER_ADDRESS_LABEL]: txEntity.from },
Expand Down Expand Up @@ -337,6 +339,7 @@ export class TransactionWatcherSignerService {
from: tx.from,
to: signer.publicAddress,
value: ZERO,
gas: ETH_TRANSFER_GAS_LIMIT,
expectedMinedInSec: this._config.expectedMinedInSec,
});
await this._transactionRepository.save(transactionEntity);
Expand Down Expand Up @@ -423,7 +426,13 @@ export class TransactionWatcherSignerService {
});
}
if (tx.blockNumber + this._config.numBlocksUntilConfirmed < latestBlockNumber) {
const txReceipt = await this._web3Wrapper.getTransactionReceiptIfExistsAsync(tx.txHash);
tx.status = TransactionStates.Confirmed;
tx.gasUsed = txReceipt.gasUsed;
// status type can be a string
tx.txStatus = utils.isNil(txReceipt.status)
? tx.txStatus
: new BigNumber(txReceipt.status).toNumber();
await this._updateTxEntityAsync(tx);
}
}
Expand Down

0 comments on commit fc9e4c6

Please sign in to comment.