Skip to content

Commit

Permalink
🔥 Removing fetchPairs and nonce in executeSwap (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-bellotti authored May 1, 2023
1 parent fba7ac9 commit b85d8b7
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 649 deletions.
29 changes: 0 additions & 29 deletions src/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
calculateMinAmount,
fetchBuildExecuteTransaction,
fetchExecuteSwapTransaction,
fetchPairs,
fetchPrices,
fetchQuotes,
fetchSources,
Expand Down Expand Up @@ -294,34 +293,6 @@ describe('Avnu services', () => {
});
});

describe('fetchPairs', () => {
it('should return a page of pairs', async () => {
// Given
const response = aPage([aPair()]);
fetchMock.get(`${BASE_URL}/swap/v1/pairs?`, response);

// When
const result = await fetchPairs();

// Then
expect(result).toStrictEqual(response);
});

it('should use throw Error with status code and text when status is higher than 400', async () => {
// Given
fetchMock.get(`${BASE_URL}/swap/v1/pairs?`, { status: 400, body: { messages: ['This is an error'] } });

// When
try {
await fetchPairs();
} catch (error) {
// Then
expect(error).toStrictEqual(new Error('This is an error'));
}
expect.assertions(1);
});
});

describe('buildApproveTx', () => {
it('should build approve', () => {
// When
Expand Down
29 changes: 8 additions & 21 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const fetchExecuteSwapTransaction = (
* It allows trader to build the data needed for executing the exchange on AVNU router
*
* @param quoteId: The id of the selected quote
* @param nonce: Taker's address nonce. See `buildGetNonce`
* @param nonce: Taker's address nonce. See `buildGetNonce`. Warning: the nonce mechanism will change
* @param takerAddress: Required when taker address was not provided during the quote request
* @param slippage: The maximum acceptable slippage of the buyAmount amount. Default value is 5%. 0.05 is 5%.
* This value is ignored if slippage is not applicable to the selected quote
Expand All @@ -152,7 +152,7 @@ const fetchExecuteSwapTransaction = (
*/
const fetchBuildExecuteTransaction = (
quoteId: string,
nonce: string,
nonce?: string,
takerAddress?: string,
slippage?: number,
options?: AvnuOptions,
Expand Down Expand Up @@ -180,19 +180,6 @@ const fetchTokens = (request?: GetTokensRequest, options?: AvnuOptions): Promise
headers: { ...(options?.avnuPublicKey && { 'ask-signature': 'true' }) },
}).then((response) => parseResponse<Page<Token>>(response, options?.avnuPublicKey));

/**
* Fetches the supported pairs
*
* @param request: The request params for the avnu API `/swap/v1/pairs` endpoint.
* @param options: Optional options.
* @returns The best quotes
*/
const fetchPairs = (request?: GetPairsRequest, options?: AvnuOptions): Promise<Page<Pair>> =>
fetch(`${options?.baseUrl ?? getBaseUrl()}/swap/v1/pairs?${qs.stringify(request ?? {})}`, {
signal: options?.abortSignal,
headers: { ...(options?.avnuPublicKey && { 'ask-signature': 'true' }) },
}).then((response) => parseResponse<Page<Pair>>(response, options?.avnuPublicKey));

/**
* Fetches the supported sources
*
Expand Down Expand Up @@ -341,7 +328,7 @@ const hashQuote = (accountAddress: string, quote: Quote, nonce: string, chainId:
const executeSwap = async (
account: AccountInterface,
quote: Quote,
{ executeApprove = true, gasless = false, nonce, takerSignature, slippage }: ExecuteSwapOptions = {},
{ executeApprove = true, gasless = false, takerSignature, slippage }: ExecuteSwapOptions = {},
options?: AvnuOptions,
): Promise<InvokeSwapResponse> => {
if (account.chainId !== quote.chainId) {
Expand All @@ -352,17 +339,18 @@ const executeSwap = async (
? buildApproveTx(quote.sellTokenAddress, quote.sellAmount, quote.chainId, options?.dev)
: undefined;

// If nonce not given, fetch it
if (!nonce) {
// /!\ Do not implement this yourself. It will change /!\
let nonce = undefined;
if (quote.liquiditySource === 'MARKET_MAKER' || gasless) {
const getNonce = buildGetNonce(account.address, account.chainId, options?.dev);
const response = await account.callContract(getNonce);
nonce = response.result[0];
}

if (gasless) {
if (approve) await account.execute([approve]);
takerSignature = takerSignature ?? (await signQuote(account, quote, nonce, quote.chainId));
return fetchExecuteSwapTransaction(quote.quoteId, takerSignature, nonce, account.address, slippage, options);
takerSignature = takerSignature ?? (await signQuote(account, quote, nonce!, quote.chainId));
return fetchExecuteSwapTransaction(quote.quoteId, takerSignature, nonce!, account.address, slippage, options);
} else {
return fetchBuildExecuteTransaction(quote.quoteId, nonce, account.address, slippage, options)
.then((call) => {
Expand Down Expand Up @@ -391,7 +379,6 @@ export {
executeSwap,
fetchBuildExecuteTransaction,
fetchExecuteSwapTransaction,
fetchPairs,
fetchPrices,
fetchQuotes,
fetchSources,
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export interface AvnuOptions {
export interface ExecuteSwapOptions {
executeApprove?: boolean;
gasless?: boolean;
nonce?: string;
takerSignature?: Signature;
slippage?: number;
}
Expand Down
Loading

0 comments on commit b85d8b7

Please sign in to comment.