0

Im looking for a way to call a contract that is deployed on the ethereum main network using Go (or JS/anything). From what i can see the foolproof way is to do so over json RPC but i would love to hear of gracefull solutions if anyone has them.

0

Here's a full example of how to query a smart contract using Go and the go-ethereum package.

MyContract.sol

pragma solidity ^0.4.24;

contract MyContract {
  string public foo;

  constructor() public {
    foo = "bar";
  }
}

Compilation

solc --abi MyContract.sol | awk '/JSON ABI/{x=1;next}x' > MyContract.abi
solc --bin MyContract.sol | awk '/Binary:/{x=1;next}x' > MyContract.bin
abigen --bin=MyContract.bin --abi=MyContract.abi --pkg=store --out=MyContract.go

main.go

package main

import (
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"

    store "./contracts" // for demo
)

func main() {
    client, err := ethclient.Dial("https://mainnet.infura.io")
    if err != nil {
        log.Fatal(err)
    }

    address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
    instance, err := store.NewMyContract(address, client)
    if err != nil {
        log.Fatal(err)
    }

    version, err := instance.Foo(nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(foo) // "bar"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.