Skip to content

Commit

Permalink
feat: update garden related libaries, implement basic flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Nov 26, 2024
1 parent 3d1f739 commit b7ec5f1
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 210 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"preview": "vite preview"
},
"dependencies": {
"@catalogfi/wallets": "^0.2.45",
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.4",
Expand All @@ -31,9 +30,9 @@
"@fontsource/inter": "^5.0.18",
"@fontsource/onest": "^5.0.3",
"@fontsource/poppins": "^5.0.8",
"@gardenfi/core": "^0.1.15",
"@gardenfi/orderbook": "^0.1.4",
"@gardenfi/react-hooks": "^0.0.1-beta.1",
"@gardenfi/core": "^0.2.0-beta.85",
"@gardenfi/orderbook": "^0.2.0-beta.24",
"@gardenfi/react-hooks": "0.0.1-beta.111",
"@gemwallet/api": "^3.8.0",
"@ledgerhq/hw-app-xrp": "^6.29.4",
"@ledgerhq/hw-transport-webusb": "^6.28.6",
Expand Down
5 changes: 4 additions & 1 deletion src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function App(): React.JSX.Element {
<WagmiProvider config={wagmiConfiguration}>
<QueryClientProvider client={queryClient}>
<GardenProvider
config={}
config={{
store: localStorage,
environment: environment.testnet,
}}
>
<NetworkConfigurationContextProvider>
<RippleNetworkConfigurationContextProvider>
Expand Down
122 changes: 69 additions & 53 deletions src/app/hooks/use-swap.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,87 @@
import { useContext } from 'react';
import { useState } from 'react';

import { BitcoinNetwork, BitcoinOTA, BitcoinProvider, EVMWallet } from '@catalogfi/wallets';
import { useEthersSigner } from '@functions/configuration.functions';
import { GardenJS } from '@gardenfi/core';
import { Asset, Assets, Chains, Orderbook } from '@gardenfi/orderbook';
import { EthereumNetworkConfigurationContext } from '@providers/ethereum-network-configuration.provider';
import { Asset, SupportedAssets } from '@gardenfi/orderbook';
import { useGarden } from '@gardenfi/react-hooks';
import { shiftValue } from 'dlc-btc-lib/utilities';
import { usePublicClient, useWalletClient } from 'wagmi';

interface UseSwapReturnType {}
const SUPPORTED_ASSETS_IBTC_MAP = {
mainnet: SupportedAssets.mainnet.base_0x13dcec0762ecc5e666c207ab44dc768e5e33070f,
testnet: SupportedAssets.testnet.base_sepolia_0x00ab86f54f436cfe15253845f139955ae0c00baf,
devnet: SupportedAssets.localnet.ethereum_localnet_0xe7f1725e7734ce288f8367e1bb143e90bb3f0512,
localhost: SupportedAssets.localnet.ethereum_localnet_0xe7f1725e7734ce288f8367e1bb143e90bb3f0512,
};

interface UseSwapReturnType {
handleSwap: () => void;
setInputToken: (token: Asset) => void;
setInputAmount: (amount: number) => void;
setOutputToken: (token: Asset) => void;
setOutputAmount: (amount: number) => void;
setBitcoinAddress: (address: string) => void;
}

export function useSwap(): UseSwapReturnType {
const { data: walletClient } = useWalletClient(); // Destructure data property
const publicClient = usePublicClient();
const { getEthersSigner } = useEthersSigner();
const {
ethereumNetworkConfiguration: { chain, dlcBTCContract },
} = useContext(EthereumNetworkConfigurationContext);
const bitcoinProvider = new BitcoinProvider(
CATALOGFI_BITCOIN_NETWORK_MAP[appConfiguration.bitcoinNetwork],
appConfiguration.bitcoinBlockchainURL
const { initializeSecretManager, swapAndInitiate, getQuote } = useGarden();

const [inputToken, setInputToken] = useState<Asset>(
SUPPORTED_ASSETS_IBTC_MAP[appConfiguration.appEnvironment]
);
const [inputAmount, setInputAmount] = useState<number>(0.01);

const [outputToken, setOutputToken] = useState<Asset>(SupportedAssets.mainnet.bitcoin_primary);
const [outputAmount, setOutputAmount] = useState<number>(0);

const [bitcoinAddress, setBitcoinAddress] = useState<string | undefined>(undefined);

async function handleSwap() {
if (!initializeSecretManager) return;

const initializeSecretManagerResponse = await initializeSecretManager();

async function signSwapRequest() {
const ethersSigner = await getEthersSigner();
if (!ethersSigner) {
throw new Error('Ethers Signer is not available');
if (
!initializeSecretManagerResponse.ok ||
!swapAndInitiate ||
!initializeSecretManagerResponse.val.getMasterPrivKey() ||
!getQuote
)
return;

const sendAmount = shiftValue(inputAmount);

const quote = await getQuote({
fromAsset: inputToken,
toAsset: outputToken,
amount: sendAmount,
});

if (quote.error) {
throw new Error(quote.error);
}

const orderbook = await Orderbook.init({
url: 'http://localhost:8080',
signer: ethersSigner,
opts: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
domain: (window as any).location.host,
store: localStorage,
const [_strategy, quoteAmount] = Object.entries(quote.val.quotes)[0];

const swapAndInitiateResponse = await swapAndInitiate({
fromAsset: inputToken,
toAsset: outputToken,
sendAmount: sendAmount.toString(),
receiveAmount: quoteAmount.toString(),
additionalData: {
btcAddress: bitcoinAddress,
strategyId: _strategy,
},
});

const account = walletClient?.account;

if (!account) {
throw new Error('Account is not available');
if (swapAndInitiateResponse.error) {
throw new Error(swapAndInitiateResponse.error);
}
const wallets = {
[Chains.bitcoin_regtest]: new BitcoinOTA(bitcoinProvider, account, chain.id),
[Chains.ethereum_localnet]: new EVMWallet(walletClient!, publicClient!),
};

const garden = new GardenJS(orderbook, wallets);

const iBTCAsset: Asset = {
chain: Chains.ethereum_sepolia,
symbol: 'IBTC',
address: await dlcBTCContract.getAddress(),
decimals: await dlcBTCContract.decimals(),
isToken: true,
name: 'iBTC',
thumbnail: '',
};

await garden.swap(iBTCAsset, Assets.bitcoin_regtest.BTC, shiftValue(0.01), shiftValue(0.01), {
btcUserAddress: 'bcrt1qk5q0takwdva20adgw8zf4vy07w9529gpfkrv6v',
});
}

return {
signSwapRequest,
handleSwap,
setInputToken,
setInputAmount,
setOutputToken,
setOutputAmount,
setBitcoinAddress,
};
}
Loading

0 comments on commit b7ec5f1

Please sign in to comment.