Skip to content

redis store service to log relayer errors #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"bull": "^3.22.11",
"class-validator": "^0.13.1",
"ethers": "^5.4.6",
"ioredis": "^5.0.4",
"gas-price-oracle": "^0.4.7",
"redis": "^3.1.2",
"reflect-metadata": "^0.1.13",
Expand All @@ -50,6 +51,7 @@
"@types/express": "^4.17.13",
"@types/jest": "^26.0.24",
"@types/node": "^16.0.0",
"@types/redis": "^4.0.0",
"@types/supertest": "^2.0.11",
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^4.28.2",
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';

import { AppModule } from './app.module';
import { RedisStoreService } from '@/services';

async function bootstrap() {
try {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { cors: true });

const configService = app.get(ConfigService);
const redisStore = app.get(RedisStoreService).getClient();
await redisStore.clearErrors();
await app.listen(configService.get('base.port'));
} catch (err) {
console.log('err', err.message);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/api/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { ApiService } from './api.service';
import { ApiController } from './api.controller';

import { QueueModule } from '@/modules';
import { ProviderService } from '@/services';
import { ProviderService, RedisStoreService } from '@/services';

@Module({
imports: [ConfigModule, QueueModule],
providers: [ApiService, ProviderService],
providers: [ApiService, ProviderService, RedisStoreService],
controllers: [ApiController],
exports: [],
})
Expand Down
10 changes: 7 additions & 3 deletions src/modules/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { v4 as uuid } from 'uuid';
import { InjectQueue } from '@nestjs/bull';
import { Injectable } from '@nestjs/common';

import { ProviderService } from '@/services';
import { ProviderService, RedisStoreService } from '@/services';
import { ConfigService } from '@nestjs/config';
import { jobStatus, NETWORKS_INFO } from '@/constants';

Expand All @@ -14,8 +14,11 @@ class ApiService {
constructor(
private configService: ConfigService,
private providerService: ProviderService,
private redisStore: RedisStoreService,
@InjectQueue('transaction') private transactionQueue: Queue,
) {}
) {
this.redisStore = redisStore.getClient();
}

async status(): Promise<Status> {
const { rewardAddress, version, chainId, serviceFee } = this.configService.get('base');
Expand Down Expand Up @@ -58,12 +61,13 @@ class ApiService {

private async healthCheck(): Promise<Health> {
const status = await this.providerService.checkSenderBalance();

const { chainId, minimumBalance } = this.configService.get('base');
const errorsLog = await this.redisStore.readErrors();

return {
status,
error: status ? '' : `Not enough balance, less than ${minimumBalance} ${NETWORKS_INFO[chainId].symbol}`,
errorsLog,
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type Health = {
errorsLog: Array<{ message: string; score: number }>;
status: boolean;
error: string;
};
Expand Down
5 changes: 2 additions & 3 deletions src/modules/queue/queue.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { BullModule } from '@nestjs/bull';

import { GasPriceService, ProviderService, OffchainPriceService } from '@/services';
import { GasPriceService, OffchainPriceService, ProviderService, RedisStoreService } from '@/services';

import { TransactionProcessor } from './transaction.processor';

Expand All @@ -14,7 +13,7 @@ import bullConfig from '@/config/bull.config';
useFactory: bullConfig,
}),
],
providers: [GasPriceService, ProviderService, TransactionProcessor, OffchainPriceService],
providers: [GasPriceService, ProviderService, TransactionProcessor, OffchainPriceService, RedisStoreService],
exports: [BullModule],
})
export class QueueModule {}
16 changes: 7 additions & 9 deletions src/modules/queue/transaction.processor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { BigNumber } from 'ethers';
import { TxManager } from 'tx-manager';
import { Job, Queue, DoneCallback } from 'bull';
import { DoneCallback, Job, Queue } from 'bull';

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectQueue, Process, Processor, OnQueueActive, OnQueueCompleted, OnQueueFailed } from '@nestjs/bull';
import { InjectQueue, OnQueueActive, OnQueueCompleted, OnQueueFailed, Process, Processor } from '@nestjs/bull';

import { Transaction } from '@/types';
import { getToIntegerMultiplier, toWei } from '@/utilities';
import { CONTRACT_ERRORS, SERVICE_ERRORS, jobStatus } from '@/constants';
import { GasPriceService, ProviderService, OffchainPriceService } from '@/services';

import { CONTRACT_ERRORS, jobStatus, SERVICE_ERRORS } from '@/constants';
import { GasPriceService, OffchainPriceService, ProviderService, RedisStoreService } from '@/services';
import txMangerConfig from '@/config/txManager.config';

import { BaseProcessor } from './base.processor';
Expand All @@ -24,22 +23,24 @@ export class TransactionProcessor extends BaseProcessor<Transaction> {
private gasPriceService: GasPriceService,
private providerService: ProviderService,
private offChainPriceService: OffchainPriceService,
private redisStoreService: RedisStoreService,
) {
super();
this.queueName = 'transaction';
this.queue = transactionQueue;
this.redisStoreService = redisStoreService.getClient();
}

