在Go语言中构建原始的以太坊交易 – 合约函数调用

huangapple go评论115阅读模式
英文:

Build raw Ethereum transaction in Go - contract function call

问题

我正在修改go-ethereum以便能够处理与我们正在创建的代币的合并挖矿。因此,每当一个矿工运行并挖矿我们版本的geth时,如果他们找到一个区块,他们将通过我们的合约获得X数量的代币。

合约被构建用于向挖掘出区块的矿工发放奖励。我只需要修改go-ethereum以处理调用合约中奖励函数的交易,并将其添加到交易池中,然后再提交区块。

在go-ethereum中,我在miner包中添加了一个新的go文件,token_claim.go。查看miner.go文件,似乎我需要在/go-ethereum-1.6.7/miner/worker.go的第474行左右,在封闭区块之前添加以下代码来构建并签署奖励领取交易。

有人可以提供一个在Go中调用合约函数构建原始交易的示例吗?我有abi、bytecode和合约地址。

谢谢。

英文:

I'm modifying go-ethereum to be able to handle merged mining with a token we're creating. So every time a miner has our version of geth running and mining, if they find a block, they will receive X amount of our token via our contract.

The contract is built to handle giving out the reward to the miner of the block. I just need to modify go-ethereum to handle adding the transaction of calling the reward function in the contract and adding it to the tx pool before submitting the block.

In go-ethereum, I've added a new go file, token_claim.go in the miner package. Looking in miner.go file, it appears that I need to add in this code to build an sign the reward claim transaction in /go-ethereum-1.6.7/miner/worker.go around line 474 right before sealing the block.

Can someone provide an example of building a raw transaction in Go calling a contract function. I have the abi, bytecode, and contract address.

Thanks

答案1

得分: 2

这是一个使用Go与智能合约进行交互的简单示例。假设您已经安装了solcabigen

Store.sol

  1. pragma solidity ^0.4.24;
  2. contract Store {
  3. event ItemSet(bytes32 key, bytes32 value);
  4. string public version;
  5. mapping (bytes32 => bytes32) public items;
  6. constructor(string _version) public {
  7. version = _version;
  8. }
  9. function setItem(bytes32 key, bytes32 value) external {
  10. items[key] = value;
  11. emit ItemSet(key, value);
  12. }
  13. }

main.go

  1. package main
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "fmt"
  6. "log"
  7. "math/big"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/ethereum/go-ethereum/ethclient"
  12. store "./contracts" // for demo
  13. )
  14. func main() {
  15. client, err := ethclient.Dial("https://rinkeby.infura.io")
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. publicKey := privateKey.Public()
  24. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  25. if !ok {
  26. log.Fatal("error casting public key to ECDSA")
  27. }
  28. fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
  29. nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. gasPrice, err := client.SuggestGasPrice(context.Background())
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. auth := bind.NewKeyedTransactor(privateKey)
  38. auth.Nonce = big.NewInt(int64(nonce))
  39. auth.Value = big.NewInt(0) // in wei
  40. auth.GasLimit = uint64(300000) // in units
  41. auth.GasPrice = gasPrice
  42. address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
  43. instance, err := store.NewStore(address, client)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. key := [32]byte{}
  48. value := [32]byte{}
  49. copy(key[:], []byte("foo"))
  50. copy(value[:], []byte("bar"))
  51. tx, err := instance.SetItem(auth, key, value)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. fmt.Printf("tx sent: %s", tx.Hash().Hex()) // tx sent: 0x8d490e535678e9a24360e955d75b27ad307bdfb97a1dca51d0f3035dcee3e870
  56. result, err := instance.Items(&bind.CallOpts{}, key)
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. fmt.Println(string(result[:])) // "bar"
  61. }

更多示例,请参阅Ethereum Development with Go book

英文:

Here's a simple example of how you can interact with a smart contract in Go. Assuming you have solc and abigen installed.

  1. solc --abi Store.sol > Store.abi
  2. solc --bin Store.sol > Store.bin
  3. abigen --bin=Store.bin --abi=Store.abi --pkg=store --out=Store.go

Store.sol

  1. pragma solidity ^0.4.24;
  2. contract Store {
  3. event ItemSet(bytes32 key, bytes32 value);
  4. string public version;
  5. mapping (bytes32 => bytes32) public items;
  6. constructor(string _version) public {
  7. version = _version;
  8. }
  9. function setItem(bytes32 key, bytes32 value) external {
  10. items[key] = value;
  11. emit ItemSet(key, value);
  12. }
  13. }

main.go

  1. package main
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "fmt"
  6. "log"
  7. "math/big"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/ethereum/go-ethereum/ethclient"
  12. store "./contracts" // for demo
  13. )
  14. func main() {
  15. client, err := ethclient.Dial("https://rinkeby.infura.io")
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. publicKey := privateKey.Public()
  24. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  25. if !ok {
  26. log.Fatal("error casting public key to ECDSA")
  27. }
  28. fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
  29. nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. gasPrice, err := client.SuggestGasPrice(context.Background())
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. auth := bind.NewKeyedTransactor(privateKey)
  38. auth.Nonce = big.NewInt(int64(nonce))
  39. auth.Value = big.NewInt(0) // in wei
  40. auth.GasLimit = uint64(300000) // in units
  41. auth.GasPrice = gasPrice
  42. address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
  43. instance, err := store.NewStore(address, client)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. key := [32]byte{}
  48. value := [32]byte{}
  49. copy(key[:], []byte("foo"))
  50. copy(value[:], []byte("bar"))
  51. tx, err := instance.SetItem(auth, key, value)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. fmt.Printf("tx sent: %s", tx.Hash().Hex()) // tx sent: 0x8d490e535678e9a24360e955d75b27ad307bdfb97a1dca51d0f3035dcee3e870
  56. result, err := instance.Items(&bind.CallOpts{}, key)
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. fmt.Println(string(result[:])) // "bar"
  61. }

For more examples, check out the Ethereum Development with Go book.

huangapple
  • 本文由 发表于 2017年8月8日 09:40:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/45558034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定