2021-08-12 09:58:13 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
import assert from 'assert';
|
2021-08-19 07:57:32 +00:00
|
|
|
import { ValueTransformer } from 'typeorm';
|
2021-10-20 10:36:03 +00:00
|
|
|
import yargs from 'yargs';
|
|
|
|
import { hideBin } from 'yargs/helpers';
|
2022-07-20 05:07:17 +00:00
|
|
|
import { utils, providers } from 'ethers';
|
2021-12-02 07:52:29 +00:00
|
|
|
import Decimal from 'decimal.js';
|
2021-10-20 10:36:03 +00:00
|
|
|
|
2021-11-17 12:44:02 +00:00
|
|
|
import { EthClient } from '@vulcanize/ipld-eth-client';
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
import { DEFAULT_CONFIG_PATH } from './constants';
|
|
|
|
import { Config } from './config';
|
|
|
|
import { JobQueue } from './job-queue';
|
2021-12-02 07:52:29 +00:00
|
|
|
import { GraphDecimal } from './graph-decimal';
|
2021-11-17 12:44:02 +00:00
|
|
|
import * as EthDecoder from './eth';
|
2021-08-19 07:57:32 +00:00
|
|
|
|
2021-08-04 13:12:59 +00:00
|
|
|
/**
|
|
|
|
* Method to wait for specified time.
|
|
|
|
* @param time Time to wait in milliseconds
|
|
|
|
*/
|
|
|
|
export const wait = async (time: number): Promise<void> => new Promise(resolve => setTimeout(resolve, time));
|
2021-08-19 07:57:32 +00:00
|
|
|
|
2021-12-02 07:52:29 +00:00
|
|
|
/**
|
|
|
|
* Transformer used by typeorm entity for GraphDecimal type fields.
|
|
|
|
*/
|
|
|
|
export const graphDecimalTransformer: ValueTransformer = {
|
|
|
|
to: (value?: GraphDecimal) => {
|
|
|
|
if (value) {
|
|
|
|
return value.toFixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
from: (value?: string) => {
|
|
|
|
if (value) {
|
|
|
|
return new GraphDecimal(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-19 07:57:32 +00:00
|
|
|
/**
|
2021-09-27 04:43:50 +00:00
|
|
|
* Transformer used by typeorm entity for Decimal type fields.
|
2021-08-19 07:57:32 +00:00
|
|
|
*/
|
|
|
|
export const decimalTransformer: ValueTransformer = {
|
|
|
|
to: (value?: Decimal) => {
|
|
|
|
if (value) {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
from: (value?: string) => {
|
|
|
|
if (value) {
|
|
|
|
return new Decimal(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2021-09-27 04:43:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer used by typeorm entity for bigint type fields.
|
|
|
|
*/
|
|
|
|
export const bigintTransformer: ValueTransformer = {
|
|
|
|
to: (value?: bigint) => {
|
|
|
|
if (value) {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
from: (value?: string) => {
|
|
|
|
if (value) {
|
|
|
|
return BigInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2021-10-20 10:36:03 +00:00
|
|
|
|
2021-11-17 11:31:09 +00:00
|
|
|
export const bigintArrayTransformer: ValueTransformer = {
|
|
|
|
to: (valueArray?: bigint[]) => {
|
|
|
|
if (valueArray) {
|
|
|
|
return valueArray.map(value => bigintTransformer.to(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueArray;
|
|
|
|
},
|
|
|
|
from: (valueArray?: string[]) => {
|
|
|
|
if (valueArray) {
|
|
|
|
return valueArray.map(value => bigintTransformer.from(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueArray;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const decimalArrayTransformer: ValueTransformer = {
|
|
|
|
to: (valueArray?: Decimal[]) => {
|
|
|
|
if (valueArray) {
|
|
|
|
return valueArray.map(value => decimalTransformer.to(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueArray;
|
|
|
|
},
|
|
|
|
from: (valueArray?: string[]) => {
|
|
|
|
if (valueArray) {
|
|
|
|
return valueArray.map(value => decimalTransformer.from(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueArray;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
export const resetJobs = async (config: Config): Promise<void> => {
|
|
|
|
const { jobQueue: jobQueueConfig } = config;
|
|
|
|
|
|
|
|
const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig;
|
|
|
|
assert(dbConnectionString, 'Missing job queue db connection string');
|
|
|
|
|
|
|
|
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });
|
|
|
|
await jobQueue.start();
|
|
|
|
await jobQueue.deleteAllJobs();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getResetYargs = (): yargs.Argv => {
|
|
|
|
return yargs(hideBin(process.argv))
|
|
|
|
.parserConfiguration({
|
|
|
|
'parse-numbers': false
|
|
|
|
}).options({
|
|
|
|
configFile: {
|
|
|
|
alias: 'f',
|
|
|
|
type: 'string',
|
|
|
|
require: true,
|
|
|
|
demandOption: true,
|
|
|
|
describe: 'configuration file path (toml)',
|
|
|
|
default: DEFAULT_CONFIG_PATH
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2021-10-25 14:21:16 +00:00
|
|
|
|
2022-07-20 05:07:17 +00:00
|
|
|
export const getCustomProvider = (url?: utils.ConnectionInfo | string, network?: providers.Networkish): providers.JsonRpcProvider => {
|
|
|
|
const provider = new providers.JsonRpcProvider(url, network);
|
2021-10-25 14:21:16 +00:00
|
|
|
provider.formatter = new CustomFormatter();
|
|
|
|
return provider;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CustomFormatter extends providers.Formatter {
|
|
|
|
blockTag (blockTag: any): string {
|
|
|
|
if (blockTag == null) { return 'latest'; }
|
|
|
|
|
|
|
|
if (blockTag === 'earliest') { return '0x0'; }
|
|
|
|
|
|
|
|
if (blockTag === 'latest' || blockTag === 'pending') {
|
|
|
|
return blockTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof (blockTag) === 'number' || utils.isHexString(blockTag)) {
|
|
|
|
// Return value if hex string is of block hash length.
|
|
|
|
if (utils.isHexString(blockTag) && utils.hexDataLength(blockTag) === 32) {
|
|
|
|
return blockTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.hexValue(<number | string>blockTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('invalid blockTag');
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 12:44:02 +00:00
|
|
|
|
2021-12-01 10:48:38 +00:00
|
|
|
export const getFullBlock = async (ethClient: EthClient, ethProvider: providers.BaseProvider, blockHash: string): Promise<any> => {
|
2021-11-17 12:44:02 +00:00
|
|
|
const {
|
|
|
|
allEthHeaderCids: {
|
|
|
|
nodes: [
|
|
|
|
fullBlock
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} = await ethClient.getFullBlocks({ blockHash });
|
|
|
|
|
|
|
|
assert(fullBlock.blockByMhKey);
|
|
|
|
|
|
|
|
// Deecode the header data.
|
|
|
|
const header = EthDecoder.decodeHeader(EthDecoder.decodeData(fullBlock.blockByMhKey.data));
|
|
|
|
assert(header);
|
|
|
|
|
2022-06-08 06:43:52 +00:00
|
|
|
// TODO: Calculate size from rlp encoded data.
|
2021-12-01 10:48:38 +00:00
|
|
|
// Get block info from JSON RPC API provided by ipld-eth-server.
|
|
|
|
const provider = ethProvider as providers.JsonRpcProvider;
|
|
|
|
const { size } = await provider.send('eth_getBlockByHash', [blockHash, false]);
|
|
|
|
|
2021-11-17 12:44:02 +00:00
|
|
|
return {
|
2021-12-29 07:51:39 +00:00
|
|
|
headerId: fullBlock.id,
|
2021-11-17 12:44:02 +00:00
|
|
|
cid: fullBlock.cid,
|
|
|
|
blockNumber: fullBlock.blockNumber,
|
|
|
|
blockHash: fullBlock.blockHash,
|
|
|
|
parentHash: fullBlock.parentHash,
|
|
|
|
timestamp: fullBlock.timestamp,
|
|
|
|
stateRoot: fullBlock.stateRoot,
|
|
|
|
td: fullBlock.td,
|
|
|
|
txRoot: fullBlock.txRoot,
|
|
|
|
receiptRoot: fullBlock.receiptRoot,
|
|
|
|
uncleHash: fullBlock.uncleRoot,
|
|
|
|
difficulty: header.Difficulty.toString(),
|
|
|
|
gasLimit: header.GasLimit.toString(),
|
2021-11-30 11:05:01 +00:00
|
|
|
gasUsed: header.GasUsed.toString(),
|
2021-12-01 10:48:38 +00:00
|
|
|
author: header.Beneficiary,
|
|
|
|
size: BigInt(size).toString()
|
2021-11-17 12:44:02 +00:00
|
|
|
};
|
|
|
|
};
|
2021-12-29 07:51:39 +00:00
|
|
|
|
2022-04-28 11:43:32 +00:00
|
|
|
export const getFullTransaction = async (ethClient: EthClient, txHash: string): Promise<any> => {
|
2021-12-29 07:51:39 +00:00
|
|
|
const {
|
2022-04-28 11:43:32 +00:00
|
|
|
ethTransactionCidByTxHash: fullTx
|
|
|
|
} = await ethClient.getFullTransaction(txHash);
|
2021-12-29 07:51:39 +00:00
|
|
|
|
|
|
|
assert(fullTx.blockByMhKey);
|
|
|
|
|
|
|
|
// Decode the transaction data.
|
2022-07-11 05:59:33 +00:00
|
|
|
const txData = utils.parseTransaction(EthDecoder.decodeData(fullTx.blockByMhKey.data));
|
|
|
|
assert(txData);
|
2021-12-29 07:51:39 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
hash: txHash,
|
|
|
|
from: fullTx.src,
|
|
|
|
to: fullTx.dst,
|
|
|
|
index: fullTx.index,
|
2022-07-11 05:59:33 +00:00
|
|
|
value: txData.value.toString(),
|
|
|
|
gasLimit: txData.gasLimit.toString(),
|
|
|
|
gasPrice: txData.gasPrice?.toString(),
|
|
|
|
input: txData.data
|
2021-12-29 07:51:39 +00:00
|
|
|
};
|
|
|
|
};
|