Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/XRPL connector 25 Feb Improvements #286

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"bs58": "^4.0.1",
"copyfiles": "^2.4.1",
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^4.0.1",
"google-protobuf": "^3.2.0",
Expand All @@ -151,7 +151,7 @@
"mock-ethers-provider": "^1.0.2",
"node-cache": "5.1.2",
"nodemon": "^2.0.16",
"prettier": "^2.3.0",
"prettier": "^3.2.5",
"react": "^18",
"react-dom": "^18",
"rimraf": "^3.0.2",
Expand All @@ -164,4 +164,4 @@
"resolutions": {
"web3-utils": "1.7.3"
}
}
}
16 changes: 11 additions & 5 deletions src/chains/xrpl/xrpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type Fee = {
};

const MAX_POLL_RETRY = 5;
const POLL_RETRY_INTERVAL = 300;
const POLL_RETRY_INTERVAL = 1500;

export class XRPL implements XRPLish {
private static _instances: { [name: string]: XRPL };
Expand Down Expand Up @@ -287,7 +287,7 @@ export class XRPL implements XRPLish {
}

public getTokenForSymbol(code: string): XRPTokenInfo[] | undefined {
let query = convertHexToString(code);
const query = convertHexToString(code);

return this._tokenMap[query] ? this._tokenMap[query] : undefined;
}
Expand Down Expand Up @@ -522,10 +522,16 @@ export class XRPL implements XRPLish {
} catch (error) {
retryCount++;
if (retryCount >= 5) {
throw new Error(`Transaction ${txHash} not found, error: ` + String(error));
throw new Error(
`Transaction ${txHash} not found, error: ` + String(error)
);
}
logger.info(`Transaction ${txHash} not found, retrying ${retryCount}/${MAX_POLL_RETRY}...`);
await new Promise(resolve => setTimeout(resolve, POLL_RETRY_INTERVAL)); // Add delay
logger.info(
`Transaction ${txHash} not found, retrying ${retryCount}/${MAX_POLL_RETRY}...`
);
await new Promise((resolve) =>
setTimeout(resolve, POLL_RETRY_INTERVAL)
); // Add delay
}
}

Expand Down
56 changes: 45 additions & 11 deletions src/connectors/xrpl/xrpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,25 @@ export class XRPLCLOB implements CLOBish {
return XRPLCLOB._instances.get(instanceKey) as XRPLCLOB;
}

public async loadMarkets() {
// Make a while loop and wait for the XRPL to be ready
public async loadMarkets(marketId: string = '') {
while (!this._xrpl.ready()) {
await new Promise((resolve) => setTimeout(resolve, 100));
await new Promise((resolve) => setTimeout(resolve, 1000));
}

const rawMarkets = await this.fetchMarkets();
for (const market of rawMarkets) {
this.parsedMarkets[market.marketId] = market;
if (marketId.length > 0) {
const market = await this.fetchMarkets(marketId);
this.parsedMarkets[market[0].marketId] = market[0];
} else {
const rawMarkets = await this.fetchMarkets();
for (const market of rawMarkets) {
this.parsedMarkets[market.marketId] = market;
}
}
}

public async init() {
await this._xrpl.init();
await this.loadMarkets();
// await this.loadMarkets();
this._ready = true;
}

Expand All @@ -120,11 +124,19 @@ export class XRPLCLOB implements CLOBish {
req: ClobMarketsRequest
): Promise<{ markets: Array<Market> }> {
if (req.market && req.market.split('-').length === 2) {
if (!this.parsedMarkets[req.market]) {
// const fetchedMarket = await this.fetchMarkets(req.market);
await this.loadMarkets(req.market);
}

const marketsAsArray: Array<Market> = [];
marketsAsArray.push(this.parsedMarkets[req.market]);
return { markets: marketsAsArray };
}

// Load all markets
await this.loadMarkets();

const marketsAsArray: Array<Market> = [];
for (const marketId in this.parsedMarkets) {
marketsAsArray.push(this.parsedMarkets[marketId]);
Expand All @@ -134,12 +146,33 @@ export class XRPLCLOB implements CLOBish {
}

public async orderBook(req: ClobOrderbookRequest): Promise<Orderbook> {
if (!this.parsedMarkets[req.market]) {
await this.loadMarkets(req.market);
}

return await this.getOrderBook(this.parsedMarkets[req.market]);
}

// Utility methods:
async fetchMarkets(): Promise<Market[]> {
async fetchMarkets(marketId: string = ''): Promise<Market[]> {
const loadedMarkets: Market[] = [];

// If marketId is provided, fetch only that market
if (marketId.length > 0) {
logger.info(
`Fetching 1 market ${marketId} for ${this.chain} ${this.network}`
);
const market = this._xrpl.storedMarketList.find(
(m) => m.marketId === marketId
);
if (!market)
throw new MarketNotFoundError(`Market "${marketId}" not found.`);
const processedMarket = await this.getMarket(market);
loadedMarkets.push(processedMarket);
return loadedMarkets;
}

// Fetch all markets
const markets = this._xrpl.storedMarketList;
const getMarket = async (market: MarketInfo): Promise<void> => {
const processedMarket = await this.getMarket(market);
Expand All @@ -148,7 +181,7 @@ export class XRPLCLOB implements CLOBish {

logger.info(`Fetching markets for ${this.chain} ${this.network}`);

await promiseAllInBatches(getMarket, markets, 15, 300);
await promiseAllInBatches(getMarket, markets, 6, 100);

return loadedMarkets;
}
Expand Down Expand Up @@ -396,7 +429,7 @@ export class XRPLCLOB implements CLOBish {
const quoteCurrency = market.quoteCurrency;
const baseIssuer = market.baseIssuer;
const quoteIssuer = market.quoteIssuer;
let price = parseFloat(req.price)
let price = parseFloat(req.price);

// If it is market order
// Increase price by 3% if it is buy order
Expand Down Expand Up @@ -513,6 +546,7 @@ export class XRPLCLOB implements CLOBish {
public async deleteOrder(
req: ClobDeleteOrderRequest
): Promise<{ txHash: string }> {
await this._xrpl.ensureConnection();
const wallet = await this.getWallet(req.address);
const offer: Transaction = {
TransactionType: 'OfferCancel',
Expand Down Expand Up @@ -571,7 +605,7 @@ export class XRPLCLOB implements CLOBish {
const prepared = await this._client.autofill(offer);
const signed = wallet.sign(prepared);
await this._xrpl.ensureConnection();
await this._client.submitAndWait(signed.tx_blob);
await this._client.submit(signed.tx_blob);
this._isSubmittingTxn = false;
return { prepared, signed };
}
Expand Down
Loading
Loading