Skip to content

Instantly share code, notes, and snippets.

@cdamian
Created October 31, 2022 14:50
Show Gist options
  • Save cdamian/cdfe38c7d858f8c899977fcc9ef41b99 to your computer and use it in GitHub Desktop.
Save cdamian/cdfe38c7d858f8c899977fcc9ef41b99 to your computer and use it in GitHub Desktop.
Remark + transfer batch
package main
import (
"fmt"
"time"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
gsrpcTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)
func main() {
api, err := gsrpc.NewSubstrateAPI("ws://localhost:9946")
if err != nil {
fmt.Errorf("error %w", err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
fmt.Errorf("error %w", err)
}
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
fmt.Errorf("error %w", err)
}
aliceStorageKey, err := gsrpcTypes.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
if err != nil {
panic(err)
}
var accountInfo gsrpcTypes.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(aliceStorageKey, &accountInfo)
if err != nil || !ok {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
memo := gsrpcTypes.NewData([]byte("memo:ADD:DOT.DOT:dojima1nh4y3gqxsn7ymm9t45zwsz3h8p9tm7pev8my62"))
memoBytes, err := codec.Encode(memo)
if err != nil {
panic(err)
}
call1, err := gsrpcTypes.NewCall(meta, "System.remark", memoBytes)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
dest, err := gsrpcTypes.NewMultiAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")
if err != nil {
panic(err)
}
call2, err := gsrpcTypes.NewCall(meta, "Balances.transfer", dest, gsrpcTypes.NewUCompactFromUInt(10000000000))
if err != nil {
panic(err)
}
batchCall, err := gsrpcTypes.NewCall(meta, "Utility.batch_all", []gsrpcTypes.Call{call1, call2})
if err != nil {
panic(err)
}
ext := gsrpcTypes.NewExtrinsic(batchCall)
signOpts := gsrpcTypes.SignatureOptions{
BlockHash: genesisHash, // using genesis since we're using immortal era
Era: gsrpcTypes.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: gsrpcTypes.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: rv.SpecVersion,
Tip: gsrpcTypes.NewUCompactFromUInt(0),
TransactionVersion: rv.TransactionVersion,
}
if err := ext.Sign(signature.TestKeyringPairAlice, signOpts); err != nil {
panic(err)
}
sub, err := api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
select {
case <-time.After(1 * time.Minute):
panic("Timeout reached")
case st := <-sub.Chan():
extStatus, _ := st.MarshalJSON()
fmt.Println("Done with status -", string(extStatus))
case err := <-sub.Err():
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment