Skip to content

Commit

Permalink
Init the Acre SDK in dapp w/o ethereum address
Browse files Browse the repository at this point in the history
Create the Bitcoin Provider based on the selected bitcoin account id
providede by the Ledger Live Wallet API.
  • Loading branch information
r-czajkowski committed May 8, 2024
1 parent 219377d commit c3b9568
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
3 changes: 2 additions & 1 deletion dapp/ledger-manifest-development.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"account.list",
"message.sign",
"transaction.sign",
"transaction.signAndBroadcast"
"transaction.signAndBroadcast",
"bitcoin.getXPub"
],
"domains": ["http://*"],
"type": "walletApp"
Expand Down
14 changes: 7 additions & 7 deletions dapp/src/acre-react/contexts/AcreSdkContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React, { useCallback, useMemo, useState } from "react"
import { LedgerLiveEthereumSigner } from "#/web3"
import { Acre, EthereumNetwork } from "@acre-btc/sdk"
import { BitcoinProvider } from "@acre-btc/sdk/dist/src/lib/bitcoin/providers"

const TBTC_API_ENDPOINT = import.meta.env.VITE_TBTC_API_ENDPOINT

type AcreSdkContextValue = {
acre?: Acre
init: (bitcoinAddress: string, network: EthereumNetwork) => Promise<void>
init: (
bitcoinProvider: BitcoinProvider,
network: EthereumNetwork,
) => Promise<void>
isInitialized: boolean
}

Expand All @@ -20,13 +23,10 @@ export function AcreSdkProvider({ children }: { children: React.ReactNode }) {
const [acre, setAcre] = useState<Acre | undefined>(undefined)
const [isInitialized, setIsInitialized] = useState<boolean>(false)

// TODO: initialize Acre SDK w/o Ethereum address.
const init = useCallback<AcreSdkContextValue["init"]>(
async (bitcoinAddress: string, network: EthereumNetwork) => {
if (!bitcoinAddress) throw new Error("Bitcoin address not defined")

async (bitcoinProvider: BitcoinProvider, network: EthereumNetwork) => {
const sdk = await Acre.initializeEthereum(
await LedgerLiveEthereumSigner.fromAddress(bitcoinAddress),
bitcoinProvider,
network,
TBTC_API_ENDPOINT,
)
Expand Down
9 changes: 6 additions & 3 deletions dapp/src/acre-react/hooks/useStakeFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { StakeInitialization, DepositReceipt } from "@acre-btc/sdk"
import { useAcreContext } from "./useAcreContext"

export type UseStakeFlowReturn = {
initStake: (bitcoinAddress: string, referral: number) => Promise<void>
initStake: (
referral: number,
bitcoinRecoveryAddress?: string,
) => Promise<void>
btcAddress?: string
depositReceipt?: DepositReceipt
signMessage: () => Promise<void>
Expand All @@ -22,12 +25,12 @@ export function useStakeFlow(): UseStakeFlowReturn {
>(undefined)

const initStake = useCallback(
async (bitcoinAddress: string, referral: number) => {
async (referral: number, bitcoinRecoveryAddress?: string) => {
if (!acre || !isInitialized) throw new Error("Acre SDK not defined")

const initializedStakeFlow = await acre.staking.initializeStake(
bitcoinAddress,
referral,
bitcoinRecoveryAddress,
)

const btcDepositAddress = await initializedStakeFlow.getBitcoinAddress()
Expand Down
14 changes: 11 additions & 3 deletions dapp/src/constants/chains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Chain } from "#/types"
import { EthereumNetwork, BitcoinNetwork } from "@acre-btc/sdk"
import {
EthereumNetwork,
BitcoinNetwork as AcreSDKBitcoinNetwork,
} from "@acre-btc/sdk"

export type BitcoinNetwork = Exclude<
AcreSDKBitcoinNetwork,
AcreSDKBitcoinNetwork.Unknown
>

export const BLOCK_EXPLORER: Record<Chain, { title: string; url: string }> = {
ethereum: { title: "Etherscan", url: "https://etherscan.io" },
Expand All @@ -11,5 +19,5 @@ export const ETHEREUM_NETWORK: EthereumNetwork =

export const BITCOIN_NETWORK: BitcoinNetwork =
import.meta.env.VITE_USE_TESTNET === "true"
? BitcoinNetwork.Testnet
: BitcoinNetwork.Mainnet
? AcreSDKBitcoinNetwork.Testnet
: AcreSDKBitcoinNetwork.Mainnet
13 changes: 5 additions & 8 deletions dapp/src/contexts/StakeFlowContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { REFERRAL } from "#/constants"

type StakeFlowContextValue = Omit<UseStakeFlowReturn, "initStake"> & {
initStake: (bitcoinAddress: string) => Promise<void>
initStake: () => Promise<void>
}

export const StakeFlowContext = React.createContext<StakeFlowContextValue>({
Expand All @@ -26,14 +26,11 @@ export function StakeFlowProvider({ children }: { children: React.ReactNode }) {
stake,
} = useStakeFlow()

const initStake = useCallback(
async (bitcoinAddress: string) => {
if (!acre) throw new Error("Acre SDK not defined")
const initStake = useCallback(async () => {
if (!acre) throw new Error("Acre SDK not defined")

await acreInitStake(bitcoinAddress, REFERRAL)
},
[acreInitStake, acre],
)
await acreInitStake(REFERRAL)
}, [acreInitStake, acre])

const context = useMemo(
() => ({
Expand Down
19 changes: 11 additions & 8 deletions dapp/src/hooks/sdk/useInitializeAcreSdk.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { useEffect } from "react"
import { ETHEREUM_NETWORK } from "#/constants"
import { BITCOIN_NETWORK, ETHEREUM_NETWORK } from "#/constants"
import { logPromiseFailure } from "#/utils"
import { useAcreContext } from "#/acre-react/hooks"
import { LedgerLiveWalletApiBitcoinProvider } from "@acre-btc/sdk/dist/src/lib/bitcoin/providers"
import { useWalletContext } from "../useWalletContext"

export function useInitializeAcreSdk() {
const { btcAccount } = useWalletContext()
const { init } = useAcreContext()
const bitcoinAddress = btcAccount?.address

useEffect(() => {
if (!bitcoinAddress) return
if (!btcAccount?.id) return

const initSDK = async (_bitcoinAddress: string) => {
await init(_bitcoinAddress, ETHEREUM_NETWORK)
const initSDK = async (bitcoinAccountId: string) => {
const bitcoinProvider = await LedgerLiveWalletApiBitcoinProvider.init(
bitcoinAccountId,
BITCOIN_NETWORK,
)
await init(bitcoinProvider, ETHEREUM_NETWORK)
}

logPromiseFailure(initSDK(bitcoinAddress))
}, [bitcoinAddress, init])
logPromiseFailure(initSDK(btcAccount.id))
}, [btcAccount?.id, init])
}

0 comments on commit c3b9568

Please sign in to comment.