-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement assigning args to runtime dynamic ds (#265)
* Implement assigning args to runtime dynamic ds * Update changelog
- Loading branch information
Showing
3 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import { CacheMetadataModel } from '@subql/node-core'; | ||
import { CosmosDatasourceKind, CosmosHandlerKind } from '@subql/types-cosmos'; | ||
import { SubqueryProject } from '../configure/SubqueryProject'; | ||
import { DynamicDsService } from './dynamic-ds.service'; | ||
|
||
function getMetadata(): CacheMetadataModel { | ||
let dynamicDs: any[] = []; | ||
|
||
const metadata = { | ||
set: (key: string, data: any[]) => { | ||
dynamicDs = data; | ||
}, | ||
setNewDynamicDatasource: (newDs: any) => { | ||
dynamicDs.push(newDs); | ||
}, | ||
find: (key: string) => Promise.resolve(dynamicDs), | ||
}; | ||
|
||
return metadata as CacheMetadataModel; | ||
} | ||
|
||
describe('Creating dynamic ds', () => { | ||
let dynamiDsService: DynamicDsService; | ||
let project: SubqueryProject; | ||
|
||
beforeEach(async () => { | ||
project = new SubqueryProject( | ||
'', | ||
'', | ||
null, | ||
[], | ||
null, | ||
[ | ||
{ | ||
name: 'cosmos', | ||
kind: CosmosDatasourceKind.Runtime, | ||
mapping: { | ||
file: '', | ||
handlers: [ | ||
{ | ||
handler: 'handleEvent', | ||
kind: CosmosHandlerKind.Event, | ||
filter: { | ||
type: 'execute', | ||
messageFilter: { | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
}, | ||
}, | ||
}, | ||
{ | ||
handler: 'handleMessage', | ||
kind: CosmosHandlerKind.Message, | ||
filter: { | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
null, | ||
null, | ||
null, | ||
); | ||
dynamiDsService = new DynamicDsService(null, project); | ||
|
||
await dynamiDsService.init(getMetadata()); | ||
}); | ||
|
||
// Cant test this because createDynamicDatasource calls process.exit on error | ||
it.skip('Should validate the arguments', async () => { | ||
await expect( | ||
dynamiDsService.createDynamicDatasource({ | ||
templateName: 'cosmos', | ||
startBlock: 100, | ||
args: [] as any, | ||
}), | ||
).rejects.toThrow(); | ||
|
||
await expect( | ||
dynamiDsService.createDynamicDatasource({ | ||
templateName: 'cosmos', | ||
startBlock: 100, | ||
args: { | ||
notValues: {}, | ||
attributes: [], | ||
}, | ||
}), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('Should be able to set an address for a cosmwasm contract', async () => { | ||
const ds = await dynamiDsService.createDynamicDatasource({ | ||
templateName: 'cosmos', | ||
startBlock: 100, | ||
args: { | ||
values: { contract: 'cosmos1' }, | ||
attributes: { _contract_address: 'cosmos_wasm' }, | ||
}, | ||
}); | ||
|
||
expect(ds).toEqual({ | ||
kind: CosmosDatasourceKind.Runtime, | ||
startBlock: 100, | ||
mapping: { | ||
file: '', | ||
handlers: [ | ||
{ | ||
handler: 'handleEvent', | ||
kind: CosmosHandlerKind.Event, | ||
filter: { | ||
type: 'execute', | ||
messageFilter: { | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
values: { | ||
contract: 'cosmos1', | ||
}, | ||
}, | ||
attributes: { | ||
_contract_address: 'cosmos_wasm', | ||
}, | ||
}, | ||
}, | ||
{ | ||
handler: 'handleMessage', | ||
kind: CosmosHandlerKind.Message, | ||
filter: { | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
values: { | ||
contract: 'cosmos1', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// Check that the project templates don't get mutated | ||
expect(project.templates[0].mapping.handlers[0].filter).toEqual({ | ||
type: 'execute', | ||
messageFilter: { | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
}, | ||
}); | ||
|
||
expect(project.templates[0].mapping.handlers[1].filter).toEqual({ | ||
type: '/cosmwasm.wasm.v1.MsgExecuteContract', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters