0.0.1-beta.0 • Published 2 months ago

@zkbridge/btc-token-bridge v0.0.1-beta.0

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

@zkbridge/btc-token-bridge

@zkbridge/btc-token-bridge is an SDK project for Bitcoin, Ordinals and Inscriptions Bridge

Usage

install

pnpm add @zkbridge/btc-token-bridge

Demo

Install and build dependencies across all project folders:

/* eslint-disable react/no-deprecated */
import { TokenBridgeWidget, BtcBridgeProvider, btcChains, evmChains } from "@zkbridge/btc-token-bridge";
import { useCallback, useEffect, useState } from "react";
import ReactDOM from "react-dom";


declare global {
  interface Window {
    Buffer: any;
    unisat: any;
    ethereum: any;
  }
}

//===========================================================
// config
//===========================================================
 const BRC20_SATS_TOKEN = "SATS";

 const chainMap = {
  btc_test: {
    ...btcChains.btc_test,
    tokens: [
      BRC20_SATS_TOKEN
    ],
    networkType: NetworkTypes.btc,
  },
  bitlayer_test: {
    ...evmChains.bitlayer_test,
    tokens: [
    ],
    networkType: NetworkTypes.evm,
  }
};
 const chainPair = {
  btc_test: {
    bitlayer_test: [BRC20_SATS_TOKEN]
  }
};
 const bridgeType = "brc20";  // 跨链的类型 "brc20 | btc"



const App = () => {
  const [sourceAccount, setSourceAccount] = useState<string>("");

  const handleNetworkChanged = (network: string) => {
    // 当前只支持testnet
    if (network == "livenet") {
      message.warn("network not support");
    }
  };

  const handleAccountsChanged = useCallback((_accounts: string[]) => {
    if (_accounts.length) {
      setSourceAccount(_accounts[0]);
    }
  }, []);

  useEffect(() => {
    async function init() {
      if (window.unisat) {
        const network = await window.unisat.getNetwork();
        if (network != "testnet") {
          await window.unisat.switchNetwork("testnet");
        }
        await window.unisat.requestAccounts();
        window.unisat.getAccounts().then((accounts: string[]) => {
          handleAccountsChanged(accounts);
        });
        window.unisat.on("accountsChanged", handleAccountsChanged);
        window.unisat.on("networkChanged", handleNetworkChanged);
      }
    }
    init();
    return () => {
      if (window.unisat) {
        window.unisat.removeListener("accountsChanged", handleAccountsChanged);
        window.unisat.removeListener("networkChanged", handleNetworkChanged);
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return (
    <BtcBridgeProvider
      value={{
        provider: window.unisat,
        sourceAccount,
        chainMap,
        chainPair,
        bridgeType,
      }}
    >
      <TokenBridgeWidget />
    </BtcBridgeProvider>
  );
};
ReactDOM.render(<App />, document.getElementById("app")!);

How to build

Pre-Requisites

This project uses PNPM to manage dependencies.

$ npm i -g pnpm

Setup

Install and build dependencies across all project folders:

$ pnpm install

Build:

$ pnpm run build

Examples:

$ cd examples && pnpm install