Skip to content

Commit

Permalink
Merge pull request #798 from The-K-R-O-K/AndriiSlisarchuk/6604-fix-se…
Browse files Browse the repository at this point in the history
…nd-and-subscribe-example

[Access] Fix SendAndSubscribeTransactionStatuses and example
  • Loading branch information
peterargue authored Dec 19, 2024
2 parents 4e3e26d + ca1c052 commit 64dfdc9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions access/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
63 changes: 43 additions & 20 deletions examples/send_and_subscribe_transaction_statuses/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {}
Expand All @@ -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())
}
}

0 comments on commit 64dfdc9

Please sign in to comment.