-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To connect accounts with the dApp let's use the `useRequestAccount` hook from the WALLET API for this. Also, we should store the account data in context to have global access to it.
- Loading branch information
1 parent
7a49c62
commit 65ab5ab
Showing
14 changed files
with
272 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_USE_TESTNET=true |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useContext } from "react" | ||
import { Box, Button, Image, Text } from "@chakra-ui/react" | ||
import { Account } from "@ledgerhq/wallet-api-client" | ||
import BitcoinIcon from "../../assets/bitcoin.svg" | ||
import EthereumIcon from "../../assets/ethereum.svg" | ||
import InfoIcon from "../../assets/info.svg" | ||
import { BITCOIN } from "../../constants" | ||
import { | ||
useRequestBitcoinAccount, | ||
useRequestEthereumAccount, | ||
} from "../../hooks" | ||
import { LedgerLiveAppContext } from "../../contexts/LedgerLiveAppContext" | ||
|
||
export type ConnectButtonsProps = { | ||
leftIcon: string | ||
rightIcon: string | ||
account: Account | null | ||
requestAccount: () => Promise<void> | ||
} | ||
|
||
function ConnectButton({ | ||
leftIcon, | ||
rightIcon, | ||
account, | ||
requestAccount, | ||
}: ConnectButtonsProps) { | ||
return ( | ||
<Button | ||
variant="outline" | ||
leftIcon={<Image src={leftIcon} />} | ||
rightIcon={!account ? <Image src={rightIcon} /> : undefined} | ||
onClick={requestAccount} | ||
> | ||
{account ? account.address : "Not connected"} | ||
</Button> | ||
) | ||
} | ||
|
||
export default function ConnectWallet() { | ||
const requestBitcoinAccount = useRequestBitcoinAccount() | ||
const requestEthereumAccount = useRequestEthereumAccount() | ||
const { btcAccount } = useContext(LedgerLiveAppContext) | ||
|
||
return ( | ||
<Box display="flex" alignItems="center" gap="4"> | ||
<Box display="flex" gap="2"> | ||
<Text>Balance</Text> | ||
<Text as="b"> | ||
{!btcAccount ? "0.00" : btcAccount.balance.toString()} | ||
</Text> | ||
<Text>{BITCOIN.token}</Text> | ||
</Box> | ||
<ConnectButton | ||
leftIcon={BitcoinIcon} | ||
rightIcon={InfoIcon} | ||
account={requestBitcoinAccount.account} | ||
requestAccount={async () => { | ||
await requestBitcoinAccount.requestAccount() | ||
}} | ||
/> | ||
<ConnectButton | ||
leftIcon={EthereumIcon} | ||
rightIcon={InfoIcon} | ||
account={requestEthereumAccount.account} | ||
requestAccount={async () => { | ||
await requestEthereumAccount.requestAccount() | ||
}} | ||
/> | ||
</Box> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from "react" | ||
import { Box } from "@chakra-ui/react" | ||
import ConnectWallet from "./ConnectWallet" | ||
|
||
export default function Navbar() { | ||
return ( | ||
<Box p={4} display="flex" justifyContent="end"> | ||
<ConnectWallet /> | ||
</Box> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const BITCOIN = { | ||
name: "Bitcoin", | ||
token: "BTC", | ||
} | ||
|
||
export const CURRENCY_ID_BITCOIN = | ||
import.meta.env.VITE_USE_TESTNET === "true" ? "bitcoin_testnet" : "bitcoin" | ||
|
||
export const CURRENCY_ID_ETHEREUM = | ||
import.meta.env.VITE_USE_TESTNET === "true" ? "ethereum_goerli" : "ethereum" |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./chains" |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Account } from "@ledgerhq/wallet-api-client" | ||
import React, { createContext, useMemo, useState } from "react" | ||
|
||
type LedgerLiveAppContextValue = { | ||
btcAccount: Account | undefined | ||
setBtcAccount: React.Dispatch<React.SetStateAction<Account | undefined>> | ||
ethAccount: Account | undefined | ||
setEthAccount: React.Dispatch<React.SetStateAction<Account | undefined>> | ||
} | ||
|
||
export const LedgerLiveAppContext = createContext<LedgerLiveAppContextValue>({ | ||
btcAccount: undefined, | ||
setBtcAccount: () => {}, | ||
ethAccount: undefined, | ||
setEthAccount: () => {}, | ||
}) | ||
|
||
export function LedgerLiveAppProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}): React.ReactElement { | ||
const [btcAccount, setBtcAccount] = useState<Account | undefined>(undefined) | ||
const [ethAccount, setEthAccount] = useState<Account | undefined>(undefined) | ||
|
||
const contextValue: LedgerLiveAppContextValue = | ||
useMemo<LedgerLiveAppContextValue>( | ||
() => ({ | ||
btcAccount, | ||
setBtcAccount, | ||
ethAccount, | ||
setEthAccount, | ||
}), | ||
[btcAccount, setBtcAccount, ethAccount, setEthAccount], | ||
) | ||
|
||
return ( | ||
<LedgerLiveAppContext.Provider value={contextValue}> | ||
{children} | ||
</LedgerLiveAppContext.Provider> | ||
) | ||
} |
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 +1,3 @@ | ||
export * from "./useDetectThemeMode" | ||
export * from "./useRequestBitcoinAccount" | ||
export * from "./useRequestEthereumAccount" |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Account, WalletAPIClient } from "@ledgerhq/wallet-api-client" | ||
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react" | ||
import { useCallback, useContext, useEffect } from "react" | ||
import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext" | ||
import { CURRENCY_ID_BITCOIN } from "../constants" | ||
|
||
type UseRequestAccount = { | ||
pending: boolean | ||
account: Account | null | ||
error: unknown | ||
} | ||
|
||
type RequestAccountParams = Parameters<WalletAPIClient["account"]["request"]> | ||
|
||
type UseRequestAccountReturn = { | ||
requestAccount: (...params: RequestAccountParams) => Promise<void> | ||
} & UseRequestAccount | ||
|
||
export function useRequestBitcoinAccount(): UseRequestAccountReturn { | ||
const { setBtcAccount } = useContext(LedgerLiveAppContext) | ||
const requestAccountResponse = useRequestAccount() | ||
const { account, requestAccount } = requestAccountResponse | ||
|
||
useEffect(() => { | ||
setBtcAccount(account || undefined) | ||
}, [account, setBtcAccount]) | ||
|
||
const requestBitcoinAccount = useCallback(async () => { | ||
await requestAccount({ currencyIds: [CURRENCY_ID_BITCOIN] }) | ||
}, [requestAccount]) | ||
|
||
return { ...requestAccountResponse, requestAccount: requestBitcoinAccount } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Account, WalletAPIClient } from "@ledgerhq/wallet-api-client" | ||
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react" | ||
import { useCallback, useContext, useEffect } from "react" | ||
import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext" | ||
import { CURRENCY_ID_ETHEREUM } from "../constants" | ||
|
||
type UseRequestAccount = { | ||
pending: boolean | ||
account: Account | null | ||
error: unknown | ||
} | ||
|
||
type RequestAccountParams = Parameters<WalletAPIClient["account"]["request"]> | ||
|
||
type UseRequestAccountReturn = { | ||
requestAccount: (...params: RequestAccountParams) => Promise<void> | ||
} & UseRequestAccount | ||
|
||
export function useRequestEthereumAccount(): UseRequestAccountReturn { | ||
const { setEthAccount } = useContext(LedgerLiveAppContext) | ||
const requestAccountResponse = useRequestAccount() | ||
const { account, requestAccount } = requestAccountResponse | ||
|
||
useEffect(() => { | ||
setEthAccount(account || undefined) | ||
}, [account, setEthAccount]) | ||
|
||
const requestEthereumAccount = useCallback(async () => { | ||
await requestAccount({ currencyIds: [CURRENCY_ID_ETHEREUM] }) | ||
}, [requestAccount]) | ||
|
||
return { ...requestAccountResponse, requestAccount: requestEthereumAccount } | ||
} |