Personal profile

[HundredLee]

  • In 2013, I started my own business and began to work on digital currency development, blockchain development engineer and iOS&Web developer. Currently, HE serves as CTO of a well-known digital currency trading platform developer in China. Four years of technology precipitation, customers have been all over the home and abroad, and independent research and development of JAVA high-speed matching engine.
  • Weibo: weibo.com/hundredlee2…
  • E-mail: hundred9411#gmail.com
  • Blog : blog.sodroid.com

What is a token?

In 2017, blockchain became popular in the entire Internet financial circle. In particular, the emergence of ICO made many people start to know digital currency. We know that Ethereum is currently the most popular digital currency and blockchain system, while ETH has reached 2000+RMB per system. I remember when I first got to know Ethereum, it was over 30.

  • So what is a token? In the Ethereum blockchain, we call tokens, which are digital assets that anyone in the Ethereum blockchain can issue at will. And it must be follow erc20 standards, as for erc20 standard, you can reference this article theethereum. The wiki/w/index. PHP…
  • It is actually a piece of smart contract code, and smart contract code must have the following functions and events.
contract ERC20 {
      function totalSupply() constant returns (uint totalSupply);
      function balanceOf(address _owner) constant returns (uint balance);
      function transfer(address _to, uint _value) returns (bool success);
      function transferFrom(address _from, address _to, uint _value) returns (bool success);
      function approve(address _spender, uint _value) returns (bool success);
      function allowance(address _owner, address _spender) constant returns (uint remaining);
      event Transfer(address indexed _from, address indexed _to, uint _value);
      event Approval(address indexed _owner, address indexed _spender, uint _value);
 }Copy the code
  • The smart contract code runs in the Ethereum Smart Contract VIRTUAL machine. If you are interested, you can learn about it. The enclosed document: solidity. Readthedocs. IO/en/latest/I…

  • We see the above code similar to the interface in Golang, which contains total amount, balance, transfer and other methods respectively. What we focus on today is to use Golang to realize transfer and transferFrom methods.

  • Let’s get to the subject.

Connect the Ethereum RPC node

  • Currently widely used is Go-Ethereum, whose client name is Geth. You can compile and install the node on your computer or server and enable RPC service. This step is omitted in this article and there are many articles on the web for you to read.
  • Attached to github: github.com/ethereum/go…
  • The default RPC port for GEth is8545, I use the default port, we will use laterhttp://127.0.0.1:8545As our RPC connection.

Get the Go-Ethereum code first

  • go get github.com/ethereum/go-ethereum
  • Then we go to the Go-Ethereum directory, which should be this path if your Golang environment is fine.
  • cd $GOPATH/src/github.com/ethereum/go-ethereum
  • When you enter the directory and see that the code has been pulled down in its entirety, we can proceed to the next step.

Connecting RPC Nodes

  • Dont bb.. Let’s go straight to the code.

import (
    "github.com/ethereum/go-ethereum/rpc"
    "github.com/ethereum/go-ethereum/ethclient"
)

rpcDial, err := rpc.Dial("http://127.0.0.1:8545")
iferr ! =nil {
    panic(err);    
}
client := ethclient.NewClient(rpcDial)Copy the code
  • If there is no panic, then we have successfully connected the nodes. (^ _ ^)

Creating a test Account

  • To test the transfer, we need two Ethereum accounts. We use Golang to generate. We know that the ethereum account private key is placed in the keystore file, which is a PIECE of JSON, and the password can be set when creating. It’s the same as the Wallet. Dat file for Bitcoin. If nothing happens, your assets stay in the blockchain network forever, never to be recovered.
  • Let’s create two Ethereum accounts using code.

import (
    "github.com/ethereum/go-ethereum/accounts/keystore"
)

ks := keystore.NewKeyStore("/", keystore.StandardScryptN, keystore.StandardScryptP)

address, _ := ks.NewAccount("password")
account, err := ks.Export(address, "password"."password")

iferr ! =nil {
    panic(err)
}Copy the code
  • As we can see from the code above, I created an Ethereum account and set the password topasswordAnd export. In the endaccountThe variable is the private key of the account and is a piece of JSON text. We can see what it looks like.
  • throughaddressVariable, we can get the address of the account. Such as thisaddress.Address.Hex()

{"address":"41e2f6a4eb0e61f627207ec4a3f7098388174368"."crypto": {"cipher":"aes-128-ctr"."ciphertext"
:"c0b10f9a3ca83837de83d38ca95bef200170d97e818f15bbc35642b6076c4a16"."cipherparams": {"iv":"e46c3f2c76b35eedd7b065677faf6927"},
"kdf":"scrypt"."kdfparams":
{"dklen":32."n":262144."p":1."r":8."salt"
:"05c8493a6a8518451c18ac05785e6c60507d906b130ee859e99804f0df90b63d"},
"mac"
:"62f52d9c4a078765b496cf76ed50634199c509e2d6e91106e24276d51124b971"},"id"
:"6c00339c-773b-4862-bb61-3a51a6f671ee"."version":3}Copy the code
  • It’s just a piece of JSON, so you have to keep it safe.

Generate token files

  • Open thecd $GOPATH/src/github.com/ethereum/go-ethereum/cmd/abigenYou can see the main.go file
  • Executing go build main.go generates a main binary file in the directory.

  • Save the following JSON as token.abi and place it in the current directory.


