[TOC]
Personal profile
HundredLee has been working on the first line of development of digital currency trading platform since 2013. In 2016, he came into contact with blockchain and went on the road of no return. A new high-speed matching engine is being developed. Weibo: weibo.com/hundredlee2… E-mail: hundred9411#gmail.com
Serial preparation
Serial 1, introduce Ethereum, check balance and so on. Serial 2: Go ethereum to Transfer out of Ethereum, how to interconnect tokens, how to query token balance, how to transfer out tokens. Serial three, some experience and summary of the exchange platform docking ethereum.
What is Ethereum?
Baidu Baike:
Ethereum is not an institution, but an underlying open source system capable of implementing smart contracts on the blockchain. From the birth of Ethereum to May 2017, more than 200 Ethereum applications have been born in the world in just three and a half years. Ethereum is a platform and programming language that enables developers to build and distribute the next generation of distributed applications. Ethereum can be used to program, decentralize, guarantee and trade just about anything: voting, domain names, financial exchanges, crowdfunding, corporate governance, contracts and most protocols, intellectual property, and smart assets that benefit from hardware integration.
Install the GEth client
- Geth is currently the most widely used client. It can be used for mining, setting up private chains, managing ethereum accounts, and more.
- Osx users can install directly from BREW. Specific reference blog.csdn.net/ethchinese/…
- If hope that through the source code to compile the installation, please refer to www.cnblogs.com/dophin459/p here…
- Of course, the official Github readme document is also very detailed, suggestions can be seen at github.com/ethereum/go…
- Here is no longer detailed installation methods, online information is particularly much.
The development of
Connecting to Ethereum
-
First of the more important step, go get https://github.com/ethereum/go-ethereum
-
If you successfully installed the GEth client and enabled RPC in the previous step, such as http://127.0.0.1, then you can connect to RPC in Golang.
-
Directly on the code:
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
func connectToRpc(a) (*ethclient.Client, error) {
client, err := rpc.Dial("http://127.0.0.1")
iferr ! =nil {
return nil, err
}
conn := ethclient.NewClient(client)
return conn, nil
}Copy the code
Query the balance of Ethereum (ETH)
- In the development of Ethereum applications, it is often necessary to obtain the balance in the account.
- Since we successfully connected to the Ethereum client in the previous step, all we need to do is simply call the interface directly.
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
// Note that address is the address of the ethereum balance you want to query. It is usually of the form 0xDDDDd
func GetBalance(address string) {
client,err := connectToRpc()
iferr ! =nil {
panic(err.Error())
}
balance, err := client.BalanceAt(context.TODO(),common.HexToAddress(address), nil)}Copy the code
- Note in particular that Ethereum’s Decimal is 18, so we need to multiply the balance by 10^-18 to get the normal amount of Ethereum.
- The same goes for ethereum’s other tokens, which have different Decimal names, but have a corresponding way to obtain them. Don’t worry about that, we’ll get to that in the next series.
iferr ! =nil {
beego.Error(err.Error())
} else {
// This is the balance of ethereum in the address
balanceV := float64(balance.Int64()) * math.Pow(10.18)}Copy the code
The end of the
Accept donations, more or less support.