This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
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.
Query utxos from local database (#25)
* Add Unspents model * Add Market abstraction to comsume crawlet deposit event * Add Unspent abstraction to comsume crawler balance event * 🧶 Query utxos from datastore and lock them in tradePropose service
- Loading branch information
Showing
17 changed files
with
504 additions
and
165 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,7 +1,6 @@ | ||
dist | ||
.DS_Store | ||
/test/datadir/db | ||
/test/datadir/config.json | ||
# Logs | ||
logs | ||
*.log | ||
|
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,53 @@ | ||
import Datastore from 'nedb'; | ||
import { isValidUrl, fetchUtxosWithUrl, groupByAsset } from '../utils'; | ||
import Unspents from '../models/unspents'; | ||
|
||
export default class Balance { | ||
constructor(private datastore: Datastore, private explorer: string) { | ||
if (!isValidUrl(this.explorer)) throw new Error('Not a valid explorer url'); | ||
} | ||
|
||
async fromAsset(walletAddress: string, asset: string) { | ||
let unspents = []; | ||
try { | ||
const model = new Unspents(this.datastore); | ||
unspents = await model.getUnspents({ | ||
address: walletAddress, | ||
asset, | ||
spent: false, | ||
}); | ||
} catch (error) { | ||
unspents = await fetchUtxosWithUrl(walletAddress, this.explorer); | ||
} | ||
|
||
const balance = (unspents as any) | ||
.map((x: { value: any }) => x.value) | ||
.reduce((a: number, b: number) => a + b, 0); | ||
|
||
return { | ||
[asset]: { utxos: unspents, balance }, | ||
}; | ||
} | ||
|
||
async fromMarket( | ||
walletAddress: string, | ||
{ baseAsset, quoteAsset }: { baseAsset: string; quoteAsset: string } | ||
) { | ||
let unspents = []; | ||
try { | ||
const model = new Unspents(this.datastore); | ||
unspents = await model.getUnspents({ | ||
address: walletAddress, | ||
spent: false, | ||
}); | ||
} catch (error) { | ||
unspents = await fetchUtxosWithUrl(walletAddress, this.explorer); | ||
} | ||
|
||
const groupedBy = groupByAsset(unspents); | ||
return { | ||
[baseAsset]: groupedBy[baseAsset], | ||
[quoteAsset]: groupedBy[quoteAsset], | ||
}; | ||
} | ||
} |
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,57 @@ | ||
import Datastore from 'nedb'; | ||
import { UtxoInterface } from '../utils'; | ||
import Markets from '../models/markets'; | ||
import winston from 'winston'; | ||
|
||
export default class Market { | ||
static async getWallets( | ||
datastore: Datastore, | ||
logger: winston.Logger | ||
): Promise<Array<string>> { | ||
const wallets: string[] | PromiseLike<string[]> = []; | ||
try { | ||
const model = new Markets(datastore); | ||
const markets = await model.getMarkets(); | ||
const wallets = markets.map((m) => m.walletAddress); | ||
return wallets; | ||
} catch (ignore) { | ||
logger.error(`Cannot fetch markets from datastore`); | ||
return wallets; | ||
} | ||
} | ||
|
||
static async fromFundingUtxos( | ||
walletAddress: string, | ||
fundingUtxos: Array<UtxoInterface>, | ||
datastore: Datastore, | ||
logger: winston.Logger, | ||
{ baseAsset, fee }: { baseAsset: string; fee: number } | ||
) { | ||
const [first, second] = fundingUtxos; | ||
const quoteAsset = first.asset !== baseAsset ? first.asset : second.asset; | ||
const baseFundingTx = first.asset === baseAsset ? first.txid : second.txid; | ||
const quoteFundingTx = first.asset !== baseAsset ? first.txid : second.txid; | ||
|
||
try { | ||
const model = new Markets(datastore); | ||
await model.updateMarketByWallet( | ||
{ walletAddress }, | ||
{ | ||
baseAsset, | ||
quoteAsset, | ||
baseFundingTx, | ||
quoteFundingTx, | ||
fee, | ||
tradable: true, | ||
} | ||
); | ||
logger.info( | ||
`New deposit for market ${quoteAsset} on address ${walletAddress}` | ||
); | ||
} catch (ignore) { | ||
logger.error( | ||
`Error on creation market ${quoteAsset} on address ${walletAddress}` | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.