Skip to content

Commit

Permalink
add getQuotes function to transaction kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rana Khoury authored and Rana Khoury committed May 20, 2024
1 parent de25474 commit 7e4bffb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
33 changes: 32 additions & 1 deletion __tests__/hooks/useEtherspotSwaps.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, waitFor } from '@testing-library/react';
import { ethers } from 'ethers';
import { BigNumber, ethers } from 'ethers';

// hooks
import { useEtherspotSwaps, EtherspotTransactionKit } from '../../src';
Expand Down Expand Up @@ -142,4 +142,35 @@ describe('useEtherspotSwaps()', () => {
expect(parsedCrossChainOfferTransactions[1].value).toEqual('100000000000000');
});
});
describe('getQuotes()', () => {
it('returns quotes cross chain', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
);

const { result } = renderHook(({ chainId }) => useEtherspotSwaps(chainId), {
initialProps: { chainId: 1 },
wrapper,
});

// wait for hook to load
await waitFor(() => expect(result.current).not.toBeNull());

const quote1 = {
fromAccountAddress: '0x3788bb31d134D96399744B7A423066A9258946A2',
toAddress: '0x0Eecf133F97976d71184b619da64121C3F7DeeA2',
fromChainId: 1,
toChainId: 56,
fromToken: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
fromAmount: BigNumber.from("1000000000000000000"),
slippage: 1,
}

const allQuotes = await result.current.getQuotes(quote1);
console.log(allQuotes)
expect(allQuotes.length).toBe(2);
});
});
})
7 changes: 6 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
useEtherspot,
useEtherspotAssets,
useEtherspotHistory,
useEtherspotSwaps,
useEtherspotTransactions,
useWalletAddress,
} from '@etherspot/transaction-kit';
import TreeItem from '@mui/lab/TreeItem';
import TreeView from '@mui/lab/TreeView';
import { Box, Button, Chip, Container, Paper, Tab, Tabs, Typography } from '@mui/material';
import { ethers } from 'ethers';
import { BigNumber, ethers } from 'ethers';
import React, { useEffect, useState } from 'react';
import { AiFillCaretDown, AiFillCaretRight } from 'react-icons/ai';

Expand Down Expand Up @@ -101,6 +102,10 @@ const App = () => {
const transactionStatus1 = getAccountTransactionStatus(100, 56, '0xfc46adedf462d3fd6cdbe0214ed11c06cba20c385b9875aa4d51c60afbd9725d')
console.log({ transactionStatus1 })

const { getQuotes } = useEtherspotSwaps();
const quotes1 = getQuotes('0x3788bb31d134D96399744B7A423066A9258946A2', '0x0Eecf133F97976d71184b619da64121C3F7DeeA2', 1, 56, '0x6B175474E89094C44Da98b954EedeAC495271d0F', BigNumber.from("1000000000000000000"), 1)
console.log(quotes1)

const batchesTreeView = batches.map((batchGroup, id1) => ({
...batchGroup,
treeNodeId: `batch-group-${batchGroup.id ?? id1}`,
Expand Down
48 changes: 47 additions & 1 deletion src/hooks/useEtherspotSwaps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StepTransaction } from '@etherspot/prime-sdk/dist/sdk/data';
import { BridgingProvider, Quote, StepTransaction } from '@etherspot/prime-sdk/dist/sdk/data';
import { Route } from '@lifi/types';
import { BigNumber } from 'ethers';
import { useMemo } from 'react';
Expand All @@ -23,6 +23,16 @@ interface IEtherspotSwapsHook {
fromAccountAddress?: string
) => Promise<ISameChainSwapOffers | ICrossChainSwapOffers | undefined>;
prepareCrossChainOfferTransactions: (offer: Route, accountAddress?: string) => Promise<StepTransaction[] | undefined>;
getQuotes: (
fromAccountAddress: string,
toAddress: string,
fromChainId: number,
toChainId: number,
fromToken: string,
fromAmount: BigNumber,
slippage: number,
provider?: BridgingProvider
) => Promise<Quote[] | undefined>
}

/**
Expand Down Expand Up @@ -123,9 +133,45 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
}
};


const getQuotes = async (
fromAccountAddress: string,
toAddress: string,
fromChainId: number,
toChainId: number,
fromToken: string,
fromAmount: BigNumber,
slippage: number,
provider?: BridgingProvider
): Promise<Quote[] | undefined> => {
const sdkForChainId = await getSdk(swapsChainId);

const fromAccount = fromAccountAddress ?? await sdkForChainId.getCounterFactualAddress();
if (!fromAccount) {
console.warn(`No account address provided!`);
return;
}

const dataService = getDataService();

try {
const quotes = await dataService.getQuotes({ fromAddress: fromAccount, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage, provider })

return quotes;
} catch (e) {
console.warn(
`Sorry, an error occurred whilst trying to fetch cross-chain quotes.`
+ ` Please try again. Error:`,
e,
);
}
};


return ({
getOffers,
prepareCrossChainOfferTransactions,
getQuotes,
});
};

Expand Down

0 comments on commit 7e4bffb

Please sign in to comment.