Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fail if the tx is dropped #12

Merged
merged 1 commit into from
May 9, 2018
Merged

Conversation

nicosantangelo
Copy link
Contributor

also removes repeated code for fetching transaction statuses.

The idea is to make waitForCompletion aware of dropped transactions, which return as null, but give it some time to wait in case there's congestion (hence the retriesOnEmpty).

I'm not in love with the impl, comments are welcomed

@@ -43,14 +43,22 @@ export namespace txUtils {
* @return {object} data - Current transaction data. See {@link txUtils#getTransaction}
*/
export async function waitForCompletion(txId: string): Promise<any> {
let retriesOnEmpty = 5 // (5 * TRANSACTION_FETCH_DELAY) seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a configurable function parameter with a default value?

@@ -43,14 +43,22 @@ export namespace txUtils {
* @return {object} data - Current transaction data. See {@link txUtils#getTransaction}
*/
export async function waitForCompletion(txId: string): Promise<any> {
let retriesOnEmpty = 5 // (5 * TRANSACTION_FETCH_DELAY) seconds

while (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have a discrete number of amounts to iterate, we could replace the implementation by a for-loop an it would be cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of, because the idea is to wait until the transaction ends or it's dropped. I'm thinking this is a better implementation

  export async function waitForCompletion(txId: string, retriesOnEmpty: number = 5): Promise<any> {
    const isDropped = await isTxDropped(txId, retriesOnEmpty)
    if (isDropped) {
      return { hash: txId, status: TRANSACTION_STATUS.failed }
    }

    while (true) {
      const tx = await getTransaction(txId)
      
      if (!isPending(tx) && tx.recepeit) {
        return tx
      }

      await sleep(TRANSACTION_FETCH_DELAY)
    }
  }

  export async function isTxDropped(txId: string, retryAttemps: number = 5) : boolean {
    while (retryAttemps > 0) {
      const tx = await getTransaction(txId)

      if (tx !== null) {
        return false
      }

      retryAttemps -= 1
      await sleep(TRANSACTION_FETCH_DELAY)
    }

    return true
  }

what do you think?

@nicosantangelo nicosantangelo force-pushed the feat/fail-on-dropped-tx branch 3 times, most recently from f0e8c7f to 323ed8c Compare May 8, 2018 15:54
@menduz
Copy link
Member

menduz commented May 9, 2018

@nicosantangelo
Copy link
Contributor Author

nicosantangelo commented May 9, 2018

Let me rebase and squash so it looks cleaner!

Edit: Done!

@nicosantangelo nicosantangelo force-pushed the feat/fail-on-dropped-tx branch from 6a18e8e to b83f706 Compare May 9, 2018 12:46
@menduz menduz merged commit 5f1065f into master May 9, 2018
@nicosantangelo nicosantangelo deleted the feat/fail-on-dropped-tx branch May 9, 2018 14:49
nachomazzara added a commit that referenced this pull request May 16, 2018
* master:
  feat: dont fail in connect using only a providerUrl (#13)
  feat: fail if the tx is dropped (#12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants