-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update garden related libaries, implement basic flow
- Loading branch information
1 parent
3d1f739
commit b7ec5f1
Showing
4 changed files
with
123 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
Oops, something went wrong.