Skip to content

Commit

Permalink
Merge pull request #722 from MoralisWeb3/add-advancedoptions-to-streams
Browse files Browse the repository at this point in the history
feat: add advancedOptions to streams
  • Loading branch information
ErnoW authored Oct 5, 2022
2 parents f4d5c12 + 0312b44 commit 74a2cdc
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-colts-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/streams': patch
---

Add advancedOptions to Morlais.Streams (and remove filter)
50 changes: 46 additions & 4 deletions packages/integration/mockRequests/streamApi/createStream.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { rest } from 'msw';
import { STREAM_API_ROOT, MOCK_API_KEY } from '../config';
import isEqual from 'lodash/isEqual';
import omitBy from 'lodash/omitBy';
import isNil from 'lodash/isNil';

export const mockCreateStreamOutput: Record<string, unknown> = {
const createResponse = (description: string) => ({
webhookUrl: 'https://webhook.site/c76c6361-960d-4600-8498-9fecba8abb5f',
description: 'test stream',
description,
tag: 'test',
topic0: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'],
allAddresses: false,
Expand All @@ -15,14 +18,53 @@ export const mockCreateStreamOutput: Record<string, unknown> = {
chainIds: ['0x3', '0x4'],
status: 'active',
statusMessage: 'Stream is active',
};
});

const scenarios = [
{
condition: {
chainIds: ['0x3'],
tag: 'test',
description: 'test-1',
webhookUrl: 'https://webhook.site/4f1b1b1b-1b1b-4f1b-1b1b-1b1b1b1b1b1b',
},
responseStatus: 200,
response: createResponse('test-1'),
},
{
condition: {
chainIds: ['0x3'],
tag: 'test',
description: 'test-2',
webhookUrl: 'https://webhook.site/4f1b1b1b-1b1b-4f1b-1b1b-1b1b1b1b1b1b',
advancedOptions: [
{
topic0: 'SomeEvent(address,uint256)',
filter: { eq: ['myCoolTopic', '0x0000000000000000000000000000000000000000'] },
includeNativeTxs: true,
},
],
},
responseStatus: 200,
response: createResponse('test-2'),
},
];

export const mockCreateStream = rest.put(`${STREAM_API_ROOT}/streams/evm`, (req, res, ctx) => {
const apiKey = req.headers.get('x-api-key');
const { chainIds, tag, description, webhookUrl, advancedOptions } = req.body as Record<string, any>;

const params = omitBy({ chainIds, tag, description, webhookUrl, advancedOptions }, isNil);

if (apiKey !== MOCK_API_KEY) {
return res(ctx.status(401));
}

return res(ctx.status(200), ctx.json(mockCreateStreamOutput));
const scenario = scenarios.find(({ condition }) => isEqual(condition, params));

if (scenario) {
return res(ctx.status(scenario.responseStatus), ctx.json(scenario.response));
}

throw new Error(`mockCreateStream failed, no scenarios with: ${JSON.stringify(params)}`);
});
4 changes: 3 additions & 1 deletion packages/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
"clean": "rm -rf lib && rm -rf tsconfig.tsbuildinfo && rm -rf ./node_modules/.cache/nx"
},
"devDependencies": {
"@types/lodash": "^4.14.186",
"lodash": "^4.17.21",
"prettier": "^2.5.1",
"typescript": "^4.5.5"
},
"dependencies": {
"@moralisweb3/core": "^2.6.0",
"@moralisweb3/evm-api": "^2.6.0",
"@moralisweb3/streams": "^2.6.0",
"@moralisweb3/evm-utils": "^2.6.0",
"@moralisweb3/streams": "^2.6.0",
"eventemitter3": "^4.0.7"
}
}
36 changes: 30 additions & 6 deletions packages/integration/test/streamApi/createStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,51 @@ describe('Create stream', () => {
const result = await StreamApi.add({
chains: ['0x3'],
tag: 'test',
description: 'test',
description: 'test-1',
webhookUrl: 'https://webhook.site/4f1b1b1b-1b1b-4f1b-1b1b-1b1b1b1b1b1b',
networkType: 'evm',
});

expect(result).toBeDefined();
expect(result).toEqual(expect.objectContaining({}));
expect(result.result.chainIds).toContain('0x3');
expect(result.result.description).toEqual('test-1');
});

it('should default to evm networkType', async () => {
const result = await StreamApi.add({
chains: ['0x3'],
tag: 'test',
description: 'test',
description: 'test-1',
webhookUrl: 'https://webhook.site/4f1b1b1b-1b1b-4f1b-1b1b-1b1b1b1b1b1b',
});

expect(result).toBeDefined();
expect(result.result.description).toEqual('test-1');
});

it('should create a stream with advanced options', async () => {
const result = await StreamApi.add({
chains: ['0x3'],
tag: 'test',
description: 'test-2',
webhookUrl: 'https://webhook.site/4f1b1b1b-1b1b-4f1b-1b1b-1b1b1b1b1b1b',
networkType: 'evm',
advancedOptions: [
{
topic0: 'SomeEvent(address,uint256)',
filter: { eq: ['myCoolTopic', '0x0000000000000000000000000000000000000000'] },
includeNativeTxs: true,
},
],
abi: [],
allAddresses: true,
includeContractLogs: true,
includeInternalTxs: true,
includeNativeTxs: true,
topic0: ['SomeEvent(address,uint256)'],
});

expect(result).toBeDefined();
expect(result).toEqual(expect.objectContaining({}));
expect(result.result.chainIds).toContain('0x3');
expect(result.result.description).toEqual('test-2');
});

