Skip to content

Instantly share code, notes, and snippets.

@cdamian

cdamian/debug.go Secret

Created June 18, 2024 20:31
Show Gist options
  • Save cdamian/9ed06fe165fc307eb8acf83fdba05a53 to your computer and use it in GitHub Desktop.
Save cdamian/9ed06fe165fc307eb8acf83fdba05a53 to your computer and use it in GitHub Desktop.
Check Metadata Debug
package main
import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
gsrpcTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
)
func main() {
api, err := gsrpc.NewSubstrateAPI("wss://westend-rpc.polkadot.io")
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(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)
}
call, err := gsrpcTypes.NewCall(meta, "System.remark", []byte("test"))
if err != nil {
panic(err)
}
ext := gsrpcTypes.NewExtrinsic(call)
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,
CheckMetadataMode: gsrpcTypes.CheckMetadataMode(gsrpcTypes.CheckMetadataModeDisabled),
CheckMetadataHash: gsrpcTypes.CheckMetadataHash{Hash: gsrpcTypes.NewEmptyOption[gsrpcTypes.H256]()},
}
if err := ext.Sign(signature.TestKeyringPairAlice, signOpts); err != nil {
panic(err)
}
encodedExt, err := codec.Encode(ext)
fmt.Printf("Ext - %s\n", hexutil.Encode(encodedExt))
sub, err := api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
for {
select {
case st := <-sub.Chan():
extStatus, _ := st.MarshalJSON()
fmt.Printf("Status for transaction - %s\n", 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