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

Connect wallet with ledger live app #36

Merged
merged 18 commits into from
Nov 28, 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
1 change: 1 addition & 0 deletions dapp/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_USE_TESTNET=true
4 changes: 2 additions & 2 deletions dapp/manifest-ledger-live-app.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"manifestVersion": "1",
"branch": "stable",
"categories": ["buy"],
"currencies": ["bitcoin", "bitcoin_testnet"],
"currencies": ["bitcoin", "bitcoin_testnet", "ethereum", "ethereum_goerli"],
"content": {
"shortDescription": {
"en": "Bitcoin Liquid Staking"
Expand All @@ -18,6 +18,6 @@
"en": "Bitcoin Liquid Staking"
}
},
"permissions": [],
"permissions": ["account.request"],
"domains": ["http://*"]
}
24 changes: 16 additions & 8 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import React from "react"
import { ChakraProvider, Button, Box } from "@chakra-ui/react"
import { useDetectThemeMode } from "./hooks"
import { LedgerWalletAPIProvider } from "./providers"
import { ChakraProvider, Box, Text } from "@chakra-ui/react"
import { useDetectThemeMode, useWalletContext } from "./hooks"
import theme from "./theme"
import { LedgerWalletAPIProvider, WalletContextProvider } from "./contexts"
import Navbar from "./components/Navbar"

function DApp() {
useDetectThemeMode()

const { btcAccount, ethAccount } = useWalletContext()

return (
<Box p={4}>
<Box>
<Navbar />
<h1>Ledger live - Acre dApp</h1>
<Button>Test button</Button>
{btcAccount && <Text>Account: {btcAccount.address}</Text>}
{ethAccount && <Text>Account: {ethAccount.address}</Text>}
</Box>
)
}

function DAppProviders() {
return (
<LedgerWalletAPIProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
<WalletContextProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
</WalletContextProvider>
</LedgerWalletAPIProvider>
)
}
Expand Down
16 changes: 16 additions & 0 deletions dapp/src/assets/bitcoin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions dapp/src/assets/ethereum.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions dapp/src/assets/info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions dapp/src/components/Navbar/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React 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,
useWalletContext,
} from "../../hooks"
import { formatSatoshiAmount, truncateAddress } from "../../utils"

export type ConnectButtonsProps = {
leftIcon: string
rightIcon: string
account: Account | undefined
requestAccount: () => Promise<void>
}

function ConnectButton({
leftIcon,
rightIcon,
account,
requestAccount,
}: ConnectButtonsProps) {
const styles = !account ? { color: "error", borderColor: "error" } : undefined
return (
<Button
variant="outline"
sx={styles}
leftIcon={<Image src={leftIcon} />}
// TODO: Add a tooltip here
rightIcon={!account ? <Image src={rightIcon} /> : undefined}
onClick={requestAccount}
>
{account ? truncateAddress(account.address) : "Not connected"}
</Button>
)
}

export default function ConnectWallet() {
const { requestAccount: requestBitcoinAccount } = useRequestBitcoinAccount()
const { requestAccount: requestEthereumAccount } = useRequestEthereumAccount()
const { btcAccount, ethAccount } = useWalletContext()

return (
<Box display="flex" alignItems="center" gap="4">
<Box display="flex" gap="2">
<Text>Balance</Text>
<Text as="b">
{!btcAccount || btcAccount?.balance.isZero()
? "0.00"
: formatSatoshiAmount(btcAccount.balance.toString())}
</Text>
<Text>{BITCOIN.symbol}</Text>
Comment on lines +52 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think would be great if we could create a reusable component where we just pass value and currency and all this stuff will be handled in a component. Otherwise, we always duplicate logic when displaying the amount. Ofc we can address it in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. Let's do it in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix it in #56.

</Box>
<ConnectButton
leftIcon={BitcoinIcon}
rightIcon={InfoIcon}
account={btcAccount}
requestAccount={async () => {
await requestBitcoinAccount()
}}
/>
<ConnectButton
leftIcon={EthereumIcon}
rightIcon={InfoIcon}
account={ethAccount}
requestAccount={async () => {
await requestEthereumAccount()
}}
/>
</Box>
)
}
11 changes: 11 additions & 0 deletions dapp/src/components/Navbar/index.tsx
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">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just flagging, we can use Flex component from Chakra <Flex p={4} justify="end">.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next PR I use here HStack. I'm not quite sure which is better. But let's move the discussion there.

https://github.com/thesis/acre/pull/39/files#diff-1be1f6c0a32d0f6e752f271df1c943de18e74dc12767dc65676d0a36b6c2233aR9-R20

<ConnectWallet />
</Box>
)
}
19 changes: 19 additions & 0 deletions dapp/src/constants/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Currency } from "../types"

export const BITCOIN: Currency = {
name: "Bitcoin",
symbol: "BTC",
decimals: 8,
}

export const ETHEREUM: Currency = {
name: "Ethereum",
symbol: "ETH",
decimals: 18,
}

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"
1 change: 1 addition & 0 deletions dapp/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./currency"
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LedgerWalletAPIProviderProps = {
children: React.ReactElement
}

