Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update streams to reflect api changes #744

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-days-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/streams': patch
---

Update types and endpoints for Moralis.Streams to reflect the api changes. Some types have changes, `Moralis.Streams.retry` now accepts an id and is fixed. And return types from webhooks have been updated.
35 changes: 35 additions & 0 deletions packages/integration/mockRequests/streamApi/getHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { MockScenarios } from '../../MockScenarios';
import { createPaginatedWebhookResponse } from './response/webhookResponse';

export const mockGetHistory = MockScenarios.create(
{
method: 'get',
name: 'mockGetHistory',
url: `/history`,
getParams: (req) => ({
limit: req.url.searchParams.get('limit'),
cursor: req.url.searchParams.get('cursor'),
}),
},
[
{
condition: {
limit: '20',
},
response: createPaginatedWebhookResponse(Array(20).fill('id-1'), 20),
},
{
condition: {
limit: '5',
},
response: createPaginatedWebhookResponse(Array(5).fill('id-2'), 20, 'cursor'),
},
{
condition: {
limit: '5',
cursor: 'cursor',
},
response: createPaginatedWebhookResponse(Array(5).fill('id-3'), 20, 'cursor-2'),
},
],
);
32 changes: 32 additions & 0 deletions packages/integration/mockRequests/streamApi/replayHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MockScenarios } from '../../MockScenarios';
import { createErrorResponse } from './response/errorResponse';
import { createWebhookResponse } from './response/webhookResponse';

