github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/teste2e/author_submit_extrinsic_test.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package teste2e 18 19 import ( 20 "fmt" 21 "testing" 22 "time" 23 24 "github.com/stafiprotocol/go-substrate-rpc-client/config" 25 "github.com/stafiprotocol/go-substrate-rpc-client/rpc" 26 "github.com/stafiprotocol/go-substrate-rpc-client/signature" 27 "github.com/stafiprotocol/go-substrate-rpc-client/types" 28 ) 29 30 func TestChain_Events(t *testing.T) { 31 targetURL := config.Default().RPCURL // Replace with desired endpoint 32 rpcs, err := rpc.NewRPCS(targetURL) 33 if err != nil { 34 panic(err) 35 } 36 37 meta, err := rpcs.State.GetMetadataLatest() 38 if err != nil { 39 panic(err) 40 } 41 42 key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil) 43 if err != nil { 44 panic(err) 45 } 46 47 //fmt.Printf("%x\n", key) 48 49 blockNUmber := uint64(0) // Replace with desired block to parse events 50 51 bh, err := rpcs.Chain.GetBlockHash(blockNUmber) 52 if err != nil { 53 panic(err) 54 } 55 56 raw, err := rpcs.State.GetStorageRaw(key, bh) 57 if err != nil { 58 panic(err) 59 } 60 61 //fmt.Printf("%x\n", *raw) 62 63 events := types.EventRecords{} 64 err = types.EventRecordsRaw(*raw).DecodeEventRecords(meta, &events) 65 if err != nil { 66 panic(err) 67 } 68 69 } 70 71 func TestChain_SubmitExtrinsic(t *testing.T) { 72 if testing.Short() { 73 t.Skip("skipping end-to-end test in short mode.") 74 } 75 76 from, ok := signature.LoadKeyringPairFromEnv() 77 if !ok { 78 t.Skip("skipping end-to-end that requires a private key because TEST_PRIV_KEY is not set or empty") 79 } 80 81 rpcs, err := rpc.NewRPCS(config.Default().RPCURL) 82 if err != nil { 83 panic(err) 84 } 85 86 meta, err := rpcs.State.GetMetadataLatest() 87 if err != nil { 88 panic(err) 89 } 90 91 bob, err := types.NewAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48") 92 if err != nil { 93 panic(err) 94 } 95 96 c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(6969)) 97 if err != nil { 98 panic(err) 99 } 100 101 ext := types.NewExtrinsic(c) 102 103 // blockHash, err := rpcs.Chain.GetBlockHashLatest() 104 // if err != nil { 105 // panic(err) 106 // } 107 108 era := types.ExtrinsicEra{IsMortalEra: false} 109 110 genesisHash, err := rpcs.Chain.GetBlockHash(0) 111 if err != nil { 112 panic(err) 113 } 114 115 rv, err := rpcs.State.GetRuntimeVersionLatest() 116 if err != nil { 117 panic(err) 118 } 119 120 key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey, nil) 121 if err != nil { 122 panic(err) 123 } 124 125 var accountInfo types.AccountInfo 126 ok, err = rpcs.State.GetStorageLatest(key, &accountInfo) 127 if err != nil || !ok { 128 panic(err) 129 } 130 131 nonce := uint32(accountInfo.Nonce) 132 133 for i := uint32(0); i < 4; i++ { 134 o := types.SignatureOptions{ 135 // BlockHash: blockHash, 136 BlockHash: genesisHash, // BlockHash needs to == GenesisHash if era is immortal. // TODO: add an error? 137 Era: era, 138 GenesisHash: genesisHash, 139 Nonce: types.NewUCompactFromUInt(uint64(nonce + i)), 140 SpecVersion: rv.SpecVersion, 141 Tip: types.NewUCompactFromUInt(0), 142 TransactionVersion: 1, 143 } 144 145 extI := ext 146 147 err = extI.Sign(from, o) 148 if err != nil { 149 panic(err) 150 } 151 152 extEnc, err := types.EncodeToHexString(extI) 153 if err != nil { 154 panic(err) 155 } 156 157 fmt.Printf("Extrinsic: %#v\n", extEnc) 158 159 _, err = rpcs.Author.SubmitExtrinsic(extI) 160 if err != nil { 161 panic(err) 162 } 163 } 164 165 for i := 0; ; i++ { 166 xts, err := rpcs.Author.PendingExtrinsics() 167 if err != nil { 168 panic(err) 169 } 170 171 fmt.Printf("Pending extrinsics: %#v\n", xts) 172 173 if i >= 2 { 174 break 175 } 176 177 time.Sleep(1 * time.Second) 178 } 179 }