From 39711f259deb0b4cfcc31c35fa8b81dac886827a Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Mon, 6 Jan 2025 16:08:25 +0100 Subject: [PATCH] feat: update gardenfi packages, modify functions according to changes --- package.json | 13 +++-- src/app/app.tsx | 11 +--- src/app/components/swap/swap.tsx | 33 ++++++----- src/app/hooks/use-swap-order-status.ts | 8 ++- src/app/hooks/use-swap.ts | 34 +++--------- src/app/providers/swap-context-provider.tsx | 29 ++++++++++ yarn.lock | 61 ++++++++------------- 7 files changed, 94 insertions(+), 95 deletions(-) create mode 100644 src/app/providers/swap-context-provider.tsx diff --git a/package.json b/package.json index 78f846a..b229085 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,10 @@ "@fontsource/inter": "^5.0.18", "@fontsource/onest": "^5.0.3", "@fontsource/poppins": "^5.0.8", - "@gardenfi/core": "0.3.0-beta.14", - "@gardenfi/orderbook": "0.2.0-beta.41", - "@gardenfi/react-hooks": "0.0.1-beta.135", + "@gardenfi/core": "2.0.1", + "@gardenfi/orderbook": "2.0.0", + "@gardenfi/react-hooks": "2.0.0", + "@gardenfi/utils": "2.0.0", "@gemwallet/api": "^3.8.0", "@ledgerhq/hw-app-xrp": "^6.29.4", "@ledgerhq/hw-transport-webusb": "^6.28.6", @@ -50,7 +51,7 @@ "concurrently": "^8.2.2", "d3": "^7.9.0", "decimal.js": "^10.4.3", - "dlc-btc-lib": "2.5.3", + "dlc-btc-lib": "2.5.5", "dotenv": "^16.3.1", "ethers": "5.7.2", "formik": "^2.4.5", @@ -68,9 +69,9 @@ "redux": "^4.2.1", "redux-persist": "^6.0.0", "redux-persist-expire": "^1.1.1", - "viem": "2.x", "vite-plugin-toml": "^0.7.0", - "wagmi": "2.14.3", + "viem": "^2.21.15", + "wagmi": "^2.12.16", "xrpl": "^4.0.0" }, "devDependencies": { diff --git a/src/app/app.tsx b/src/app/app.tsx index 099cd7e..6533785 100644 --- a/src/app/app.tsx +++ b/src/app/app.tsx @@ -4,7 +4,6 @@ import { AppLayout } from '@components/app.layout'; import { MerchantDetails } from '@components/proof-of-reserve/components/merchant-details/merchant-details'; import { SwapPage } from '@components/swap/pages/swap-page'; import { getWagmiConfiguration } from '@functions/configuration.functions'; -import { GardenProvider, environment } from '@gardenfi/react-hooks'; import { AttestorDetailsPage } from '@pages/attestor-details/attestor-details-page'; import { AttestorDetailsSelectPage } from '@pages/attestor-details/attestor-details-select-page'; import { MyVaults } from '@pages/my-vaults/my-vaults'; @@ -18,6 +17,7 @@ import { NetworkConfigurationContextProvider } from '@providers/network-configur import { NetworkConnectionContextProvider } from '@providers/network-connection.provider'; import { ProofOfReserveContextProvider } from '@providers/proof-of-reserve-context-provider'; import { RippleNetworkConfigurationContextProvider } from '@providers/ripple-network-configuration.provider'; +import { SwapContextProvider } from '@providers/swap-context-provider'; import { XRPWalletContextProvider } from '@providers/xrp-wallet-context-provider'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { WagmiProvider } from 'wagmi'; @@ -33,12 +33,7 @@ export function App(): React.JSX.Element { return ( - + @@ -76,7 +71,7 @@ export function App(): React.JSX.Element { - + ); diff --git a/src/app/components/swap/swap.tsx b/src/app/components/swap/swap.tsx index efbd6f0..5aca305 100644 --- a/src/app/components/swap/swap.tsx +++ b/src/app/components/swap/swap.tsx @@ -1,3 +1,5 @@ +import { useEffect } from 'react'; + import { CheckCircleIcon, ChevronDownIcon, ChevronUpIcon } from '@chakra-ui/icons'; import { Button, HStack, Input, Spinner, Text, VStack } from '@chakra-ui/react'; import { useBitcoinPrice } from '@hooks/use-bitcoin-price'; @@ -37,6 +39,12 @@ export function Swap(): React.JSX.Element { const { bitcoinPrice } = useBitcoinPrice(); + useEffect(() => { + console.log('isProcessing', isProcessing); + console.log('isCompleted', isCompleted); + console.log('quoteError', quoteError); + }, [isProcessing, isCompleted, quoteError]); + return ( )} - {isQuoteLoading || - (isProcessing && ( - - - Loading - - ))} - {quoteError || - (swapError && ( - - {`Error: ${quoteError ? quoteError : swapError}`} - - ))} + {(isQuoteLoading || isProcessing) && ( + + + Loading + + )} + {(quoteError || swapError) && ( + + {quoteError || swapError} + + )} {isCompleted && ( )} - {`${outputToken.symbol === 'BTC' ? 'Receive' : 'Refund'}`} Address diff --git a/src/app/hooks/use-swap-order-status.ts b/src/app/hooks/use-swap-order-status.ts index 94253c7..aa3f213 100644 --- a/src/app/hooks/use-swap-order-status.ts +++ b/src/app/hooks/use-swap-order-status.ts @@ -4,7 +4,7 @@ import { OrderStatus, ParseOrderStatus } from '@gardenfi/core'; import { Chain, MatchedOrder } from '@gardenfi/orderbook'; import { GARDEN_CONFIG } from '@gardenfi/react-hooks'; -async function fetchJson(url: string, options?: RequestInit): Promise { +async function fetchJSON(url: string, options?: RequestInit): Promise { const response = await fetch(url, { headers: { 'Content-Type': 'application/json' }, ...options, @@ -54,10 +54,10 @@ export const useOrderStatus = (order: MatchedOrder): OrderStatus | undefined => useEffect(() => { const fetchBlockNumbers = async () => { try { - const data = await fetchJson>( + const currentBlockNumbers = await fetchJSON>( createAPI(isTestnet).data.blockNumbers('testnet') ); - setBlockNumbers(data); + setBlockNumbers(currentBlockNumbers); } catch (error) { // eslint-disable-next-line no-console console.error('Failed to fetch Block Numbers:', error); @@ -72,12 +72,14 @@ export const useOrderStatus = (order: MatchedOrder): OrderStatus | undefined => if (!blockNumbers) return; const { source_swap, destination_swap } = order; + const sourceBlockNumber = blockNumbers[source_swap.chain]; const destinationBlockNumber = blockNumbers[destination_swap.chain]; if (!sourceBlockNumber || !destinationBlockNumber) return; const orderStatus = ParseOrderStatus(order, sourceBlockNumber, destinationBlockNumber); + setStatus(orderStatus); }, [blockNumbers, order]); diff --git a/src/app/hooks/use-swap.ts b/src/app/hooks/use-swap.ts index b90f3f4..9132274 100644 --- a/src/app/hooks/use-swap.ts +++ b/src/app/hooks/use-swap.ts @@ -22,17 +22,15 @@ enum SupportedEVMNetworks { } const SUPPORTED_ASSETS_BTC_MAP = { - mainnet: SupportedAssets.mainnet.bitcoin_primary, - testnet: SupportedAssets.testnet.bitcoin_testnet_primary, - devnet: SupportedAssets.testnet.bitcoin_testnet_primary, - localhost: SupportedAssets.testnet.bitcoin_testnet_primary, + mainnet: SupportedAssets.mainnet.bitcoin_BTC, + testnet: SupportedAssets.testnet.bitcoin_testnet_BTC, + devnet: SupportedAssets.testnet.bitcoin_testnet_BTC, + localhost: SupportedAssets.testnet.bitcoin_testnet_BTC, }; const SUPPORTED_ASSETS_IBTC_MAP = { - [SupportedEVMNetworks.ArbitrumSepolia]: - SupportedAssets.testnet.arbitrum_sepolia_0xdfe6d9363ee96152d39391009a6723819d9e25eb, - [SupportedEVMNetworks.BaseSepolia]: - SupportedAssets.testnet.base_sepolia_0xbcdad29ac77e5bb27fd528ab0045af630259fe4f, + [SupportedEVMNetworks.ArbitrumSepolia]: SupportedAssets.testnet.arbitrum_sepolia_iBTC, + [SupportedEVMNetworks.BaseSepolia]: SupportedAssets.testnet.base_sepolia_iBTC, }; function useDebounce(value: T, delay: number): T { @@ -70,7 +68,7 @@ interface UseSwapReturnType { } export function useSwap(): UseSwapReturnType { - const { initializeSecretManager, swapAndInitiate, getQuote } = useGarden(); + const { swapAndInitiate, getQuote } = useGarden(); const [inputToken, setInputToken] = useState( SUPPORTED_ASSETS_IBTC_MAP[SupportedEVMNetworks.ArbitrumSepolia] @@ -139,7 +137,7 @@ export function useSwap(): UseSwapReturnType { amount: sendAmount, }); - setOutputAmount(unshiftValue(new Decimal(amount).toNumber()).toString()); // Assuming 8 decimals for BTC + setOutputAmount(unshiftValue(new Decimal(amount).toNumber()).toString()); } catch (error) { setQuoteError(error instanceof Error ? error.message : 'Failed to fetch quote'); setOutputAmount('0'); @@ -152,20 +150,6 @@ export function useSwap(): UseSwapReturnType { // eslint-disable-next-line react-hooks/exhaustive-deps }, [debouncedInputAmount, inputToken, outputToken, getQuote]); - const initializeSecret = async () => { - if (!initializeSecretManager) { - throw new Error('Secret Manager initializer not available'); - } - - const response = await initializeSecretManager(); - - if (!response.ok || !response.val.getMasterPrivKey()) { - throw new Error('Failed to initialize Secret Manager'); - } - - return response; - }; - const fetchQuote = async (quoteParams: QuoteParams) => { if (!getQuote) { throw new Error('Quote service not available'); @@ -205,8 +189,6 @@ export function useSwap(): UseSwapReturnType { return; } - await initializeSecret(); - const sendAmount = shiftValue(new Decimal(inputAmount).toNumber()); const { strategy, amount } = await fetchQuote({ diff --git a/src/app/providers/swap-context-provider.tsx b/src/app/providers/swap-context-provider.tsx new file mode 100644 index 0000000..e25ac32 --- /dev/null +++ b/src/app/providers/swap-context-provider.tsx @@ -0,0 +1,29 @@ +import React from 'react'; + +import { GardenProvider } from '@gardenfi/react-hooks'; +import { Environment } from '@gardenfi/utils'; +import { HasChildren } from '@models/has-children'; +import { useWalletClient } from 'wagmi'; + +const SWAP_ENVIRONMENT_MAP = { + mainnet: Environment.MAINNET, + testnet: Environment.TESTNET, + devnet: Environment.TESTNET, + localhost: Environment.TESTNET, +}; + +export function SwapContextProvider({ children }: HasChildren): React.JSX.Element { + const { data: walletClient } = useWalletClient(); + + return ( + + {children} + + ); +} diff --git a/yarn.lock b/yarn.lock index 22d8e0a..6906924 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1932,52 +1932,52 @@ resolved "https://registry.npmjs.org/@fontsource/poppins/-/poppins-5.0.12.tgz" integrity sha512-0qzBxtIJLh82iMJ9pCXKYwGs1zyS+jyUmwVJ59+JdYnEaFVkDsxVOk9yDWfVOs14ALUneodU2m5YSGma6dCYCw== -"@gardenfi/core@0.3.0-beta.14", "@gardenfi/core@^0.3.0-beta.14": - version "0.3.0-beta.14" - resolved "https://registry.yarnpkg.com/@gardenfi/core/-/core-0.3.0-beta.14.tgz#e66599cff3ddb4584648d628dbae02d4aa3836a6" - integrity sha512-V73jDvdchN09UqYdcyOCI4s4ekHY94NLoGnxqp2579y6sSYjqJf+5WXyn89Pb7bjrCOpYOfldivWKXYfwfLkWQ== +"@gardenfi/core@2.0.1", "@gardenfi/core@^2.0.0": + version "2.0.1" + resolved "https://registry.yarnpkg.com/@gardenfi/core/-/core-2.0.1.tgz#987a49267c08c1f8b7951d1c6a0c15d2dd14328d" + integrity sha512-EQrLsJPpagUwqV85f/eNUi8uEWe/4oXE560rX8u1SCfaZM2MAeaat3+K6yUjWTkSuuR0gUQlga0TnTj2Zb76og== dependencies: "@catalogfi/utils" "^0.1.6" "@catalogfi/wallets" "^0.2.51" - "@gardenfi/orderbook" "^0.2.0-beta.41" - "@gardenfi/utils" "^0.0.1-beta.19" + "@gardenfi/orderbook" "^2.0.0" + "@gardenfi/utils" "^2.0.0" bignumber.js "^9.1.2" bitcoinjs-lib "^6.1.6" tiny-secp256k1 "^2.2.3" varuint-bitcoin "^1.1.2" viem "^2.21.15" -"@gardenfi/orderbook@0.2.0-beta.41", "@gardenfi/orderbook@^0.2.0-beta.41": - version "0.2.0-beta.41" - resolved "https://registry.yarnpkg.com/@gardenfi/orderbook/-/orderbook-0.2.0-beta.41.tgz#a802ddba48023956160fa2d2fda0676c520a5dbb" - integrity sha512-I8GisEgUGNX84WKZSKEAqMsSD9wvfY9hzY5iL2V5vc+jqblpc+5Exbk/yVFINI3DoqtuaVb1h/dqTmMFotu0hA== +"@gardenfi/orderbook@2.0.0", "@gardenfi/orderbook@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@gardenfi/orderbook/-/orderbook-2.0.0.tgz#d13b3d3e8667245a8b1ffe52e262e0b83ad33359" + integrity sha512-mNBhgrpCRkMP9far0883hZ1eECBujioyc+joetHuhRJ//dOzWvHSaEP+uxdb4WNhcWahb6ZbHLa7+FBa3+DdJQ== dependencies: "@catalogfi/utils" "^0.1.6" - "@gardenfi/utils" "^0.0.1-beta.19" + "@gardenfi/utils" "^2.0.0" bufferutil "^4.0.8" siwe "^2.1.4" utf-8-validate "^6.0.3" viem "^2.21.15" ws "^8.14.2" -"@gardenfi/react-hooks@0.0.1-beta.135": - version "0.0.1-beta.135" - resolved "https://registry.yarnpkg.com/@gardenfi/react-hooks/-/react-hooks-0.0.1-beta.135.tgz#a74d538b455b4a2baff1401eb65c4ea42e194c80" - integrity sha512-I4R391jusGV30K8s+6cDgc7xwAcwj1rUeg2Uw3a/N96KjO2f0l+cn8NQZsSIcJMzL4GTgICpQv2Ci/JIe0VdbQ== +"@gardenfi/react-hooks@2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@gardenfi/react-hooks/-/react-hooks-2.0.0.tgz#03d7c3d6fd982adb21901b04ded2f8f3ffe557cd" + integrity sha512-lG14FCBM09p84PVSld2k1x091uEE4oUjJUcUgHItSvTwy3x7rvRbj4jEoeYdq4gyTmLMCYRWOFOjA+HKkTIadg== dependencies: "@catalogfi/utils" "^0.1.6" "@catalogfi/wallets" "^0.2.51" - "@gardenfi/core" "^0.3.0-beta.14" - "@gardenfi/orderbook" "^0.2.0-beta.41" - "@gardenfi/utils" "^0.0.1-beta.19" + "@gardenfi/core" "^2.0.0" + "@gardenfi/orderbook" "^2.0.0" + "@gardenfi/utils" "^2.0.0" react "^18.3.1" viem "^2.21.15" wagmi "^2.12.16" -"@gardenfi/utils@^0.0.1-beta.19": - version "0.0.1-beta.19" - resolved "https://registry.yarnpkg.com/@gardenfi/utils/-/utils-0.0.1-beta.19.tgz#e74cdc12ff04bed142ebedc32f063daec145ef06" - integrity sha512-hgTc3iy2TM7Rr2J7ApM+yzSOSsisHW3T3+PKMWcBZcc4CIwBAquTCRdQNkMKFbawsvB4LMieIiWYWjm+IQqckQ== +"@gardenfi/utils@2.0.0", "@gardenfi/utils@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@gardenfi/utils/-/utils-2.0.0.tgz#2d68dae3ebc7d0bc4fdcec74af50b714cdc0338b" + integrity sha512-J0q0L76+hIi+N6QOWko8idizxuo2fTNs9fGMxwXkpA1pQigHdSdBbdYg8GmJQhZk0a1tim8D6mjB6GaOIx0xEA== dependencies: "@catalogfi/utils" "^0.1.11" axios "^1.7.2" @@ -9388,21 +9388,6 @@ varuint-bitcoin@1.1.2, varuint-bitcoin@^1.0.4, varuint-bitcoin@^1.1.2: dependencies: safe-buffer "^5.1.1" -viem@2.x: - version "2.21.57" - resolved "https://registry.yarnpkg.com/viem/-/viem-2.21.57.tgz#bedbb444bb42e07ccc2264a9a0441903a113aab8" - integrity sha512-Mw4f4Dw0+Y/wSHdynVmP4uh+Cw15HEoj8BOKvKH5nGA6oFZYRxSy9Ruu7ZG8jexeAVCZ57aIuXb0gNg6Vb1x0g== - dependencies: - "@noble/curves" "1.7.0" - "@noble/hashes" "1.6.1" - "@scure/bip32" "1.6.0" - "@scure/bip39" "1.5.0" - abitype "1.0.7" - isows "1.0.6" - ox "0.1.2" - webauthn-p256 "0.0.10" - ws "8.18.0" - viem@^2.1.1: version "2.18.6" resolved "https://registry.yarnpkg.com/viem/-/viem-2.18.6.tgz#527c54a032cdd4c1f1795fcb44072e8c3efd16c6" @@ -9525,7 +9510,7 @@ vitest@^0.34.6: vite-node "0.34.6" why-is-node-running "^2.2.2" -wagmi@2.14.3, wagmi@^2.12.16: +wagmi@^2.12.16: version "2.14.3" resolved "https://registry.yarnpkg.com/wagmi/-/wagmi-2.14.3.tgz#3abfe5e9f796890d75d1b7d506e9dccd6b87f254" integrity sha512-sr8o7+EBw22GhieDyXLv8Zr2vgC6xKrYoSuUIpq0xOqEhvMP1q880VtR4lFG1capc2QEWvK72pJ/+jpsEQcMYQ==