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

@turnkey/http: add withAsyncPolling(...) helper #23

Merged
merged 8 commits into from
Apr 11, 2023
Merged

Conversation

keyz-tk
Copy link
Contributor

@keyz-tk keyz-tk commented Apr 11, 2023

Summary & Motivation

All Turnkey mutation endpoints are asynchronous (with the exception of signing endpoints) because we support consensus via policy on all write actions. To simplify async mutations, this PR adds a withAsyncPolling(...) wrapper. Here's a quick example:

import {
  PublicApiService,
  withAsyncPolling,
  TurnkeyActivityError,
} from "@turnkey/http";

// Use `withAsyncPolling(...)` to wrap & create a fetcher with built-in async polling support
const fetcher = withAsyncPolling({
  request: PublicApiService.postCreatePrivateKeys,
  refreshIntervalMs: 500,
});

// The fetcher remains fully typed. After submitting the request,
// it'll poll until the activity reaches a terminal state.
try {
  const activity = await fetcher({
    body: {
      /* ... */
    },
  });

  // Success!
  console.log(activity.result.createPrivateKeysResult?.privateKeyIds?.[0]);
} catch (error) {
  if (error instanceof TurnkeyActivityError) {
    // In case the activity is rejected, failed, or requires consensus,
    // a rich `TurnkeyActivityError` will be thrown. You can read from
    // `TurnkeyActivityError` to find out why the activity didn't succeed.
    //
    // For instance, if your activity requires consensus and doesn't have
    // enough approvals, you can get the `activityId` from `TurnkeyActivityError`,
    // store it somewhere, then re-fetch the activity via `.postGetActivity(...)`
    // when the required approvals/rejections are in place.
  }
}

Note to reviewer

Feel free to review commit by commit (they are stacked). See changes in createNewEthereumPrivateKey.ts as a real word example.

How I Tested These Changes

  • CI is green
  • Ran the with-ethers example locally

@keyz-tk keyz-tk changed the title [WIP] Async helpers @turnkey/http: add withAsyncPolling(...) helper Apr 11, 2023
@keyz-tk keyz-tk marked this pull request as ready for review April 11, 2023 22:23
Copy link
Contributor

@r-n-o r-n-o left a comment

Choose a reason for hiding this comment

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

Nice!!

examples/deployer/src/createNewEthereumPrivateKey.ts Outdated Show resolved Hide resolved
examples/with-gnosis/src/createNewEthereumPrivateKey.ts Outdated Show resolved Hide resolved
@@ -76,3 +76,13 @@ main().catch((error) => {
process.exit(1);
});
```

## More Examples
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

packages/http/README.md Outdated Show resolved Hide resolved
packages/http/README.md Outdated Show resolved Hide resolved
packages/http/src/__tests__/async-test.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@r-n-o r-n-o left a comment

Choose a reason for hiding this comment

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

🚢

@keyz-tk keyz-tk merged commit 34e3ba6 into main Apr 11, 2023
@keyz-tk keyz-tk deleted the keyan/async branch April 19, 2023 19:22
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.

3 participants