Provider are now starting asynchronously

This commit is contained in:
2021-01-25 19:10:13 +01:00
parent 868a408a9d
commit e00a4fbf1c
6 changed files with 96 additions and 72 deletions

View File

@@ -25,7 +25,7 @@ export abstract class BackendProvider {
*
* @returns If `false` is returned, then the provider failed to initialize.
*/
abstract onEnable(): boolean;
abstract onEnable(): Promise<void>;
/**
* Generate a new address to receive new funds.

View File

@@ -28,7 +28,7 @@ export class ProviderManager {
.filter(dirent => dirent.name.endsWith('.ts'))
.map(dirent => dirent.name)
getDirectories().forEach(file => {
getDirectories().forEach(async file => {
const absolutePath = join(this.providerFilePath, file);
const providerModule = require(absolutePath);
const provider = new providerModule.Provider() as BackendProvider;
@@ -44,13 +44,14 @@ export class ProviderManager {
});
// Execute onEnable() function of this provider
const startUp = provider.onEnable();
if (!startUp) {
logger.error(`Provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) failed to start! (check previous logs)`);
return;
try {
await provider.onEnable();
logger.info(`Loaded provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) for ${provider.CRYPTO.join(', ')}`);
} catch (err) {
logger.error(`Provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) failed to start: ${err}`);
this.disable(provider.NAME);
}
logger.info(`Loaded provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) for ${provider.CRYPTO.join(', ')}`);
});
}

View File

@@ -18,19 +18,26 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.BITCOIN];
onEnable() {
this.sock = new Subscriber();
this.sock.connect('tcp://127.0.0.1:29000');
this.sock.subscribe('rawtx');
return new Promise<void>((resolve, reject) => {
this.sock = new Subscriber();
this.sock.connect('tcp://127.0.0.1:29000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 18332,
auth: 'admin:admin'
});
this.rpcClient = rpc.Client.http({
port: 18332,
auth: 'admin:admin'
});
this.listener();
return true;
// We perfom a small test call to check if everything works.
this.rpcClient.request('getblockchaininfo', [], (err, message) => {
if (err) {
reject(`Cannot connect with Bitcoin Core: ${err.message}`);
return;
}
this.listener();
resolve();
});
});
}
async getNewAddress(): Promise<string> {

View File

@@ -18,20 +18,28 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.DOGECOIN];
onEnable() {
this.sock = new Subscriber();
this.sock.connect('tcp://127.0.0.1:30000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 22556,
auth: 'admin:admin'
return new Promise<void>((resolve, reject) => {
this.sock = new Subscriber();
this.sock.connectTimeout = 2;
this.sock.connect('tcp://127.0.0.1:30000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 22556,
auth: 'admin:admin'
});
// We perfom a small test call to check if everything works.
this.rpcClient.request('getblockchaininfo', [], (err, message) => {
if (err) {
reject(`Cannot connect with Dogecoin Core: ${err.message}`);
return;
}
this.listener();
resolve();
});
});
this.listener();
return true;
//logger.info('The Bitcoin Core backend is now available!');
}

View File

@@ -18,19 +18,28 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.LITECOIN];
onEnable() {
this.sock = new Subscriber();
this.sock.connect('tcp://127.0.0.1:40000');
this.sock.subscribe('rawtx');
return new Promise<void>((resolve, reject) => {
this.sock = new Subscriber();
this.sock.connectTimeout = 2;
this.sock.connect('tcp://127.0.0.1:40000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 22557,
auth: 'admin:admin'
this.rpcClient = rpc.Client.http({
port: 22557,
auth: 'admin:admin'
});
// We perfom a small test call to check if everything works.
this.rpcClient.request('getblockchaininfo', [], (err, message) => {
if (err) {
reject(`Cannot connect with Bitcoin Core: ${err.message}`);
return;
}
this.listener();
resolve();
});
});
this.listener();
return true;
}
async getNewAddress(): Promise<string> {

View File

@@ -19,39 +19,38 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.MONERO];
onEnable() {
if (process.env.MONERO_WALLET_PASSWORD === undefined) {
logger.error(`Enviroment variable MONERO_WALLET_PASSWORD is required but not set!`);
return false;
}
if (process.env.MONERO_WALLET_NAME === undefined) {
logger.error(`Enviroment variable MONERO_WALLET_FILEPATH is required but not set!`);
return false;
}
if (process.env.MONERO_ZMQ_ADDRESS === undefined) {
logger.error(`Enviroment variable MONERO_ZMQ_ADDRESS is required but not set!`);
return false;
}
this.rpcClient = rpc.Client.http({
path: '/json_rpc',
port: 38085
});
this.rpcClient.request('open_wallet', {
filename: process.env.MONERO_WALLET_NAME,
password: process.env.MONERO_WALLET_PASSWORD
}, (err, message) => {
if (err) {
console.log(err);
logger.error(`Failed to open Monero wallet: ${err}\nMaybe a wrong password or path?`);
return new Promise<void>((resolve, reject) => {
if (process.env.MONERO_WALLET_PASSWORD === undefined) {
reject('Enviroment variable MONERO_WALLET_PASSWORD is required but not set!');
return;
}
if (process.env.MONERO_WALLET_NAME === undefined) {
reject('Enviroment variable MONERO_WALLET_FILEPATH is required but not set!');
return;
}
if (process.env.MONERO_ZMQ_ADDRESS === undefined) {
reject('Enviroment variable MONERO_ZMQ_ADDRESS is required but not set!');
return;
}
this.rpcClient = rpc.Client.http({
path: '/json_rpc',
port: 38085
});
this.rpcClient.request('open_wallet', {
filename: process.env.MONERO_WALLET_NAME,
password: process.env.MONERO_WALLET_PASSWORD
}, (err, message) => {
if (err) {
reject(`Failed to open Monero wallet: ${err}\nMaybe a wrong password or path?`)
return;
}
this.listener();
resolve();
});
});
this.listener();
return true;
}
async listener() {