WaitTxConfirmation waits for the transaction with target hash txHash to be confirmed. It also returns if any error occurs or a final state is returned from BP.
( ctx context.Context, txHash hash.Hash)
| 462 | // WaitTxConfirmation waits for the transaction with target hash txHash to be confirmed. It also |
| 463 | // returns if any error occurs or a final state is returned from BP. |
| 464 | func WaitTxConfirmation( |
| 465 | ctx context.Context, txHash hash.Hash) (state interfaces.TransactionState, err error, |
| 466 | ) { |
| 467 | var ( |
| 468 | ticker = time.NewTicker(1 * time.Second) |
| 469 | method = route.MCCQueryTxState |
| 470 | req = &types.QueryTxStateReq{Hash: txHash} |
| 471 | resp = &types.QueryTxStateResp{} |
| 472 | count = 0 |
| 473 | ) |
| 474 | defer ticker.Stop() |
| 475 | defer fmt.Printf("\n") |
| 476 | for { |
| 477 | if err = requestBP(method, req, resp); err != nil { |
| 478 | err = errors.Wrapf(err, "failed to call %s", method) |
| 479 | return |
| 480 | } |
| 481 | |
| 482 | state = resp.State |
| 483 | |
| 484 | count++ |
| 485 | fmt.Printf("\rWaiting blockproducers confirmation %vs, state: %v\033[0K", count, state) |
| 486 | log.WithFields(log.Fields{ |
| 487 | "tx_hash": txHash, |
| 488 | "tx_state": state, |
| 489 | }).Debug("waiting for tx confirmation") |
| 490 | |
| 491 | switch state { |
| 492 | case interfaces.TransactionStatePending: |
| 493 | case interfaces.TransactionStatePacked: |
| 494 | case interfaces.TransactionStateConfirmed, |
| 495 | interfaces.TransactionStateExpired, |
| 496 | interfaces.TransactionStateNotFound: |
| 497 | return |
| 498 | default: |
| 499 | err = errors.Errorf("unknown transaction state %d", state) |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | select { |
| 504 | case <-ticker.C: |
| 505 | case <-ctx.Done(): |
| 506 | err = ctx.Err() |
| 507 | return |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | func getNonce(addr proto.AccountAddress) (nonce interfaces.AccountNonce, err error) { |
| 513 | nonceReq := new(types.NextAccountNonceReq) |