github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/teste2e/author_submit_and_watch_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 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) { 32 if testing.Short() { 33 t.Skip("skipping end-to-end test in short mode.") 34 } 35 36 from, ok := signature.LoadKeyringPairFromEnv() 37 if !ok { 38 t.Skip("skipping end-to-end that requires a private key because TEST_PRIV_KEY is not set or empty") 39 } 40 41 rpcs, err := rpc.NewRPCS(config.Default().RPCURL) 42 if err != nil { 43 panic(err) 44 } 45 46 meta, err := rpcs.State.GetMetadataLatest() 47 if err != nil { 48 panic(err) 49 } 50 51 bob, err := types.NewAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48") 52 if err != nil { 53 panic(err) 54 } 55 56 c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompactFromUInt(6969)) 57 if err != nil { 58 panic(err) 59 } 60 61 ext := types.NewExtrinsic(c) 62 if err != nil { 63 panic(err) 64 } 65 66 era := types.ExtrinsicEra{IsMortalEra: false} 67 68 genesisHash, err := rpcs.Chain.GetBlockHash(0) 69 if err != nil { 70 panic(err) 71 } 72 73 rv, err := rpcs.State.GetRuntimeVersionLatest() 74 if err != nil { 75 panic(err) 76 } 77 78 key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey, nil) 79 if err != nil { 80 panic(err) 81 } 82 83 var accountInfo types.AccountInfo 84 ok, err = rpcs.State.GetStorageLatest(key, &accountInfo) 85 if err != nil || !ok { 86 panic(err) 87 } 88 89 nonce := uint32(accountInfo.Nonce) 90 91 o := types.SignatureOptions{ 92 // BlockHash: blockHash, 93 BlockHash: genesisHash, // BlockHash needs to == GenesisHash if era is immortal. // TODO: add an error? 94 Era: era, 95 GenesisHash: genesisHash, 96 Nonce: types.NewUCompactFromUInt(uint64(nonce)), 97 SpecVersion: rv.SpecVersion, 98 Tip: types.NewUCompactFromUInt(0), 99 TransactionVersion: 1, 100 } 101 102 err = ext.Sign(from, o) 103 if err != nil { 104 panic(err) 105 } 106 107 sub, err := rpcs.Author.SubmitAndWatchExtrinsic(ext) 108 if err != nil { 109 panic(err) 110 } 111 defer sub.Unsubscribe() 112 113 timeout := time.After(10 * time.Second) 114 115 for { 116 select { 117 case status := <-sub.Chan(): 118 fmt.Printf("%#v\n", status) 119 120 if status.IsInBlock { 121 assert.False(t, types.Eq(status.AsInBlock, types.ExtrinsicStatus{}.AsInBlock), 122 "expected AsFinalized not to be empty") 123 return 124 } 125 case <-timeout: 126 assert.FailNow(t, "timeout of 10 seconds reached without getting finalized status for extrinsic") 127 return 128 } 129 } 130 }