Giter VIP home page Giter VIP logo

gosdk's Introduction

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 BIN declare
contractBIN := ""
// contract ABI declare
contractABI := ``
transaction = NewTransaction(common.BytesToAddress(newAddress).Hex()).Deploy(bin)
//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)
	}
	fmt.Println(txReceipt.ContractAddress)
//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.

gosdk's People

Contributors

jackzing avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.