Skip to content

Commit

Permalink
feedback: update errors and changelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 13, 2024
1 parent 375518f commit cdf1bbd
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 51 deletions.
20 changes: 19 additions & 1 deletion .changeset/orange-beers-smash.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,23 @@

Support activity polling (e.g. for awaiting consensus)

- Add an `activityPoller` parameter for configuring polling behavior
- [Breaking] Update the `activityPoller` parameter for configuring polling behavior
- Polling continues until either a max number of retries is reached, or if the activity hits a terminal status

The shape of the parameter has gone from:

```
{
duration: number;
timeout: number;
}
```

to

```
{
intervalMs: number;
numRetries: number;
}
```
2 changes: 1 addition & 1 deletion .changeset/tasty-feet-provide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Add new helpers and update types and errors

- `getSignatureFromActivity` returns the signature corresponding to a completed activity
- `getSignedTransactionFromActivity` returns the signed transaction corresponding to a completed activity
- `checkActivityStatus` checks the state of an activity and throws an error if the activity either requires consensus or is otherwise not yet completed
- `assertActivityCompleted` checks the state of an activity and throws an error if the activity either requires consensus or is otherwise not yet completed
- `TERMINAL_ACTIVITY_STATUSES` is a const containing all terminal activity statuses. Useful for checking on an activity
- `TurnkeyActivityError` now uses `undefined` instead of `null`
- Export some additional types: `TActivity`, `TActivityId`, `TActivityStatus`, `TActivityType`
6 changes: 3 additions & 3 deletions examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ async function main() {
defaultOrganizationId: process.env.ORGANIZATION_ID!,
// The following config is useful in contexts where an activity requires consensus.
// By default, if the activity is not initially successful, it will poll a maximum
// of 3 times with an interval of 10000 milliseconds.
// of 3 times with an interval of 1000 milliseconds. Otherwise, use the values below.
//
// -----
//
// activityPoller: {
// intervalMs: 10_000,
// numRetries: 5,
// intervalMs: 5_000,
// numRetries: 10,
// },
});

Expand Down
6 changes: 3 additions & 3 deletions examples/with-solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ async function main() {
defaultOrganizationId: organizationId,
// The following config is useful in contexts where an activity requires consensus.
// By default, if the activity is not initially successful, it will poll a maximum
// of 3 times with an interval of 10000 milliseconds.
// of 3 times with an interval of 1000 milliseconds. Otherwise, use the values below.
//
// -----
//
// activityPoller: {
// intervalMs: 10_000,
// numRetries: 5,
// intervalMs: 5_000,
// numRetries: 10,
// },
});

Expand Down
11 changes: 5 additions & 6 deletions packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TurnkeyActivityError,
TurnkeyRequestError,
TurnkeyActivityConsensusNeededError,
checkActivityStatus,
assertActivityCompleted,
assertNonNull,
type TSignature,
} from "@turnkey/http";
Expand Down Expand Up @@ -120,7 +120,7 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer {
timestampMs: String(Date.now()), // millisecond timestamp
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand All @@ -137,7 +137,7 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer {
}
);

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down Expand Up @@ -254,22 +254,21 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer {
timestampMs: String(Date.now()), // millisecond timestamp
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});

