Skip to content
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
3 changes: 2 additions & 1 deletion .cspell.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export default {
'Valuizable',
'Valuize',
'VALUIZABLE',
'wrongpath'
'wrongpath',
'refetched'
],
flagWords: [
'recieve'
Expand Down
51 changes: 40 additions & 11 deletions src/services/fx/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,21 +831,23 @@ test('FX Server Estimate to Exchange Test', async function() {
});
}));

const serverDoesNotRequireQuoteReturnValue = {
convertedAmount: 1002n,
convertedAmountBound: 850n,
cost: {
amount: 0n,
token: testCurrencyUSD
}
};

await using serverDoesNotRequireQuote = new KeetaNetFXAnchorHTTPServer(await makeServerConfig({
quoteConfiguration: {
requiresQuote: false,
validateQuoteBeforeExchange: false,
issueQuotes: true
}
}, async function() {
return({
convertedAmount: 1002n,
convertedAmountBound: 850n,
cost: {
amount: 0n,
token: testCurrencyUSD
}
});
return(serverDoesNotRequireQuoteReturnValue);
}));

await using serverDoesNotRequireDoesNotIssueQuote = new KeetaNetFXAnchorHTTPServer(await makeServerConfig({
Expand Down Expand Up @@ -979,9 +981,7 @@ test('FX Server Estimate to Exchange Test', async function() {
}

const sortedQuotes = quotes.sort(function(a, b) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return(String(a['provider'].providerID).localeCompare(String(b['provider'].providerID), ...localeCompareArgs));
return(String(a.provider.providerID).localeCompare(String(b.provider.providerID), ...localeCompareArgs));
});

expect(sortedQuotes.map(function(entry) {
Expand Down Expand Up @@ -1161,5 +1161,34 @@ test('FX Server Estimate to Exchange Test', async function() {
}
}
}

{
/**
* Check refetching quotes
*/

const quotes = await fxClient.getEstimates({ from: testCurrencyUSD, to: testCurrencyEUR, amount: 1000n, affinity: 'from' }, undefined, {
providerIDs: [ 'TestDoesNotRequireQuote' ]
});

const singleEstimate = quotes?.[0];
if (singleEstimate === undefined) {
throw(new Error('Could not get quote from TestDoesNotRequireQuote'));
}

expect(singleEstimate.estimate.convertedAmount).toBe(1002n);

serverDoesNotRequireQuoteReturnValue.convertedAmount = 999n;

const refetchedEstimate = await singleEstimate.refetch();
expect(refetchedEstimate.estimate.convertedAmount).toBe(999n);

const singleQuote = await singleEstimate.getQuote();
expect(singleQuote.quote.convertedAmount).toBe(999n);

serverDoesNotRequireQuoteReturnValue.convertedAmount = 1002n;
const refetchedQuote = await singleQuote.refetch();
expect(refetchedQuote.quote.convertedAmount).toBe(1002n);
}
}
});
26 changes: 23 additions & 3 deletions src/services/fx/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class KeetaFXAnchorProviderBase extends KeetaFXAnchorBase {
* Estimate(optional) -> Quote(optional) -> Exchange -> ExchangeStatus
*/
class KeetaFXAnchorExchangeWithProvider {
private readonly provider: KeetaFXAnchorProviderBase;
readonly provider: KeetaFXAnchorProviderBase;
readonly exchange: KeetaFXAnchorExchange

constructor(provider: KeetaFXAnchorProviderBase, exchange: KeetaFXAnchorExchange) {
Expand All @@ -548,7 +548,7 @@ interface CanCreateExchange {
}

class KeetaFXAnchorQuoteWithProvider implements CanCreateExchange {
private readonly provider: KeetaFXAnchorProviderBase;
readonly provider: KeetaFXAnchorProviderBase;
readonly quote: KeetaFXAnchorQuote;
readonly isQuote = true as const;

Expand All @@ -565,10 +565,20 @@ class KeetaFXAnchorQuoteWithProvider implements CanCreateExchange {
const exchange = await this.provider.createExchange({ quote: this.quote }, block);
return(new KeetaFXAnchorExchangeWithProvider(this.provider, exchange));
}

/**
* Re-fetch the quote from the provider.
*
* @returns a new KeetaFXAnchorQuoteWithProvider with the updated quote
*/
async refetch(): Promise<KeetaFXAnchorQuoteWithProvider> {
const quote = await this.provider.getQuote();
return(new KeetaFXAnchorQuoteWithProvider(this.provider, quote));
}
}

class KeetaFXAnchorEstimateWithProvider implements CanCreateExchange {
private readonly provider: KeetaFXAnchorProviderBase;
readonly provider: KeetaFXAnchorProviderBase;
readonly estimate: KeetaFXAnchorEstimate;
readonly isQuote = false as const;

Expand All @@ -590,6 +600,16 @@ class KeetaFXAnchorEstimateWithProvider implements CanCreateExchange {
const exchange = await this.provider.createExchange({ estimate: this.estimate }, block);
return(new KeetaFXAnchorExchangeWithProvider(this.provider, exchange));
}

/**
* Re-fetch the estimate from the provider.
*
* @returns a new KeetaFXAnchorEstimateWithProvider with the updated estimate
*/
async refetch(): Promise<KeetaFXAnchorEstimateWithProvider> {
const estimate = await this.provider.getEstimate();
return(new KeetaFXAnchorEstimateWithProvider(this.provider, estimate));
}
}

class KeetaFXAnchorClient extends KeetaFXAnchorBase {
Expand Down