Skip to content

Commit

Permalink
Merge pull request #21 from jelly-swap/xinfin-web3-all
Browse files Browse the repository at this point in the history
Xinfin web3 all
  • Loading branch information
kraikov authored Apr 23, 2021
2 parents 3e64bbe + fd446f3 commit fabd79d
Show file tree
Hide file tree
Showing 3 changed files with 1,008 additions and 1,481 deletions.
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
"main": "index.js",
"author": "kraikov",
"license": "MIT",
"engines": {
"node": "12.x",
"npm": "6.13.x",
"yarn": "1.22.x"
},
"scripts": {
"heroku-prebuild": "npm install -g [email protected]",
"start": "ts-node src/index.ts",
"start-prod": "cross-env NODE_ENV=production ts-node src/index.ts",
"start-dev": "cross-env NODE_ENV=development ts-node src/index.ts"
Expand Down Expand Up @@ -47,11 +41,10 @@
"tsyringe": "^4.3.0",
"typeorm": "^0.2.25",
"typescript": "^3.9.5",
"web3": "^1.3.4",
"websocket": "^1.0.31",
"winston": "^3.2.1",
"ws": "^7.3.0",
"xdc3": "^1.2.1211"
"xdc3": "^1.3.13416"
},
"devDependencies": {
"@types/body-parser": "^1.19.0",
Expand Down
116 changes: 17 additions & 99 deletions src/blockchain/xinfin/index.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,51 @@
import Web3 from 'xdc3';


import Config from './config';

import { getRefund, getWithdraw, getSwap } from './utils';

import Swap from '../../components/swap/entity';
import Withdraw from '../../components/withdraw/entity';
import Refund from '../../components/refund/entity';

import Emitter from '../../websocket/emitter';
import Logger from '../../logger';

const SYNC_PERIOD = 20000; //20 seconds

export default class XinfinEvent {
public readonly syncBlocksMargin = Config.syncBlocksMargin;
public provider: any;
public wsProvider: any;
private contract: any;
private wsContract: any;
private emitter: Emitter;
private web3: any;
private lastScanned: number;

constructor() {
this.web3 = new Web3(Config.provider);
this.provider = new this.web3.providers.HttpProvider(Config.provider);
this.wsProvider = new this.web3.providers.WebsocketProvider(Config.providerWs);
this.contract = new this.web3.eth.Contract(Config.abi, Config.contractAddress);
this.wsContract = new (new Web3(this.wsProvider)).eth.Contract(Config.abi, Config.contractAddress);
this.provider = new Web3(Config.provider);
this.contract = new this.provider.eth.Contract(Config.abi, Config.contractAddress);
this.emitter = Emitter.Instance;
}

async getBlock() {
return await this.web3.eth.getBlockNumber();
return await this.provider.eth.getBlockNumber();
}

subscribe(){
this.wsContract.events
.NewContract()
.on('data', (event) => {
const baseTx = {
network: 'XDC',
transactionHash: event.transactionHash,
blockNumber: parseInt(event.blockNumber),
};
const swap = { ...baseTx, ...getSwap(event.returnValues) };

this.emitter.emit(
'SWAPS',
new Swap(
swap.network,
swap.transactionHash,
swap.blockNumber,
swap.inputAmount.toString(),
swap.outputAmount.toString(),
Number(swap.expiration),
swap.id,
swap.hashLock,
swap.sender,
swap.receiver,
swap.outputNetwork,
swap.outputAddress
)
);
})
.on('error', (err) => Logger.error(`Xinfin NewContract ${err}`));

this.wsContract.events
.Withdraw()
.on('data', (event) => {
const baseTx = {
network: 'XDC',
transactionHash: event.transactionHash,
blockNumber: parseInt(event.blockNumber),
};
const withdraw = { ...baseTx, ...getWithdraw(event.returnValues) };

this.emitter.emit(
'WITHDRAWS',
new Withdraw(
withdraw.network,
withdraw.transactionHash,
withdraw.blockNumber,
withdraw.id,
withdraw.secret,
withdraw.hashLock,
withdraw.sender,
withdraw.receiver
)
);
})
.on('error', (err) => Logger.error(`Xinfin Withdraw ${err}`));

this.wsContract.events
.Refund()
.on('data', (event) => {
const baseTx = {
network: 'XDC',
transactionHash: event.transactionHash,
blockNumber: parseInt(event.blockNumber),
};
const refund = { ...baseTx, ...getRefund(event.returnValues) };

this.emitter.emit(
'REFUNDS',
new Refund(
refund.network,
refund.transactionHash,
refund.blockNumber,
refund.id,
refund.hashLock,
refund.sender,
refund.receiver
)
);
})
.on('error', (err) => Logger.error(`XDC Refund ${err}`));
async subscribe() {
this.lastScanned = await this.getBlock();
setInterval(async () => {
const latestBlock = await this.getBlock();
await this.getPast(this.lastScanned);
this.lastScanned = latestBlock + 1;
}, SYNC_PERIOD);
}

async getPast(fromBlock?: number) {
const swaps: Swap[] = [];
const withdraws: Withdraw[] = [];
const refunds: Refund[] = [];

const result = await this.contract.getPastEvents('allEvents',
{
fromBlock: '0x' + (fromBlock || Config.originBlock).toString(16),
toBlock: 'latest'
},
);
const result = await this.contract.getPastEvents('allEvents', {
fromBlock: '0x' + (fromBlock || Config.originBlock).toString(16),
toBlock: 'latest',
});

result.forEach((log) => {
const baseTx = {
Expand Down
Loading

0 comments on commit fabd79d

Please sign in to comment.