[{"constant":true."inputs": []."name":"name"."outputs": [{"name":""."type":"bytes32"}]."payable":false."type":"function"}, {"constant":false."inputs": []."name":"stop"."outputs": []."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"guy"."type":"address"}, {"name":"wad"."type":"uint256"}]."name":"approve"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"owner_"."type":"address"}]."name":"setOwner"."outputs": []."payable":false."type":"function"}, {"constant":true."inputs": []."name":"totalSupply"."outputs": [{"name":""."type":"uint256"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"src"."type":"address"}, {"name":"dst"."type":"address"}, {"name":"wad"."type":"uint256"}]."name":"transferFrom"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":true."inputs": []."name":"decimals"."outputs": [{"name":""."type":"uint256"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"dst"."type":"address"}, {"name":"wad"."type":"uint128"}]."name":"push"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"name_"."type":"bytes32"}]."name":"setName"."outputs": []."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"wad"."type":"uint128"}]."name":"mint"."outputs": []."payable":false."type":"function"}, {"constant":true."inputs": [{"name":"src"."type":"address"}]."name":"balanceOf"."outputs": [{"name":""."type":"uint256"}]."payable":false."type":"function"}, {"constant":true."inputs": []."name":"stopped"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"authority_"."type":"address"}]."name":"setAuthority"."outputs": []."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"src"."type":"address"}, {"name":"wad"."type":"uint128"}]."name":"pull"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":true."inputs": []."name":"owner"."outputs": [{"name":""."type":"address"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"wad"."type":"uint128"}]."name":"burn"."outputs": []."payable":false."type":"function"}, {"constant":true."inputs": []."name":"symbol"."outputs": [{"name":""."type":"bytes32"}]."payable":false."type":"function"}, {"constant":false."inputs": [{"name":"dst"."type":"address"}, {"name":"wad"."type":"uint256"}]."name":"transfer"."outputs": [{"name":""."type":"bool"}]."payable":false."type":"function"}, {"constant":false."inputs": []."name":"start"."outputs": []."payable":false."type":"function"}, {"constant":true."inputs": []."name":"authority"."outputs": [{"name":""."type":"address"}]."payable":false."type":"function"}, {"constant":true."inputs": [{"name":"src"."type":"address"}, {"name":"guy"."type":"address"}]."name":"allowance"."outputs": [{"name":""."type":"uint256"}]."payable":false."type":"function"}, {"inputs": [{"name":"symbol_"."type":"bytes32"}]."payable":false."type":"constructor"}, {"anonymous":true."inputs": [{"indexed":true."name":"sig"."type":"bytes4"}, {"indexed":true."name":"guy"."type":"address"}, {"indexed":true."name":"foo"."type":"bytes32"}, {"indexed":true."name":"bar"."type":"bytes32"}, {"indexed":false."name":"wad"."type":"uint256"}, {"indexed":false."name":"fax"."type":"bytes"}]."name":"LogNote"."type":"event"}, {"anonymous":false."inputs": [{"indexed":true."name":"authority"."type":"address"}]."name":"LogSetAuthority"."type":"event"}, {"anonymous":false."inputs": [{"indexed":true."name":"owner"."type":"address"}]."name":"LogSetOwner"."type":"event"}, {"anonymous":false."inputs": [{"indexed":true."name":"from"."type":"address"}, {"indexed":true."name":"to"."type":"address"}, {"indexed":false."name":"value"."type":"uint256"}]."name":"Transfer"."type":"event"}, {"anonymous":false."inputs": [{"indexed":true."name":"owner"."type":"address"}, {"indexed":true."name":"spender"."type":"address"}, {"indexed":false."name":"value"."type":"uint256"}]."name":"Approval"."type":"event"}]Copy the code
  • Run the./main –abi token.abi — PKG main –type token –out token.go command

  • We can see that a token.go file is generated under the directory. And you’re done.

Begin to transfer

  • I’m going to start with what I generated in the last steptoken.goDrag into the project.
  • I’ll write the following statement in the comments of the code
import (
    "github.com/ethereum/go-ethereum/accounts/abi/bind"
)

// First import the account key (JSON) and password generated above
auth, err := bind.NewTransactor(strings.NewReader("json"), "password")
// This sentence uses the method in the generated token.go
// The client variable was created when we first connected to the Ethereum RPC node

/ / contractAddress is tokens, address, such as the address of the eos is 0 x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0
// Then the transfer is for the EOS token in the account
/ / specific look here at https://etherscan.io/token/0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0
token, err := NewToken(common.HexToAddress("0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0"), client)
iferr ! =nil {
    panic(err)
}

// Each token has a number of digits. For example, EOS is 18 digits, so when we transfer money, we need to add 18 zeros after the amount
decimal, err := token.Decimals(nil)
iferr ! =nil {
    panic(err)
}

// This is a code snippet that handles bits
tenDecimal := big.NewFloat(math.Pow(10.float64(decimal)))
convertAmount, _ := new(big.Float).Mul(tenDecimal, amount).Int(&big.Int{})

// Then you can transfer the money to the account you need to accept
//toAddress is the address of the account that accepts EOS
txs, err := token.Transfer(auth, common.HexToAddress(toAddress), convertAmount)Copy the code
  • From there, the transfer is complete. In addition, there are many other methods in token.go. If you are interested, you can research them by yourself.

In this paper, the end

  • My etheric lane address 0 x67f883a42031215622e0b84c96d0e4dca7a3ce81
  • Lol support donation of EOS, OMG, TNT, etc. Buy me a Starbucks.
  • Of course, ETH will not refuse. Or you can scan the code, below is my Ethereum payment QR code. Thank you.
-w265