Skip to content

Commit

Permalink
Merge pull request #270 from onflow/gregor/tx-send-status
Browse files Browse the repository at this point in the history
Report Cadence transaction status
  • Loading branch information
devbugging authored May 28, 2024
2 parents 9d1196a + 7147b50 commit 1ee7712
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"math/big"
"strings"
"time"

"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
Expand Down Expand Up @@ -533,9 +534,14 @@ func (e *EVM) executeScriptAtHeight(
)
}

// signAndSend creates a flow transaction from the provided script with the arguments and signs it with the
// configured COA account and then submits it to the network.
func (e *EVM) signAndSend(ctx context.Context, script []byte, args ...cadence.Value) (flow.Identifier, error) {
// signAndSend creates a flow transaction from the provided script
// with the arguments and signs it with the configured COA account
// and then submits it to the network.
func (e *EVM) signAndSend(
ctx context.Context,
script []byte,
args ...cadence.Value,
) (flow.Identifier, error) {
var (
g = errgroup.Group{}
err1, err2 error
Expand Down Expand Up @@ -578,6 +584,41 @@ func (e *EVM) signAndSend(ctx context.Context, script []byte, args ...cadence.Va
return flow.EmptyID, fmt.Errorf("failed to send transaction: %w", err)
}

// get transaction status after it is submitted
go func(id flow.Identifier) {
const fetchInterval = time.Millisecond * 500
const fetchTimeout = time.Second * 60

fetchTicker := time.NewTicker(fetchInterval)
timeoutTicker := time.NewTicker(fetchTimeout)

defer fetchTicker.Stop()
defer timeoutTicker.Stop()

for {
select {
case <-fetchTicker.C:
res, err := e.client.GetTransactionResult(context.Background(), id)
if err != nil {
e.logger.Error().Str("id", id.String()).Err(err).Msg("failed to get transaction result")
return
}
if res != nil && res.Status > flow.TransactionStatusPending {
e.logger.Info().
Str("status", res.Status.String()).
Str("id", id.String()).
Int("events", len(res.Events)).
Err(res.Error).
Msg("transaction result received")
return
}
case <-timeoutTicker.C:
e.logger.Error().Str("id", id.String()).Msg("could not get transaction result")
return
}
}
}(flowTx.ID())

return flowTx.ID(), nil
}

Expand Down

0 comments on commit 1ee7712

Please sign in to comment.