export const mockReplayHistory = MockScenarios.create(
{
method: 'post',
name: 'mockReplayHistory',
url: `/history/replay/:streamId/:id`,
getParams: (req) => ({
streamId: req.params.streamId,
id: req.params.id,
}),
},
[
{
condition: {
id: 'id-1',
streamId: 'stream-1',
},
response: createWebhookResponse('stream-1'),
},
{
condition: {
id: 'not-found',
streamId: 'stream-1',
},
responseStatus: 404,
response: createErrorResponse('History not found'),
},
],
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const defaultWebhookResponse = {
id: 'string',
date: '2022-10-12T09:23:13.895Z',
payload: {
block: {
number: 'string',
hash: 'string',
timestamp: 'string',
},
chainId: 'string',
logs: [
{
logIndex: 'string',
transactionHash: 'string',
address: 'string',
data: 'string',
topic0: 'string',
topic1: 'string',
topic2: 'string',
topic3: 'string',
},
],
txs: [
{
hash: 'string',
gas: 'string',
gasPrice: 'string',
nonce: 'string',
input: 'string',
transactionIndex: 'string',
fromAddress: 'string',
toAddress: 'string',
value: 'string',
type: 'string',
v: 'string',
r: 'string',
s: 'string',
receiptCumulativeGasUsed: 'string',
receiptGasUsed: 'string',
receiptContractAddress: 'string',
receiptRoot: 'string',
receiptStatus: 'string',
},
],
txsInternal: [
{
from: 'string',
to: 'string',
value: 'string',
transactionHash: 'string',
gas: 'string',
},
],
abi: [{}],
retries: 0,
confirmed: true,
tag: 'string',
streamId: 'string',
},
tinyPayload: {
chainId: 'string',
confirmed: true,
block: 'string',
records: 0,
retries: 0,
},
errorMessage: 'string',
webhookUrl: 'string',
streamId: 'string',
tag: 'string',
};

export const createWebhookResponse = (id: string) => ({
...defaultWebhookResponse,
id,
});

export const createPaginatedWebhookResponse = (ids: string[], total: number, cursor?: string) => ({
result: ids.map((id) => ({ ...defaultWebhookResponse, id })),
total,
cursor,
});
4 changes: 4 additions & 0 deletions packages/integration/mockRequests/streamServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { mockCreateStream } from './streamApi/createStream';
import { mockDeleteAddressEvm } from './streamApi/deleteAddress';
import { mockDeleteStream } from './streamApi/deleteStream';
import { mockGetAddressesEvm } from './streamApi/getAddresses';
import { mockGetHistory } from './streamApi/getHistory';
import { mockGetSettings } from './streamApi/getSettings';
import { mockGetStreams } from './streamApi/getStreams';
import { mockReplayHistory } from './streamApi/replayHistory';
import { mockSetSettings } from './streamApi/setSettings';
import { mockUpdateStream } from './streamApi/updateStream';

Expand All @@ -20,4 +22,6 @@ export const streamServer = MockServer.create({ apiRoot: STREAM_API_ROOT }, [
mockGetSettings,
mockSetSettings,
mockUpdateStream,
mockReplayHistory,
mockGetHistory,
]).start();
57 changes: 57 additions & 0 deletions packages/integration/test/streamApi/getHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { MoralisStreams } from '@moralisweb3/streams';
import { cleanStreamsApi, setupStreamApi } from './setup';

describe('getHistory', () => {
let StreamApi: MoralisStreams;

beforeAll(() => {
StreamApi = setupStreamApi();
});

afterAll(() => {
cleanStreamsApi();
});

it('should get all streams', async () => {
const result = await StreamApi.getHistory({
limit: 20,
});

expect(result).toBeDefined();
expect(result.result).toBeDefined();
expect(result.pagination.total).toEqual(20);
expect(result.result.length).toEqual(20);
expect(result.result[0].id).toEqual('id-1');
});

it('should get a cursor', async () => {
const result = await StreamApi.getHistory({
limit: 5,
});

expect(result).toBeDefined();
expect(result.result).toBeDefined();
expect(result.pagination.total).toEqual(20);
expect(result.result.length).toEqual(5);
expect(result.result[0].id).toEqual('id-2');
expect(result.pagination.cursor).toEqual('cursor');
expect(result.next).toBeDefined();
expect(result.hasNext).toBeTruthy();
});

it('should use a provided cursor', async () => {
const result = await StreamApi.getHistory({
limit: 5,
cursor: 'cursor',
});

expect(result).toBeDefined();
expect(result.result).toBeDefined();
expect(result.pagination.total).toEqual(20);
expect(result.result.length).toEqual(5);
expect(result.result[0].id).toEqual('id-3');
expect(result.pagination.cursor).toEqual('cursor-2');
expect(result.next).toBeDefined();
expect(result.hasNext).toBeTruthy();
});
});
35 changes: 35 additions & 0 deletions packages/integration/test/streamApi/replayHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { MoralisStreams } from '@moralisweb3/streams';
import { cleanStreamsApi, setupStreamApi } from './setup';

describe('replayHistory', () => {
let StreamApi: MoralisStreams;

beforeAll(() => {
StreamApi = setupStreamApi();
});

afterAll(() => {
cleanStreamsApi();
});

describe('evm', () => {
it('should trigger replay history succesfully', async () => {
const result = await StreamApi.retry({
id: 'id-1',
streamId: 'stream-1',
});

expect(result).toBeDefined();
expect(result.result.id).toBe('stream-1');
});

it('should throw an error when no history is found', async () => {
expect(
StreamApi.retry({
id: 'not-found',
streamId: 'stream-1',
}),
).rejects.toThrowError('[C0006] Request failed, Not Found(404): History not found');
});
});
});
2 changes: 1 addition & 1 deletion packages/integration/test/streamApi/setSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Set settings', () => {
region: 'eu-central-1',
});

expect(result.result.success).toEqual(true);
expect(result.result.region).toEqual('eu-central-1');
});

it('should throw an error on invalid region ', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/streams/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dev": "tsc --watch"
},
"devDependencies": {
"@moralisweb3/streams-typings": "^1.0.3",
"@moralisweb3/streams-typings": "^1.0.5",
"@types/ethereumjs-util": "5.2.0",
"eslint-plugin-jest": "^26.8.3",
"jest": "^28.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/streams/src/MoralisStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class MoralisStreams extends ApiModule {
public readonly getAddresses = makeGetAddresses(this.endpoints);
public readonly deleteAddress = makeDeleteAddress(this.endpoints);

public readonly getHistory = this.endpoints.createFetcher(getHistory);
public readonly getHistory = this.endpoints.createPaginatedFetcher(getHistory);
public readonly retry = this.endpoints.createFetcher(replayHistory);

public readonly setSettings = this.endpoints.createFetcher(setSettings);
Expand Down
Loading