以太坊学习笔记(二):通过SDK调用以太坊合约

以太坊学习笔记(二):通过SDK调用以太坊合约,第1张

环境准备 搭建以太坊

详细可参考之前博客:以太坊学习笔记(一)

1、创建创世块文件

{
    "config": {
       "chainId": 10001, 
       "homesteadBlock": 0, 
       "eip155Block": 0, 
       "eip158Block": 0
   },
   "alloc" : {},
   "coinbase" : "0x0000000000000000000000000000000000000000",
   "difficulty" : "0x20000",
   "extraData" : "",
   "gasLimit" : "0x2fefd8",
   "nonce" : "0x0000000000000042",
   "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
   "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
   "timestamp" : "0x00"
}

2、启动节点

//节点1
mkdir node1
geth init --datadir node1 genesis.json
//启动节点
geth --datadir node1 --port 30312 --rpc --rpcaddr 127.0.0.1 --rpcport 8541 --networkid 10001 console

//控制台查看
> admin.nodeInfo
{
  enode: "enode://9314e0962ca2c328a1bb0c184800ef3992b033e3643a32b7c4b10d16be556bf2e13ca481d0d0112b74b2034409cae50e32aabba86c5390405b1951b909b903ed@115.227.7.13:30312",
  enr: "enr:-Je4QKZ2D_79sj9G_gNsjFQZZf54UGgStuUAYFqKp7sDCDdJEunDu8I2ND_iRgqR2N7Tcra2vEH1h_uvWwJ3NtGDwlwGg2V0aMfGhHEB0w2AgmlkgnY0gmlwhHPjBw2Jc2VjcDI1NmsxoQOTFOCWLKLDKKG7DBhIAO85krAz42Q6MrfEsQ0WvlVr8oN0Y3CCdmiDdWRwgnZo",
  id: "4ae7a565d53f83de2fce194daa0731748edb7e1483eee131e96db69be60aa37c",
  ip: "115.227.7.13",
  listenAddr: "[::]:30312",
  name: "Sipe/v2.0.3-stable-61c08104/darwin-amd64/go1.15",
  ports: {
    discovery: 30312,
    listener: 30312
  },
  protocols: {
    eth: {
      config: {
        chainId: 10001
      },
      consensus: "ethash",
      difficulty: 57359637554,
      genesis: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
      head: "0x1afc02a4c62d3f1814fecbcfee3e753f5572a01145bad42ed1dcfb7c0327b52d",
      network: 10001
    }
  }
}
> 

//节点2
mkdir node2
geth init --datadir node2 genesis.json
geth --datadir node2 --port 30313 --rpc --rpcaddr 127.0.0.1 --rpcport 8542 --networkid 10001 --bootnodes "enode://9314e0962ca2c328a1bb0c184800ef3992b033e3643a32b7c4b10d16be556bf2e13ca481d0d0112b74b2034409cae50e32aabba86c5390405b1951b909b903ed@127.0.0.1:30312" console --allow-insecure-unlock

3、开始挖矿

//创建账户
personal.newAccount()
miner.setEtherbase(”account1“)

//启动挖矿
miner.start()

//创建另一个账户开始转账
personal.newAcount()
personal.unlockAccount("acount1")

> eth.sendTransaction({from:"acount1",to:"account1",value:web3.toWei(10,"ether")}) 
> eth.getBalance("account")
智能合约

1、准备一个solidity合约
2、用abigen solc (可以网上下载对应版本) 工具生成将solidity合约生成go合约

//demo.sol
pragma solidity^0.8.7;
contract demo {
    string message;
    constructor(string memory _msg) {
        message = _msg;
    }
    function getMsg() public view returns (string memory) {
        return message;
    }
    function setMsg(string memory _msg) public {
        message = _msg;
    }
}

abigen solc 工具生成demo.go

 abigen -sol demo.sol -pkg main -type demo -out demo.go
SDK 调用

1、通过ectclient创建与以太坊链连接
2、身份准备
3、部署合约
4、调用合约

func main() {
	//1、创建客户端
	cli, err := ethclient.Dial("http://localhost:8541")
	if err != nil {
		log.Panic("failed to dial", err)
	}

	//2.身份准备
	keyin := strings.NewReader(keyinfo)
	chainID, err := cli.ChainID(context.Background())
	if err != nil {
		log.Panic("failed to get chainID", err)
	}

	auth, err := bind.NewTransactorWithChainID(keyin, "123456", chainID)
	if err != nil {
		log.Panic("auth create failed:", err)
	}
	fmt.Println("chainID: ", chainID)

	//3、部署合约
	addr, tx, instance, err := DeployDemo(auth, cli, "hello world")
	if err != nil {
		log.Panic("failed to deploy demo: ", err)
	}
	fmt.Println("addr:", addr.Hex())
	fmt.Println("tx:", tx.Hash())
	msg, err := instance.GetMsg(nil)
	if err != nil {
		log.Panic("failed to getMsg: ", err)
	}
	fmt.Println("msg: ", msg)
}
运行
go run main.go demo.go

结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/1298485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存