{
  "slug": "Deploy-smart-contract-and-connect-with-go-Ethereum-935f4290249f",
  "title": "Deploy smart contract and connect with go Ethereum",
  "subtitle": "In this article, I am going to deploy a smart contract to go and go-Ethereum on the local block chain.",
  "excerpt": "In this article, I am going to deploy a smart contract to go and go-Ethereum on the local block chain.",
  "date": "2023-12-19",
  "tags": [
    "Go"
  ],
  "readingTime": "9 min",
  "url": "https://medium.com/@mobinshaterian/deploy-smart-contract-and-connect-with-go-ethereum-935f4290249f",
  "hero": "https://cdn-images-1.medium.com/max/800/1*zbZbzJsILxLMVURK608fOg.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Deploy smart contract and connect with go Ethereum"
    },
    {
      "type": "paragraph",
      "html": "In this article, I am going to deploy a smart contract to go and go-Ethereum on the local block chain."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-ethereum"
    },
    {
      "type": "paragraph",
      "html": "Go, a programming language developed by Google, is one of the languages used to build the Ethereum network, and Go Ethereum is <strong>the interface used to write, edit and manipulate the Go code for the blockchain</strong>. Aside from Go, the Ethereum blockchain and protocol are also heavily based on Python and C++."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "USDT smart contract"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Proof-of-work (PoW)"
    },
    {
      "type": "paragraph",
      "html": "The Ethereum network began by using a consensus mechanism that involved <a href=\"https://ethereum.org/en/developers/docs/consensus-mechanisms/pow/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Proof-of-work (PoW)</strong></a>. This allowed the nodes of the Ethereum network to agree on the state of all information recorded on the Ethereum blockchain and prevented certain kinds of economic attacks. However, Ethereum switched off proof-of-work in 2022 and started using <a href=\"https://ethereum.org/en/developers/docs/consensus-mechanisms/pos/\" target=\"_blank\" rel=\"noreferrer noopener\">proof-of-stake</a> instead."
    },
    {
      "type": "paragraph",
      "html": "Proof-of-work has now been deprecated. Ethereum no longer uses proof-of-work as part of its consensus mechanism. Instead, it uses proof-of-stake. Read more on <a href=\"https://ethereum.org/en/developers/docs/consensus-mechanisms/pos/\" target=\"_blank\" rel=\"noreferrer noopener\">proof-of-stake</a> and <a href=\"https://ethereum.org/en/staking/\" target=\"_blank\" rel=\"noreferrer noopener\">staking</a>."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Proof-of-stake (PoS)"
    },
    {
      "type": "paragraph",
      "html": "Proof-of-stake (PoS) underlies Ethereum’s <a href=\"https://ethereum.org/en/developers/docs/consensus-mechanisms/\" target=\"_blank\" rel=\"noreferrer noopener\">consensus mechanism</a>. Ethereum switched on its proof-of-stake mechanism in 2022 because it is more secure, less energy-intensive, and better for implementing new scaling solutions compared to the previous <a href=\"https://ethereum.org/en/developers/docs/consensus-mechanisms/pow/\" target=\"_blank\" rel=\"noreferrer noopener\">proof-of-work</a> architecture."
    },
    {
      "type": "paragraph",
      "html": "Proof-of-stake is a way to prove that validators have put something of value into the network that can be destroyed if they act dishonestly. In Ethereum’s proof-of-stake, validators explicitly stake capital in the form of ETH into a smart contract on Ethereum. The validator is then responsible for checking that new blocks propagated over the network are valid and occasionally creating and propagating new blocks themselves. If they try to defraud the network (for example by proposing multiple blocks when they ought to send one or sending conflicting attestations), some or all of their staked ETH can be destroyed."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Get project"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "git clone\nhttps://github.com/owanhunte/ethereum-solidity-course-updated-code.git\ncd lottery"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "node -vv18.19.0npm init"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "lottery.sol file"
    },
    {
      "type": "code",
      "lang": "solidity",
      "code": "pragma solidity >=0.5.0 <0.9.0;\ncontract Lottery {\n  address public manager;\n  address payable[] public players;\n  constructor() {\n    manager = msg.sender;\n  }\n  function enter() public payable {\n    require(msg.value >=.01 ether,\n    \"A minimum payment of .01 ether must be sent to enter the lottery\");\n    players.push(payable(msg.sender));\n  }\n  function random() private view returns (uint256) {\n    return uint256(keccak256(abi.encodePacked(block.prevrandao, block.number, players)));\n  }\n  function pickWinner() public onlyOwner {\n    uint256 index = random() % players.length;\n    address contractAddress = address(this);\n    players[index].transfer(contractAddress.balance);\n    players = new address payable[](0);\n  }\n  function getPlayers() public view returns (address payable[] memory) {\n    return players;\n  }\n  modifier onlyOwner() {\n    require(msg.sender == manager, \"Only owner can call this function.\");\n    _;\n  }\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install dependency"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "npm install solc\nnpm install @truffle/hdwallet-provider\nnpm install --save mocha ganache-cli web3\nnpm update\nnpm install solc@0.8.23 //deploy on infura"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "We need to change in package.json"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"scripts\": {    \"test\": \"mocha\"  },"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Run the tests"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "npm run test"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*PxulUK_zh5GqMdBn9YSUTw.png",
      "alt": "Deploy smart contract and connect with go Ethereum",
      "caption": "",
      "width": 337,
      "height": 135
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Deploy Contract"
    },
    {
      "type": "paragraph",
      "html": "set&nbsp;.env file"
    },
    {
      "type": "code",
      "lang": "dotenv",
      "code": "A\nC\nC\nO\nU\nN\nT_\nM\nN\nE\nM\nO\nN\nI\nC=\"neutral ... \"\nG\nO\nE\nR\nL\nI_\nE\nN\nD\nP\nO\nI\nN\nT=\"https://sepolia.infura.io/v3/code\""
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Run the deploy.js"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "node deploy.js"
    },
    {
      "type": "paragraph",
      "html": "Result"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Attempting to deploy from account 0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935Contract deployed to\n0xc7e367A2EaE77148459b048ba3a3f6288EB82832"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "TXhash:"
    },
    {
      "type": "paragraph",
      "html": "0xe87abc8cf10bde4395220b2c2eed29e378be3f425719354dc1c65ced9a7dd03d"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-Ethereum"
    },
    {
      "type": "paragraph",
      "html": "popularly referred to as Geth, is the official Ethereum client for building decentralized applications using the Go programming language. Geth is one of the preferred alternatives for running, setting up nodes, and interacting with Ethereum blockchains due to its ease of use."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go get github.com/ethereum/go-ethereum"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package main\nimport (\n\"context\"\n\"fmt\"\n\"log\"\n\"github.com/ethereum/go-ethereum/common\"\n\"github.com/ethereum/go-ethereum/ethclient\"\n)\nvar (contractAddress = common.HexToAddress(\"0xc7e367A2EaE77148459b048ba3a3f6288EB82832\") myAddress =\ncommon.HexToAddress(\"0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935\")\n)\nfunc main() {\n    client, err := ethclient.Dial(\"https://sepolia.infura.io/v3/code\") if err != nil {\n        log.Fatal(err)\n    }\n    networkID, err := client.NetworkID(context.Background()) if err != nil {\n        log.Fatal(err)\n    }\n    fmt.Println(networkID) balance, err := client.BalanceAt(context.Background(), contractAddress,\n    nil) if err != nil {\n        log.Fatal(err)\n    }\n    fmt.Println(balance) myBalance, err := client.BalanceAt(context.Background(), myAddress, nil) if\n    err != nil {\n        log.Fatal(err)\n    }\n    fmt.Println(myBalance)\n}"
    },
    {
      "type": "paragraph",
      "html": "You can connect to the <a href=\"https://goethereumbook.org/GLOSSARY.html#infura\" target=\"_blank\" rel=\"noreferrer noopener\">infura</a> gateway if you don’t have an existing client. <a href=\"https://goethereumbook.org/GLOSSARY.html#infura\" target=\"_blank\" rel=\"noreferrer noopener\">Infura</a> manages a bunch of <a href=\"https://goethereumbook.org/GLOSSARY.html#ethereum\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum</a> [<a href=\"https://goethereumbook.org/GLOSSARY.html#geth\" target=\"_blank\" rel=\"noreferrer noopener\">geth</a> and <a href=\"https://goethereumbook.org/GLOSSARY.html#parity\" target=\"_blank\" rel=\"noreferrer noopener\">parity</a>] nodes that are secure, reliable, scalable and lowers the barrier to entry for newcomers when it comes to plugging into the <a href=\"https://goethereumbook.org/GLOSSARY.html#ethereum\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum</a> network."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "‍‍‍client, err := ethclient.Dial(\"\nhttps://mainnet.infura.io\")"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Using Ganache"
    },
    {
      "type": "paragraph",
      "html": "Ganache is an Ethereum simulator that makes developing Ethereum applications faster, easier, and safer. It includes all popular RPC functions and features (like events) and can be run deterministically to make development a breeze."
    },
    {
      "type": "paragraph",
      "html": "You must first install <a href=\"https://nodejs.org/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js</a> &gt;= v16.0.0 and npm &gt;= 7.10.0."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\nnpm install -g ganache-cli"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "ganache"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*zbZbzJsILxLMVURK608fOg.png",
      "alt": "Deploy smart contract and connect with go Ethereum",
      "caption": "",
      "width": 642,
      "height": 477
    },
    {
      "type": "code",
      "lang": "text",
      "code": "ganache-cli"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Connect to Ganache"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "client, err := ethclient.Dial(\"\nhttp://localhost:8545\")if err != nil {  log.Fatal(err)}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "ganache-cli -m \"much repair shock carbon improve miss forget sock include bullet interest solution\""
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Download UI Ganache"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "https://trufflesuite.com/ganache/"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "wget\nhttps://github.com/trufflesuite/ganache-ui/releases/download/v2.7.1/ganache-2.7.1-linux-x86_64.AppImage\nchmod u+x <AppImage File>./exampleName.AppImagecd ~; ./ganache-2.7.1-linux-x86_64.AppImage"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "dlopen(): error loading libfuse.so.2sudo apt install libfuse2"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Golang codes"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "address = \"http://127.0.0.1:7545\" client, err := ethclient.Dial(address) if err != nil {\nlog.Fatal(err) }"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Deploy on Ganache-cli"
    },
    {
      "type": "code",
      "lang": "properties",
      "code": "GOERLI_ENDPOINT=\"\nhttp://localhost:8545\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "ganache-cli -m \"neutral ...\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "node deploy.js"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Attempting to deploy from account 0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935[\n'0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935']Contract deployed to\n0xf6e2551d2AFaFBA200D3803aE6a72B515Fa9f825"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "In Ganache Terminal"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Transaction: 0x40fb2033ec7b4c8a9c95945d7f3b83f9b73405eb43723cc7e4729d12a225b673  Contract created:\n0xf6e2551d2afafba200d3803ae6a72b515fa9f825  Gas usage: 646841  Block number: 1  Block time: Mon Dec\n18 2023 16:58:05 GMT"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Account Balances"
    },
    {
      "type": "paragraph",
      "html": "Reading the balance of an account is pretty simple; call the <code>BalanceAt</code> method of the client passing it the account address and optional block number. Setting <code>nil</code> as the block number will return the latest balance."
    },
    {
      "type": "code",
      "lang": "javascript",
      "code": "var (contractAddress = common.HexToAddress(\"0xf6e2551d2afafba200d3803ae6a72b515fa9f825\") myAddress =\ncommon.HexToAddress(\"0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935\"))"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "account := common.HexToAddress(myAddress)balance, err := client.BalanceAt(context.Background(),\naccount, nil)if err != nil {  log.Fatal(err)}fmt.Println(balance) // 999997816911625000000"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Get balance on blockchain 1"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "blockNumber := big.NewInt(1) balance, err = client.BalanceAt(context.Background(), myAddress,\nblockNumber) if err != nil {  log.Fatal(err) }"
    },
    {
      "type": "paragraph",
      "html": "Because we’re dealing with big numbers we’ll have to import the native Go math and math/big packages"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Pending balance"
    },
    {
      "type": "paragraph",
      "html": "‍‍‍Sometimes you’ll want to know what the pending account balance is, for example after submitting or waiting for a transaction to be confirmed. The client provides a similar method to <code>BalanceAt</code> called <code>PendingBalanceAt</code> which accepts the account address as a parameter."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "pendingBalance, err := client.PendingBalanceAt(context.Background(), myAddress)\nfmt.Println(pendingBalance) // 25729324269165216042"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Block header"
    },
    {
      "type": "paragraph",
      "html": "You can call the client’s <code>HeaderByNumber</code> to return header information about a block. It'll return the latest block header if you pass <code>nil</code>."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "header, err := client.HeaderByNumber(context.Background(), nil)if err != nil {\nlog.Fatal(err)}fmt.Println(header.Number.String()) // 1"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Full block"
    },
    {
      "type": "paragraph",
      "html": "Call the client’s <code>BlockByNumber</code> method to get the full block. You can read all the contents and metadata of the block such as block number, block timestamp, block hash, block <a href=\"https://goethereumbook.org/GLOSSARY.html#difficulty\" target=\"_blank\" rel=\"noreferrer noopener\">difficulty</a>, as well as the list of transactions and much much more."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Count Block"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "count, err := client.TransactionCount(context.Background(), block.Hash()) if err != nil {\nlog.Fatal(err) } fmt.Println(count)"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Transaction data"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "for _, tx := range block.Transactions() {  fmt.Println(tx.Hash().Hex())\nfmt.Println(tx.Value().String())      fmt.Println(tx.Gas())\nfmt.Println(tx.GasPrice().Uint64())   fmt.Println(tx.Nonce())              }"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "___________________________Transaction0x40fb2033ec7b4c8a9c95945d7f3b83f9b73405eb43723cc7e4729d12a225\nb673064684145000000000"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Smart Contract Compilation & ABI"
    },
    {
      "type": "paragraph",
      "html": "Solc is available as a snapcraft package for Ubuntu."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo snap install solc --edge"
    },
    {
      "type": "paragraph",
      "html": "‍‍‍‍‍‍We also need to install a tool called <code>abigen</code> for generating the ABI from a solidity <a href=\"https://goethereumbook.org/GLOSSARY.html#smart-contract\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract</a>."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go get -u github.com/ethereum/go-ethereum\ncd $GOPATH/src/github.com/ethereum/go-ethereum/make\nmake\ndevtools"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Abigen"
    },
    {
      "type": "paragraph",
      "html": "Abigen is a binding-generator for easily interacting with Ethereum using Go. Abigen creates easy-to-use, type-safe Go packages from Ethereum smart contract definitions known as ABIs. This abstracts away a lot of the complexity of handling smart contract deployment and interaction in Go native applications such as encoding and decoding smart contracts into EVM bytecode. Abigen comes bundled with Geth. A full Geth installation includes the abigen binary. Abigen can also be built independently by navigating to go-ethereum/cmd/abigen and running go build, or equivalently:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "$ cd $GOPATH/src/github.com/ethereum/go-ethereum$ go build ./cmd/abigen"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Use smart contract in Golang"
    },
    {
      "type": "paragraph",
      "html": "Make ABI from contract."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "solc --abi Lottery.sol"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "======= Lottery.sol:Lottery =======Contract JSON\nABI[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[],\"name\":\"enter\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"internalType\":\"address payable[]\",\"name\":\"\",\"type\":\"address[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"pickWinner\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"internalType\":\"address payable\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]"
    },
    {
      "type": "paragraph",
      "html": "Or build in file&nbsp;:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "solc --abi Storage.sol -o build"
    },
    {
      "type": "paragraph",
      "html": "The contract binding can then be generated by passing the ABI to abigen as follows:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "abigen --abi ./build/Lottery.abi --pkg main --type Lottery --out Lottery.go"
    },
    {
      "type": "paragraph",
      "html": "In order to deploy a <a href=\"https://goethereumbook.org/GLOSSARY.html#smart-contract\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract</a> from <a href=\"https://goethereumbook.org/GLOSSARY.html#go\" target=\"_blank\" rel=\"noreferrer noopener\">Go</a>, we also need to compile the solidity <a href=\"https://goethereumbook.org/GLOSSARY.html#smart-contract\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract</a> to <a href=\"https://goethereumbook.org/GLOSSARY.html#evm\" target=\"_blank\" rel=\"noreferrer noopener\">EVM</a> bytecode. The <a href=\"https://goethereumbook.org/GLOSSARY.html#evm\" target=\"_blank\" rel=\"noreferrer noopener\">EVM</a> bytecode is what will be sent in the data field of the transaction. The bin file is required for generating the deploy methods on the <a href=\"https://goethereumbook.org/GLOSSARY.html#go\" target=\"_blank\" rel=\"noreferrer noopener\">Go</a> contract file."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "solc --bin Lottery.sol"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "======= Lottery.sol:Lottery\n=======Binary:608060405234801561000f575f80fd5b50335f806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610a7f8061005c5f395ff3fe608060405260043610610049575f3560e01c8063481c6a751461004d5780635d495aea146100775780638b5b9ccc1461008d578063e97dcb62146100b7578063f71d96cb146100c1575b5f80fd5b348015610058575f80fd5b506100616100fd565b60405161006e919061054c565b60405180910390f35b348015610082575f80fd5b5061008b610120565b005b348015610098575f80fd5b506100a16102c5565b6040516100ae919061062d565b60405180910390f35b6100bf610350565b005b3480156100cc575f80fd5b506100e760048036038101906100e29190610684565b6103fc565b6040516100f491906106be565b60405180910390f35b5f8054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f8054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101ad576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101a490610757565b60405180910390fd5b5f6001805490506101bc610437565b6101c691906107a2565b90505f309050600182815481106101e0576101df6107d2565b5b905f5260205f20015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc8273ffffffffffffffffffffffffffffffffffffffff163190811502906040515f60405180830381858888f19350505050158015610261573d5f803e3d5ffd5b505f67ffffffffffffffff81111561027c5761027b6107ff565b5b6040519080825280602002602001820160405280156102aa5781602001602082028036833780820191505090505b50600190805190602001906102c092919061046b565b505050565b6060600180548060200260200160405190810160405280929190818152602001828054801561034657602002820191905f5260205f20905b815f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190600101908083116102fd575b5050505050905090565b662386f26fc1000034101561039a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103919061089c565b60405180910390fd5b600133908060018154018082558091505060019003905f5260205f20015f9091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b6001818154811061040b575f80fd5b905f5260205f20015f915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f4443600160405160200161044e939291906109ea565b604051602081830303815290604052805190602001205f1c905090565b828054828255905f5260205f209081019282156104e1579160200282015b828111156104e0578251825f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555091602001919060010190610489565b5b5090506104ee91906104f2565b5090565b5b80821115610509575f815f9055506001016104f3565b5090565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6105368261050d565b9050919050565b6105468161052c565b82525050565b5f60208201905061055f5f83018461053d565b92915050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f6105988261050d565b9050919050565b6105a88161058e565b82525050565b5f6105b9838361059f565b60208301905092915050565b5f602082019050919050565b5f6105db82610565565b6105e5818561056f565b93506105f08361057f565b805f5b8381101561062057815161060788826105ae565b9750610612836105c5565b9250506001810190506105f3565b5085935050505092915050565b5f6020820190508181035f83015261064581846105d1565b905092915050565b5f80fd5b5f819050919050565b61066381610651565b811461066d575f80fd5b50565b5f8135905061067e8161065a565b92915050565b5f602082840312156106995761069861064d565b5b5f6106a684828501610670565b91505092915050565b6106b88161058e565b82525050565b5f6020820190506106d15f8301846106af565b92915050565b5f82825260208201905092915050565b7f4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f5f8201527f6e2e000000000000000000000000000000000000000000000000000000000000602082015250565b5f6107416022836106d7565b915061074c826106e7565b604082019050919050565b5f6020820190508181035f83015261076e81610735565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f6107ac82610651565b91506107b783610651565b9250826107c7576107c6610775565b5b828206905092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f41206d696e696d756d207061796d656e74206f66202e3031206574686572206d5f8201527f7573742062652073656e7420746f20656e74657220746865206c6f7474657279602082015250565b5f6108866040836106d7565b91506108918261082c565b604082019050919050565b5f6020820190508181035f8301526108b38161087a565b9050919050565b5f819050919050565b6108d46108cf82610651565b6108ba565b82525050565b5f81549050919050565b5f81905092915050565b5f819050815f5260205f209050919050565b6109098161058e565b82525050565b5f61091a8383610900565b60208301905092915050565b5f815f1c9050919050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61096261095d83610926565b610931565b9050919050565b5f6109748254610950565b9050919050565b5f600182019050919050565b5f610991826108da565b61099b81856108e4565b93506109a6836108ee565b805f5b838110156109dd576109ba82610969565b6109c4888261090f565b97506109cf8361097b565b9250506001810190506109a9565b5085935050505092915050565b5f6109f582866108c3565b602082019150610a0582856108c3565b602082019150610a158284610987565b915081905094935050505056fea264697066735822122002a23570c68d06ef9b64bc5066fb949367588412ac467a89d0e3d8098007384864736f6c637829302e382e32342d646576656c6f702e323032332e31322e31352b636f6d6d69742e3162356336663636005a"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Deploying contracts to Ethereum"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "solc --bin Lottery.sol -o Lottery.bin"
    },
    {
      "type": "paragraph",
      "html": "Then abigen can be run again, this time passing Storage.bin:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "abigen --abi ./build/Lottery.abi --pkg main --type Lottery --out Lottery.go --bin\n./Lottery.bin/Lottery.bin"
    },
    {
      "type": "paragraph",
      "html": "The new DeployLottery() function can be used to deploy the contract to an Ethereum testnet from a Go application. To do this requires incorporating the bindings into a Go application that also handles account management, authorization and Ethereum backend to deploy the contract through. Specifically, this requires:"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "A running Geth node connected to an Ethereum testnet",
        "An account in the keystore prefunded with enough ETH to cover gas costs for deploying and interacting with the contract"
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Use Private key in Ganache-cli"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "ganache-cli -m \"neutral ...\""
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*1E49NNCrl20o0Ltqp4m60w.png",
      "alt": "Deploy smart contract and connect with go Ethereum",
      "caption": "",
      "width": 648,
      "height": 253
    },
    {
      "type": "code",
      "lang": "go",
      "code": "address := \"http://localhost:8545\" // ganache cli client, err := ethclient.Dial(address) if err !=\nnil {  log.Fatal(err) } privateKey, err :=\ncrypto.HexToECDSA(\"872e066455dcfb1e38e1c6109f3522f2d2344e3b58fbfbe0b47803c971784760\") if err != nil\n{  log.Fatal(err) } nonce, err := client.PendingNonceAt(context.Background(),\ncrypto.PubkeyToAddress(privateKey.PublicKey)) if err != nil {  log.Fatal(err) } gasPrice, err :=\nclient.SuggestGasPrice(context.Background()) if err != nil {  log.Fatal(err) }\nfmt.Println(\"gasPrice: \", gasPrice) chainID, err := client.ChainID(context.Background())\nfmt.Println(\"networkID :\", chainID) auth, err := bind.NewKeyedTransactorWithChainID(privateKey,\nchainID) if err != nil {  fmt.Println(err) } auth.Nonce = big.NewInt(int64(nonce)) auth.Value =\nbig.NewInt(0)      // in wei auth.GasLimit = uint64(300000) // in units auth.GasPrice = gasPrice\ntxAddress, tx, instance, err := contracts.DeployLottery(auth, client) if err != nil {\nfmt.Println(\"DeployLottery\")  log.Fatal(err.Error()) } fmt.Println(instance, address, tx, instance)\nfmt.Println(txAddress.Hex()) // 0x147B8eb97fD247D06C4006D269c90C1908Fb5D54\nfmt.Println(tx.Hash().Hex())"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "fmt.Println(txAddress.Hex())\nfmt.Println(tx.Hash().Hex())0xf6e2551d2AFaFBA200D3803aE6a72B515Fa9f8250x81ea1d8c480a316f5721edf31a3c1f39f3827b5cc75e9b6fcad928c3e261dd08"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Transaction: 0x81ea1d8c480a316f5721edf31a3c1f39f3827b5cc75e9b6fcad928c3e261dd08  Contract created:\n0xf6e2551d2afafba200d3803ae6a72b515fa9f825  Gas usage: 300000  Block number: 1  Block time: Tue Dec\n19 2023 14:10:57   Runtime error: code size to deposit exceeds maximum code size"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Loading a Smart Contract"
    },
    {
      "type": "paragraph",
      "html": "Once you’ve compiled your <a href=\"https://goethereumbook.org/GLOSSARY.html#smart-contract\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract</a>’s ABI to a <a href=\"https://goethereumbook.org/GLOSSARY.html#go\" target=\"_blank\" rel=\"noreferrer noopener\">Go</a> package using the <code>abigen</code> tool, the next step is to call the \"New\" method, which is in the format <code>New&lt;ContractName&gt;</code>, so in our example if you recall it's going to be <em>NewStore</em>. This initializer method takes in the address of the <a href=\"https://goethereumbook.org/GLOSSARY.html#smart-contract\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract</a> and returns a contract instance that you can start interact with it."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Transaction: 0x25b79f942ebccfae067bb94a0e76282e7c1c2db825e5cbee18da1bb487596e06"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "address := \"http://localhost:8545\" // ganache cli client, err := ethclient.Dial(address) if err !=\nnil {  log.Fatal(err) } contractAddress :=\ncommon.HexToAddress(\"0x25b79f942ebccfae067bb94a0e76282e7c1c2db825e5cbee18da1bb487596e06\") instance,\nerr := contracts.NewLottery(contractAddress, client) if err != nil {  log.Fatal(err) }"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Writing to a Smart Contract"
    },
    {
      "type": "paragraph",
      "html": "Contract transaction:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Transaction: 0x25b79f942ebccfae067bb94a0e76282e7c1c2db825e5cbee18da1bb487596e06"
    },
    {
      "type": "paragraph",
      "html": "Use privates keys in ganache:"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*GRvJGyAb-ysyl2rYx1zUAg.png",
      "alt": "Deploy smart contract and connect with go Ethereum",
      "caption": "",
      "width": 648,
      "height": 253
    },
    {
      "type": "code",
      "lang": "go",
      "code": "address := \"http://localhost:8545\" // ganache cli client, err := ethclient.Dial(address) if err !=\nnil {  log.Fatal(err) } privateKey, err :=\ncrypto.HexToECDSA(\"872e066455dcfb1e38e1c6109f3522f2d2344e3b58fbfbe0b47803c971784760\") if err != nil\n{  log.Fatal(err) } publicKey := privateKey.Public() publicKeyECDSA, ok :=\npublicKey.(*ecdsa.PublicKey) if !ok {  log.Fatal(\"error casting public key to ECDSA\") } fromAddress\n:= crypto.PubkeyToAddress(*publicKeyECDSA) nonce, err := client.PendingNonceAt(context.Background(),\nfromAddress) if err != nil {  log.Fatal(err) } gasPrice, err :=\nclient.SuggestGasPrice(context.Background()) if err != nil {  log.Fatal(err) } fmt.Println(gasPrice)\nchainID, err := client.ChainID(context.Background()) if err != nil {  log.Fatal(err) }\nfmt.Println(\"networkID :\", chainID) auth, err := bind.NewKeyedTransactorWithChainID(privateKey,\nchainID) if err != nil {  log.Fatal(err) } auth.Nonce = big.NewInt(int64(nonce)) auth.Value =\nbig.NewInt(0)      // in wei auth.GasLimit = uint64(3000000) // in units auth.GasPrice = gasPrice\ncontractAddress :=\ncommon.HexToAddress(\"0x25b79f942ebccfae067bb94a0e76282e7c1c2db825e5cbee18da1bb487596e06\") balance,\nerr := client.BalanceAt(context.Background(), contractAddress, nil) if err != nil {  log.Fatal(err)\n} fmt.Println(\"balance : \", balance) myAddress :=\ncommon.HexToAddress(\"0x6e06Dc176Ee86DCb3C428a42Ed9AAeD9c0Eb1935\") myBalance, err :=\nclient.BalanceAt(context.Background(), myAddress, nil) if err != nil {  log.Fatal(err) }\nfmt.Println(\"myBalance : \", myBalance) instance, err := contracts.NewLottery(contractAddress,\nclient) if err != nil {  fmt.Println(\"NewLottery\")  log.Fatal(err) } tx, err := instance.Enter(auth)\nif err != nil {  fmt.Println(\"Enter\")  log.Fatal(err) }"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go run *.go2000000000networkID : 1337balance :  0myBalance :  999997000000000000000"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Transaction: 0xa531650fe246012062408f17e4d56ace4622a4680ac799279262c60666201fe5  Gas usage: 21064\nBlock number: 6  Block time: Tue Dec 19 2023 15:18:07 GMT"
    }
  ]
}
