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';
|
2021-10-25 14:21:16 +00:00
|
|
|
import { utils, getDefaultProvider, providers } from 'ethers';
|
2021-12-02 07:52:29 +00:00
|
|
|
import Decimal from 'decimal.js';
|
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-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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
export const getCustomProvider = (network?: providers.Network | string, options?: any): providers.BaseProvider => {
|
|
|
|
const provider = getDefaultProvider(network, options);
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|