From 8246d868c5e2db3bfaefd82171e6477d1b0bb104 Mon Sep 17 00:00:00 2001 From: kraikov Date: Fri, 13 Nov 2020 23:04:20 +0200 Subject: [PATCH] BTC and ALGO performance. --- src/blockchain/algorand/index.ts | 22 ++++++++++++---------- src/blockchain/bitcoin/index.ts | 10 ++++++---- src/blockchain/index.ts | 2 +- src/components/swap/entity.ts | 3 +++ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/blockchain/algorand/index.ts b/src/blockchain/algorand/index.ts index b448453..61ac8b9 100644 --- a/src/blockchain/algorand/index.ts +++ b/src/blockchain/algorand/index.ts @@ -9,7 +9,6 @@ import Config from './config'; import Emitter from '../../websocket/emitter'; export default class AlgorandEvent { - public readonly syncBlocksMargin = Config.syncBlocksMargin; private emitter: Emitter; private lastBlock: number; @@ -28,9 +27,8 @@ export default class AlgorandEvent { async subscribe() { setInterval(async () => { const lastBlock = (await axios.get(`${Config.apiUrl}/lastBlock`)).data; - if (this.lastBlock !== lastBlock) { - const { swaps, withdraws, refunds } = await this._getEvents(lastBlock); + const { swaps, withdraws, refunds } = await this._getEvents(this.lastBlock); swaps.forEach((s: Swap) => { this.emitter.emit('SWAPS', s); @@ -58,10 +56,15 @@ export default class AlgorandEvent { } async _getEvents(fromBlock = 0) { - const swapResponse = await axios.get(`${Config.apiUrl}/swap/block/${fromBlock}`); + const [swapResponse, withdrawResponse, refundResponse] = await Promise.all([ + axios.get(`${Config.apiUrl}/swap/block/${fromBlock}`), + axios.get(`${Config.apiUrl}/withdraw/block/${fromBlock}`), + axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`), + ]); + const swaps: Swap[] = (swapResponse.data as Array).reduce((p: any, c: any) => { - p.push( - new Swap( + p.push({ + ...new Swap( 'ALGO', c.transactionHash, c.blockHeight, @@ -75,13 +78,13 @@ export default class AlgorandEvent { c.outputNetwork, c.outputAddress, c.refundAddress - ) - ); + ), + expireBlock: c.expireBlock, + }); return p; }, [] as any); - const withdrawResponse = await axios.get(`${Config.apiUrl}/withdraw/block/${fromBlock}`); const withdraws = (withdrawResponse.data as Array).reduce((p: any, c: any) => { p.push( new Withdraw('ALGO', c.transactionHash, c.blockHeight, c.id, c.secret, c.hashLock, c.sender, c.receiver) @@ -89,7 +92,6 @@ export default class AlgorandEvent { return p; }, [] as any); - const refundResponse = await axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`); const refunds = (refundResponse.data as Array).reduce((p: any, c: any) => { p.push(new Refund('ALGO', c.transactionHash, c.blockHeight, c.id, c.hashLock, c.sender, c.receiver)); return p; diff --git a/src/blockchain/bitcoin/index.ts b/src/blockchain/bitcoin/index.ts index 0b4dff8..1e32fe5 100644 --- a/src/blockchain/bitcoin/index.ts +++ b/src/blockchain/bitcoin/index.ts @@ -9,7 +9,6 @@ import Config from './config'; import Emitter from '../../websocket/emitter'; export default class BitcoinEvent { - public readonly syncBlocksMargin = Config.syncBlocksMargin; private emitter: Emitter; private lastBlock: number; @@ -58,7 +57,12 @@ export default class BitcoinEvent { } async _getEvents(fromBlock = 0) { - const swapResponse = await axios.get(`${Config.apiUrl}/swap/block/${fromBlock}`); + const [swapResponse, withdrawResponse, refundResponse] = await Promise.all([ + axios.get(`${Config.apiUrl}/swap/block/${fromBlock}`), + axios.get(`${Config.apiUrl}/withdraw/block/${fromBlock}`), + axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`), + ]); + const swaps: Swap[] = (swapResponse.data as Array).reduce((p: any, c: any) => { p.push( new Swap( @@ -81,7 +85,6 @@ export default class BitcoinEvent { return p; }, [] as any); - const withdrawResponse = await axios.get(`${Config.apiUrl}/withdraw/block/${fromBlock}`); const withdraws = (withdrawResponse.data as Array).reduce((p: any, c: any) => { p.push( new Withdraw('BTC', c.transactionHash, c.blockHeight, c.id, c.secret, c.hashLock, c.sender, c.receiver) @@ -89,7 +92,6 @@ export default class BitcoinEvent { return p; }, [] as any); - const refundResponse = await axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`); const refunds = (refundResponse.data as Array).reduce((p: any, c: any) => { p.push(new Refund('BTC', c.transactionHash, c.blockHeight, c.id, c.hashLock, c.sender, c.receiver)); return p; diff --git a/src/blockchain/index.ts b/src/blockchain/index.ts index 33362c5..d4cef4e 100644 --- a/src/blockchain/index.ts +++ b/src/blockchain/index.ts @@ -24,7 +24,7 @@ const Networks = { AVA: AvalancheEvent, MATIC: MaticEvent, BNB: BinanceEvent, - ALGO: AlgorandEvent + ALGO: AlgorandEvent, }; export default async () => { diff --git a/src/components/swap/entity.ts b/src/components/swap/entity.ts index e153479..c2c5f1b 100644 --- a/src/components/swap/entity.ts +++ b/src/components/swap/entity.ts @@ -24,6 +24,9 @@ export default class Swap { @Column() expiration: number; + @Column() + expireBlock: number; + @Column() @Index({ unique: true }) id: string;