[get] go invokes the smart contract | together with the school district block chain

Xiaosheng Fanyi 2021-08-07 19:01:43 阅读数:827

invokes smart contract school district

1. obtain abi file : The interface of the contract

stay remix After compiling the contract in the tool , There will be one. abi, Copy
 Insert picture description here
And then create a new one xx.abi file , Paste the assigned value into it

 Insert picture description here

Be careful : The code has changed , After recompilation abi It will change too. , So be sure to use the latest abi

It can also be done through solc Generate abi:

solc --bin test.sol -o test.abi

2. install abigen Tools : Used to generate go file ,

Download the file :github Address

 Insert picture description here

 Insert picture description here
To find the main.go file , Conduct go build, Generate abigen.exe file .
 Insert picture description here
And then on bin Here in the directory
 Insert picture description here Then you can verify it here
 Insert picture description here
Use abigen Tool generation go file

abigen --abi xx.abi --pkg packagename --type structname --out xx.go
  • abi The file in remix You can get
  • pkg Specify the package name of the output file , That is to say package name
  • type Specify the contract structure name
  • out Specify the output go File name

 Insert picture description here

3. remix Connect private chain

geth Start the private chain , To specify rpccorsdomain *, Otherwise, in the remix It can't be connected in .
 Insert picture description here

choice Web3 Provider The way , Default connection private chain 8545 port

 Insert picture description here
But you can see that we don't have account Of
 Insert picture description here
So we can create a account
 Insert picture description here
After creating two
 Insert picture description here
You can see it here .

4. Contract deployment

1. Connect private chain : choice Web3 Provider, Default port number 8545, If geth Starting up rpc The port numbers are inconsistent , Change to be consistent
2. Deployment contract

The following errors may occur , List the solutions here
1 .Error: authentication needed: password or unlock: This error report can unlock the account
Account unlock :personal.unlockAccount(“ Account address ”)

 Insert picture description here
unlock Just a moment
 Insert picture description here

2 .Returned error: exceeds block gas limit: This error depends on the block number gaslimit, When deploying, change to always
Get block number :eth.blockNumber
Get details according to the block number :eth.getBlock(0) Will see gasLimit Value , At deployment time gaslimit Can't be greater than this
 Insert picture description here  Insert picture description here

3 .Returned error: insufficient funds for gas * price + value: The deployment contract requires a handling fee , Have to earn , Through mining

 Insert picture description here
Don't have the money , Mining to make money .
 Insert picture description here

  • Start mining with the specified number of threads :miner.start(1)
  • Stop mining :miner.stop()
  • Check if you are digging :eth.mining true: Mining ,false: Not mining
  • Get the account address array :eth.accounts
  • Get mining address :eth.coinbase By default, the first account created
  • Check the first account balance :eth.getBalance(eth.accounts[0])
  • Get the balance of the specified account :eth.getBalance(“ Account address ”)

 Insert picture description here

5. Initialize the creation block of the private chain node

Create a new one genesis.json file , The contents are as follows





meaning :

  • coinbase: Mining account address , Random filling , Later, you can set , Generally, the first user created by default
  • difficulty: The current difficulty of setting blocks , If it's too difficult ,cpu Mining is hard , It's a little bit more difficult here
  • gasLimit: This value is set for GAS The total consumption limit of , Used to limit the total amount of transaction information a block can contain , Fill in the maximum .
  • nonce: nonce It's just one. 64 Bit random number , For mining , Pay attention to him and me mixhash The setting of needs to meet Ethereum's Yellow paper
  • mixhash: And nonce It is used for mining , Generated by a part of the previous block hash.
  • parentHash: Of the last block hash value , Because it's genesis , So this value is 0extraData:
  • timestamp: Set the timestamp of the creation block
  • alloc: Used to preset the account number and the number of ethers in the account , Because private chain mining is easier , So we don't need to preset an account with currency , You can create it yourself when you need it .

cmd Enter to store json Path to file

geth -datadir "fanone" init genesis.json

 Insert picture description here
 Insert picture description here  Insert picture description here

6. Contract deployment

You need to pay a handling fee when deploying

7. call

github.com/ethereum/go-ethereum v1.10.0

package main
import (
func main() {

// Connect rpc
client,err := ethclient.Dial("")
// Define the account address to operate the contract 
addr := "0x29794ab2ed6c47faff7ebdd6dcdd71a263e25460"
// Convert the string address to common.Address
common_addr := common.HexToAddress(addr)
if err != nil {

panic(" Error connecting Ethereum contract ")
// Create contract object 
contract_obj,err11 := contract.NewTestAddress(common_addr,client)
if err11 !=nil {

panic(" Error creating contract object ")
fmt.Println(contract_obj.TestAddressCaller) // Caller Access function 
fmt.Println(contract_obj.TestAddressTransactor) // Transactor Have function 
fmt.Println(contract_obj.TestAddressFilterer) // It doesn't work 
版权声明:本文为[Xiaosheng Fanyi]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/08/20210807185920185P.html