gosdk

module
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: Apache-2.0

README

GO-SDK

This is gosdk for Hyperchain.

To get more information you can view docs, use gosdk to start a enjoyable journey.

Get started

Contract Deploy & Invoke

Before invoke Contract, you should deploy contract at first step

1. New RPC Client

RPC is a client that can provide HTTP requests, which is convenient for users to send transactions to the hyperchain

RPC needs to provide configuration file path for initialization

rpc := NewRPCWithPath("../conf")
2. Generate Account

Create GM type account, you can create other eg.. ECDSA or ED25519 account

guomiKey, _ := gm.GenerateSM2Key()
pubKey := &account.SM2Key{SM2PrivateKey: guomiKey}
newAddress := pubKey.GetAddress()
3. New Contract Deploy Transaction

Declare the bin and ABI of the contract, and create the contract deploy transaction

// contract ABI declare
abiContract := ""
// contract BIN declare
binContract := ``
// contract 
pubKey, _ := guomiKey.Public().(*gm.SM2PublicKey).Bytes()
h, _ := hash.NewHasher(hash.KECCAK_256).Hash(pubKey)
newAddress := h[12:]

transaction := NewTransaction(common.BytesToAddress(newAddress).Hex()).Deploy(binContract).DeployArgs(abiContract)
//sign with the private key of the account created before
transaction.Sign(guomiKey)
4. Send Contract Deploy Transaction

Use rpc client send contract deploy transaction and get contract address from txReceipt

txReceipt, err := rpc.DeployContract(transaction)
if err != nil {
	fmt.Println(err)
}
//Assignment contract address
contractAddress := txReceipt.ContractAddress
5. New Contract Invoke Transaction

Package and compress the contract method and parameters to create the contract call transaction

//decode string contractABI to ABI struct
ABI, serr := abi.JSON(contractABI)
if err != nil {
    t.Error(serr)
    return
}
// pack contract invoke function and params
packed, serr := ABI.Pack("add", uint32(1), uint32(2))
if serr != nil {
    t.Error(serr)
    return
}
transaction := NewTransaction(common.BytesToAddress(newAddress).Hex()).Invoke(contractAddress, packed)
//sign with the private key of the account created before  
transaction.Sign(privateKey)
6. Send Contract Invoke Transaction

Use rpc client send contract invoke transaction and get contract invoke receipt

receipt, err := rpc.InvokeContract(transaction)
if err != nil {
	fmt.println(err)
}
7. Decode Invoke Result

Decode receipt to specific result type.

var result uint32
if err := ABI.UnpackResult(v, method, ret); err != nil {
	fmt.println(err)
}
result = v
fmt.Println(result)

Issue

If you have any suggestions or idea, please submit issue in this project!

Doc

If you want to know more about GO-SDK, you can read manual at here.

Contribute

Welcome to contribute code

If the code you submit contains test files, and the test function has an RPC request, please start the test function with TestRPC_, The CI server will skip these tests. You need to start the flato node locally for testing.

Directories

Path Synopsis
Package abi implements the Ethereum ABI (Application Binary Interface).
Package abi implements the Ethereum ABI (Application Binary Interface).
Package abi2 implements the Ethereum ABI (Application Binary Interface).
Package abi2 implements the Ethereum ABI (Application Binary Interface).
Package common contains various helper functions.
Package common contains various helper functions.
bitutil
Package bitutil implements fast bitwise operations.
Package bitutil implements fast bitwise operations.
hexutil
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.
math
Package math provides integer math utilities.
Package math provides integer math utilities.
fuzzers
abi
fvm
grpc
api
utils

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL