Its a bit of a long question... I am using Go to deploy a Smart Contract using the following code:
client, err := ethclient.Dial("http://localhost:8545")
if err != nil {
errorhandler.HandleErr(err, 1)
}
privateKey, err := crypto.HexToECDSA(hexKey)
if err != nil {
errorhandler.HandleErr(err, 2)
}
publicKey := privateKey.Public()
publicKeyEcdsa, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
errorhandler.HandleErr(errors.New("pub key not of type ecdsa.PublicKey"), 3)
}
fromAddr := crypto.PubkeyToAddress(*publicKeyEcdsa)
nonce, err := client.PendingNonceAt(context.Background(), fromAddr)
if err != nil {
errorhandler.HandleErr(err, 4)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
errorhandler.HandleErr(err, 5)
}
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.GasLimit = uint64(4700000)
auth.GasPrice = gasPrice
address, tx, instance, err := eddns.DeployEddns(auth, client)
if err != nil {
errorhandler.HandleErr(err, 6)
}
_ = instance
return address.Hex(), tx.Hash(), auth
The above code completes successfully and I get a contract address (as a hex) and transaction hash.
I then use the hex address returned by the above function to load the contract like this:
client, err := ethclient.Dial(rawurl)
if err != nil {
errorhandler.HandleErr(err, 7)
}
address := common.HexToAddress(hexAddr)
instance, err := eddns.NewEddns(address, client)
if err != nil {
errorhandler.HandleErr(err, 8)
}
return instance
The above code complete successfully as well. No errors are thrown and I get the instance returned.
I then try to query my smart contract like this:
ip, err := contract.GetIp(nil, []byte(domain), tld, rType)
This is a function call and not a transaction so it should work. But I get an error thrown which is No contract code at give address
I am deploying to a private Ethereum network which I bring up using docker-compose up
. The docker-compose.yml
file is a bit long but I will show the Dockerfile of the node which I connect to at localhost:8545
:
FROM ubuntu:xenial
RUN apt-get update \
&& apt-get install -y wget software-properties-common \
&& rm -rf /var/lib/apt/lists/*
WORKDIR "/root"
RUN add-apt-repository -y ppa:ethereum/ethereum
ARG binary
RUN apt-get update \
&& apt-get install -y ethereum
ARG password
ARG privatekey
RUN echo $password > ~/.accountpassword
RUN echo $privatekey > ~/.privatekey
ADD ./genesis.json ./genesis.json
RUN geth init genesis.json
RUN geth account import --password ~/.accountpassword ~/.privatekey
ENV address=""
ENV bootnodeId=""
ENV bootnodeIp=""
CMD exec geth --bootnodes "enode://$bootnodeId@$bootnodeIp:30301" --networkid "500" --verbosity=4 --rpc --rpcaddr "0.0.0.0" --rpcapi "eth,web3,net,admin,debug,db,personal" --rpccorsdomain "*" --syncmode="full"
EXPOSE 8545
EXPOSE 30303
If the entire docker-compose.yml
file is needed I can provide it.
This works perfectly when using the Ganache test network. No errors are thrown and I can successfully query the smart contract. Its when I try to interact with the private Ethereum network, I run into problems.
Thanks.
contract.GetIp()
where iscontract
being set? – Miguel Mota Mar 25 at 21:18