Skip to content

Commit a74acb4

Browse files
committed
Add tests and fix type issue
1 parent 2e1513a commit a74acb4

File tree

4 files changed

+146
-7
lines changed

4 files changed

+146
-7
lines changed

packages/snaps-controllers/coverage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"branches": 93.36,
2+
"branches": 93.37,
33
"functions": 97.38,
44
"lines": 98.34,
55
"statements": 98.07

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,6 +4354,145 @@ describe('SnapController', () => {
43544354
});
43554355
});
43564356

4357+
describe('onAssetHistoricalPrice', () => {
4358+
it('throws if `onAssetHistoricalPrice` handler returns an invalid response', async () => {
4359+
const rootMessenger = getControllerMessenger();
4360+
const messenger = getSnapControllerMessenger(rootMessenger);
4361+
const snapController = getSnapController(
4362+
getSnapControllerOptions({
4363+
messenger,
4364+
state: {
4365+
snaps: getPersistedSnapsState(),
4366+
},
4367+
}),
4368+
);
4369+
4370+
rootMessenger.registerActionHandler(
4371+
'PermissionController:getPermissions',
4372+
() => ({
4373+
[SnapEndowments.Assets]: {
4374+
caveats: [
4375+
{
4376+
type: SnapCaveatType.ChainIds,
4377+
value: ['bip122:000000000019d6689c085ae165831e93'],
4378+
},
4379+
],
4380+
date: 1664187844588,
4381+
id: 'izn0WGUO8cvq_jqvLQuQP',
4382+
invoker: MOCK_SNAP_ID,
4383+
parentCapability: SnapEndowments.Assets,
4384+
},
4385+
}),
4386+
);
4387+
4388+
rootMessenger.registerActionHandler(
4389+
'SubjectMetadataController:getSubjectMetadata',
4390+
() => MOCK_SNAP_SUBJECT_METADATA,
4391+
);
4392+
4393+
rootMessenger.registerActionHandler(
4394+
'ExecutionService:handleRpcRequest',
4395+
async () =>
4396+
Promise.resolve({
4397+
historicalPrice: { foo: {} },
4398+
}),
4399+
);
4400+
4401+
await expect(
4402+
snapController.handleRequest({
4403+
snapId: MOCK_SNAP_ID,
4404+
origin: 'foo.com',
4405+
handler: HandlerType.OnAssetHistoricalPrice,
4406+
request: {
4407+
jsonrpc: '2.0',
4408+
method: ' ',
4409+
params: {},
4410+
id: 1,
4411+
},
4412+
}),
4413+
).rejects.toThrow(
4414+
`Assertion failed: At path: historicalPrice.intervals -- Expected an object, but received: undefined.`,
4415+
);
4416+
4417+
snapController.destroy();
4418+
});
4419+
4420+
it('returns the value when `onAssetHistoricalPrice` returns a valid response', async () => {
4421+
const rootMessenger = getControllerMessenger();
4422+
const messenger = getSnapControllerMessenger(rootMessenger);
4423+
const snapController = getSnapController(
4424+
getSnapControllerOptions({
4425+
messenger,
4426+
state: {
4427+
snaps: getPersistedSnapsState(),
4428+
},
4429+
}),
4430+
);
4431+
4432+
rootMessenger.registerActionHandler(
4433+
'PermissionController:getPermissions',
4434+
() => ({
4435+
[SnapEndowments.Assets]: {
4436+
caveats: [
4437+
{
4438+
type: SnapCaveatType.ChainIds,
4439+
value: ['bip122:000000000019d6689c085ae165831e93'],
4440+
},
4441+
],
4442+
date: 1664187844588,
4443+
id: 'izn0WGUO8cvq_jqvLQuQP',
4444+
invoker: MOCK_SNAP_ID,
4445+
parentCapability: SnapEndowments.Assets,
4446+
},
4447+
}),
4448+
);
4449+
4450+
rootMessenger.registerActionHandler(
4451+
'SubjectMetadataController:getSubjectMetadata',
4452+
() => MOCK_SNAP_SUBJECT_METADATA,
4453+
);
4454+
4455+
rootMessenger.registerActionHandler(
4456+
'ExecutionService:handleRpcRequest',
4457+
async () =>
4458+
Promise.resolve({
4459+
historicalPrice: {
4460+
intervals: {
4461+
P1D: [[1737548790, '400']],
4462+
},
4463+
updateTime: 1737548790,
4464+
},
4465+
}),
4466+
);
4467+
4468+
expect(
4469+
await snapController.handleRequest({
4470+
snapId: MOCK_SNAP_ID,
4471+
origin: 'foo.com',
4472+
handler: HandlerType.OnAssetHistoricalPrice,
4473+
request: {
4474+
jsonrpc: '2.0',
4475+
method: ' ',
4476+
params: {
4477+
from: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
4478+
to: 'swift:0/iso4217:USD',
4479+
},
4480+
id: 1,
4481+
},
4482+
}),
4483+
).toStrictEqual({
4484+
historicalPrice: {
4485+
intervals: {
4486+
P1D: [[1737548790, '400']],
4487+
},
4488+
updateTime: 1737548790,
4489+
},
4490+
});
4491+
4492+
snapController.destroy();
4493+
});
4494+
});
4495+
43574496
describe('getRpcRequestHandler', () => {
43584497
it('handlers populate the "jsonrpc" property if missing', async () => {
43594498
const rootMessenger = getControllerMessenger();
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 80,
2+
"branches": 80.66,
33
"functions": 89.1,
4-
"lines": 90.64,
5-
"statements": 89.59
4+
"lines": 90.78,
5+
"statements": 89.72
66
}

packages/snaps-sdk/src/types/handlers/asset-historical-price.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export type HistoricalPriceIntervals = {
1919
*
2020
* @property historicalPrice - The historical price object
2121
* @property historicalPrice.intervals - The historical price of the asset pair.
22-
* @property historicalPrice.conversionTime - The time at which the conversion rate was calculated.
23-
* @property historicalPrice.expirationTime - The time at which the conversion rate expires.
22+
* @property historicalPrice.updateTime - The time at which the historical price has been calculated.
23+
* @property historicalPrice.expirationTime - The time at which the historical price expires.
2424
*/
2525
export type OnAssetHistoricalPriceResponse = {
2626
historicalPrice: {
2727
intervals: HistoricalPriceIntervals;
28-
conversionTime: number;
28+
updateTime: number;
2929
expirationTime?: number;
3030
};
3131
} | null;

0 commit comments

Comments
 (0)