Skip to content

Commit 3459d8f

Browse files
committed
better error handling
1 parent f102ee3 commit 3459d8f

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

src/server/utils/wallets/circle/index.ts

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ export async function provisionCircleWallet({
5151
});
5252

5353
if (!walletSetId) {
54-
const walletSet = await circleDeveloperSdk.createWalletSet({
55-
name: `Engine WalletSet ${new Date().toISOString()}`,
56-
});
54+
const walletSet = await circleDeveloperSdk
55+
.createWalletSet({
56+
name: `Engine WalletSet ${new Date().toISOString()}`,
57+
})
58+
.catch((e) => {
59+
throw new CircleWalletError(
60+
`[Circle] Could not create walletset:\n${JSON.stringify(
61+
e?.response?.data,
62+
)}`,
63+
);
64+
});
5765

5866
walletSetId = walletSet.data?.walletSet.id;
5967
}
@@ -63,12 +71,20 @@ export async function provisionCircleWallet({
6371
"Did not receive walletSetId, and failed to create one automatically",
6472
);
6573

66-
const provisionWalletResponse = await circleDeveloperSdk.createWallets({
67-
accountType: "EOA",
68-
blockchains: ["EVM"],
69-
count: 1,
70-
walletSetId: walletSetId,
71-
});
74+
const provisionWalletResponse = await circleDeveloperSdk
75+
.createWallets({
76+
accountType: "EOA",
77+
blockchains: ["EVM"],
78+
count: 1,
79+
walletSetId: walletSetId,
80+
})
81+
.catch((e) => {
82+
throw new CircleWalletError(
83+
`[Circle] Could not provision wallet:\n${JSON.stringify(
84+
e?.response?.data,
85+
)}`,
86+
);
87+
});
7288

7389
const provisionedWallet = provisionWalletResponse.data?.wallets?.[0];
7490

@@ -119,7 +135,16 @@ export async function getCircleAccount({
119135
entitySecret,
120136
});
121137

122-
const walletResponse = await circleDeveloperSdk.getWallet({ id: walletId });
138+
const walletResponse = await circleDeveloperSdk
139+
.getWallet({ id: walletId })
140+
.catch((e) => {
141+
throw new CircleWalletError(
142+
`[Circle] Could not get wallet with id:${walletId}:\n${JSON.stringify(
143+
e?.response?.data,
144+
)}`,
145+
);
146+
});
147+
123148
if (!walletResponse) {
124149
throw new CircleWalletError(
125150
`Unable to get circle wallet with id:${walletId}`,
@@ -138,10 +163,18 @@ export async function getCircleAccount({
138163
}
139164

140165
async function signTransaction(tx: SerializableTransaction) {
141-
const signature = await circleDeveloperSdk.signTransaction({
142-
walletId,
143-
transaction: stringify(tx),
144-
});
166+
const signature = await circleDeveloperSdk
167+
.signTransaction({
168+
walletId,
169+
transaction: stringify(tx),
170+
})
171+
.catch((e) => {
172+
throw new CircleWalletError(
173+
`[Circle] Could not get transaction signature:\n${JSON.stringify(
174+
e?.response?.data,
175+
)}`,
176+
);
177+
});
145178

146179
if (!signature.data?.signature) {
147180
throw new CircleWalletError("Unable to sign transaction");
@@ -177,10 +210,18 @@ export async function getCircleAccount({
177210
const typedData extends TypedData | Record<string, unknown>,
178211
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
179212
>(_typedData: TypedDataDefinition<typedData, primaryType>): Promise<Hex> {
180-
const signatureResponse = await circleDeveloperSdk.signTypedData({
181-
data: stringify(_typedData),
182-
walletId,
183-
});
213+
const signatureResponse = await circleDeveloperSdk
214+
.signTypedData({
215+
data: stringify(_typedData),
216+
walletId,
217+
})
218+
.catch((e) => {
219+
throw new CircleWalletError(
220+
`[Circle] Could not get signature:\n${JSON.stringify(
221+
e?.response?.data,
222+
)}`,
223+
);
224+
});
184225

185226
if (!signatureResponse.data?.signature) {
186227
throw new CircleWalletError("Could not sign typed data");
@@ -201,11 +242,19 @@ export async function getCircleAccount({
201242
messageToSign = toHex(messageToSign);
202243
}
203244

204-
const signatureResponse = await circleDeveloperSdk.signMessage({
205-
walletId,
206-
message: messageToSign,
207-
encodedByHex: isRawMessage,
208-
});
245+
const signatureResponse = await circleDeveloperSdk
246+
.signMessage({
247+
walletId,
248+
message: messageToSign,
249+
encodedByHex: isRawMessage,
250+
})
251+
.catch((e) => {
252+
throw new CircleWalletError(
253+
`[Circle] Could not get signature:\n${JSON.stringify(
254+
e?.response?.data,
255+
)}`,
256+
);
257+
});
209258

210259
if (!signatureResponse.data?.signature)
211260
throw new CircleWalletError("Could not get signature");

0 commit comments

Comments
 (0)