Hemi
DiscordGithub
  • 🏠Main
    • Welcome to the Hemi Docs
    • Getting Started
      • Developer Quickstart
      • Enthusiast Quickstart
      • Miner Quickstart
    • Network Details
  • ⛰️Foundational Topics
    • The Architecture
      • Consensus and Security Protocols
      • Ethereum Rollups
        • Pros & Cons
        • Decentralized Rollups
      • Sequencer Consensus
        • Proof-of-Stake (PoS)
        • PoS-Only Pitfalls
        • PoS Solutions
      • Proof-of-Proof (PoP)
        • Proof-of-Proof Consensus & Bitcoin Finality
        • Proof-of-Proof vs. Merged Mining
        • Running a PoP Mining
      • Tunnels
        • Ethereum Tunnel
        • Bitcoin Tunnel
      • Ethereum Virtual Machine (EVM)
      • Blocks
      • Transactions
      • Gas
    • Nodes & Clients
      • Node Guides
    • Wallet Support
  • 📖How-To Tutorials
    • Using Hemi
      • Wallet Setup
        • EVM Wallet Setup
        • BTC Wallet Setup
          • Switch Bitcoin Networks
      • Tunnel from Ethereum
        • Tunnel ERC20s via Native Tunnel
        • Tunnel ERC20s via 3rd Party
          • Tunnel via Stargate
      • Tunnel from Bitcoin
        • Tunnel BTC via Native Tunnel
        • Tunnel BTC via 3rd Party
      • Stake
      • Developer Tooling
        • Set Up a Safe Wallet
        • Create a Capsule
      • PoP Mining
        • CLI PoP Miner
          • (Testnet) Add tHEMI to MetaMask
          • (Mainnet) Add PoPPoints to Metamask
        • Web PoP Miner (deprecated)
    • Developing on Hemi
      • General
        • HelloWorld.sol
        • Deploy an ERC-20 Token
      • hVM & hBK
        • Using the hBK Demo App
        • Get Bitcoin Balance with Remix
  • ⚙️Building Bitcoin Apps
    • Introduction
    • Hemi Virtual Machine (hVM)
      • Motivation
      • Feature Summary
      • Deploy on hVM
    • Hemi Bitcoin Kit (hBK)
      • Overview
      • hBK Smart Contract
  • ⚙️Tooling
    • viem
    • Contract Addresses
    • Contract Verification
    • Data Indexing
    • Oracles
  • 📝Incentives
    • Points
    • Grants
    • Retroactive Funding
    • One-Off Spends
  • ⚡Additional Resources
    • Partners
    • FAQ
    • Official Links
    • Brand Kit
  • 📨Send Feedback
    • Bug Report
    • Contact Us
Powered by GitBook
On this page
  • 🏁 Prerequisites
  • 📚 Tutorial
  • 1. Create Contract

Was this helpful?

Edit on GitHub
Export as PDF
  1. How-To Tutorials
  2. Developing on Hemi
  3. hVM & hBK

Get Bitcoin Balance with Remix

PreviousUsing the hBK Demo AppNextIntroduction

Last updated 7 months ago

Was this helpful?

📜 TL;DR:

  • In this section, we’ll deploy a small contract in with a single method which calls the Bitcoin Balance precompile and returns the balance as a uint256.

  • We recommend you use Solidity version 0.8.25 or later, but previous versions are also expected to work.


🏁 Prerequisites

To follow along, you’ll need:

  • Metamask (or another Remix-supported web wallet) connected to Hemi testnet as a custom network;

  • A nonzero ETH balance tunneled over to a Hemi address in your wallet you want to deploy the demo contract with to pay deployment gas fees.


📚 Tutorial

1. Create Contract

  • To start, open Remix, delete any existing contracts in the “contracts” folder,.

  • Create a new contract file, which for our example we’ll call “BitcoinBalDemo.sol”.

  • Paste in the following code:

pragma solidity ^0.8.25;

contract BitcoinBalDemo {
    function getBitcoinAddressBalance(string calldata btcAddress) public view returns (uint256 balance) {
    bytes memory converted = bytes(btcAddress);
    (bool ok, bytes memory out) = address(0x40).staticcall(converted);
    require(ok, "Failed to Call Bitcoin Balance hVM Precompile (0x40)");
    
    return uint64(bytes8(out));
  }
}

You can now compile and deploy this contract, and try out calling it with a Bitcoin address string to see the address's balance (in satoshis) returned by hVM.

📖
Remix IDE