@Process()
async processTransactions(job: Job<Transaction>, cb: DoneCallback) {
try {
const { extData } = job.data;

await this.checkFee({ fee: extData.fee, externalAmount: extData.extAmount });
const txHash = await this.submitTx(job);

cb(null, txHash);
} catch (err) {
this.redisStoreService.addErrorToSet(err.message);
cb(err);
}
}
Expand Down Expand Up @@ -155,13 +156,10 @@ export class TransactionProcessor extends BaseProcessor<Transaction> {

handleError({ message }: Error) {
const contractError = CONTRACT_ERRORS.find((knownError) => message.includes(knownError));

if (contractError) {
throw new Error(`Revert by smart contract: ${contractError}`);
}

const serviceError = Object.values(SERVICE_ERRORS).find((knownError) => message.includes(knownError));

if (serviceError) {
throw new Error(`Relayer internal error: ${serviceError}`);
}
Expand Down
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './provider.service';

export * from './gas-price.service';
export * from './offchain-price.service';
export * from './redis-store.service';
35 changes: 35 additions & 0 deletions src/services/redis-store.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import { parseURL } from 'ioredis/built/utils';

@Injectable()
export class RedisStoreService {
private client: Redis;

getClient() {
if (!this.client) {
const url = process.env.REDIS_URL || 'localhost';
const { host, port = 6379 } = parseURL(url);
this.client = new Redis(+port, host);
}
return this;
}

addErrorToSet(value: string, score = 1) {
this.client.zadd('errors', 'INCR', score, value);
}

async readErrors() {
const set = await this.client.zrevrange('errors', 0, -1, 'WITHSCORES');
const errors = [];
while (set.length) {
const [message, score] = set.splice(0, 2);
errors.push({ message, score });
}
return errors;
}

clearErrors() {
this.client.del('errors');
}
}
102 changes: 99 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,11 @@
resolved "https://registry.npmjs.org/@humanwhocodes/object-schema/-/object-schema-1.2.0.tgz#87de7af9c231826fdd68ac7258f77c429e0e5fcf"
integrity sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==

"@ioredis/commands@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@ioredis/commands/-/commands-1.1.1.tgz#2ba4299ea624a6bfac15b35f6df90b0015691ec3"
integrity sha512-fsR4P/ROllzf/7lXYyElUJCheWdTJVJvOTps8v9IWKFATxR61ANOlnoPqhH099xYLrJGpc2ZQ28B3rMeUt5VQg==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down Expand Up @@ -1109,6 +1114,41 @@
optional "0.1.4"
tslib "2.3.0"

"@node-redis/[email protected]":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@node-redis/bloom/-/bloom-1.0.1.tgz#144474a0b7dc4a4b91badea2cfa9538ce0a1854e"
integrity sha512-mXEBvEIgF4tUzdIN89LiYsbi6//EdpFA7L8M+DHCvePXg+bfHWi+ct5VI6nHUFQE5+ohm/9wmgihCH3HSkeKsw==

"@node-redis/[email protected]":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@node-redis/client/-/client-1.0.5.tgz#ebac5e2bbf12214042a37621604973a954ede755"
integrity sha512-ESZ3bd1f+od62h4MaBLKum+klVJfA4wAeLHcVQBkoXa1l0viFesOWnakLQqKg+UyrlJhZmXJWtu0Y9v7iTMrig==
dependencies:
cluster-key-slot "1.1.0"
generic-pool "3.8.2"
redis-parser "3.0.0"
yallist "4.0.0"

"@node-redis/[email protected]":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@node-redis/graph/-/graph-1.0.0.tgz#baf8eaac4a400f86ea04d65ec3d65715fd7951ab"
integrity sha512-mRSo8jEGC0cf+Rm7q8mWMKKKqkn6EAnA9IA2S3JvUv/gaWW/73vil7GLNwion2ihTptAm05I9LkepzfIXUKX5g==

"@node-redis/[email protected]":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@node-redis/json/-/json-1.0.2.tgz#8ad2d0f026698dc1a4238cc3d1eb099a3bee5ab8"
integrity sha512-qVRgn8WfG46QQ08CghSbY4VhHFgaTY71WjpwRBGEuqGPfWwfRcIf3OqSpR7Q/45X+v3xd8mvYjywqh0wqJ8T+g==

"@node-redis/[email protected]":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@node-redis/search/-/search-1.0.5.tgz#96050007eb7c50a7e47080320b4f12aca8cf94c4"
integrity sha512-MCOL8iCKq4v+3HgEQv8zGlSkZyXSXtERgrAJ4TSryIG/eLFy84b57KmNNa/V7M1Q2Wd2hgn2nPCGNcQtk1R1OQ==

"@node-redis/[email protected]":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@node-redis/time-series/-/time-series-1.0.2.tgz#5dd3638374edd85ebe0aa6b0e87addc88fb9df69"
integrity sha512-HGQ8YooJ8Mx7l28tD7XjtB3ImLEjlUxG1wC1PAjxu6hPJqjPshUZxAICzDqDjtIbhDTf48WXXUcx8TQJB1XTKA==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -1364,6 +1404,13 @@
resolved "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc"
integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==

"@types/redis@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/redis/-/redis-4.0.0.tgz#cf98ef470e744fd86f632b027767498480e14bea"
integrity sha512-7wZGFZmKo9S/0FryXRyJ63uxZ3Du8LrGD/IIUy4+HRkQh6ypudHj+yECpi9kkLDQBV29pKnAMCf2haVRMQ5FnA==
dependencies:
redis "*"

"@types/serve-static@*":
version "1.13.10"
resolved "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.10.tgz#f5e0ce8797d2d7cc5ebeda48a52c96c4fa47a8d9"
Expand Down Expand Up @@ -2210,7 +2257,7 @@ clone@^1.0.2:
resolved "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz#da309cc263df15994c688ca902179ca3c7cd7c7e"
integrity sha1-2jCcwmPfFZlMaIypAheco8fNfH4=

cluster-key-slot@^1.1.0:
cluster-key-slot@1.1.0, cluster-key-slot@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz#30474b2a981fb12172695833052bc0d01336d10d"
integrity sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==
Expand Down Expand Up @@ -2430,6 +2477,13 @@ debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1:
dependencies:
ms "2.1.2"

debug@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

debuglog@^1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
Expand Down Expand Up @@ -2479,6 +2533,11 @@ denque@^1.1.0, denque@^1.5.0:
resolved "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de"
integrity sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==

denque@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/denque/-/denque-2.0.1.tgz#bcef4c1b80dc32efe97515744f21a4229ab8934a"
integrity sha512-tfiWc6BQLXNLpNiR5iGd0Ocu3P3VpxfzFiqubLgMfhfOw9WyvgJBd46CClNn9k3qfbjvT//0cf7AlYRX/OslMQ==

depd@~1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
Expand Down Expand Up @@ -3193,6 +3252,11 @@ gas-price-oracle@^0.4.7:
axios "^0.21.2"
bignumber.js "^9.0.0"

[email protected]:
version "3.8.2"
resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.8.2.tgz#aab4f280adb522fdfbdc5e5b64d718d3683f04e9"
integrity sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg==

gensync@^1.0.0-beta.2:
version "1.0.0-beta.2"
resolved "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
Expand Down Expand Up @@ -3513,6 +3577,21 @@ ioredis@^4.27.0:
redis-parser "^3.0.0"
standard-as-callback "^2.1.0"

ioredis@^5.0.4:
version "5.0.4"
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-5.0.4.tgz#0d4abfd818adfc5ef5029fddac4b8f503a1433b7"
integrity sha512-qFJw3MnPNsJF1lcIOP3vztbsasOXK3nDdNAgjQj7t7/Bn/w10PGchTOpqylQNxjzPbLoYDu34LjeJtSWiKBntQ==
dependencies:
"@ioredis/commands" "^1.1.1"
cluster-key-slot "^1.1.0"
debug "^4.3.4"
denque "^2.0.1"
lodash.defaults "^4.2.0"
lodash.isarguments "^3.1.0"
redis-errors "^1.2.0"
redis-parser "^3.0.0"
standard-as-callback "^2.1.0"

[email protected]:
version "1.9.1"
resolved "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3"
Expand Down Expand Up @@ -4338,6 +4417,11 @@ [email protected]:
resolved "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz#d19f4dc1095058cccbe2b0cdf4ee0fe4aa37c862"
integrity sha1-0Z9NwQlQWMzL4rDN9O4P5Ko3yGI=

lodash.isarguments@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
integrity sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
Expand Down Expand Up @@ -5078,13 +5162,25 @@ redis-errors@^1.0.0, redis-errors@^1.2.0:
resolved "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=

redis-parser@^3.0.0:
redis-parser@3.0.0, redis-parser@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
integrity sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=
dependencies:
redis-errors "^1.0.0"

redis@*:
version "4.0.6"
resolved "https://registry.yarnpkg.com/redis/-/redis-4.0.6.tgz#a2ded4d9f4f4bad148e54781051618fc684cd858"
integrity sha512-IaPAxgF5dV0jx+A9l6yd6R9/PAChZIoAskDVRzUODeLDNhsMlq7OLLTmu0AwAr0xjrJ1bibW5xdpRwqIQ8Q0Xg==
dependencies:
"@node-redis/bloom" "1.0.1"
"@node-redis/client" "1.0.5"
"@node-redis/graph" "1.0.0"
"@node-redis/json" "1.0.2"
"@node-redis/search" "1.0.5"
"@node-redis/time-series" "1.0.2"

redis@^3.1.2:
version "3.1.2"
resolved "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz#766851117e80653d23e0ed536254677ab647638c"
Expand Down Expand Up @@ -6173,7 +6269,7 @@ y18n@^5.0.5:
resolved "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"
integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==

yallist@^4.0.0:
yallist@4.0.0, yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
Expand Down