/go-client-web3

golang-web3 client of ethereum

Primary LanguageGoGNU General Public License v3.0GPL-3.0

Go-web3 Client

Build Status

This is a Ethereum compatible Go Client which implements the Eth JSON RPC Module and NET JSON RPC Module.

Usage

Deploying a contract

bytecode := ... #contract bytecode
abi := ... #contract abi

var connection = web3.NewWeb3(providers.NewHTTPProvider("127.0.0.1:8545", 10, false))
contract, err := connection.Eth.NewContract(abi)

transaction := new(dto.TransactionParameters)
coinbase, err := connection.Eth.GetCoinbase()
transaction.From = coinbase
transaction.Gas = big.NewInt(4000000)

hash, err := contract.Deploy(transaction, bytecode, nil)

fmt.Println(hash)
	

Using contract public functions

result, err = contract.Call(transaction, "balanceOf", coinbase)
if result != nil && err == nil {
	balance, _ := result.ToComplexIntResponse()
	fmt.Println(balance.ToBigInt())
}
	

Using contract payable functions

hash, err = contract.Send(transaction, "approve", coinbase, 10)
	

Using RPC commands

GetBalance

balance, err := connection.Eth.GetBalance(coinbase, block.LATEST)

SendTransaction

transaction := new(dto.TransactionParameters)
transaction.From = coinbase
transaction.To = coinbase
transaction.Value = big.NewInt(10)
transaction.Gas = big.NewInt(40000)
transaction.Data = types.ComplexString("p2p transaction")

txID, err := connection.Eth.SendTransaction(transaction)

Installation

go get

go get -u github.com/regcostajr/go-web3

glide

glide get github.com/regcostajr/go-web3

Requirements

  • go ^1.8.3
  • golang.org/x/net

Testing

Node running in dev mode:

geth --dev --ws --wsorigins="*" --rpc --rpcapi eth,web3,personal,ssh,net --mine

Full test:

go test -v test/modulename/*.go

Individual test:

go test -v test/modulename/filename.go

License

Package go-web3 is licensed under the GPLv3 License.

Office website

– CirclePlus, @Circleplus