Skip to content

Commit

Permalink
fix: dont throw on missing smart wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Nov 16, 2023
1 parent 8f4eb02 commit ddf428a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
11 changes: 7 additions & 4 deletions packages/rpc/src/batchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@ export const batchVstorageQuery = (
(Array.isArray(res) ? res : [res]).map(entry => {
const { id: index } = entry;
if (entry.result.response.code) {
const { code, codespace, log } = entry.result.response;
return [
pathToKey(paths[index]),
{ error: entry.result.response.log },
{ error: { code, codespace, log } },
];
}

if (!entry.result.response.value) {
return [
pathToKey(paths[index]),
{
error: `Cannot parse value of response for path [${
paths[index]
}]: ${JSON.stringify(entry)}`,
error: {
log: `Cannot parse value of response for path [${
paths[index]
}]: ${JSON.stringify(entry)}`,
},
},
];
}
Expand Down
9 changes: 5 additions & 4 deletions packages/rpc/src/chainStorageWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { UpdateHandler } from './types';

type Subscriber<T> = {
onUpdate: UpdateHandler<T>;
onError?: (log: string) => void;
onError?: (log: string, code?: number, codespace?: string) => void;
};

const defaults = {
Expand All @@ -25,7 +25,7 @@ const randomRefreshPeriod = (

const makePathSubscriber = <T>(
onUpdate: UpdateHandler<T>,
onError?: (log: string) => void,
onError?: (log: string, code?: number, codespace?: string) => void,
) => ({
onUpdate,
onError,
Expand Down Expand Up @@ -116,8 +116,9 @@ export const makeAgoricChainStorageWatcher = (

if (data[path].error) {
subscribers.forEach(s => {
const { log, code, codespace } = harden(data[path].error);
if (s.onError) {
s.onError(harden(data[path].error));
s.onError(log, code, codespace);
}
});
return;
Expand Down Expand Up @@ -177,7 +178,7 @@ export const makeAgoricChainStorageWatcher = (
const watchLatest = <T>(
path: [AgoricChainStoragePathKind, string],
onUpdate: (latestValue: T) => void,
onPathError?: (log: string) => void,
onPathError?: (log: string, code?: number, codespace?: string) => void,
) => {
const pathKey = pathToKey(path);
const subscriber = makePathSubscriber(onUpdate, onPathError);
Expand Down
71 changes: 41 additions & 30 deletions packages/rpc/test/chainStorageWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ describe('makeAgoricChainStorageWatcher', () => {
});

it('handles errors', async () => {
const expected = 'test error log';
const expectedLog = 'test error log';
const expectedCodespace = 'sdk';
const expectedCode = 6;
const path = 'vitest.fakePath';

fetch.mockResolvedValue(
Expand All @@ -235,22 +237,27 @@ describe('makeAgoricChainStorageWatcher', () => {
id: 0,
value: null,
kind: AgoricChainStoragePathKind.Children,
code: 6,
log: expected,
code: expectedCode,
log: expectedLog,
codespace: 'sdk',
},
]),
);

const result = future<string>();
const result = future<{ log: string; codespace?: string; code?: number }>();
watcher.watchLatest<string>(
[AgoricChainStoragePathKind.Children, path],
_value => {
/* noop */
},
result.resolve,
(log: string, code?: number, codespace?: string) =>
result.resolve({ log, code, codespace }),
);
vi.advanceTimersToNextTimer();
expect(await result.value).toEqual(expected);
const { log, code, codespace } = await result.value;
expect(log).toEqual(expectedLog);
expect(code).toEqual(expectedCode);
expect(codespace).toEqual(expectedCodespace);
});

it('can unsubscribe from paths', async () => {
Expand Down Expand Up @@ -291,39 +298,43 @@ describe('makeAgoricChainStorageWatcher', () => {

const createFetchResponse = (
values: {
kind?: AgoricChainStoragePathKind;
id: number;
value: unknown;
kind?: AgoricChainStoragePathKind;
blockHeight?: number;
code?: number;
log?: string;
id: number;
codespace?: string;
}[],
) => ({
json: () =>
new Promise(res =>
res(
values.map(({ kind, value, blockHeight, code = 0, log, id }) => {
const data =
kind === AgoricChainStoragePathKind.Children
? { children: value }
: {
value: JSON.stringify({
values: [JSON.stringify(marshal(value))],
blockHeight: String(blockHeight ?? 0),
}),
};

return {
id,
result: {
response: {
value: window.btoa(JSON.stringify(data)),
code,
log,
values.map(
({ kind, value, blockHeight, code = 0, log, id, codespace }) => {
const data =
kind === AgoricChainStoragePathKind.Children
? { children: value }
: {
value: JSON.stringify({
values: [JSON.stringify(marshal(value))],
blockHeight: String(blockHeight ?? 0),
}),
};

return {
id,
result: {
response: {
value: window.btoa(JSON.stringify(data)),
code,
log,
codespace,
},
},
},
};
}),
};
},
),
),
),
});
Expand Down Expand Up @@ -364,7 +375,7 @@ const createUnserializedFetchResponse = (
const future = <T>() => {
let resolve: (value: T) => void;
let isComplete = false;
const value = new Promise(res => {
const value = new Promise<T>(res => {
resolve = (v: T) => {
isComplete = true;
res(v);
Expand Down
11 changes: 8 additions & 3 deletions packages/web-components/src/wallet-connection/watchWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,21 @@ export const watchWallet = async (chainStorageWatcher, address) => {
harden(currentPaths),
);
},
err => {
(log, code, codespace) => {
// Check the error code to see if smart wallet just does not exist.
// Otherwise, there was an error doing the query, so throw.
//
// Also check the log for backwards compatibility, as the code changed.
if (
!lastPaths &&
err === 'could not get vstorage path: unknown request'
(log === 'could not get vstorage path: unknown request' ||
(code === 38 && codespace === 'sdk'))
) {
smartWalletStatusNotifierKit.updater.updateState(
harden({ provisioned: false }),
);
} else {
throw Error(err);
throw Error(log);
}
},
);
Expand Down

0 comments on commit ddf428a

Please sign in to comment.