This is gosdk for Hyperchain.
To get more information you can view docs, use gosdk to start a enjoyable journey.
Before invoke Contract, you should deploy contract at first step
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")
Create GM type account, you can create other eg.. ECDSA or ED25519 account
guomiKey, _ := gm.GenerateSM2Key()
pubKey := &account.SM2Key{SM2PrivateKey: guomiKey}
newAddress := pubKey.GetAddress()
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)
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
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)
Use rpc client send contract invoke transaction and get contract invoke receipt
receipt, err := rpc.InvokeContract(transaction)
if err != nil {
fmt.println(err)
}
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)
If you have any suggestions or idea, please submit issue in this project!
If you want to know more about GO-SDK, you can read manual at here.
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.