Created
July 3, 2024 15:45
-
-
Save cdamian/0f329ae74f5c0ef5fae5e9cef0ca8cec to your computer and use it in GitHub Desktop.
Script for testing dynamic extrinsics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" | |
"github.com/centrifuge/go-substrate-rpc-client/v4/types" | |
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" | |
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic" | |
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic/extensions" | |
"github.com/ethereum/go-ethereum/common/hexutil" | |
"github.com/centrifuge/go-substrate-rpc-client/v4/signature" | |
) | |
const ( | |
AlicePubKeyHex = "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" | |
BobPubKeyHex = "0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" | |
CharliePubKeyHex = "0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22" | |
) | |
func mustDecodeHexString(s string) []byte { | |
b, err := hexutil.Decode(s) | |
if err != nil { | |
panic(err) | |
} | |
return b | |
} | |
var ( | |
AliceKeyRingPair = signature.KeyringPair{ | |
URI: "//Alice", | |
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", | |
PublicKey: mustDecodeHexString(AlicePubKeyHex), | |
} | |
BobKeyRingPair = signature.KeyringPair{ | |
URI: "//Bob", | |
Address: "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty", | |
PublicKey: mustDecodeHexString(BobPubKeyHex), | |
} | |
CharlieKeyRingPair = signature.KeyringPair{ | |
URI: "//Charlie", | |
Address: "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y", | |
PublicKey: mustDecodeHexString(CharliePubKeyHex), | |
} | |
) | |
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) | |
} | |
// Alice cannot be used on wested due to lack of funds(?) | |
accountStorageKey, err := types.CreateStorageKey(meta, "System", "Account", BobKeyRingPair.PublicKey) | |
if err != nil { | |
panic(err) | |
} | |
var accountInfo types.AccountInfo | |
ok, err := api.RPC.State.GetStorageLatest(accountStorageKey, &accountInfo) | |
if err != nil || !ok { | |
panic(err) | |
} | |
call, err := types.NewCall(meta, "System.remark", []byte("test")) | |
if err != nil { | |
panic(err) | |
} | |
ext := extrinsic.NewDynamicExtrinsic(&call) | |
err = ext.Sign( | |
BobKeyRingPair, | |
meta, | |
extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash), | |
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(accountInfo.Nonce))), | |
extrinsic.WithTip(types.NewUCompactFromUInt(0)), | |
extrinsic.WithSpecVersion(rv.SpecVersion), | |
extrinsic.WithTransactionVersion(rv.TransactionVersion), | |
extrinsic.WithGenesisHash(genesisHash), | |
extrinsic.WithMetadataMode(extensions.CheckMetadataModeDisabled, extensions.CheckMetadataHash{Hash: types.NewEmptyOption[types.H256]()}), | |
) | |
if err != nil { | |
panic(err) | |
} | |
encodedExt, err := codec.EncodeToHex(ext) | |
fmt.Printf("Ext - %s\n", encodedExt) | |
sub, err := api.RPC.Author.SubmitAndWatchDynamicExtrinsic(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