From 7e4bffb382589ec341e52ce04b675892a1ce606b Mon Sep 17 00:00:00 2001 From: Rana Khoury Date: Mon, 20 May 2024 18:04:30 +0100 Subject: [PATCH] add getQuotes function to transaction kit --- __tests__/hooks/useEtherspotSwaps.test.js | 33 +++++++++++++++- example/src/App.tsx | 7 +++- src/hooks/useEtherspotSwaps.ts | 48 ++++++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/__tests__/hooks/useEtherspotSwaps.test.js b/__tests__/hooks/useEtherspotSwaps.test.js index e826c34..7222096 100644 --- a/__tests__/hooks/useEtherspotSwaps.test.js +++ b/__tests__/hooks/useEtherspotSwaps.test.js @@ -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'; @@ -142,4 +142,35 @@ describe('useEtherspotSwaps()', () => { expect(parsedCrossChainOfferTransactions[1].value).toEqual('100000000000000'); }); }); + describe('getQuotes()', () => { + it('returns quotes cross chain', async () => { + const wrapper = ({ children }) => ( + + {children} + + ); + + 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); + }); + }); }) diff --git a/example/src/App.tsx b/example/src/App.tsx index 85c0c2b..2ee828c 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -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'; @@ -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}`, diff --git a/src/hooks/useEtherspotSwaps.ts b/src/hooks/useEtherspotSwaps.ts index 59a0491..53e5ce1 100644 --- a/src/hooks/useEtherspotSwaps.ts +++ b/src/hooks/useEtherspotSwaps.ts @@ -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'; @@ -23,6 +23,16 @@ interface IEtherspotSwapsHook { fromAccountAddress?: string ) => Promise; prepareCrossChainOfferTransactions: (offer: Route, accountAddress?: string) => Promise; + getQuotes: ( + fromAccountAddress: string, + toAddress: string, + fromChainId: number, + toChainId: number, + fromToken: string, + fromAmount: BigNumber, + slippage: number, + provider?: BridgingProvider + ) => Promise } /** @@ -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 => { + 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, }); };