-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b225db
commit 0c33029
Showing
12 changed files
with
462 additions
and
5 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
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,47 @@ | ||
import { DataTypes, Model, Sequelize } from 'sequelize'; | ||
import Swap from './Swap'; | ||
|
||
export type PendingLockupTransactionType = { | ||
swapId: string; | ||
chain: string; | ||
}; | ||
|
||
class PendingLockupTransaction | ||
extends Model | ||
implements PendingLockupTransactionType | ||
{ | ||
public swapId!: string; | ||
public chain!: string; | ||
|
||
public static load = (sequelize: Sequelize) => { | ||
PendingLockupTransaction.init( | ||
{ | ||
swapId: { | ||
type: new DataTypes.STRING(255), | ||
primaryKey: true, | ||
allowNull: false, | ||
}, | ||
chain: { | ||
type: new DataTypes.STRING(255), | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
sequelize, | ||
modelName: 'pendingLockupTransaction', | ||
indexes: [ | ||
{ | ||
unique: false, | ||
fields: ['chain'], | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
PendingLockupTransaction.belongsTo(Swap, { | ||
foreignKey: 'swapId', | ||
}); | ||
}; | ||
} | ||
|
||
export default PendingLockupTransaction; |
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,22 @@ | ||
import PendingLockupTransaction from '../models/PendingLockupTransaction'; | ||
|
||
class PendingLockupTransactionRepository { | ||
public static create = (swapId: string, chain: string) => | ||
PendingLockupTransaction.create({ swapId, chain }); | ||
|
||
public static destroy = async (swapId: string) => | ||
PendingLockupTransaction.destroy({ | ||
where: { | ||
swapId, | ||
}, | ||
}); | ||
|
||
public static getForChain = (chain: string) => | ||
PendingLockupTransaction.findAll({ | ||
where: { | ||
chain, | ||
}, | ||
}); | ||
} | ||
|
||
export default PendingLockupTransactionRepository; |
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,83 @@ | ||
import AsyncLock from 'async-lock'; | ||
import { Op } from 'sequelize'; | ||
import Logger from '../Logger'; | ||
import { formatError, getChainCurrency, splitPairId } from '../Utils'; | ||
import { IChainClient } from '../chain/ChainClient'; | ||
import Swap from '../db/models/Swap'; | ||
import PendingLockupTransactionRepository from '../db/repositories/PendingLockupTransactionRepository'; | ||
import SwapRepository from '../db/repositories/SwapRepository'; | ||
import { Currency } from '../wallet/WalletManager'; | ||
import RateProvider from './RateProvider'; | ||
|
||
class LockupTransactionTracker { | ||
private readonly lock = new AsyncLock(); | ||
private readonly zeroConfAcceptedMap = new Map<string, boolean>(); | ||
|
||
constructor( | ||
private readonly logger: Logger, | ||
currencies: Map<string, Currency>, | ||
private readonly rateProvider: RateProvider, | ||
) { | ||
for (const currency of currencies.values()) { | ||
if (currency.chainClient === undefined) { | ||
continue; | ||
} | ||
|
||
this.listenToBlocks(currency.chainClient); | ||
this.zeroConfAcceptedMap.set(currency.chainClient.symbol, true); | ||
} | ||
} | ||
|
||
public zeroConfAccepted = (symbol: string): boolean => | ||
this.zeroConfAcceptedMap.get(symbol) || false; | ||
|
||
public addPendingTransactionToTrack = async (swap: Swap) => { | ||
const { base, quote } = splitPairId(swap.pair); | ||
await PendingLockupTransactionRepository.create( | ||
swap.id, | ||
getChainCurrency(base, quote, swap.orderSide, false), | ||
); | ||
}; | ||
|
||
private listenToBlocks = (chainClient: IChainClient) => { | ||
chainClient.on('block', async () => { | ||
await this.lock.acquire(chainClient.symbol, () => | ||
this.checkPendingLockupsForChain(chainClient), | ||
); | ||
}); | ||
}; | ||
|
||
private checkPendingLockupsForChain = async (chainClient: IChainClient) => { | ||
const pendingLockups = await PendingLockupTransactionRepository.getForChain( | ||
chainClient.symbol, | ||
); | ||
const swaps = await SwapRepository.getSwaps({ | ||
id: { | ||
[Op.in]: pendingLockups.map((p) => p.swapId), | ||
}, | ||
}); | ||
|
||
for (const swap of swaps) { | ||
try { | ||
const info = await chainClient.getRawTransactionVerbose( | ||
swap.lockupTransactionId!, | ||
); | ||
if (info.confirmations !== undefined && info.confirmations > 0) { | ||
this.logger.debug( | ||
`Pending lockup transaction of Submarine Swap ${swap.id} (${swap.lockupTransactionId}) confirmed`, | ||
); | ||
await PendingLockupTransactionRepository.destroy(swap.id); | ||
} | ||
} catch (e) { | ||
this.logger.warn( | ||
`Could find pending lockup transaction of Submarine Swap ${swap.id} (${swap.lockupTransactionId}): ${formatError(e)}`, | ||
); | ||
this.logger.warn(`Disabling 0-conf for ${chainClient.symbol}`); | ||
this.zeroConfAcceptedMap.set(chainClient.symbol, false); | ||
await this.rateProvider.setZeroConfAmount(chainClient.symbol, 0); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export default LockupTransactionTracker; |
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
Oops, something went wrong.