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: add token to metamask button #9

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Button, HStack, Image, Link, Text } from "@chakra-ui/react";
import { TutorialVideo } from "@components/tutorial-video/tutorial-video";
import { BlockchainContext } from "../../../../providers/blockchain-context-provider";

import { WalkthroughHeader } from "./components/walkthrough-header";
import { WalkthroughLayout } from "./components/walkthrough.layout";
import { useContext } from "react";

interface WalkthroughProps {
flow: "mint" | "unmint";
Expand All @@ -13,6 +15,9 @@ export function Walkthrough({
flow,
currentStep,
}: WalkthroughProps): React.JSX.Element {
const blockchainContext = useContext(BlockchainContext);
const ethereum = blockchainContext?.ethereum;

switch (flow) {
case "mint":
switch (currentStep) {
Expand Down Expand Up @@ -93,7 +98,10 @@ export function Walkthrough({
simply <span style={{ fontWeight: 800 }}>add them </span>
to your Ethereum Wallet.
</Text>
<Button variant={"vault"}>
<Button
variant={"vault"}
onClick={() => ethereum?.recommendDlcBtcTokenToMetamask()}
>
<HStack>
<Image
src={"/images/logos/dlc-btc-logo.svg"}
Expand All @@ -113,16 +121,6 @@ export function Walkthrough({
title={"Minted dlcBTC"}
blockchain={"ethereum"}
/>
<Button variant={"vault"}>
<HStack>
<Image
src={"/images/logos/dlc-btc-logo.svg"}
alt={"dlcBTC"}
boxSize={"25px"}
/>
<Text> Add Token to Wallet</Text>
</HStack>
</Button>
</WalkthroughLayout>
);
}
Expand Down
26 changes: 26 additions & 0 deletions src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
getVault: (vaultUUID: string) => Promise<void>;
setupVault: (btcDepositAmount: number) => Promise<void>;
closeVault: (vaultUUID: string) => Promise<void>;
recommendDlcBtcTokenToMetamask: () => Promise<boolean>;
}

export function useEthereum(): UseEthereumReturn {
Expand Down Expand Up @@ -53,9 +54,9 @@
if (!address || !network) return;

if (!protocolContract || !dlcManagerContract || !dlcBTCContract) {
setupEthereumConfiguration(network);

Check warning on line 57 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
}, [address, network]);

Check warning on line 59 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

React Hook useEffect has missing dependencies: 'dlcBTCContract', 'dlcManagerContract', 'protocolContract', and 'setupEthereumConfiguration'. Either include them or remove the dependency array

useEffect(() => {
if (!address || !protocolContract || !dlcManagerContract || !dlcBTCContract)
Expand All @@ -66,11 +67,11 @@
await getDLCBTCBalance();
await getLockedBTCBalance();
} catch (error) {
console.error(error);

Check warning on line 70 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
}
};

fetchBalance();

Check warning on line 74 in src/app/hooks/use-ethereum.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}, [address, fundedVaults]);

function formatVault(vault: any): Vault {
Expand Down Expand Up @@ -294,6 +295,30 @@
}
}

async function recommendDlcBtcTokenToMetamask(): Promise<boolean> {
try {
const { ethereum } = window;
if (!ethereum) return false;
const added = await ethereum.request({
method: "wallet_watchAsset",
params: {
type: "ERC20",
options: {
address: dlcBTCContract?.address,
symbol: "dlcBTC",
decimals: 8,
image:
"https://cdn.discordapp.com/attachments/994505799902691348/1035507437748367360/DLC.Link_Emoji.png",
},
},
});
return added;
} catch (error) {
console.error(error);
return false;
}
}

return {
protocolContract,
dlcManagerContract,
Expand All @@ -306,5 +331,6 @@
getVault,
setupVault,
closeVault,
recommendDlcBtcTokenToMetamask,
};
}
Loading