it('should not create stream', async () => {
Expand Down
24 changes: 15 additions & 9 deletions packages/streams/src/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface components {
/** Format: double */
gas?: number;
};
IAbi: { [key: string]: components["schemas"]["AbiItem"] };
IAbi: { [key: string]: components["schemas"]["AbiItem"][] };
IERC20Transfer: {
transactionHash: string;
contract: string;
Expand Down Expand Up @@ -300,6 +300,12 @@ export interface components {
* @example {}
*/
StreamsFilter: { [key: string]: unknown };
/** @description Advanced Options for each specific topic */
advancedOptions: {
topic0: string;
filter?: components["schemas"]["StreamsFilter"];
includeNativeTxs?: boolean;
};
StreamsModel: {
/** @description Webhook URL where moralis will send the POST request. */
webhookUrl: string;
Expand All @@ -317,8 +323,8 @@ export interface components {
includeContractLogs?: boolean;
/** @description Include or not include internal transactions defaults to false */
includeInternalTxs?: boolean;
abi?: components["schemas"]["StreamsAbi"] | null;
filter?: components["schemas"]["StreamsFilter"] | null;
abi?: components["schemas"]["StreamsAbi"][] | null;
advancedOptions?: components["schemas"]["advancedOptions"][] | null;
/** @description The ids of the chains for this stream in hex Ex: ["0x1","0x38"] */
chainIds: string[];
/** @description The unique uuid of the stream */
Expand Down Expand Up @@ -356,8 +362,8 @@ export interface components {
includeContractLogs?: boolean;
/** @description Include or not include internal transactions defaults to false */
includeInternalTxs?: boolean;
abi?: components["schemas"]["StreamsAbi"] | null;
filter?: components["schemas"]["StreamsFilter"] | null;
abi?: components["schemas"]["StreamsAbi"][] | null;
advancedOptions?: components["schemas"]["advancedOptions"][] | null;
/** @description The ids of the chains for this stream in hex Ex: ["0x1","0x38"] */
chainIds: string[];
/** @description The unique uuid of the stream */
Expand All @@ -384,8 +390,8 @@ export interface components {
includeContractLogs?: boolean;
/** @description Include or not include internal transactions defaults to false */
includeInternalTxs?: boolean;
abi?: components["schemas"]["StreamsAbi"] | null;
filter?: components["schemas"]["StreamsFilter"] | null;
abi?: components["schemas"]["StreamsAbi"][] | null;
advancedOptions?: components["schemas"]["advancedOptions"][] | null;
/** @description The ids of the chains for this stream in hex Ex: ["0x1","0x38"] */
chainIds: string[];
};
Expand Down Expand Up @@ -413,8 +419,8 @@ export interface components {
includeContractLogs?: boolean;
/** @description Include or not include internal transactions defaults to false */
includeInternalTxs?: boolean;
abi?: components["schemas"]["StreamsAbi"] | null;
filter?: components["schemas"]["StreamsFilter"] | null;
abi?: components["schemas"]["StreamsAbi"][] | null;
advancedOptions?: components["schemas"]["advancedOptions"][] | null;
/** @description The ids of the chains for this stream in hex Ex: ["0x1","0x38"] */
chainIds?: string[];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const bodyParams = [
'includeNativeTxs',
'includeContractLogs',
'includeInternalTxs',
'filter',
'chainIds',
'abi',
'advancedOptions',
] as const;

export interface CreateStreamEvmParams extends Camelize<Omit<ApiParams, 'chainIds'>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const bodyParams = [
'includeContractLogs',
'includeInternalTxs',
'abi',
'filter',
'chainIds',
'advancedOptions',
] as const;

export interface UpdateStreamEvmParams extends Camelize<Omit<ApiParams, 'chainIds'>> {
Expand Down
9 changes: 7 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6376,6 +6376,11 @@
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.182.tgz"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==

"@types/lodash@^4.14.186":
version "4.14.186"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.186.tgz#862e5514dd7bd66ada6c70ee5fce844b06c8ee97"
integrity sha512-eHcVlLXP0c2FlMPm56ITode2AgLMSa6aJ05JTTbYbI+7EMkCEE5qk2E41d5g2lCVTqRe0GnnRFurmlCsDODrPw==

"@types/long@^4.0.0", "@types/long@^4.0.1":
version "4.0.2"
resolved "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz"
Expand Down Expand Up @@ -16710,7 +16715,7 @@ lodash.isboolean@^3.0.3:

lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==

lodash.isinteger@^4.0.4:
Expand Down Expand Up @@ -16820,7 +16825,7 @@ lodash.uniq@^4.5.0:

[email protected], lodash@^4.12.0, lodash@^4.17.10, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.5, lodash@^4.6.1, lodash@^4.7.0:
version "4.17.21"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

log-symbols@^4.1.0:
Expand Down

0 comments on commit 74a2cdc

Please sign in to comment.