Skip to content

Commit

Permalink
Merge pull request #10 from jelly-swap/btc-algo-improvements
Browse files Browse the repository at this point in the history
BTC and ALGO performance.
  • Loading branch information
kraikov authored Nov 16, 2020
2 parents 978c861 + 8246d86 commit da673d6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/blockchain/algorand/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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<Object>).reduce((p: any, c: any) => {
p.push(
new Swap(
p.push({
...new Swap(
'ALGO',
c.transactionHash,
c.blockHeight,
Expand All @@ -75,21 +78,20 @@ 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<Object>).reduce((p: any, c: any) => {
p.push(
new Withdraw('ALGO', c.transactionHash, c.blockHeight, c.id, c.secret, c.hashLock, c.sender, c.receiver)
);
return p;
}, [] as any);

const refundResponse = await axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`);
const refunds = (refundResponse.data as Array<Object>).reduce((p: any, c: any) => {
p.push(new Refund('ALGO', c.transactionHash, c.blockHeight, c.id, c.hashLock, c.sender, c.receiver));
return p;
Expand Down
10 changes: 6 additions & 4 deletions src/blockchain/bitcoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Object>).reduce((p: any, c: any) => {
p.push(
new Swap(
Expand All @@ -81,15 +85,13 @@ export default class BitcoinEvent {
return p;
}, [] as any);

const withdrawResponse = await axios.get(`${Config.apiUrl}/withdraw/block/${fromBlock}`);
const withdraws = (withdrawResponse.data as Array<Object>).reduce((p: any, c: any) => {
p.push(
new Withdraw('BTC', c.transactionHash, c.blockHeight, c.id, c.secret, c.hashLock, c.sender, c.receiver)
);
return p;
}, [] as any);

const refundResponse = await axios.get(`${Config.apiUrl}/refund/block/${fromBlock}`);
const refunds = (refundResponse.data as Array<Object>).reduce((p: any, c: any) => {
p.push(new Refund('BTC', c.transactionHash, c.blockHeight, c.id, c.hashLock, c.sender, c.receiver));
return p;
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Networks = {
AVA: AvalancheEvent,
MATIC: MaticEvent,
BNB: BinanceEvent,
ALGO: AlgorandEvent
ALGO: AlgorandEvent,
};

export default async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/swap/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default class Swap {
@Column()
expiration: number;

@Column()
expireBlock: number;

@Column()
@Index({ unique: true })
id: string;
Expand Down

0 comments on commit da673d6

Please sign in to comment.