2023-10-27 10:01:59 +00:00
|
|
|
import {
|
|
|
|
Coordinator,
|
2023-10-27 11:00:53 +00:00
|
|
|
type Exchange,
|
|
|
|
type Garage,
|
|
|
|
type Origin,
|
|
|
|
type PublicOrder,
|
|
|
|
type Settings,
|
2023-10-27 10:01:59 +00:00
|
|
|
defaultExchange,
|
|
|
|
} from '.';
|
|
|
|
import defaultFederation from '../../static/federation.json';
|
2023-12-30 12:27:27 +00:00
|
|
|
import { getHost } from '../utils';
|
2024-06-25 10:52:20 +00:00
|
|
|
import { coordinatorDefaultValues } from './Coordinator.model';
|
2023-10-27 10:01:59 +00:00
|
|
|
import { updateExchangeInfo } from './Exchange.model';
|
|
|
|
|
2024-03-07 16:06:53 +00:00
|
|
|
type FederationHooks = 'onCoordinatorUpdate' | 'onFederationUpdate';
|
2023-10-27 10:01:59 +00:00
|
|
|
|
|
|
|
export class Federation {
|
2024-03-28 22:21:15 +00:00
|
|
|
constructor(origin: Origin, settings: Settings, hostUrl: string) {
|
2024-06-25 10:52:20 +00:00
|
|
|
this.coordinators = {};
|
|
|
|
this.exchange = { ...defaultExchange };
|
2023-10-27 10:01:59 +00:00
|
|
|
this.book = [];
|
|
|
|
this.hooks = {
|
|
|
|
onCoordinatorUpdate: [],
|
2024-03-07 16:06:53 +00:00
|
|
|
onFederationUpdate: [],
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
2024-03-28 22:21:15 +00:00
|
|
|
|
2024-06-25 10:52:20 +00:00
|
|
|
Object.keys(defaultFederation).forEach((key) => {
|
|
|
|
if (key !== 'local' || getHost() === '127.0.0.1:8000') {
|
|
|
|
// Do not add `Local Dev` unless it is running on localhost
|
|
|
|
this.addCoordinator(origin, settings, hostUrl, defaultFederation[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-28 22:21:15 +00:00
|
|
|
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
|
2024-06-25 10:52:20 +00:00
|
|
|
this.loading = true;
|
2024-03-28 22:21:15 +00:00
|
|
|
|
|
|
|
const host = getHost();
|
|
|
|
const url = `${window.location.protocol}//${host}`;
|
2024-06-25 10:52:20 +00:00
|
|
|
|
2024-03-28 22:21:15 +00:00
|
|
|
const tesnetHost = Object.values(this.coordinators).find((coor) => {
|
|
|
|
return Object.values(coor.testnet).includes(url);
|
|
|
|
});
|
|
|
|
if (tesnetHost) settings.network = 'testnet';
|
2023-10-27 10:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public coordinators: Record<string, Coordinator>;
|
|
|
|
public exchange: Exchange;
|
|
|
|
public book: PublicOrder[];
|
|
|
|
public loading: boolean;
|
|
|
|
|
2023-10-27 11:00:53 +00:00
|
|
|
public hooks: Record<FederationHooks, Array<() => void>>;
|
2023-10-27 10:01:59 +00:00
|
|
|
|
2024-06-25 10:52:20 +00:00
|
|
|
addCoordinator = (
|
|
|
|
origin: Origin,
|
|
|
|
settings: Settings,
|
|
|
|
hostUrl: string,
|
|
|
|
attributes: Record<any, any>,
|
|
|
|
) => {
|
|
|
|
const value = {
|
|
|
|
...coordinatorDefaultValues,
|
|
|
|
...attributes,
|
|
|
|
};
|
|
|
|
this.coordinators[value.shortAlias] = new Coordinator(value, origin, settings, hostUrl);
|
|
|
|
this.exchange.totalCoordinators = Object.keys(this.coordinators).length;
|
|
|
|
this.updateEnabledCoordinators();
|
|
|
|
this.triggerHook('onFederationUpdate');
|
|
|
|
};
|
|
|
|
|
2023-10-27 10:01:59 +00:00
|
|
|
// Hooks
|
|
|
|
registerHook = (hookName: FederationHooks, fn: () => void): void => {
|
|
|
|
this.hooks[hookName].push(fn);
|
|
|
|
};
|
|
|
|
|
|
|
|
triggerHook = (hookName: FederationHooks): void => {
|
2023-10-27 11:00:53 +00:00
|
|
|
this.hooks[hookName]?.forEach((fn) => {
|
|
|
|
fn();
|
|
|
|
});
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
|
|
|
|
2023-12-30 10:32:18 +00:00
|
|
|
onCoordinatorSaved = (): void => {
|
|
|
|
this.book = Object.values(this.coordinators).reduce<PublicOrder[]>((array, coordinator) => {
|
|
|
|
return [...array, ...coordinator.book];
|
|
|
|
}, []);
|
2023-10-27 10:01:59 +00:00
|
|
|
this.triggerHook('onCoordinatorUpdate');
|
2024-01-23 23:30:16 +00:00
|
|
|
this.exchange.loadingCoordinators =
|
|
|
|
this.exchange.loadingCoordinators < 1 ? 0 : this.exchange.loadingCoordinators - 1;
|
|
|
|
this.loading = this.exchange.loadingCoordinators > 0;
|
2024-03-15 20:06:54 +00:00
|
|
|
this.updateExchange();
|
|
|
|
this.triggerHook('onFederationUpdate');
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
|
|
|
|
2024-03-28 22:21:15 +00:00
|
|
|
updateUrl = async (origin: Origin, settings: Settings, hostUrl: string): Promise<void> => {
|
2023-12-30 16:45:45 +00:00
|
|
|
for (const coor of Object.values(this.coordinators)) {
|
2024-03-28 22:21:15 +00:00
|
|
|
coor.updateUrl(origin, settings, hostUrl);
|
2023-12-30 16:45:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-02 14:15:18 +00:00
|
|
|
update = async (): Promise<void> => {
|
2024-01-23 23:30:16 +00:00
|
|
|
this.loading = true;
|
2024-03-15 20:06:54 +00:00
|
|
|
this.exchange.info = {
|
|
|
|
num_public_buy_orders: 0,
|
|
|
|
num_public_sell_orders: 0,
|
|
|
|
book_liquidity: 0,
|
|
|
|
active_robots_today: 0,
|
|
|
|
last_day_nonkyc_btc_premium: 0,
|
|
|
|
last_day_volume: 0,
|
|
|
|
lifetime_volume: 0,
|
|
|
|
version: { major: 0, minor: 0, patch: 0 },
|
|
|
|
};
|
2024-03-28 22:21:15 +00:00
|
|
|
this.exchange.onlineCoordinators = 0;
|
2024-01-23 23:30:16 +00:00
|
|
|
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
|
2024-03-28 22:21:15 +00:00
|
|
|
this.updateEnabledCoordinators();
|
2023-11-02 14:15:18 +00:00
|
|
|
for (const coor of Object.values(this.coordinators)) {
|
2024-06-16 22:14:32 +00:00
|
|
|
coor.update(() => {
|
2024-03-28 22:21:15 +00:00
|
|
|
this.exchange.onlineCoordinators = this.exchange.onlineCoordinators + 1;
|
2023-12-30 10:32:18 +00:00
|
|
|
this.onCoordinatorSaved();
|
2023-10-27 11:00:53 +00:00
|
|
|
});
|
2023-11-02 14:15:18 +00:00
|
|
|
}
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
|
|
|
|
2023-12-22 12:58:59 +00:00
|
|
|
updateBook = async (): Promise<void> => {
|
2024-01-23 23:30:16 +00:00
|
|
|
this.loading = true;
|
2024-06-16 22:14:32 +00:00
|
|
|
this.book = [];
|
2024-01-24 00:09:54 +00:00
|
|
|
this.triggerHook('onCoordinatorUpdate');
|
2024-01-23 23:30:16 +00:00
|
|
|
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
|
2023-12-22 12:58:59 +00:00
|
|
|
for (const coor of Object.values(this.coordinators)) {
|
2024-06-16 22:14:32 +00:00
|
|
|
coor.updateBook(() => {
|
2023-12-30 10:32:18 +00:00
|
|
|
this.onCoordinatorSaved();
|
2023-12-22 12:58:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-02 14:15:18 +00:00
|
|
|
updateExchange = (): void => {
|
2023-10-27 10:01:59 +00:00
|
|
|
this.exchange.info = updateExchangeInfo(this);
|
2024-03-07 16:06:53 +00:00
|
|
|
this.triggerHook('onFederationUpdate');
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fetchs
|
2023-11-21 17:36:59 +00:00
|
|
|
fetchRobot = async (garage: Garage, token: string): Promise<void> => {
|
2023-10-27 10:01:59 +00:00
|
|
|
Object.values(this.coordinators).forEach((coor) => {
|
2024-01-14 16:56:03 +00:00
|
|
|
void coor.fetchRobot(garage, token);
|
2023-10-27 10:01:59 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-10-27 11:00:53 +00:00
|
|
|
// Coordinators
|
2023-10-27 10:01:59 +00:00
|
|
|
getCoordinator = (shortAlias: string): Coordinator => {
|
|
|
|
return this.coordinators[shortAlias];
|
|
|
|
};
|
|
|
|
|
2023-11-02 14:15:18 +00:00
|
|
|
disableCoordinator = (shortAlias: string): void => {
|
2023-10-27 10:01:59 +00:00
|
|
|
this.coordinators[shortAlias].disable();
|
2024-01-23 23:30:16 +00:00
|
|
|
this.updateEnabledCoordinators();
|
2023-11-01 12:18:00 +00:00
|
|
|
this.triggerHook('onCoordinatorUpdate');
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
|
|
|
|
2023-11-02 14:15:18 +00:00
|
|
|
enableCoordinator = (shortAlias: string): void => {
|
2023-11-01 12:18:00 +00:00
|
|
|
this.coordinators[shortAlias].enable(() => {
|
2024-01-23 23:30:16 +00:00
|
|
|
this.updateEnabledCoordinators();
|
2023-11-01 12:18:00 +00:00
|
|
|
this.triggerHook('onCoordinatorUpdate');
|
|
|
|
});
|
2023-10-27 10:01:59 +00:00
|
|
|
};
|
2024-01-23 23:30:16 +00:00
|
|
|
|
|
|
|
updateEnabledCoordinators = (): void => {
|
|
|
|
this.exchange.enabledCoordinators = Object.values(this.coordinators).filter(
|
|
|
|
(c) => c.enabled,
|
|
|
|
).length;
|
2024-03-15 20:06:54 +00:00
|
|
|
this.triggerHook('onFederationUpdate');
|
2024-01-23 23:30:16 +00:00
|
|
|
};
|
2023-10-27 10:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Federation;
|