-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ferrumnet/main
Merged with main
- Loading branch information
Showing
16 changed files
with
145 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './web3.interface'; | ||
export * from './job.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface JobRequestBody { | ||
name: string; | ||
rpcURL: string; | ||
txId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |