feat: cargo project at root

This commit is contained in:
binarybaron 2024-08-08 00:49:04 +02:00
parent aa0c0623ca
commit 709a2820c4
No known key found for this signature in database
GPG key ID: 99B75D3E1476A26E
313 changed files with 1 additions and 740 deletions

View file

@ -0,0 +1,42 @@
export function satsToBtc(sats: number): number {
return sats / 100000000;
}
export function btcToSats(btc: number): number {
return btc * 100000000;
}
export function piconerosToXmr(piconeros: number): number {
return piconeros / 1000000000000;
}
export function isXmrAddressValid(address: string, stagenet: boolean) {
const re = stagenet
? '[57][0-9AB][1-9A-HJ-NP-Za-km-z]{93}'
: '[48][0-9AB][1-9A-HJ-NP-Za-km-z]{93}';
return new RegExp(`(?:^${re}$)`).test(address);
}
export function isBtcAddressValid(address: string, testnet: boolean) {
const re = testnet
? '(tb1)[a-zA-HJ-NP-Z0-9]{25,49}'
: '(bc1)[a-zA-HJ-NP-Z0-9]{25,49}';
return new RegExp(`(?:^${re}$)`).test(address);
}
export function getBitcoinTxExplorerUrl(txid: string, testnet: boolean) {
return `https://blockchair.com/bitcoin${
testnet ? '/testnet' : ''
}/transaction/${txid}`;
}
export function getMoneroTxExplorerUrl(txid: string, stagenet: boolean) {
if (stagenet) {
return `https://stagenet.xmrchain.net/tx/${txid}`;
}
return `https://xmrchain.net/tx/${txid}`;
}
export function secondsToDays(seconds: number): number {
return seconds / 86400;
}

View file

@ -0,0 +1,5 @@
import { createHash } from 'crypto';
export function sha256(data: string): string {
return createHash('md5').update(data).digest('hex');
}

View file

@ -0,0 +1,21 @@
export class SingleTypeEventEmitter<T> {
private listeners: Array<(data: T) => void> = [];
// Method to add a listener for the event
on(listener: (data: T) => void) {
this.listeners.push(listener);
}
// Method to remove a listener
off(listener: (data: T) => void) {
const index = this.listeners.indexOf(listener);
if (index > -1) {
this.listeners.splice(index, 1);
}
}
// Method to emit the event
emit(data: T) {
this.listeners.forEach((listener) => listener(data));
}
}

View file

@ -0,0 +1,7 @@
import pino from 'pino';
export default pino(
{
level: 'trace',
}
);

View file

@ -0,0 +1,24 @@
import { Multiaddr } from 'multiaddr';
import semver from 'semver';
import { ExtendedProviderStatus, Provider } from 'models/apiModel';
import { isTestnet } from 'store/config';
const MIN_ASB_VERSION = '0.12.0';
export function providerToConcatenatedMultiAddr(provider: Provider) {
return new Multiaddr(provider.multiAddr)
.encapsulate(`/p2p/${provider.peerId}`)
.toString();
}
export function isProviderCompatible(
provider: ExtendedProviderStatus,
): boolean {
if (provider.version) {
if (!semver.satisfies(provider.version, `>=${MIN_ASB_VERSION}`))
return false;
}
if (provider.testnet !== isTestnet()) return false;
return true;
}

View file

@ -0,0 +1,65 @@
import { CliLog, isCliLog } from 'models/cliModel';
/*
Extract btc amount from string
E.g: "0.00100000 BTC"
Output: 0.001
*/
export function extractAmountFromUnitString(text: string): number | null {
if (text != null) {
const parts = text.split(' ');
if (parts.length === 2) {
const amount = Number.parseFloat(parts[0]);
return amount;
}
}
return null;
}
// E.g 2021-12-29 14:25:59.64082 +00:00:00
export function parseDateString(str: string): number {
const parts = str.split(' ').slice(0, -1);
if (parts.length !== 2) {
throw new Error(
`Date string does not consist solely of date and time Str: ${str} Parts: ${parts}`,
);
}
const wholeString = parts.join(' ');
const date = Date.parse(wholeString);
if (Number.isNaN(date)) {
throw new Error(
`Date string could not be parsed Str: ${str} Parts: ${parts}`,
);
}
return date;
}
export function getLinesOfString(data: string): string[] {
return data
.toString()
.replace('\r\n', '\n')
.replace('\r', '\n')
.split('\n')
.filter((l) => l.length > 0);
}
export function getLogsAndStringsFromRawFileString(
rawFileData: string,
): (CliLog | string)[] {
return getLinesOfString(rawFileData).map((line) => {
try {
return JSON.parse(line);
} catch (e) {
return line;
}
});
}
export function getLogsFromRawFileString(rawFileData: string): CliLog[] {
return getLogsAndStringsFromRawFileString(rawFileData).filter(isCliLog);
}
export function logsToRawString(logs: (CliLog | string)[]): string {
return logs.map((l) => JSON.stringify(l)).join('\n');
}

View file

@ -0,0 +1,19 @@
import { ExtendedProviderStatus } from 'models/apiModel';
export function sortProviderList(list: ExtendedProviderStatus[]) {
return list.concat().sort((firstEl, secondEl) => {
// If neither of them have a relevancy score, sort by max swap amount
if (firstEl.relevancy === undefined && secondEl.relevancy === undefined) {
if (firstEl.maxSwapAmount > secondEl.maxSwapAmount) {
return -1;
}
}
// If only on of the two don't have a relevancy score, prioritize the one that does
if (firstEl.relevancy === undefined) return 1;
if (secondEl.relevancy === undefined) return -1;
if (firstEl.relevancy > secondEl.relevancy) {
return -1;
}
return 1;
});
}

View file

@ -0,0 +1,7 @@
export function exhaustiveGuard(_value: never): never {
throw new Error(
`ERROR! Reached forbidden guard function with unexpected value: ${JSON.stringify(
_value,
)}`,
);
}