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

SDK: Support for L2 direct bridging #798

Merged
merged 10 commits into from
Mar 19, 2024
Merged

SDK: Support for L2 direct bridging #798

merged 10 commits into from
Mar 19, 2024

Conversation

lukasz-zimnoch
Copy link
Member

@lukasz-zimnoch lukasz-zimnoch commented Mar 13, 2024

Closes: #750

This pull request enhances the tBTC Typescript SDK with support of the L2 direct bridging mechanism introduced by #792. Moreover, we are adding Base as the first L2 chain with direct bridging enabled.

Initialization of the cross-chain contracts

To enable the SDK to work with a supported L2 chain, we introduce a new initializeCrossChain method that is exposed from the main TBTC component. This function sets up the SDK to work with the given L2 chain and attaches the provided signer to the tBTC cross-chain contracts deployed there. It's worth noting that the signer can be read-only (e.g. Ethers provider) but in that case, only read functions of the L2 contracts will be available.

After initialization, the cross-chain contracts for the given chain can be directly accessed using the new TBTC.crossChainContracts method. This low-level access method is especially useful for reading data, e.g. get canonical L2 TBTC balance for the given address.

Cross-chain deposits

To leverage the new L2 direct bridging mechanism, the SDK adds the concept of a cross-chain deposit. A cross-chain deposit is a deposit that targets an L2 chain other than the L1 chain the tBTC system is deployed on. Such a deposit is currently initiated using a transaction on the L2 chain (plans for the future include gas-less initiation). On the technical level, this is handled by the new initiateCrossChainDeposit method exposed by the DepositsService component.

Usage

import { Hex, TBTC } from "../src"
import { JsonRpcProvider, Provider } from "@ethersproject/providers"
import { Signer, Wallet } from "ethers"

async function main() {
  // Create a readonly Ethers provider for the Ethereum L1 chain.
  const ethereumProvider: Provider = new JsonRpcProvider("...")
  // Create an instance of the tBTC SDK. It is enough to pass a readonly
  // Ethers provider as parameter. In this example, the SDK does not issue
  // transactions on the Ethereum L1 chain.
  const sdk: TBTC = await TBTC.initializeMainnet(ethereumProvider)

  // Create a signer for Base. This signer will be used to issue transactions
  // on the Base chain and will be used as the owner of the deposit.
  const baseSigner: Signer = new Wallet("...", new JsonRpcProvider("..."))
  // Initialize cross-chain contracts for the Base chain.
  await sdk.initializeCrossChain("Base", baseSigner)

  // Get BTC recovery address for the deposit.
  const bitcoinRecoveryAddress: string = "..."

  // Initiate a cross-chain deposit to the Base chain.
  const deposit = await sdk.deposits.initiateCrossChainDeposit(
    bitcoinRecoveryAddress,
    "Base"
  )
  // Get BTC deposit address and send funds to it.
  const depositAddress: string = await deposit.getBitcoinAddress()
  // Initiate minting once BTC transaction is made. This will call
  // revealDepositOnBehalf function of the ExampleDepositor contract
  // under the hood.
  const baseTxHash: Hex = await deposit.initiateMinting()

  console.log(
    `Minting initiated. Base transaction hash: ${baseTxHash.toPrefixedString()}`
  )
}

This is needed to fetch latest L2 contracts.
Here we enhance the SDK to support cross-chain contracts.
We are achieving this by introducing a new `crossChainContracts`
method exposed by the main `TBTC` component. This method allows to
obtain cross-chain contracts handles for a specific supported L2 chain.

This feature will serve as a base to write a cross-chain Bitcoin depositor
component that will be used for direct bridging of deposits initiated on
supported L2 chains.

By the way, we are taking an opportunity and refactor the chain representation
used across the SDK. Namely, we are getting rid of the `EthereumNetwork`
type, and we are replacing it with a global `Chains` namespace
that aggregates all chains supported by the tBTC v2 SDK.
@lukasz-zimnoch lukasz-zimnoch self-assigned this Mar 13, 2024
@lukasz-zimnoch lukasz-zimnoch added the 🔌 typescript TypeScript library label Mar 13, 2024
@lukasz-zimnoch lukasz-zimnoch added this to the typescript/v2.4.0 milestone Mar 13, 2024
Here we expose a possibility to initiate cross-chain deposit that targets an
L2 chain other than the L1 chain the tBTC system is deployed on.
This is achieved by exposing the `initiateCrossChainDeposit` method
on the `DepositsService` component.
@lukasz-zimnoch lukasz-zimnoch changed the title Add support for L2 direct bridging to the SDK SDK: Support for L2 direct bridging Mar 14, 2024
The previous `@keep-network/tbtc-v2` upgrade bumped up a lot of
dependencies. Some changes around Ethers turned out to be breaking
for unit tests. Here we revert the unnecessary upgrades.
@lukasz-zimnoch lukasz-zimnoch marked this pull request as ready for review March 14, 2024 16:44
Copy link
Contributor

@michalsmiarowski michalsmiarowski left a comment

Choose a reason for hiding this comment

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

LGTM 🔥

typescript/src/lib/contracts/chain.ts Show resolved Hide resolved
const artifactLoader = {
getMainnet: (l2ChainName: L2Chain) => {
switch (l2ChainName) {
// TODO: Uncomment once BaseL1BitcoinDepositor is available on Ethereum mainnet.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not forget about those TODOs, when the mainnet contracts will be available on Ethereum mainnet. I assume it's a non-blocking issue and we can merge anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. We will open another PR just before the release.

@michalsmiarowski michalsmiarowski merged commit c57d64b into main Mar 19, 2024
38 checks passed
@michalsmiarowski michalsmiarowski deleted the base-sdk-testnet branch March 19, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌 typescript TypeScript library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Base direct bridging
2 participants