How to interact with smart contract in GO

0 votes
I’m modifying go-ethereum for a particular function in my application. I am writing a contract to give rewards to miners if a certain condition is met. For this, I need to change the go-ethereum to call a function that gives out reward to miners.  So I wanted to know how to build a Raw transaction in Go to call the reward function.  Can someone help me out with this?
Jul 26, 2018 in Blockchain by slayer
• 29,370 points
2,784 views

1 answer to this question.

0 votes

I am providing an example. Modify it according to your requirement.

store.sol

pragma solidity ^0.4.24;

contract Store {

  event ItemSet(bytes32 key, bytes32 value);

  string public version;

  mapping (bytes32 => bytes32) public items;

  constructor(string _version) public {

    version = _version;

  }

  function setItem(bytes32 key, bytes32 value) external {

    items[key] = value;

    emit ItemSet(key, value);

  }

}

main.go

package main

import (

    "context"

    "crypto/ecdsa"

    "fmt"

    "log"

    "math/big"

    "github.com/ethereum/go-ethereum/accounts/abi/bind"

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

    "github.com/ethereum/go-ethereum/crypto"

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

    store "./contracts" // for demo

)

func main() {

    client, err := ethclient.Dial("https://rinkeby.infura.io")

    if err != nil {

        log.Fatal(err)

    }

    privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")

    if err != nil {

        log.Fatal(err)

    }

    publicKey := privateKey.Public()

    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)

    if !ok {

        log.Fatal("error casting public key to ECDSA")

    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)

    if err != nil {

        log.Fatal(err)

    }

    gasPrice, err := client.SuggestGasPrice(context.Background())

    if err != nil {

        log.Fatal(err)

    }

    auth := bind.NewKeyedTransactor(privateKey)

    auth.Nonce = big.NewInt(int64(nonce))

    auth.Value = big.NewInt(0)     // in wei

    auth.GasLimit = uint64(300000) // in units

    auth.GasPrice = gasPrice

    address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")

    instance, err := store.NewStore(address, client)

    if err != nil {

        log.Fatal(err)

    }

    key := [32]byte{}

    value := [32]byte{}

    copy(key[:], []byte("foo"))

    copy(value[:], []byte("bar"))

    tx, err := instance.SetItem(auth, key, value)

    if err != nil {

        log.Fatal(err)

    }

    fmt.Printf("tx sent: %s", tx.Hash().Hex()) // tx sent: 0x8d490e535678e9a24360e955d75b27ad307bdfb97a1dca51d0f3035dcee3e870

    result, err := instance.Items(&bind.CallOpts{}, key)

    if err != nil {

        log.Fatal(err)

    }

    fmt.Println(string(result[:])) // "bar"

}

answered Jul 26, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer
+1 vote
1 answer

How can nodes interact with a Smart Contract?

If you are using Remix IDE to ...READ MORE

answered Apr 27, 2018 in Blockchain by Perry
• 17,100 points
814 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,601 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 6, 2018 in Blockchain by aryya
• 7,460 points
808 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
+1 vote
5 answers

Where does the smart contracts stored and executed on blockchain?

Ethereum smart contracts are executed on EVM ...READ MORE

answered May 3, 2018 in Blockchain by Shashank
• 10,400 points
9,331 views
0 votes
1 answer

How to use real world/live data in my smart contract?

You cant access/embed real world data using ...READ MORE

answered Jul 12, 2018 in Blockchain by digger
• 26,740 points
1,136 views
0 votes
1 answer

How are the ethers sent to smart contract in the IBM example?

They are in magical variable msg. The function ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
543 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP