Skip to content

Commit

Permalink
Updates to SDK and dApp after Depositor contract changes
Browse files Browse the repository at this point in the history
Depends on: #91

In this PR we adjust SDK and dApp to reflect changes introduced in
#91.

The contract was renamed from TbtcDepositor to AcreBitcoinDepositor.
  • Loading branch information
nkuba committed Mar 1, 2024
1 parent 3121c67 commit 4b23edc
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 61 deletions.
6 changes: 3 additions & 3 deletions dapp/src/contexts/StakeFlowContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "#/acre-react/hooks"
import { REFERRAL } from "#/constants"
import { RelayerDepositorProxy } from "#/web3"
import { EthereumTBTCDepositor } from "@acre-btc/sdk"
import { EthereumBitcoinDepositor } from "@acre-btc/sdk"

type StakeFlowContextValue = Omit<UseStakeFlowReturn, "initStake"> & {
initStake: (
Expand Down Expand Up @@ -39,8 +39,8 @@ export function StakeFlowProvider({ children }: { children: React.ReactNode }) {
bitcoinRecoveryAddress,
ethereumAddress,
REFERRAL,
RelayerDepositorProxy.fromEthereumTbtcDepositor(
acre.contracts.tbtcDepositor as EthereumTBTCDepositor,
RelayerDepositorProxy.fromEthereumBitcoinDepositor(
acre.contracts.bitcoinDepositor as EthereumBitcoinDepositor,
),
)
},
Expand Down
33 changes: 18 additions & 15 deletions dapp/src/web3/relayer-depositor-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
BitcoinRawTxVectors,
DepositorProxy,
EthereumTBTCDepositor,
EthereumBitcoinDepositor,
ChainIdentifier,
DepositReceipt,
Hex,
packRevealDepositParameters,
TBTCDepositor,
BitcoinDepositor,
} from "@acre-btc/sdk"
import axios from "axios"

Expand All @@ -18,11 +18,13 @@ const DEFENDER_WEBHOOK_URL = import.meta.env.VITE_DEFENDER_RELAYER_WEBHOOK_URL
* Sends the HTTP POST request to the webhook at the provided URL with the data
* necessary to initialize stake.
*/
class RelayerDepositorProxy<T extends TBTCDepositor> implements DepositorProxy {
class RelayerDepositorProxy<T extends BitcoinDepositor>
implements DepositorProxy
{
/**
* Chain-specific handle to @see TBTCDepositor contract.
* Chain-specific handle to @see BitcoinDepositor contract.
*/
#tbtcDepositor: T
#bitcoinDepositor: T

/**
* Defines the Open Zeppelin Defender webhook URL.
Expand All @@ -31,25 +33,25 @@ class RelayerDepositorProxy<T extends TBTCDepositor> implements DepositorProxy {

/**
* Creates the instance of the relayer depositor proxy for Ethereum chain.
* @param tbtcDepositor Ethereum handle to @see TBTCDepositor contract.
* @param bitcoinDepositor Ethereum handle to @see BitcoinDepositor contract.
* @returns Instance of @see RelayerDepositorProxy.
*/
static fromEthereumTbtcDepositor(
tbtcDepositor: EthereumTBTCDepositor,
): RelayerDepositorProxy<EthereumTBTCDepositor> {
return new RelayerDepositorProxy(tbtcDepositor, DEFENDER_WEBHOOK_URL)
static fromEthereumBitcoinDepositor(
bitcoinDepositor: EthereumBitcoinDepositor,
): RelayerDepositorProxy<EthereumBitcoinDepositor> {
return new RelayerDepositorProxy(bitcoinDepositor, DEFENDER_WEBHOOK_URL)
}

private constructor(_tbtcDepositor: T, _defenderWebhookUr: string) {
this.#tbtcDepositor = _tbtcDepositor
private constructor(_bitcoinDepositor: T, _defenderWebhookUr: string) {
this.#bitcoinDepositor = _bitcoinDepositor
this.#defenderWebhookUrl = _defenderWebhookUr
}

/**
* @see {DepositorProxy#getChainIdentifier}
*/
getChainIdentifier(): ChainIdentifier {
return this.#tbtcDepositor.getChainIdentifier()
return this.#bitcoinDepositor.getChainIdentifier()
}

/**
Expand All @@ -65,12 +67,13 @@ class RelayerDepositorProxy<T extends TBTCDepositor> implements DepositorProxy {
depositTx,
depositOutputIndex,
deposit,
await this.#tbtcDepositor.getTbtcVaultChainIdentifier(),
await this.#bitcoinDepositor.getTbtcVaultChainIdentifier(),
)

if (!extraData) throw new Error("Invalid extra data")

const { staker, referral } = this.#tbtcDepositor.decodeExtraData(extraData)
const { staker, referral } =
this.#bitcoinDepositor.decodeExtraData(extraData)

// TODO: Catch and handle errors + sentry.
const response = await axios.post<{ result: string }>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export type DecodedExtraData = {
}

/**
* Interface for communication with the TBTCDepositor on-chain contract.
* Interface for communication with the AcreBitcoinDepositor on-chain contract.
*/
export interface TBTCDepositor extends DepositorProxy {
export interface BitcoinDepositor extends DepositorProxy {
/**
* @returns The chain-specific identifier of this contract.
*/
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/lib/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TBTCDepositor } from "./tbtc-depositor"
import { BitcoinDepositor } from "./bitcoin-depositor"

export * from "./tbtc-depositor"
export * from "./bitcoin-depositor"
export * from "./chain-identifier"
export * from "./depositor-proxy"

/**
* Represents all contracts that allow interaction with the Acre network.
*/
export type AcreContracts = {
tbtcDepositor: TBTCDepositor
bitcoinDepositor: BitcoinDepositor
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import {
ChainIdentifier,
DecodedExtraData,
TBTCDepositor,
BitcoinDepositor,
DepositReceipt,
} from "../contracts"
import { BitcoinRawTxVectors } from "../bitcoin"
Expand All @@ -26,17 +26,15 @@ import { EthereumNetwork } from "./network"

import SepoliaBitcoinDepositor from "./artifacts/sepolia/AcreBitcoinDepositor.json"

// TODO: Rename TBTCDepositor to AcreBitcoinDepositor

/**
* Ethereum implementation of the TBTCDepositor.
* Ethereum implementation of the BitcoinDepositor.
*/
class EthereumTBTCDepositor
class EthereumBitcoinDepositor
// @ts-expect-error TODO: Figure out why type generated by typechain does not
// satisfy the constraint `Contract`. Error: `Property '[internal]' is missing
// in type 'AcreBitcoinDepositor' but required in type 'Contract'`.
extends EthersContractWrapper<AcreBitcoinDepositorTypechain>
implements TBTCDepositor
implements BitcoinDepositor
{
constructor(config: EthersContractConfig, network: EthereumNetwork) {
let artifact: EthersContractDeployment
Expand All @@ -54,14 +52,14 @@ class EthereumTBTCDepositor
}

/**
* @see {TBTCDepositor#getChainIdentifier}
* @see {BitcoinDepositor#getChainIdentifier}
*/
getChainIdentifier(): ChainIdentifier {
return this.getAddress()
}

/**
* @see {TBTCDepositor#getTbtcVaultChainIdentifier}
* @see {BitcoinDepositor#getTbtcVaultChainIdentifier}
*/
async getTbtcVaultChainIdentifier(): Promise<ChainIdentifier> {
const vault = await this.instance.tbtcVault()
Expand All @@ -70,7 +68,7 @@ class EthereumTBTCDepositor
}

/**
* @see {TBTCDepositor#revealDeposit}
* @see {BitcoinDepositor#revealDeposit}
*/
async revealDeposit(
depositTx: BitcoinRawTxVectors,
Expand Down Expand Up @@ -99,7 +97,7 @@ class EthereumTBTCDepositor
}

/**
* @see {TBTCDepositor#encodeExtraData}
* @see {BitcoinDepositor#encodeExtraData}
* @dev Packs the data to bytes32: 20 bytes of staker address and 2 bytes of
* referral, 10 bytes of trailing zeros.
*/
Expand All @@ -119,7 +117,7 @@ class EthereumTBTCDepositor
}

/**
* @see {TBTCDepositor#decodeExtraData}
* @see {BitcoinDepositor#decodeExtraData}
* @dev Unpacks the data from bytes32: 20 bytes of staker address and 2
* bytes of referral, 10 bytes of trailing zeros.
*/
Expand All @@ -132,4 +130,4 @@ class EthereumTBTCDepositor
}
}

export { EthereumTBTCDepositor, packRevealDepositParameters }
export { EthereumBitcoinDepositor, packRevealDepositParameters }
8 changes: 4 additions & 4 deletions sdk/src/lib/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { AcreContracts } from "../contracts"
import { EthereumSigner } from "./contract"
import { EthereumTBTCDepositor } from "./tbtc-depositor"
import { EthereumBitcoinDepositor } from "./bitcoin-depositor"
import { EthereumNetwork } from "./network"

export * from "./eip712-signer"
export * from "./tbtc-depositor"
export * from "./bitcoin-depositor"
export * from "./address"
export { EthereumSigner }

function getEthereumContracts(
signer: EthereumSigner,
network: EthereumNetwork,
): AcreContracts {
const tbtcDepositor = new EthereumTBTCDepositor({ signer }, network)
const bitcoinDepositor = new EthereumBitcoinDepositor({ signer }, network)

return { tbtcDepositor }
return { bitcoinDepositor }
}

export { getEthereumContracts, EthereumNetwork }
4 changes: 2 additions & 2 deletions sdk/src/modules/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class StakingModule {
) {
const deposit = await this.#tbtc.deposits.initiateDepositWithProxy(
bitcoinRecoveryAddress,
depositorProxy ?? this.#contracts.tbtcDepositor,
this.#contracts.tbtcDepositor.encodeExtraData(staker, referral),
depositorProxy ?? this.#contracts.bitcoinDepositor,
this.#contracts.bitcoinDepositor.encodeExtraData(staker, referral),
)

return new StakeInitialization(
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/modules/staking/stake-initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class StakeInitialization {
*/
#getStakeMessageTypedData() {
const domain: Domain = {
name: "TbtcDepositor",
name: "AcreBitcoinDepositor",
version: "1",
verifyingContract: this.#contracts.tbtcDepositor.getChainIdentifier(),
verifyingContract: this.#contracts.bitcoinDepositor.getChainIdentifier(),
}

// TODO: revisit the message structure before the launch. Let's think about
Expand All @@ -131,7 +131,7 @@ class StakeInitialization {
}

/**
* Stakes BTC based on the Bitcoin funding transaction via TBTCDepositor
* Stakes BTC based on the Bitcoin funding transaction via BitcoinDepositor
* contract. It requires signed staking message, which means `stake` should be
* called after message signing. By default, it detects and uses the outpoint
* of the recent Bitcoin funding transaction and throws if such a transaction
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/lib/ethereum/eip712.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

const signMessageData = {
domain: {
name: "TBTCDepositor",
name: "AcreBitcoinDepositor",
version: "1",
verifyingContract: EthereumAddress.from(
ethers.Wallet.createRandom().address,
Expand Down
8 changes: 4 additions & 4 deletions sdk/test/lib/ethereum/tbtc-depositor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ethers, { Contract, ZeroAddress } from "ethers"
import {
EthereumTBTCDepositor,
EthereumBitcoinDepositor,
EthereumAddress,
Hex,
EthereumSigner,
Expand All @@ -12,7 +12,7 @@ jest.mock("ethers", (): object => ({
...jest.requireActual("ethers"),
}))

describe("TBTCDepositor", () => {
describe("BitcoinDepositor", () => {
const spyOnEthersDataSlice = jest.spyOn(ethers, "dataSlice")

const vaultAddress = EthereumAddress.from(
Expand All @@ -23,7 +23,7 @@ describe("TBTCDepositor", () => {
tbtcVault: jest.fn().mockImplementation(() => vaultAddress.identifierHex),
initializeStake: jest.fn(),
}
let depositor: EthereumTBTCDepositor
let depositor: EthereumBitcoinDepositor
let depositorAddress: EthereumAddress

beforeEach(async () => {
Expand All @@ -38,7 +38,7 @@ describe("TBTCDepositor", () => {
await ethers.Wallet.createRandom().getAddress(),
)

depositor = new EthereumTBTCDepositor(
depositor = new EthereumBitcoinDepositor(
{
signer: {} as EthereumSigner,
address: depositorAddress.identifierHex,
Expand Down
16 changes: 8 additions & 8 deletions sdk/test/modules/staking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ describe("Staking", () => {
let result: StakeInitialization

beforeEach(async () => {
contracts.tbtcDepositor.decodeExtraData = jest
contracts.bitcoinDepositor.decodeExtraData = jest
.fn()
.mockReturnValue({ staker, referral })

contracts.tbtcDepositor.encodeExtraData = jest
contracts.bitcoinDepositor.encodeExtraData = jest
.fn()
.mockReturnValue(extraData)

Expand All @@ -101,13 +101,13 @@ describe("Staking", () => {
})

it("should encode extra data", () => {
expect(contracts.tbtcDepositor.encodeExtraData(staker, referral))
expect(contracts.bitcoinDepositor.encodeExtraData(staker, referral))
})

it("should initiate tBTC deposit", () => {
expect(tbtc.deposits.initiateDepositWithProxy).toHaveBeenCalledWith(
bitcoinRecoveryAddress,
contracts.tbtcDepositor,
contracts.bitcoinDepositor,
extraData,
)
})
Expand Down Expand Up @@ -148,7 +148,7 @@ describe("Staking", () => {

beforeEach(async () => {
mockedSignedMessage.verify.mockReturnValue(staker)
contracts.tbtcDepositor.getChainIdentifier = jest
contracts.bitcoinDepositor.getChainIdentifier = jest
.fn()
.mockReturnValue(EthereumAddress.from(depositorAddress))

Expand All @@ -158,10 +158,10 @@ describe("Staking", () => {
it("should sign message", () => {
expect(messageSigner.sign).toHaveBeenCalledWith(
{
name: "TbtcDepositor",
name: "AcreBitcoinDepositor",
version: "1",
verifyingContract:
contracts.tbtcDepositor.getChainIdentifier(),
contracts.bitcoinDepositor.getChainIdentifier(),
},
{
Stake: [
Expand Down Expand Up @@ -244,7 +244,7 @@ describe("Staking", () => {
let result: StakeInitialization

beforeEach(async () => {
contracts.tbtcDepositor.encodeExtraData = jest
contracts.bitcoinDepositor.encodeExtraData = jest
.fn()
.mockReturnValue(extraData)

Expand Down
8 changes: 4 additions & 4 deletions sdk/test/utils/mock-acre-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AcreContracts, TBTCDepositor } from "../../src/lib/contracts"
import { AcreContracts, BitcoinDepositor } from "../../src/lib/contracts"

// eslint-disable-next-line import/prefer-default-export
export class MockAcreContracts implements AcreContracts {
public readonly tbtcDepositor: TBTCDepositor
public readonly bitcoinDepositor: BitcoinDepositor

constructor() {
this.tbtcDepositor = {
this.bitcoinDepositor = {
getChainIdentifier: jest.fn(),
getTbtcVaultChainIdentifier: jest.fn(),
decodeExtraData: jest.fn(),
encodeExtraData: jest.fn(),
revealDeposit: jest.fn(),
} as TBTCDepositor
} as BitcoinDepositor
}
}

0 comments on commit 4b23edc

Please sign in to comment.