@shogun-sdk/money-legos
Version:
Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.
50 lines (43 loc) • 1.41 kB
text/typescript
import { fetchQuote, QuoteParams, QuoteTypes } from '@shogun-sdk/money-legos';
import { isBscSwap } from '../utils/index.js';
import { tryHandleFourMemeQuote } from '../quoteHandlers/index.js';
export class OneShotClient {
private readonly apiKey: string;
private readonly apiUrl: string;
constructor(apiKey: string, apiUrl: string) {
this.apiKey = apiKey;
this.apiUrl = apiUrl;
}
/**
* Fetches a quote from the Shogun API
* @param params Quote parameters
* @param signal AbortSignal for cancelling the request
* @returns Promise<QuoteTypes>
*/
public async fetchQuote(params: QuoteParams, signal?: AbortSignal): Promise<QuoteTypes> {
const result = await this.tryToHandleQuoteManually(params);
if (result.error) {
return { error: result.error } as unknown as QuoteTypes;
}
if (result.isHandled) {
return result.quote as QuoteTypes;
}
return fetchQuote(
{
key: this.apiKey,
url: this.apiUrl,
},
params,
signal || new AbortSignal(),
);
}
private async tryToHandleQuoteManually(
params: QuoteParams,
): Promise<{ isHandled: boolean; quote?: QuoteTypes; error?: string }> {
if (isBscSwap(params)) {
// if its a bsc swap then it might be a four meme swap
return tryHandleFourMemeQuote(this.apiKey, this.apiUrl, params);
}
return { isHandled: false };
}
}