Skip to content

Commit

Permalink
Feat/horoscope handler (develop) (#364)
Browse files Browse the repository at this point in the history
* feat: add docs for horoscope handler

* feat: update doc

* feat: update doc

* feat: add horoscope handler service

* fix: remove doc horoscope handler

* fix: add order for query

* fix: group transaction and block into one by lodash

* feat: create branch merge develop

* fix: add empty array txs for block has no txs
  • Loading branch information
fibonacci998 authored Sep 11, 2023
1 parent d93e410 commit b2938f6
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ export const SERVICE = {
key: 'CrawlIBCAppService',
name: 'v1.CrawlIBCAppService',
},
HoroscopeHandlerService: {
key: 'HoroscopeHandlerService',
path: 'v1.HoroscopeHandlerService',
getData: {
key: 'getData',
path: 'v1.HoroscopeHandlerService.getData',
},
},
},
};

Expand Down
7 changes: 7 additions & 0 deletions src/services/api-gateways/api_gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ApiGateway from 'moleculer-web';
import { ServiceBroker } from 'moleculer';
import { Service } from '@ourparentcenter/moleculer-decorators-extended';

import BaseService from '../../base/base.service';
import { bullBoardMixin } from '../../mixins/bullBoard/bullBoard.mixin';

Expand Down Expand Up @@ -37,6 +38,12 @@ import { bullBoardMixin } from '../../mixins/bullBoard/bullBoard.mixin';
'v2.statistics.syncPrevDateStatsByChainId',
],
},
{
path: '/auth',
autoAliases: true,
mappingPolicy: 'restrict',
whitelist: ['v1.horoscope-handler.getDataByChainId'],
},
],
// empty cors object will have moleculer to generate handler for preflight request and CORS header which allow all origin
cors: {},
Expand Down
48 changes: 48 additions & 0 deletions src/services/api-gateways/horoscope_handler.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Post, Service } from '@ourparentcenter/moleculer-decorators-extended';
import { Context, ServiceBroker } from 'moleculer';
import { SERVICE } from '../../common';
import BaseService from '../../base/base.service';
import networks from '../../../network.json' assert { type: 'json' };

@Service({
name: 'horoscope-handler',
version: 1,
})
export default class HoroscopeHandlerService extends BaseService {
public constructor(public broker: ServiceBroker) {
super(broker);
}

@Post('/getDataByChainId', {
name: 'getDataByChainId',
params: {
chainid: {
type: 'string',
optional: false,
enum: networks.map((network) => network.chainId),
},
startBlock: {
type: 'number',
optional: false,
},
endBlock: {
type: 'number',
optional: false,
},
},
})
async getDataByChainId(
ctx: Context<
{ chainid: string; startBlock: number; endBlock: number },
Record<string, unknown>
>
) {
const selectedChain = networks.find(
(network) => network.chainId === ctx.params.chainid
);
return this.broker.call(
`${SERVICE.V1.HoroscopeHandlerService.getData.path}@${selectedChain?.moleculerNamespace}`,
ctx.params
);
}
}
111 changes: 111 additions & 0 deletions src/services/horoscope-handler/handler.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
Action,
Service,
} from '@ourparentcenter/moleculer-decorators-extended';
import { Context, ServiceBroker } from 'moleculer';
import _ from 'lodash';
import BaseService from '../../base/base.service';
import { SERVICE } from '../../common';
import { Block, Transaction } from '../../models';
import networks from '../../../network.json' assert { type: 'json' };

@Service({
name: SERVICE.V1.HoroscopeHandlerService.key,
version: 1,
})
export default class HoroscopeHandlerService extends BaseService {
public constructor(public broker: ServiceBroker) {
super(broker);
}

@Action({
name: SERVICE.V1.HoroscopeHandlerService.getData.key,
params: {
chainid: {
type: 'string',
optional: false,
enum: networks.map((network) => network.chainId),
},
startBlock: {
type: 'number',
optional: false,
},
endBlock: {
type: 'number',
optional: false,
},
},
})
public async getData(
ctx: Context<
{ chainid: string; startBlock: number; endBlock: number },
Record<string, unknown>
>
) {
// TODO: handler filter from request

// query to get data
const { startBlock, endBlock } = ctx.params;
const queryBlock = Block.query()
.select('height', 'hash', 'time')
.withGraphFetched('events.[attributes]')
.modifyGraph('events', (builder) => {
builder.select('type', 'source');
})
.modifyGraph('events.[attributes]', (builder) => {
builder.select('key', 'value');
})
.where('height', '>=', startBlock)
.andWhere('height', '<', endBlock)
.orderBy('height', 'asc');

const queryTransaction = Transaction.query()
.select('height', 'hash', 'code', 'codespace', 'memo', 'index')
.withGraphFetched('messages')
.withGraphFetched('events.[attributes]')
.modifyGraph('messages', (builder) => {
builder
.select('type', 'sender', 'content', 'index')
.whereNull('parent_id');
})
.modifyGraph('events', (builder) => {
builder.select('type', 'source');
})
.modifyGraph('events.[attributes]', (builder) => {
builder.select('key', 'value');
})
.where('height', '>=', startBlock)
.andWhere('height', '<', endBlock)
.orderBy('height', 'asc')
.orderBy('index', 'asc');
const [listBlock, listTransaction] = await Promise.all([
queryBlock,
queryTransaction,
]);

const resultGroupBy = _.groupBy(
[...listBlock, ...listTransaction],
'height'
);
const listData: any[] = [];
Object.keys(resultGroupBy).forEach((height) => {
if (resultGroupBy[height].length > 0) {
if (resultGroupBy[height][0] instanceof Block) {
const data: any = resultGroupBy[height][0];

// resultGroupBy[height].length === 1 means there is block only, not has any txs
if (resultGroupBy[height].length === 1) {
data.txs = [];
} else {
data.txs = resultGroupBy[height].slice(1);
}
listData.push(data);
} else {
throw Error('Block not found');
}
}
});
// handler response
return listData;
}
}

0 comments on commit b2938f6

Please sign in to comment.