Skip to content

Commit

Permalink
Merge pull request #3 from ferrumnet/main
Browse files Browse the repository at this point in the history
Merged with main
  • Loading branch information
zikriya authored Jan 31, 2023
2 parents 1a3c3de + 7bd4d62 commit 9e57abc
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PORT=3000
MONGODB_URL=mongodb://127.0.0.1:27017/multiswap-node
QUEUE=Transaction
QUEUE=Transaction
GATEWAY_BACKEND_URL=
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Getting Started

run `npm install` OR `yarn install` at the root of the repo.
Node Version `v18.12.1`

Redis Version `v7.0.8`

Run the Redis Server in your machine by using `redis-server` command

Run `yarn` OR `yarn install` at the root of the repo

Run `yarn dev` to run Server
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"typescript": "^4.5.5"
},
"dependencies": {
"axios": "^1.2.6",
"bullmq": "^3.5.11",
"dotenv": "^16.0.3",
"express": "^4.17.2",
"mongoose": "^6.9.0",
"web3": "^1.8.1"
}
}
1 change: 0 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ app.use('/', routes);

// send back a 404 error for any unknown api request
app.use((req: Request, res: Response, next: NextFunction) => {
console.log(req.path);
next(Error('Not found'));
});

Expand Down
9 changes: 9 additions & 0 deletions src/controllers/job.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export const createJob = async (req: Request, res: Response): Promise<any> => {
console.error(err);
}
};

export const getJob = async (req: Request, res: Response): Promise<any> => {
try {
const job = await jobService.getJobById(req.params.id);
res.send(job);
} catch (err) {
console.error(err);
}
};
13 changes: 0 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import app from './app';
import './worker';
dotenv.config();

mongoose.set('strictQuery', true);
mongoose
.connect(process.env.MONGODB_URL as string)
.then(() => {
console.info('Connected to MongoDB');
})
.catch(err => {
console.error(
`MongoDB connection error. Please make sure MongoDB is running. ${err}`,
);
});

const server = app.listen(process.env.PORT, () => {
console.info(`Listening to port ${process.env.PORT}`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './web3.interface';
export * from './job.interface';
5 changes: 5 additions & 0 deletions src/interfaces/job.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface JobRequestBody {
name: string;
rpcURL: string;
txId: string;
}
57 changes: 57 additions & 0 deletions src/interfaces/web3.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export interface Transaction {
hash: string;
nonce: number;
blockHash: string | null;
blockNumber: number | null;
transactionIndex: number | null;
from: string;
to: string | null;
value: string;
gasPrice: string;
maxPriorityFeePerGas?: number | string | any;
maxFeePerGas?: number | string | any;
gas: number;
input: string;
}

export interface TransactionReceipt {
status: boolean;
transactionHash: string;
transactionIndex: number;
blockHash: string;
blockNumber: number;
from: string;
to: string;
contractAddress?: string;
cumulativeGasUsed: number;
gasUsed: number;
effectiveGasPrice: number;
logs: Log[];
logsBloom: string;
events?: {
[eventName: string]: EventLog;
};
}

export interface EventLog {
event: string;
address: string;
returnValues: any;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
raw?: { data: string; topics: any[] };
}
export interface Log {
address: string;
data: string;
topics: string[];
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
removed: boolean;
}
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import jobRoute from './jobRouter.route';
import jobRoute from './job.route';

const router = Router();

Expand Down
2 changes: 1 addition & 1 deletion src/routes/jobRouter.route.ts → src/routes/job.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { jobController } from '../controllers';
const router = Router();

router.route('/').post(jobController.createJob);

router.route('/:id').get(jobController.getJob);
export default router;
15 changes: 15 additions & 0 deletions src/services/axios.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from 'axios';
import dotenv from 'dotenv';
import { Transaction } from '../interfaces';
dotenv.config();

export const updateTransactionJobStatus = async (
txHash: string,
body: Transaction,
) => {
const url = process.env.GATEWAY_BACKEND_URL;
return axios.put(
`${url}/api/v1/transactions/update/swap/and/withdraw/job/${txHash}`,
body,
);
};
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as jobService from './job.service';
export * as web3Service from './web3.service';
export * as axiosService from './axios.service';
12 changes: 9 additions & 3 deletions src/services/job.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Queue } from 'bullmq';
import { Queue, Job } from 'bullmq';
import { JobRequestBody } from '../interfaces';
import dotenv from 'dotenv';
dotenv.config();

const queue = new Queue(process.env.QUEUE as string);

export const addJobs = async (jobBody: any) => {
const job = await queue.add(jobBody.name, jobBody.data);
export const addJobs = async (jobBody: JobRequestBody): Promise<Job> => {
const job = await queue.add(jobBody.name, jobBody);
return job;
};

export const getJobById = async (jobId: string): Promise<any> => {
const job = await queue.getJob(jobId);
return job;
};
25 changes: 18 additions & 7 deletions src/services/web3.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import Web3 from 'web3';
import { TransactionReceipt, Transaction } from '../interfaces';

export const getTransactionReceipt = async (txId: string, rpcURL: string) => {
export const getTransactionReceipt = async (
txId: string,
rpcURL: string,
): Promise<TransactionReceipt> => {
const web3 = new Web3(rpcURL);
let transaction: any;
while (
transaction &&
(transaction.status === null || transaction.status === 'pending')
) {
transaction = await web3.eth.getTransactionReceipt(txId);
const transaction: TransactionReceipt = await web3.eth.getTransactionReceipt(
txId,
);
if (transaction === null || transaction.status === null) {
getTransactionReceipt(txId, rpcURL);
}
return transaction;
};

export const getTransactionByHash = async (
txHash: string,
rpcURL: string,
): Promise<Transaction> => {
const web3 = new Web3(rpcURL);
return web3.eth.getTransaction(txHash);
};
21 changes: 15 additions & 6 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { Worker } from 'bullmq';
import { web3Service } from './services';
import { web3Service, axiosService } from './services';

const worker = new Worker(
process.env.QUEUE as string,
async job =>
await web3Service.getTransactionReceipt(job.data.txId, job.data.rpcURL),
);
worker.on('completed', job => {
console.log(`${job.id} has completed!`);
// return job.data;
worker.on('completed', async job => {
console.info(`${job.id} has completed!`);
const tx = await web3Service.getTransactionByHash(
job.data.txId,
job.data.rpcURL,
);
axiosService.updateTransactionJobStatus(tx.hash, tx);
});

worker.on('failed', (job: any, err) => {
console.log(`${job.id} has failed with ${err.message}`);
worker.on('failed', async (job: any, err) => {
console.info(`${job.id} has failed with ${err.message}`);
const tx = await web3Service.getTransactionByHash(
job.data.txId,
job.data.rpcURL,
);
axiosService.updateTransactionJobStatus(tx.hash, tx);
});

export default worker;

0 comments on commit 9e57abc

Please sign in to comment.