Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/swap UI #211

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Prev Previous commit
Next Next commit
wip: add garden swap initial code
Polybius93 committed Nov 25, 2024
commit 7b6889108a4140ed1ffe2c6a5031dd58afb7b689
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
"preview": "vite preview"
},
"dependencies": {
"@catalogfi/wallets": "^0.2.51",
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.4",
@@ -30,6 +31,8 @@
"@fontsource/inter": "^5.0.18",
"@fontsource/onest": "^5.0.3",
"@fontsource/poppins": "^5.0.8",
"@gardenfi/core": "^0.1.15",
"@gardenfi/orderbook": "^0.1.11",
"@gemwallet/api": "^3.8.0",
"@ledgerhq/hw-app-xrp": "^6.29.4",
"@ledgerhq/hw-transport-webusb": "^6.28.6",
@@ -46,9 +49,9 @@
"concurrently": "^8.2.2",
"d3": "^7.9.0",
"decimal.js": "^10.4.3",
"dlc-btc-lib": "2.4.17",
"dlc-btc-lib": "file:../dlc-btc-lib",
"dotenv": "^16.3.1",
"ethers": "5.7.2",
"ethers": "^6.0.0",
"formik": "^2.4.5",
"framer-motion": "^11.2.10",
"ledger-bitcoin": "^0.2.3",
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ export function BurnTokenTransactionForm({

const { ethereumNetworkConfiguration } = useContext(EthereumNetworkConfigurationContext);

const signer = useEthersSigner();
const { getEthersSigner } = useEthersSigner();

const { unmintStep } = useSelector((state: RootState) => state.mintunmint);
const [isSubmitting, setIsSubmitting] = useState(false);
@@ -60,8 +60,11 @@ export function BurnTokenTransactionForm({
if (currentRisk === 'High') throw new Error('Risk Level is too high');
const formattedWithdrawAmount = BigInt(shiftValue(withdrawAmount));

const signer = await getEthersSigner();
if (!signer) throw new Error('No signer available');

await withdraw(
ethereumNetworkConfiguration.dlcManagerContract.connect(signer!),
ethereumNetworkConfiguration.dlcManagerContract.connect(signer),
currentVault.uuid,
formattedWithdrawAmount
);
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export function SetupVaultScreen(): React.JSX.Element {
const { ethereumNetworkConfiguration } = useContext(EthereumNetworkConfigurationContext);
const { rippleNetworkConfiguration } = useContext(RippleNetworkConfigurationContext);

const signer = useEthersSigner();
const { getEthersSigner } = useEthersSigner();

const [isSubmitting, setIsSubmitting] = useState(false);

@@ -40,7 +40,7 @@ export function SetupVaultScreen(): React.JSX.Element {
);
break;
case NetworkType.EVM:
await setupVault(ethereumNetworkConfiguration.dlcManagerContract.connect(signer!));
await setupVault(ethereumNetworkConfiguration.dlcManagerContract.connect());
break;
default:
throw new Error('Unsupported Network Type');
22 changes: 15 additions & 7 deletions src/app/functions/configuration.functions.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { RippleNetwork, RippleNetworkID } from '@models/ripple.models';
import { supportedEthereumNetworks } from 'dlc-btc-lib/constants';
import { getEthereumContract, getProvider } from 'dlc-btc-lib/ethereum-functions';
import { EthereumDeploymentPlan, EthereumNetwork, EthereumNetworkID } from 'dlc-btc-lib/models';
import { Contract, providers } from 'ethers';
import { BrowserProvider, Contract, JsonRpcSigner } from 'ethers';
import { filter, fromPairs, includes, map, pipe } from 'ramda';
import { Account, Chain, Client, HttpTransport, Transport, http } from 'viem';
import { Config, createConfig, useConnectorClient } from 'wagmi';
@@ -84,20 +84,28 @@ export function getWagmiConfiguration(ethereumNetworkIDs: EthereumNetworkID[]):
});
}

function clientToSigner(client: Client<Transport, Chain, Account>): providers.JsonRpcSigner {
async function clientToSigner(client: Client<Transport, Chain, Account>): Promise<JsonRpcSigner> {
const { account, chain, transport } = client;

const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
return new providers.Web3Provider(transport, network).getSigner(account.address);
return await new BrowserProvider(transport, network).getSigner(account.address);
}

export function useEthersSigner({ chainId }: { chainId?: number } = {}):
| providers.JsonRpcSigner
| undefined {
export function useEthersSigner({ chainId }: { chainId?: number } = {}): {
getEthersSigner: () => Promise<JsonRpcSigner | undefined>;
} {
const { data: client } = useConnectorClient<Config>({ chainId });
return useMemo(() => (client ? clientToSigner(client) : undefined), [client]);

async function getEthersSigner() {
if (!client) return undefined;
return await clientToSigner(client);
}

return {
getEthersSigner,
};
}
49 changes: 49 additions & 0 deletions src/app/hooks/use-swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { BitcoinNetwork, BitcoinOTA, BitcoinProvider } from '@catalogfi/wallets';
import { useEthersSigner } from '@functions/configuration.functions';
import { Chains, JsonRp, Orderbook } from '@gardenfi/orderbook';

const CATALOGFI_BITCOIN_NETWORK_MAP = {
regtest: BitcoinNetwork.Regtest,
testnet: BitcoinNetwork.Testnet,
mainnet: BitcoinNetwork.Mainnet,
};

interface UseSwapReturnType {}

export function useSwap(): UseSwapReturnType {
const { getEthersSigner } = useEthersSigner();
const bitcoinProvider = new BitcoinProvider(
CATALOGFI_BITCOIN_NETWORK_MAP[appConfiguration.bitcoinNetwork],
appConfiguration.bitcoinBlockchainURL
);

async function signSwapRequest() {
const ethersSigner = await getEthersSigner();
if (!ethersSigner) {
throw new Error('Ethers Signer is not available');
}

const orderbook = await Orderbook.init({
url: 'http://localhost:8080',
signer: ethersSigner as JsonRpcSigner,
opts: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
domain: (window as any).location.host,
store: localStorage,
},
});

const wallets = {
[Chains.bitcoin_regtest]: new BitcoinOTA(bitcoinProvider, signer),
[Chains.ethereum_localnet]: new EVMWallet(signer),
};

const garden = new GardenJS(orderbook, wallets);
}

setGarden(garden, wallets[Chains.bitcoin_regtest]);

return {
swap,
};
}
Loading