result = assertNonNull(activity?.result?.signRawPayloadResult);
} else {
// const trying = this.client as TurnkeyClient;
const { activity, r, s, v } = await this.client.signRawPayload({
signWith: this.signWith,
payload: message,
encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
hashFunction: "HASH_FUNCTION_NO_OP",
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export { init, browserInit } from "./config";
export { TurnkeyRequestError } from "./base";
export {
assertNonNull,
checkActivityStatus,
assertActivityCompleted,
getSignatureFromActivity,
getSignaturesFromActivity,
getSignedTransactionFromActivity,
InvalidArgumentError,
TurnkeyActivityError,
TurnkeyActivityConsensusNeededError,
type TActivity,
Expand Down
44 changes: 34 additions & 10 deletions packages/http/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,31 @@ export class TurnkeyActivityConsensusNeededError extends Error {
}
}

export function checkActivityStatus(input: {
export class InvalidArgumentError extends Error {
activityId: TActivityId | undefined;
activityStatus: TActivityStatus | undefined;
activityType: TActivityType | undefined;
cause: Error | undefined;

constructor(input: {
message: string;
cause?: Error | undefined;
activityId?: TActivityId | undefined;
activityStatus?: TActivityStatus | undefined;
activityType?: TActivityType | undefined;
}) {
const { message, cause, activityId, activityStatus, activityType } = input;
super(message);

this.name = "InvalidArgumentError";
this.activityId = activityId ?? undefined;
this.activityStatus = activityStatus ?? undefined;
this.activityType = activityType ?? undefined;
this.cause = cause ?? undefined;
}
}

export function assertActivityCompleted(input: {
id: string;
status: TActivityStatus;
}) {
Expand Down Expand Up @@ -114,14 +138,14 @@ export function getSignatureFromActivity(activity: TActivity): TSignature {
"ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
].includes(activity.type)
) {
throw new TurnkeyActivityError({
message: `Unexpected activity type: ${activity.type}`,
throw new InvalidArgumentError({
message: `Cannot get signature from activity type: ${activity.type}`,
activityId: activity.id,
activityStatus: activity.status,
});
}

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand All @@ -140,14 +164,14 @@ export function getSignatureFromActivity(activity: TActivity): TSignature {
*/
export function getSignaturesFromActivity(activity: TActivity): TSignature[] {
if (!["ACTIVITY_TYPE_SIGN_RAW_PAYLOADS"].includes(activity.type)) {
throw new TurnkeyActivityError({
message: `Unexpected activity type: ${activity.type}`,
throw new InvalidArgumentError({
message: `Cannot get signature from activity type: ${activity.type}`,
activityId: activity.id,
activityStatus: activity.status,
});
}

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand All @@ -173,14 +197,14 @@ export function getSignedTransactionFromActivity(
"ACTIVITY_TYPE_SIGN_TRANSACTION_V2",
].includes(activity.type)
) {
throw new TurnkeyActivityError({
message: `Unexpected activity type: ${activity.type}`,
throw new InvalidArgumentError({
message: `Cannot get signed transaction from activity type: ${activity.type}`,
activityId: activity.id,
activityStatus: activity.status,
});
}

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down
14 changes: 6 additions & 8 deletions packages/sdk-browser/src/__types__/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TActivityId, TurnkeyApiTypes } from "@turnkey/http";
import type { TActivityId, TActivityStatus } from "@turnkey/http";

export type GrpcStatus = {
message: string;
Expand All @@ -25,13 +25,6 @@ export type THttpConfig = {
baseUrl: string;
};

export type TActivityPollerConfig = {
intervalMs: number;
numRetries: number;
};

export type TActivityStatus = TurnkeyApiTypes["v1ActivityStatus"];

export class TurnkeyRequestError extends Error {
details: any[] | null;
code: number;
Expand Down Expand Up @@ -66,6 +59,11 @@ export interface ActivityMetadata {
};
}

export type TActivityPollerConfig = {
intervalMs: number;
numRetries: number;
};

interface BaseSDKClientConfig {
apiBaseUrl: string;
organizationId: string;
Expand Down
14 changes: 6 additions & 8 deletions packages/sdk-server/src/__types__/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TActivityId, TurnkeyApiTypes } from "@turnkey/http";
import type { TActivityId, TActivityStatus } from "@turnkey/http";

export type GrpcStatus = {
message: string;
Expand All @@ -25,11 +25,6 @@ export type THttpConfig = {
baseUrl: string;
};

export type TActivityPollerConfig = {
intervalMs: number;
numRetries: number;
};

export class TurnkeyRequestError extends Error {
details: any[] | null;
code: number;
Expand Down Expand Up @@ -73,6 +68,11 @@ export type commandOverrideParams = {
timestampMs?: string;
};

export type TActivityPollerConfig = {
intervalMs: number;
numRetries: number;
};

export interface TurnkeySDKClientConfig {
stamper: TStamper;
apiBaseUrl: string;
Expand All @@ -92,8 +92,6 @@ export interface TurnkeyProxyHandlerConfig {
allowedMethods?: string[];
}

export type TActivityStatus = TurnkeyApiTypes["v1ActivityStatus"];

export interface NextApiRequest {
body: any;
query: { [key: string]: string };
Expand Down
10 changes: 5 additions & 5 deletions packages/solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "@solana/web3.js";
import {
assertNonNull,
checkActivityStatus,
assertActivityCompleted,
TurnkeyClient,
type TSignature,
} from "@turnkey/http";
Expand Down Expand Up @@ -115,7 +115,7 @@ export class TurnkeySigner {

const { id, status, result } = response.activity;

checkActivityStatus({
assertActivityCompleted({
id,
status,
});
Expand All @@ -131,7 +131,7 @@ export class TurnkeySigner {
hashFunction: "HASH_FUNCTION_NOT_APPLICABLE",
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down Expand Up @@ -162,7 +162,7 @@ export class TurnkeySigner {

const { id, status, result } = response.activity;

checkActivityStatus({
assertActivityCompleted({
id,
status,
});
Expand All @@ -178,7 +178,7 @@ export class TurnkeySigner {
hashFunction: "HASH_FUNCTION_NOT_APPLICABLE",
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from "viem";
import {
assertNonNull,
checkActivityStatus,
assertActivityCompleted,
TActivityStatus,
TActivityId,
TSignature,
Expand Down Expand Up @@ -361,7 +361,7 @@ async function signTransactionImpl(
timestampMs: String(Date.now()), // millisecond timestamp
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand All @@ -376,7 +376,7 @@ async function signTransactionImpl(
unsignedTransaction: unsignedTransaction,
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down Expand Up @@ -446,7 +446,7 @@ async function signMessageImpl(
timestampMs: String(Date.now()), // millisecond timestamp
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand All @@ -460,7 +460,7 @@ async function signMessageImpl(
hashFunction: "HASH_FUNCTION_NO_OP",
});

checkActivityStatus({
assertActivityCompleted({
id: activity.id,
status: activity.status,
});
Expand Down

0 comments on commit cdf1bbd

Please sign in to comment.