Skip to content

Instantly share code, notes, and snippets.

@canhlinh
Created May 16, 2021 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canhlinh/d608f75bdb813b8c86dc3bcbd48fbb9c to your computer and use it in GitHub Desktop.
Save canhlinh/d608f75bdb813b8c86dc3bcbd48fbb9c to your computer and use it in GitHub Desktop.
simple_transfer_substrate.go
package main
import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client"
"github.com/centrifuge/go-substrate-rpc-client/config"
"github.com/centrifuge/go-substrate-rpc-client/signature"
"github.com/centrifuge/go-substrate-rpc-client/types"
)
func main() {
// This sample shows how to create a transaction to make a transfer from one an account to another.
// Instantiate the API
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
// Create a call, transferring 12345 units to Bob
bob, err := types.NewAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")
if err != nil {
panic(err)
}
c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(12345))
if err != nil {
panic(err)
}
// Create the extrinsic
ext := types.NewExtrinsic(c)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
nonce := uint32(accountInfo.Nonce)
o := types.SignatureOptions{
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
}
// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, o)
if err != nil {
panic(err)
}
// Send the extrinsic
hash, err := api.RPC.Author.SubmitExtrinsic(ext)
if err != nil {
panic(err)
}
fmt.Printf("Transfer sent with hash %#x\n", hash)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment