Hemi 백서

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 13쪽 중 9쪽

racts with ease. *** ### 2. **Add the Smart Contract** In Remix IDE, start by creating a new file named `HelloWorld.sol`. ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F6obzJvFi0ctxe6cZWUxy%2F1c.png?alt=media\&token=daa3cbb7-ba29-4102-9f4f-b6ef14ee3897) * The following contract is a basic example designed for interaction. Copy the code below and paste it into the file `HelloWorld.sol`. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract HelloWorld { string public greeting = "Hello, World!"; function getGreeting() public view returns (string memory) { return greeting; } function setGreeting(string memory _greeting) public { greeting = _greeting; } } ``` *** ### **3. Compile the Contract** Select `Compile HelloWorld.sol` button to use the Remix IDE's Solidity compiler. Check for any compilation errors and fix them. ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FatKV6rX5cahQs1HpKgdl%2F2c.png?alt=media\&token=a63fc20e-bd3d-4fcf-8750-2cdfb2794c57) *** ### 4. Connect Remix To MetaMask {% hint style="warning" %} The address you choose to connect **MUST** have some ETH (recommended at least 0.01 ETH) to pay for contract deployment fees. Refer back to earlier documentation to [fund your Hemi address](/how-to-tutorials/using-hemi/tunnel-from-ethereum/tunnel-eth-to-hemi) with ETH. {% endhint %} Choose `Injected Provider - MetaMask` under "Environment". ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FB1k0F4sgSrWtROGUZlLi%2F3c.png?alt=media\&token=06a3f599-d4f8-4e9c-b350-6551e14127c6) *** ### 5. Confirm the Connection * A pop-up from MetaMask will appear to confirm the connection. Select `Next`. ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FlyVRVIJS8gcKBRYUgVod%2F4c.png?alt=media\&token=b55876ad-68ce-4566-b7f0-93905266d101) * Select `Connect` ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F9ofW5rL8Z8cQUVhJhbKm%2F5c.png?alt=media\&token=628d1c06-0169-4703-b112-03278cdda1b0) *** ### 6. Link to Hemi Account * Link to the relevant Hemi account in the "Account" drop-down. ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2Fotb7GJUYvjmfkY3HFLaG%2F6c.png?alt=media\&token=de838aef-aeba-4208-9528-8522c95a04cf) *** ### 7. Deploy Execute the deployment of your smart contract to the Hemi network directly from Remix IDE. {% hint style="warning" %} Uncheck `Publish to IPFS.`Note: if you get a warning about Gas Limit, you may ignore it and proceed. {% endhint %} * Select `Deploy` ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FZe8AnO66mmQgTtLle1Bj%2F7c.png?alt=media\&token=084ef860-0d9e-4622-9948-0486cb14608e) * Your MetaMask will pop-up to confirm the deployment of your smart contract. Select `Confirm` ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2Fa6yWS1TQ7KQSd1DcPpMd%2F8c.png?alt=media\&token=f564ebca-db9a-44f0-a2f9-0db9131d816d) 🥳 Wohoooo! If successful you should see a ✅ in the console. ⚠️ If it fails, double check that the Solidity version in the compiler is the correct version. ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FyEmvarNHNUYLdUdejhpD%2F9c.png?alt=media\&token=e0a31165-45e5-4007-8765-5f843c2fd316) # Deploy an ERC-20 Token {% hint style="info" %} ## 📜 **TL;DR:** * This tutorial guides you through deploying an ERC-20 token using [Hardhat](https://hardhat.org/), a comprehensive Ethereum development environment. Hardhat is recommended for its powerful features, but you can also use other Ethereum development tools like [Foundry](https://book.getfoundry.sh/). * Ensure Node.js, Hardhat, and Solidity are set up correctly for effective ERC-20 token development and deployment. * Earn and track Hemi [points](https://points.absinthe.network/hemi/start) for completing this tutorial! {% endhint %} *** ## 🏁 Prerequisites 1. Download [VSCode ](https://code.visualstudio.com/download)(or any other IDE) 2. Install [Node.js](https://nodejs.org/en/download/) or update it to [current version](https://blog.hubspot.com/website/update-node-js) 3. [MetaMask Wallet Setup](/how-to-tutorials/using-hemi/wallet-setup/metamask-wallet-setup) 4. [Tunnel ETH to Hemi](/how-to-tutorials/using-hemi/tunnel-from-ethereum/tunnel-eth-to-hemi) *** ## 📚 Tutorial ## Video {% embed url="<https://vimeo.com/1027100635?share=copy>" %} ### 1. Create A Project Directory * Create a folder on your computer and name it `TestToken`. * This folder will serve as your project directory, containing all the elements and code needed to deploy your ERC-20 token. *** ### 2. Initialize Your NPM Project * In your `TestToken` project directory, open a terminal window and initialize a Node.js project ```shell npm init -y ``` *** ### 3. Install Hardhat & Ethers.js Plugin * Install Hardhat along with the Ether.js plugin, and the OpenZeppelin contracts library. ```shell npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts ``` *** ### 4. Create a HardHat Project a) Inside your Node.js project, start a Hardhat project ```shell npx hardhat init ``` b) Select `Create an empty hardhat.config.js` <img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FlTn4GtrDlR7rlWskuIEb%2F1d.png?alt=media&amp;token=4e4ee622-f55b-4186-abf2-15a0615a5dc3" alt="" width="563"> *** ### 5. Add Folder In the root directory of your project, create `contracts` and `scripts` folders: ```shell mkdir contracts && mkdir scripts ``` *** ### 6. Write Your Contract <img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F2vTMzZlOdEzLXgT95tm2%2F2d.png?alt=media&amp;token=f7fff485-10a4-4724-8e1f-ae6bee359c7d" alt="" width="358"> * In the `contracts` folder, create a file named `MyToken.sol` . This will be your ERC-20 token contract. Here's a basic example: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract MyToken is ERC20 { constructor(uint256 initialSupply) ERC20("MyToken", "MTK") { _mint(msg.sender, initialSupply); } } ``` * This code defines a simple ERC-20 token with an initial supply and basic ERC-20 functionalities. *** ### 7. Compile Your Contract ```shell npx hardhat compile ``` *** ### 8. Secure Your Private Key for Deployment {% hint style="danger" %} Your private key provides access to your wallet and your funds. **Never share your private key with anyone.** Ensure this file is never shared or committed to version control. {% endhint %} a) Export your private key from MetaMask: * Open MetaMask, select your account icon, and go to `Account Details` <img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2Fs38JMpX9hWdURMRQbwbd%2F3d.png?alt=media&amp;token=37087880-36a9-4a15-a73d-936e48188912" alt="" width="353"> * Select `Show private key` * Enter your password. * Select `Confirm` * Select the unlock button to reveal your password. b) Install `dotenv` package ```shell npm install dotenv ``` c) Run the command `touch .env` to create an `.env` file in the root directory of your project.&#x20; ``` touch .env ``` d) Run the command `nano .env` to open the CLI editor&#x20; ```none nano .env ``` e) Add your private key to the `.env` file. ```none PRIVATE_KEY=your_exported_private_key ``` *** ### 9. Configure Hardhat for the Testnet a) Open `hardhat.config.js` in your project. b) Configure Hemi Network * Add the required modules at the top of the config file * Add the Hemi Network settings in`module.exports`. Ensure you include the network's URL, Chain ID, and the accounts array with your private key (stored in an environment variable for security). Here's an example configuration: ```javascript /** @type import('hardhat/config').HardhatUserConfig */ require('dotenv').config() require("@nomiclabs/hardhat-ethers"); module.exports = { solidity: "0.8.20", networks: { hemi: { url: "https://rpc.hemi.network/rpc", chainId: 43111, accounts: [`0x${process.env.PRIVATE_KEY}`], }, } }; ``` *** ### 10. Write a Deployment Script <img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FkmS8ywpXRRrGxrpkozB5%2F4d.png?alt=media&amp;token=5843f596-5727-49fe-a044-c86cadb15fbb" alt="" width="358"> In the `scripts` folder, create a file named `deploy.js` to write a script for deploying your contract. ```javascript const { ethers } = require("hardhat"); async function main() { const [deployer] = await ethers.getSigners(); const initialSupply = ethers.utils.parseUnits("1000", "ether"); const Token = await ethers.getContractFactory("MyToken"); const token = await Token.deploy(initialSupply); console.log("Token deployed to:", token.address); } main().catch((error) => { console.error(error); process.exit(1); }); ``` This script is deploying `MyToken` with an initial supply (customize the supply as needed). *** ### 11. Deploy the Contract ```shell npx hardhat run scripts/deploy.js --network hemi ``` {% hint style="info" %} If the deployment is successful, you will see messages indicating the token was deployed to `Token deployed to: 0x5fc5c2265E4f77E63e82f7F10FE803d04Cc53D82` {% endhint %} To view the details of your deployed contract, enter the contract address from your success message into the [Hemi Testnet explorer](https://testnet.explorer.hemi.xyz). This will provide you with information about the contract's transactions and state. Contract details we just deployed: ![](https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FGDwmWU8kARdBnLUohqZt%2F5d.png?alt=media\&token=4e1dd76e-d670-437d-931d-4b894439f254) # hVM & hBK # Using the hBK Demo App {% hint style="info" %} ## 📜 **TL;DR:** * The [Hemi Bitcoin Kit (hBK) Demo App](https://bitcoin-kit.hemi.xyz/code-editor) enables seamless interaction with Bitcoin data in an Ethereum-compatible environment. * Key features include: * Query [Bitcoin address balances](#id-1.-get-address-balance). * Retrieve the [latest block header](#id-2.-get-latest-block-header) or specific [block headers by height](#id-3.-get-block-header-by-height). * Fetch [Bitcoin transaction details](#id-4.-get-transaction-details) and [confirmation counts.](#id-5.-get-transaction-confirmations) * [List UTXOs](#id-6.-list-address-utxos) (Unspent Transaction Outputs) for a Bitcoin address. * Developers can easily integrate Bitcoin data into dApps without external services, simplifying Bitcoin-aware application development. {% endhint %} *** ## 🌐 Overview * The [Hemi Bitcoin Kit (hBK) Demo App](https://bitcoin-kit.hemi.xyz/code-editor) is a tool designed for developers to interact with Bitcoin blockchain data directly from an Ethereum-compatible environment.&#x20; * This demo app provides a user-friendly interface for querying Bitcoin information. * By abstracting the complexities of interacting with Bitcoin data, the app allows developers to seamlessly retrieve and use Bitcoin blockchain data in their dApps without needing external services.&#x20; * This makes it an effective resource for experimenting with Bitcoin-aware applications and understanding hBK’s capabilities within the Hemi ecosystem. *** ## 🏁 Prerequisites To follow along, you’ll need: * Nothing! *** ## 1. Get Address Balance Retrieves the current balance (in satoshis) of a specified Bitcoin address. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FXyzCARdaJY7DOmgEFC7a%2Fimage.png?alt=media&amp;token=b3afed93-91bd-41e4-92d3-45d13d621501" alt=""><figcaption></figcaption></figure> ``` // Sample output 514853116 ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the Bitcoin address to confirm the above output. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F7IcUD3SSsxxobmuJoqD5%2Fimage.png?alt=media&amp;token=f5252946-7e5e-4034-99f5-41624086d436" alt=""><figcaption></figcaption></figure> *** ## 2. Get Latest Block Header Returns the latest Bitcoin block header containing metadata like hash, height, and timestamp. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F42qVZ8yJgJOCBdLiDEVj%2Fimage.png?alt=media&amp;token=81fef230-84d9-432b-a116-8570103c4166" alt=""><figcaption></figcaption></figure> ``` // Sample output { "height": 3520643, "blockHash": "0x00000000002be0cd198317218f374a484bd0b3c5fe4eaf098bb5dfc97d0fb8a7", "version": 536870912, "previousBlockHash": "0x00000000000000019bebcf5e1d1dc6baf664c5859f82add17f5543b3018c82de", "merkleRoot": "0xcc50e3a3ca39e37a7ccd5867a7f2a82192c98861f28a50d94f558305672d65de", "timestamp": 1733342457, "bits": 486604799, "nonce": 1100503385 } ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the Block header to confirm the above output. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FBgFfuqSiv67J1IO2VFke%2Fimage.png?alt=media&amp;token=f6592594-06c0-405a-8ef2-07d97db5ca9c" alt=""><figcaption></figcaption></figure> *** ## 3. Get Block Header by Height Fetches the block header for a specific block height. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FqSPSLHfmTFVwxlcvzjIL%2Fimage.png?alt=media&amp;token=9c97e3e2-1371-4971-b051-2740531414d1" alt=""><figcaption></figcaption></figure> ``` // Sample output { "height": 1321358, "blockHash": "0x000000000000023bf34cd847acfcd73ad3b010f9a9434069e9bc5941cf61a1bf", "version": 536870912, "previousBlockHash": "0x00000000000002d213e95ae971060d49f93d51c3ce0ac84c452f76a45c962036", "merkleRoot": "0xd0f34249cd9b3929b56a418ab6fe965e685f1765f2dcd605ed08a1382199e506", "timestamp": 1527980035, "bits": 436539705, "nonce": 3252632758 } ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the Block header height to confirm the above output. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2FgsxAm7mVVfWgvQBfQ9vy%2Fimage.png?alt=media&amp;token=b556abfa-beb6-41b8-afcb-7fc658a775a7" alt=""><figcaption></figcaption></figure> *** ## 4. Get Transaction Details Fetches the details of a Bitcoin transaction using its transaction ID, including all of its inputs and outputs. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F2noCflfs6YgxpZnjetZQ%2Fimage.png?alt=media&amp;token=60fed774-625a-4251-ba2c-bbd08141c0ae" alt=""><figcaption></figcaption></figure> ``` // Sample output { "containingBlockHash": "0x00000000280425e8f1f2170230b3946bf4bf058d5ef9677a3f361d2fbead994f", "transactionVersion": "2", "size": "116", "vSize": "116", "lockTime": "0", "inputs": [ { "inValue": "4834", "inputTxId": "0xe7b262c5ead12343f1c5734b65848fbe19362907fafcd9d07c999c9aa2ec79f0", "sourceIndex": "11689", "scriptSig": "0x", "sequence": "2", "fullScriptSigLength": "0", "containsFullScriptSig": false } ], "outputs": [], "totalInputs": "1", "totalOutputs": "0", "containsAllInputs": true, "containsAllOutputs": true } ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the transaction ID to confirm the above output. *** ## 5. Get Transaction Confirmations Retrieves the number of confirmations for a specific transaction based on its transaction ID. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2F8caf53xxPmwktp4eDAEc%2Fimage.png?alt=media&amp;token=089214e5-0e10-4044-b33c-8cab06531db6" alt=""><figcaption></figcaption></figure> ``` // Sample output 653358 ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the transaction ID to confirm the above output. *** ## 6. List Address UTXOs Returns the UTXOs (Unspent Transaction Outputs) of a Bitcoin address, with pagination support. <figure><img src="https://3063395300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBrGAUP9hcnvqWQvHZlN0%2Fuploads%2Fw6ElVvWnRoSquEaD4NDj%2Fimage.png?alt=media&amp;token=8944c199-4a5a-4903-9025-49c7fb4c3ab9" alt=""><figcaption></figcaption></figure> ``` // Sample output [ { "txId": "0xbd7b64df58115ca4e9c273b0bae477de3607154412a517dc31fdf5cd9f46e048", "index": "0", "value": "197506250" }, { "txId": "0x452142a3dc4c9231ef8fd26674fa31ad55dfac5e880183d4b9d7452dd84e8b4f", "index": "0", "value": "199244750" }, ... ] ``` **Verifying our Output** Navigate to <https://blockstream.info> and input the Bitcoin address to confirm the above output. # Get Bitcoin Balance with Remix {% hint style="info" %} ## 📜 **TL;DR:** * In this section, we’ll deploy a small contract in[ Remix IDE](https://remix.ethereum.org) with a single method which calls the Bitcoin Balance precompile and returns the balance as a uint256.&#x20; * We recommend you use Solidity version **0.8.25 or later**, but previous versions are also expected to work. {% endhint %} *** ## 🏁 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: ```solidity 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. # Introduction {% hint style="info" %} ### 📜 **TL;DR:** * The **Hemi Virtual Machine (hVM)** and **Hemi Bitcoin Kit (hBK)** power Hemi’s Bitcoin interoperability. * **hVM** is an indexed Bitcoin full node directly accessible inside the EVM. * **hBK** is a smart contract library running on Hemi that utilizes hVM and makes Hemi’s Bitcoin awareness easier to use. * hVM’s precompile calls and hBK's functions are subject to change in future versions of the Hemi testnet. * **Latest hBK release on Hemi:** [0x7007dd1C09527B92AEcd8Ae6570B73d09E0B8F12](https://explorer.hemi.xyz/address/0x7007dd1C09527B92AEcd8Ae6570B73d09E0B8F12) * **Latest hBK release on Hemi testnet:** [0xeC9fa5daC1118963933e1A675a4EEA0009b7f215](https://testnet.explorer
프로젝트로 돌아가기