export default function LedgerWalletAPIProvider({
export function LedgerWalletAPIProvider({
children,
}: LedgerWalletAPIProviderProps): JSX.Element {
const transport = getWalletAPITransport()
Expand Down
38 changes: 38 additions & 0 deletions dapp/src/contexts/WalletContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Account } from "@ledgerhq/wallet-api-client"
import React, { createContext, useMemo, useState } from "react"

type WalletContextValue = {
btcAccount: Account | undefined
setBtcAccount: React.Dispatch<React.SetStateAction<Account | undefined>>
ethAccount: Account | undefined
setEthAccount: React.Dispatch<React.SetStateAction<Account | undefined>>
}

export const WalletContext = createContext<WalletContextValue | undefined>(
undefined,
)

export function WalletContextProvider({
children,
}: {
children: React.ReactNode
}): React.ReactElement {
const [btcAccount, setBtcAccount] = useState<Account | undefined>(undefined)
const [ethAccount, setEthAccount] = useState<Account | undefined>(undefined)

const contextValue: WalletContextValue = useMemo<WalletContextValue>(
() => ({
btcAccount,
setBtcAccount,
ethAccount,
setEthAccount,
}),
[btcAccount, setBtcAccount, ethAccount, setEthAccount],
)

return (
<WalletContext.Provider value={contextValue}>
{children}
</WalletContext.Provider>
)
}
2 changes: 2 additions & 0 deletions dapp/src/contexts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./WalletContext"
export * from "./LedgerWalletAPIProvider"
3 changes: 3 additions & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./useDetectThemeMode"
export * from "./useRequestBitcoinAccount"
export * from "./useRequestEthereumAccount"
export * from "./useWalletContext"
20 changes: 20 additions & 0 deletions dapp/src/hooks/useRequestBitcoinAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react"
import { useCallback, useContext, useEffect } from "react"
import { CURRENCY_ID_BITCOIN } from "../constants"
import { UseRequestAccountReturn } from "../types"
import { WalletContext } from "../contexts"

export function useRequestBitcoinAccount(): UseRequestAccountReturn {
const walletContext = useContext(WalletContext)
const { account, requestAccount } = useRequestAccount()

useEffect(() => {
walletContext?.setBtcAccount(account || undefined)
}, [account, walletContext])

const requestBitcoinAccount = useCallback(async () => {
await requestAccount({ currencyIds: [CURRENCY_ID_BITCOIN] })
}, [requestAccount])

return { requestAccount: requestBitcoinAccount }
}
20 changes: 20 additions & 0 deletions dapp/src/hooks/useRequestEthereumAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react"
import { useCallback, useContext, useEffect } from "react"
import { CURRENCY_ID_ETHEREUM } from "../constants"
import { UseRequestAccountReturn } from "../types"
import { WalletContext } from "../contexts"

export function useRequestEthereumAccount(): UseRequestAccountReturn {
const walletContext = useContext(WalletContext)
const { account, requestAccount } = useRequestAccount()

useEffect(() => {
walletContext?.setEthAccount(account || undefined)
}, [account, walletContext])

const requestEthereumAccount = useCallback(async () => {
await requestAccount({ currencyIds: [CURRENCY_ID_ETHEREUM] })
}, [requestAccount])

return { requestAccount: requestEthereumAccount }
}
12 changes: 12 additions & 0 deletions dapp/src/hooks/useWalletContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useContext } from "react"
import { WalletContext } from "../contexts"

export function useWalletContext() {
const context = useContext(WalletContext)

if (!context) {
throw new Error("WalletContext used outside of WalletContext component")
}

return context
}
1 change: 0 additions & 1 deletion dapp/src/providers/index.ts

This file was deleted.

7 changes: 7 additions & 0 deletions dapp/src/theme/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { mode } from "@chakra-ui/theme-tools"
import type { StyleFunctionProps } from "@chakra-ui/styled-system"

const Button = {
baseStyle: {
rounded: "none",
},
variants: {
solid: (props: StyleFunctionProps) => ({
backgroundColor: mode("black", "purple")(props),
color: "white",
}),
outline: (props: StyleFunctionProps) => ({
color: mode("black", "grey.80")(props),
borderColor: mode("black", "grey.50")(props),
}),
},
}

Expand Down
11 changes: 10 additions & 1 deletion dapp/src/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { extendTheme } from "@chakra-ui/react"
import { StyleFunctionProps, extendTheme } from "@chakra-ui/react"
import { mode } from "@chakra-ui/theme-tools"
import Button from "./Button"
import { colors } from "./utils"

const defaultTheme = {
colors,
styles: {
global: (props: StyleFunctionProps) => ({
body: {
backgroundColor: mode("lightGrey", "darkGrey")(props),
color: mode("black", "grey.80")(props),
},
}),
},
components: {
Button,
},
Expand Down
7 changes: 7 additions & 0 deletions dapp/src/theme/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ export const colors = {
white: "#FFF",
black: "#000",
purple: "#7D00FF",
error: "#F00",
grey: {
50: "rgba(255, 255, 255, 0.50)",
80: "rgba(255, 255, 255, 0.80)",
},
lightGrey: "#ECECEC",
darkGrey: "#1A1B1D",
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions dapp/src/types/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Currency = {
name: string
symbol: string
decimals: number
}
2 changes: 2 additions & 0 deletions dapp/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ledger-live-app"
export * from "./currency"
Loading
Loading