Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK: Support for L2 direct bridging (#798)
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 ```typescript 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()}` ) } ```
- Loading branch information