Skip to content

Commit

Permalink
Avoid ignoring ethers v5 Signer related errors (#654)
Browse files Browse the repository at this point in the history
Closes: #429 

Install the ethers v5 lib used by the tBTC SDK and create the Signer
from ethers v5 when initializing tBTC SDK.
  • Loading branch information
nkuba authored Jul 23, 2024
2 parents e66cbb6 + 7dae7f8 commit 9bcc21c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 102 deletions.
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.

3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@ledgerhq/wallet-api-client": "1.5.0",
"@orangekit/sdk": "1.0.0-beta.14",
"@swan-bitcoin/xpub-lib": "0.1.5",
"ethers": "6.10.0"
"ethers": "6.10.0",
"ethers-v5": "npm:ethers@^5.5.2"
}
}
14 changes: 11 additions & 3 deletions sdk/src/acre.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { GelatoTransactionSender, OrangeKitSdk } from "@orangekit/sdk"
import { getDefaultProvider, Provider as EthereumProvider } from "ethers"
import {
getDefaultProvider,
Provider as EthereumProvider,
VoidSigner,
} from "ethers"
import {
EthereumAddress,
EthereumNetwork,
getEthereumContracts,
} from "./lib/ethereum"
import Account from "./modules/account"
import Tbtc from "./modules/tbtc"
import { VoidSigner } from "./lib/utils"
import { BitcoinProvider, BitcoinNetwork } from "./lib/bitcoin"
import { getChainIdByNetwork } from "./lib/ethereum/network"
import AcreSubgraphApi from "./lib/api/AcreSubgraphApi"
Expand All @@ -25,6 +28,8 @@ class Acre {

readonly #ethereumProvider: EthereumProvider

readonly #ethereumRpcURL: string

readonly #acreSubgraph: AcreSubgraphApi

#account: Account | undefined
Expand All @@ -38,13 +43,15 @@ class Acre {
tbtcApiUrl: string,
acreSubgraphApi: AcreSubgraphApi,
protocol: Protocol,
ethereumRpcURL: string,
) {
this.#network = bitcoinNetwork
this.#ethereumProvider = ethereumProvider
this.#tbtcApiUrl = tbtcApiUrl
this.#orangeKit = orangeKit
this.#acreSubgraph = acreSubgraphApi
this.protocol = protocol
this.#ethereumRpcURL = ethereumRpcURL
}

/**
Expand Down Expand Up @@ -95,6 +102,7 @@ class Acre {
tbtcApiUrl,
subgraph,
protocol,
ethereumRpcUrl,
)
}

Expand All @@ -120,8 +128,8 @@ class Acre {
const contracts = await getEthereumContracts(signer, ethereumNetwork)

const tbtc = await Tbtc.initialize(
signer,
this.#network,
this.#ethereumRpcURL,
this.#tbtcApiUrl,
contracts.bitcoinDepositor,
)
Expand Down
41 changes: 0 additions & 41 deletions sdk/src/lib/utils/ethereum-signer.ts

This file was deleted.

1 change: 0 additions & 1 deletion sdk/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./hex"
export * from "./ethereum-signer"
export * from "./backoff"
export * from "./satoshi-converter"
26 changes: 11 additions & 15 deletions sdk/src/modules/tbtc/Tbtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import {
BitcoinHashUtils,
} from "@keep-network/tbtc-v2.ts"

import { ethers } from "ethers"
import { ethers, ZeroAddress } from "ethers"
import { getDefaultProvider, VoidSigner } from "ethers-v5"
import TbtcApi, { DepositStatus } from "../../lib/api/TbtcApi"
import { BitcoinDepositor } from "../../lib/contracts"
import {
Hex,
IEthereumSignerCompatibleWithEthersV5 as EthereumSignerCompatibleWithEthersV5,
} from "../../lib/utils"
import { Hex } from "../../lib/utils"

import Deposit from "./Deposit"
import { BitcoinNetwork } from "../../lib/bitcoin"
Expand Down Expand Up @@ -45,30 +43,28 @@ export default class Tbtc {
/**
* Initializes the Tbtc module.
*
* @param signer The Ethereum signer compatible with ethers v5.
* @param network The Ethereum network.
* @param ethereumRpcUrl The Ethereum RPC URL.
* @param tbtcApiUrl The tBTC API URL.
* @param bitcoinDepositor The Bitcoin depositor contract handle.
* @returns A Promise that resolves to an instance of Tbtc.
*/
static async initialize(
signer: EthereumSignerCompatibleWithEthersV5,
network: BitcoinNetwork,
ethereumRpcUrl: string,
tbtcApiUrl: string,
bitcoinDepositor: BitcoinDepositor,
): Promise<Tbtc> {
const tbtcApi = new TbtcApi(tbtcApiUrl)
const signer = new VoidSigner(
ZeroAddress,
getDefaultProvider(ethereumRpcUrl),
)

const tbtcSdk =
network === BitcoinNetwork.Mainnet
? // @ts-expect-error We require the `signer` must include the ethers 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.
await TbtcSdk.initializeMainnet(signer)
: // @ts-expect-error We require the `signer` must include the ethers 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.
await TbtcSdk.initializeSepolia(signer)
? await TbtcSdk.initializeMainnet(signer)
: await TbtcSdk.initializeSepolia(signer)

return new Tbtc(tbtcApi, tbtcSdk, bitcoinDepositor, network)
}
Expand Down
54 changes: 13 additions & 41 deletions sdk/test/modules/tbtc/Tbtc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ import {
Deposit as TbtcSdkDeposit,
} from "@keep-network/tbtc-v2.ts"

import { ZeroAddress, Provider, ethers } from "ethers"
import {
IEthereumSignerCompatibleWithEthersV5,
VoidSigner,
Hex,
BitcoinNetwork,
} from "../../../src"
import { ethers } from "ethers"
import { ethers as ethersV5 } from "ethers-v5"
import { Hex, BitcoinNetwork } from "../../../src"
import Deposit from "../../../src/modules/tbtc/Deposit"
import TbtcApi from "../../../src/lib/api/TbtcApi"

Expand All @@ -28,35 +24,12 @@ import {
import { MockAcreContracts } from "../../utils/mock-acre-contracts"

import { MockTbtcSdk } from "../../utils/mock-tbtc-sdk"
import { getChainIdByNetwork } from "../../../src/lib/ethereum/network"

jest.mock("@keep-network/tbtc-v2.ts", (): object => ({
TbtcSdk: jest.fn(),
...jest.requireActual("@keep-network/tbtc-v2.ts"),
}))

class MockEthereumSignerCompatibleWithEthersV5 extends VoidSigner {
constructor() {
super(ZeroAddress, {} as Provider)
}

getAddress = jest.fn()

connect = jest.fn()

signTransaction = jest.fn()

signMessage = jest.fn()

signTypedData = jest.fn()

_isSigner: boolean = true

_checkProvider = jest.fn()

getChainId = jest.fn().mockResolvedValue(getChainIdByNetwork("sepolia"))
}

describe("Tbtc", () => {
const tbtcApiUrl = "https://api.acre.fi/v1/deposit/"

Expand All @@ -65,17 +38,17 @@ describe("Tbtc", () => {

const { bitcoinDepositor } = new MockAcreContracts()
const tbtcApi: TbtcApi = new TbtcApi(tbtcApiUrl)
const mockedSigner = {} as ethersV5.VoidSigner

const tbtc = new Tbtc(
tbtcApi,
tbtcSdk,
bitcoinDepositor,
BitcoinNetwork.Testnet,
)
let tbtc: Tbtc

beforeAll(() => {
jest.spyOn(ethersV5, "VoidSigner").mockReturnValue(mockedSigner)
tbtc = new Tbtc(tbtcApi, tbtcSdk, bitcoinDepositor, BitcoinNetwork.Testnet)
})

describe("initialize", () => {
const mockedSigner: IEthereumSignerCompatibleWithEthersV5 =
new MockEthereumSignerCompatibleWithEthersV5()
const ethereumRpcUrl = "https://test.com"

describe("when network is mainnet", () => {
const network: BitcoinNetwork = BitcoinNetwork.Mainnet
Expand All @@ -84,10 +57,9 @@ describe("Tbtc", () => {

beforeAll(async () => {
jest.spyOn(TbtcSdk, "initializeMainnet").mockResolvedValueOnce(tbtcSdk)

result = await Tbtc.initialize(
mockedSigner,
network,
ethereumRpcUrl,
tbtcApiUrl,
bitcoinDepositor,
)
Expand All @@ -111,8 +83,8 @@ describe("Tbtc", () => {
jest.spyOn(TbtcSdk, "initializeSepolia").mockResolvedValueOnce(tbtcSdk)

result = await Tbtc.initialize(
mockedSigner,
network,
ethereumRpcUrl,
tbtcApiUrl,
bitcoinDepositor,
)
Expand Down

0 comments on commit 9bcc21c

Please sign in to comment.