Sunday, 6 March 2022

Smart Contract Lab One

Get balance of an account

Use this tool. Look like it works for only test network.

Go codes to convert hex to weis and eth

package main

import (
    "fmt"
    "strconv"
)

//convert hex weis to weis(deci) and eth
func main() {
	//weis in hex format
    hex := "ddf7d27685de8f1"
    //weis for one eth
    var weis float64 = 1000000000000000000
    value, err := strconv.ParseInt(hex, 16, 64)
    if err != nil {
        fmt.Printf("Conversion failed: %s\n", err)
    } else {
        //convert to float64 before do division
        var val = float64(value)
        var ans = val / weis
        fmt.Printf("%d wei\n", value)
        fmt.Printf("%f eth\n", ans)
    }
}

Show testnet in Metamask

By default, you can not see testnet in Metamask. To see testnet, need to enable it in Metamask setting.

Development tools

Hardhat

It is a development environment to compile, deploy, test and debug Ethereum software.

npm install --save-dev hardhat

Ethers.js

It wraps standardJSON-RPC methods with more user friendly method. This library makes it easier to interact and make requests to Ethereum.

npm install --save-dev @nomiclabs/hardhat-ethers ethers

Connect hardhat project with Metamask and Alchemy

Create a .env file

API_URL = "https://eth-ropsten.alchemyapi.io/v2/your_api_key"
PRIVATE_KEY = "your_metamask_private_key"

Verify contract on Etherscan

npx hardhat verify --network ropsten 0x4827E8a9858f73d061262fB3BAaa4FA8461F7904 'Hello World!'

See codes

See codes

No comments:

Post a Comment