diff --git a/access/grpc/grpc.go b/access/grpc/grpc.go index 7e9e9138d..a033daed1 100644 --- a/access/grpc/grpc.go +++ b/access/grpc/grpc.go @@ -1284,7 +1284,7 @@ func (c *BaseClient) SendAndSubscribeTransactionStatuses( defer close(txStatusChan) defer close(errChan) - messageIndex := uint64(0) + messageIndex := uint64(1) for { txResultsResponse, err := subscribeClient.Recv() diff --git a/access/grpc/grpc_test.go b/access/grpc/grpc_test.go index 7aa83b6df..fc0df4a8b 100644 --- a/access/grpc/grpc_test.go +++ b/access/grpc/grpc_test.go @@ -2539,7 +2539,7 @@ func TestClient_SendAndSubscribeTransactionStatuses(t *testing.T) { var resTransactionResults []*access.SendAndSubscribeTransactionStatusesResponse results := test.TransactionResultGenerator(encodingVersion) - for i := uint64(0); i < count; i++ { + for i := uint64(1); i <= count; i++ { expectedResult := results.New() transactionResult, _ := convert.TransactionResultToMessage(expectedResult, encodingVersion) @@ -2573,7 +2573,7 @@ func TestClient_SendAndSubscribeTransactionStatuses(t *testing.T) { wg.Add(1) go assertNoErrors(t, errCh, wg.Done) - expectedCounter := uint64(0) + expectedCounter := uint64(1) for i := uint64(0); i < responseCount; i++ { actualTxResult := <-txResultCh @@ -2608,7 +2608,7 @@ func TestClient_SendAndSubscribeTransactionStatuses(t *testing.T) { wg.Add(1) go assertNoErrors(t, errCh, wg.Done) - expectedCounter := uint64(0) + expectedCounter := uint64(1) for i := uint64(0); i < responseCount; i++ { actualTxResult := <-txResultCh expectedTxResult, err := convert.MessageToTransactionResult(stream.responses[i].GetTransactionResults(), DefaultClientOptions().jsonOptions) diff --git a/examples/send_and_subscribe_transaction_statuses/main.go b/examples/send_and_subscribe_transaction_statuses/main.go index a4da405f5..931f684bb 100644 --- a/examples/send_and_subscribe_transaction_statuses/main.go +++ b/examples/send_and_subscribe_transaction_statuses/main.go @@ -21,10 +21,11 @@ package main import ( "context" "fmt" - - "github.com/onflow/flow-go-sdk/access/grpc" + "log" "github.com/onflow/flow-go-sdk" + "github.com/onflow/flow-go-sdk/access/grpc" + "github.com/onflow/flow-go-sdk/crypto" "github.com/onflow/flow-go-sdk/examples" ) @@ -34,14 +35,33 @@ func main() { func demo() { ctx := context.Background() - flowClient, err := grpc.NewClient(grpc.EmulatorHost) + flowClient, err := grpc.NewClient(grpc.TestnetHost) examples.Handle(err) - serviceAcctAddr, serviceAcctKey, serviceSigner := examples.ServiceAccount(flowClient) + signerIndex := uint32(0) + + signerPublicAddress := flow.HexToAddress("YOUR_ACCOUNT_ADDRESS") + signerAccount, err := flowClient.GetAccount(ctx, signerPublicAddress) + if err != nil { + log.Fatalf("Failed to get account: %v", err) + } + seqNumber := signerAccount.Keys[0].SequenceNumber + + privateKeyHex := "YOUR_PRIVATE_KEY" + privateKey, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, privateKeyHex) + if err != nil { + log.Fatalf("Failed to decode private key: %v", err) + } + + // Create crypto signer + signer, err := crypto.NewInMemorySigner(privateKey, crypto.SHA3_256) + if err != nil { + log.Fatalf("Failed to decode private key: %v", err) + } tx := flow.NewTransaction(). - SetPayer(serviceAcctAddr). - SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber). + SetPayer(signerPublicAddress). + SetProposalKey(signerPublicAddress, signerIndex, seqNumber). SetScript([]byte(` transaction { prepare(acc: &Account) {} @@ -50,27 +70,30 @@ func demo() { } } `)). - AddAuthorizer(serviceAcctAddr). + AddAuthorizer(signerPublicAddress). SetReferenceBlockID(examples.GetReferenceBlockId(flowClient)) - err = tx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner) + err = tx.SignEnvelope(signerPublicAddress, signerIndex, signer) examples.Handle(err) txResultChan, errChan, initErr := flowClient.SendAndSubscribeTransactionStatuses(ctx, *tx) examples.Handle(initErr) - select { - case <-ctx.Done(): - return - case txResult, ok := <-txResultChan: - if !ok { - panic("transaction result channel is closed") - } - examples.Print(txResult) - case err, ok := <-errChan: - if !ok { - panic("error channel is closed") + for { + select { + case <-ctx.Done(): + return + case txResult, ok := <-txResultChan: + if !ok { + examples.Print("transaction result channel is closed") + return + } + examples.Print(txResult) + case err := <-errChan: + if err != nil { + examples.Print(fmt.Errorf("~~~ ERROR: %w ~~~\n", err)) + } + return } - fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error()) } }