Removed OB socket.io

This commit is contained in:
Aleksandr Kraiz
2022-06-09 20:13:24 +04:00
parent 97448f65b6
commit 0248a12f90
9 changed files with 4 additions and 601 deletions

View File

@@ -7,7 +7,6 @@ import {
addPoolSchema, adminPoolsListSchema,
atomicSummarySchema,
} from './schemas';
import { OrionBlockchainSocketIO } from './ws';
import redeemOrderSchema from '../OrionAggregator/schemas/redeemOrderSchema';
import { sourceAtomicHistorySchema, targetAtomicHistorySchema } from './schemas/atomicHistorySchema';
import { makePartial } from '../../utils';
@@ -52,11 +51,8 @@ type AtomicSwapHistoryTargetQuery = AtomicSwapHistoryBaseQuery & {
class OrionBlockchain {
private readonly apiUrl: string;
readonly ws: OrionBlockchainSocketIO;
constructor(apiUrl: string) {
this.apiUrl = apiUrl;
this.ws = new OrionBlockchainSocketIO(`${apiUrl}/`);
this.getAtomicSwapAssets = this.getAtomicSwapAssets.bind(this);
this.getAtomicSwapHistory = this.getAtomicSwapHistory.bind(this);
@@ -350,6 +346,5 @@ class OrionBlockchain {
);
}
export * as ws from './ws';
export * as schemas from './schemas';
export { OrionBlockchain };

View File

@@ -1,63 +0,0 @@
import io from 'socket.io-client';
import { z } from 'zod';
import balancesSchema from './schemas/balancesSchema';
const handleBalancesMessage = (
rawData: unknown,
updateData: (balancesData: z.infer<typeof balancesSchema>) => void,
) => {
const data = balancesSchema.parse(rawData);
updateData(data);
};
type UpdateBalanceDataHandler = (balancesData: z.infer<typeof balancesSchema>) => void;
export class OrionBlockchainSocketIO {
private socket: typeof io.Socket;
constructor(orionBlockchainWSUrl: string) {
const url = new URL(orionBlockchainWSUrl);
// https://stackoverflow.com/questions/29511404/connect-to-socket-io-server-with-specific-path-and-namespace
this.socket = io(url.origin, {
path: `${url.pathname}socket.io`,
transports: ['websocket'],
autoConnect: false,
});
}
connect(updateDataHandler: UpdateBalanceDataHandler) {
if (updateDataHandler) {
this.socket.on('balanceChange', (data: unknown) => handleBalancesMessage(data, updateDataHandler));
this.socket.on('balances', (data: unknown) => handleBalancesMessage(data, updateDataHandler));
}
this.socket.connect();
}
close() {
this.socket.removeAllListeners();
this.socket.close();
}
resetConnection() {
// Because Orion Blockchain does not have a subscription / unsubscribe system
// Only way to "unsubscribe" is reset connection
this.socket.disconnect();
this.socket.open();
}
subscribeBalancesUpdate(walletAddress: string) {
if (this.socket.connected) {
this.socket.emit('clientAddress', walletAddress);
} else {
this.socket.on('connect', () => {
this.socket.emit('clientAddress', walletAddress);
});
}
}
updateAllBalances(walletAddress: string) {
this.socket.emit('getAllBalances', walletAddress);
}
}
export * as schemas from './schemas';

View File

@@ -1,10 +0,0 @@
import { z } from 'zod';
import { makePartial } from '../../../../utils';
const balancesSchema = z.object({
contractBalances: z.record(z.string()).transform(makePartial).optional(),
walletBalances: z.record(z.string()).transform(makePartial),
allowances: z.record(z.string()).transform(makePartial).optional(),
});
export default balancesSchema;

View File

@@ -1 +0,0 @@
export { default as balancesSchema } from './balancesSchema';