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

@@ -1,109 +0,0 @@
/* eslint-disable camelcase */
import { Provider } from '@ethersproject/providers';
import {
Exchange as ExchangeContract,
Exchange__factory as ExchangeContract__factory,
} from '@orionprotocol/contracts';
import { LibAtomic } from '@orionprotocol/contracts/lib/ethers/Exchange';
import { BytesLike, ethers, Signer } from 'ethers';
import {
DEPOSIT_ERC20_GAS_LIMIT, DEPOSIT_ETH_GAS_LIMIT, LOCKATOMIC_GAS_LIMIT,
REDEEMATOMIC_GAS_LIMIT, SWAP_THROUGH_ORION_POOL_GAS_LIMIT, WITHDRAW_GAS_LIMIT,
} from '../constants';
import { SupportedChainId } from '../types';
export default class Exchange {
chainId: SupportedChainId;
private exchangeContract: ExchangeContract;
constructor(chainId: SupportedChainId, signerOrProvider: Signer | Provider, address: string) {
this.chainId = chainId;
this.exchangeContract = ExchangeContract__factory.connect(address, signerOrProvider);
}
swapThroughOrionPool(
amount_spend: ethers.BigNumberish,
amount_receive: ethers.BigNumberish,
path: string[],
is_exact_spend: boolean,
value?: ethers.BigNumberish,
) {
return this.exchangeContract.populateTransaction.swapThroughOrionPool(
amount_spend,
amount_receive,
path,
is_exact_spend,
{
gasLimit: SWAP_THROUGH_ORION_POOL_GAS_LIMIT,
value,
},
);
}
depositNativeCurrency(value: ethers.BigNumberish) {
return this.exchangeContract.populateTransaction.deposit({
gasLimit: DEPOSIT_ETH_GAS_LIMIT,
value,
});
}
depositERC20(assetAddress: string, amount: ethers.BigNumberish) {
return this.exchangeContract.populateTransaction.depositAsset(
assetAddress,
amount,
{
gasLimit: DEPOSIT_ERC20_GAS_LIMIT,
},
);
}
withdraw(assetAddress: string, amount: ethers.BigNumberish) {
return this.exchangeContract.populateTransaction.withdraw(
assetAddress,
amount,
{
gasLimit: WITHDRAW_GAS_LIMIT,
},
);
}
lockAtomic(lockOrder: LibAtomic.LockOrderStruct) {
return this.exchangeContract.populateTransaction.lockAtomic(
lockOrder,
{
gasLimit: LOCKATOMIC_GAS_LIMIT,
},
);
}
redeemAtomic(redeemOrder: LibAtomic.RedeemOrderStruct, secret: BytesLike) {
return this.exchangeContract.populateTransaction.redeemAtomic(
redeemOrder,
secret,
{
gasLimit: REDEEMATOMIC_GAS_LIMIT,
},
);
}
redeem2Atomics(
order1: LibAtomic.RedeemOrderStruct,
secret1: BytesLike,
order2: LibAtomic.RedeemOrderStruct,
secret2: BytesLike,
) {
return this.exchangeContract.populateTransaction.redeem2Atomics(
order1,
secret1,
order2,
secret2,
);
}
refundAtomic(secretHash: BytesLike) {
return this.exchangeContract.populateTransaction.refundAtomic(
secretHash,
);
}
}

View File

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

View File

@@ -3,7 +3,6 @@ import BigNumber from 'bignumber.js';
BigNumber.config({ EXPONENTIAL_AT: 1e9 });
export * as config from './config';
// export * from './entities';
export { default as OrionUnit } from './OrionUnit';
export { default as initOrionUnit } from './initOrionUnit';
export { default as fetchWithValidation } from './fetchWithValidation';

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';