Skip to content

Commit

Permalink
Integrate SDK with tBTC API for deposit creation (#377)
Browse files Browse the repository at this point in the history
In this PR we integrate Acre SDK with [tBTC
API](https://github.com/thesis/acre-bots/tree/main/tbtc-api).

In deposit flow, SDK will interact with the tBTC API in the following
cases:
- to backup the generated deposit receipt (this will replace Sentry
deposit registration) - the data are stored in Cloudflare D1 database
named `reveals`.
- to create a deposit and initialize bridging (this will replace
OpenZeppelin Defender relayer integration) - the data are stored in
Cloudflare D1 database named `deposits`. Initially, the deposit is
stored with the status `QUEUED` in the DB, Ethereum transaction is not
sent yet. The relayer bot runs periodically goes through the `QUEUED`
deposits, and submits the `initializeDeposit` transaction.

Refs: thesis/acre-bots#12

Closes: #340
Closes: #323
  • Loading branch information
r-czajkowski authored May 7, 2024
2 parents 216ef05 + 6ebaceb commit a327646
Show file tree
Hide file tree
Showing 28 changed files with 851 additions and 500 deletions.
3 changes: 3 additions & 0 deletions dapp/src/acre-react/contexts/AcreSdkContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { useCallback, useMemo, useState } from "react"
import { LedgerLiveEthereumSigner } from "#/web3"
import { Acre, EthereumNetwork } from "@acre-btc/sdk"

const TBTC_API_ENDPOINT = import.meta.env.VITE_TBTC_API_ENDPOINT

type AcreSdkContextValue = {
acre?: Acre
init: (ethereumAddress: string, network: EthereumNetwork) => Promise<void>
Expand All @@ -25,6 +27,7 @@ export function AcreSdkProvider({ children }: { children: React.ReactNode }) {
const sdk = await Acre.initializeEthereum(
await LedgerLiveEthereumSigner.fromAddress(ethereumAddress),
network,
TBTC_API_ENDPOINT,
)
setAcre(sdk)
setIsInitialized(true)
Expand Down
4 changes: 0 additions & 4 deletions dapp/src/acre-react/hooks/useStakeFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useState } from "react"
import {
StakeInitialization,
EthereumAddress,
DepositorProxy,
DepositReceipt,
} from "@acre-btc/sdk"
import { useAcreContext } from "./useAcreContext"
Expand All @@ -12,7 +11,6 @@ export type UseStakeFlowReturn = {
bitcoinRecoveryAddress: string,
ethereumAddress: string,
referral: number,
depositor?: DepositorProxy,
) => Promise<void>
btcAddress?: string
depositReceipt?: DepositReceipt
Expand All @@ -36,15 +34,13 @@ export function useStakeFlow(): UseStakeFlowReturn {
bitcoinRecoveryAddress: string,
ethereumAddress: string,
referral: number,
depositor?: DepositorProxy,
) => {
if (!acre || !isInitialized) throw new Error("Acre SDK not defined")

const initializedStakeFlow = await acre.staking.initializeStake(
bitcoinRecoveryAddress,
EthereumAddress.from(ethereumAddress),
referral,
depositor,
)

const btcDepositAddress = await initializedStakeFlow.getBitcoinAddress()
Expand Down
11 changes: 1 addition & 10 deletions dapp/src/contexts/StakeFlowContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
useStakeFlow,
} from "#/acre-react/hooks"
import { REFERRAL } from "#/constants"
import { RelayerDepositorProxy } from "#/web3"
import { EthereumBitcoinDepositor } from "@acre-btc/sdk"

type StakeFlowContextValue = Omit<UseStakeFlowReturn, "initStake"> & {
initStake: (
Expand Down Expand Up @@ -35,14 +33,7 @@ export function StakeFlowProvider({ children }: { children: React.ReactNode }) {
async (bitcoinRecoveryAddress: string, ethereumAddress: string) => {
if (!acre) throw new Error("Acre SDK not defined")

await acreInitStake(
bitcoinRecoveryAddress,
ethereumAddress,
REFERRAL,
RelayerDepositorProxy.fromEthereumBitcoinDepositor(
acre.contracts.bitcoinDepositor as EthereumBitcoinDepositor,
),
)
await acreInitStake(bitcoinRecoveryAddress, ethereumAddress, REFERRAL)
},
[acreInitStake, acre],
)
Expand Down
2 changes: 1 addition & 1 deletion dapp/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface ImportMetaEnv {
readonly VITE_SENTRY_DSN: string
readonly VITE_ETH_HOSTNAME_HTTP: string
readonly VITE_REFERRAL: number
readonly VITE_DEFENDER_RELAYER_WEBHOOK_URL: string
readonly VITE_TBTC_API_ENDPOINT: string
}

interface ImportMeta {
Expand Down
1 change: 0 additions & 1 deletion dapp/src/web3/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./ledger-live-signer"
export * from "./relayer-depositor-proxy"
96 changes: 0 additions & 96 deletions dapp/src/web3/relayer-depositor-proxy.ts

This file was deleted.

3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"extends": ["@thesis-co"],
"overrides": [
{
"files": ["test/*/*.test.ts"],
"files": ["test/**/*.test.ts"],
"rules": {
"@typescript-eslint/unbound-method": "off"
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/jest.config.js → sdk/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
preset: "ts-jest",
testPathIgnorePatterns: ["<rootDir>/dist/", "<rootDir>/node_modules/"],
}
3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@babel/preset-env": "^7.23.7",
"@ethersproject/bignumber": "^5.7.0",
"@thesis-co/eslint-config": "github:thesis/eslint-config#7b9bc8c",
"@types/jest": "^29.5.11",
"@types/node": "^20.9.4",
Expand All @@ -26,8 +27,8 @@
"typescript": "^5.3.2"
},
"dependencies": {
"@keep-network/tbtc-v2.ts": "2.4.0-dev.3",
"@acre-btc/contracts": "workspace:*",
"@keep-network/tbtc-v2.ts": "2.4.0-dev.3",
"ethers": "^6.10.0"
}
}
34 changes: 11 additions & 23 deletions sdk/src/acre.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TBTC } from "@keep-network/tbtc-v2.ts"
import { AcreContracts } from "./lib/contracts"
import { ChainEIP712Signer } from "./lib/eip712-signer"
import {
Expand All @@ -7,10 +6,11 @@ import {
getEthereumContracts,
} from "./lib/ethereum"
import { StakingModule } from "./modules/staking"
import Tbtc from "./modules/tbtc"
import { EthereumSignerCompatibleWithEthersV5 } from "./lib/utils"

class Acre {
readonly #tbtc: TBTC
readonly #tbtc: Tbtc

readonly #messageSigner: ChainEIP712Signer

Expand All @@ -21,7 +21,7 @@ class Acre {
constructor(
_contracts: AcreContracts,
_messageSigner: ChainEIP712Signer,
_tbtc: TBTC,
_tbtc: Tbtc,
) {
this.contracts = _contracts
this.#tbtc = _tbtc
Expand All @@ -36,31 +36,19 @@ class Acre {
static async initializeEthereum(
signer: EthereumSignerCompatibleWithEthersV5,
network: EthereumNetwork,
tbtcApiUrl: string,
): Promise<Acre> {
const tbtc = await Acre.#getTBTCEthereumSDK(signer, network)
const contracts = getEthereumContracts(signer, network)
const messages = new EthereumEIP712Signer(signer)

return new Acre(contracts, messages, tbtc)
}
const tbtc = await Tbtc.initialize(
signer,
network,
tbtcApiUrl,
contracts.bitcoinDepositor,
)

static #getTBTCEthereumSDK(
signer: EthereumSignerCompatibleWithEthersV5,
network: EthereumNetwork,
): Promise<TBTC> {
switch (network) {
case "sepolia":
// @ts-expect-error We require the `signer` must include the ether v5
// signer's methods used in tBTC-v2.ts SDK so if we pass signer from
// ethers v6 it won't break the Acre SDK initialization.
return TBTC.initializeSepolia(signer)
case "mainnet":
default:
// @ts-expect-error We require the `signer` must include the ether v5
// signer's methods used in tBTC-v2.ts SDK so if we pass signer from
// ethers v6 it won't break the Acre SDK initialization.
return TBTC.initializeMainnet(signer)
}
return new Acre(contracts, messages, tbtc)
}
}

Expand Down
47 changes: 47 additions & 0 deletions sdk/src/lib/api/HttpApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Represents an abstract HTTP API.
*/
export default abstract class HttpApi {
#apiUrl: string

constructor(apiUrl: string) {
this.#apiUrl = apiUrl
}

/**
* Factory function for running GET requests.
* @param endpoint API endpoint.
* @param requestInit Additional data passed to request.
* @returns API response.
*/
protected async getRequest(
endpoint: string,
requestInit?: RequestInit,
): Promise<Response> {
return fetch(new URL(endpoint, this.#apiUrl), {
credentials: "include",
...requestInit,
})
}

/**
* Factory function for running POST requests.
* @param endpoint API endpoint,
* @param body Data passed to POST request.
* @param requestInit Additional data passed to request.
* @returns API response.
*/
protected async postRequest(
endpoint: string,
body: unknown,
requestInit?: RequestInit,
): Promise<Response> {
return fetch(new URL(endpoint, this.#apiUrl), {
method: "POST",
body: JSON.stringify(body),
credentials: "include",
headers: { "Content-Type": "application/json" },
...requestInit,
})
}
}
Loading

0 comments on commit a327646

Please sign in to comment.