-
Notifications
You must be signed in to change notification settings - Fork 3
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
Feat/horoscope handler #358
Open
fibonacci998
wants to merge
12
commits into
main
Choose a base branch
from
feat/horoscope-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
84d1d34
feat: add docs for horoscope handler
fibonacci998 96854ad
feat: update doc
fibonacci998 90aa9e6
feat: update doc
fibonacci998 316b4c8
feat: add horoscope handler service
fibonacci998 0bb9e46
fix: remove doc horoscope handler
fibonacci998 1067571
fix: add order for query
fibonacci998 d8a688b
fix: group transaction and block into one by lodash
fibonacci998 02986e4
Fix/lint json file (#359)
fibonacci998 1067a63
fix: add empty array txs for block has no txs
fibonacci998 263c250
feat: add tx_msg_index to query event tx
fibonacci998 a5bb468
fix: split query event block (not in block query)
fibonacci998 ccfeee2
fix: get block in resultGroupBy (return array)
fibonacci998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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 | ||
); | ||
} | ||
} |
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,121 @@ | ||
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, Event } 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> | ||
> | ||
) { | ||
// query to get data | ||
const { startBlock, endBlock } = ctx.params; | ||
const queryBlock = Block.query() | ||
.select('height', 'hash', 'time') | ||
.where('height', '>=', startBlock) | ||
.andWhere('height', '<', endBlock) | ||
.orderBy('height', 'asc'); | ||
|
||
const queryEventBlock = Event.query() | ||
.select('type', 'source', 'block_height as height') | ||
.withGraphFetched('attributes') | ||
.modifyGraph('attributes', (builder) => { | ||
builder.select('key', 'value'); | ||
}) | ||
.whereIn('source', [ | ||
Event.SOURCE.BEGIN_BLOCK_EVENT, | ||
Event.SOURCE.END_BLOCK_EVENT, | ||
]) | ||
.andWhere('block_height', '>=', startBlock) | ||
.andWhere('block_height', '<', endBlock) | ||
.orderBy('block_height') | ||
.orderBy('id'); | ||
|
||
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', 'tx_msg_index'); | ||
}) | ||
.modifyGraph('events.[attributes]', (builder) => { | ||
builder.select('key', 'value'); | ||
}) | ||
.where('height', '>=', startBlock) | ||
.andWhere('height', '<', endBlock) | ||
.orderBy('height', 'asc') | ||
.orderBy('index', 'asc'); | ||
const [listBlock, listTransaction, listEventBlock] = await Promise.all([ | ||
queryBlock, | ||
queryTransaction, | ||
queryEventBlock, | ||
]); | ||
|
||
const resultGroupBy = _.groupBy( | ||
[...listBlock, ...listTransaction, ...listEventBlock], | ||
'height' | ||
); | ||
const listData: any[] = []; | ||
Object.keys(resultGroupBy).forEach((height) => { | ||
if (resultGroupBy[height].length > 0) { | ||
if (resultGroupBy[height][0] instanceof Block) { | ||
const block: any = resultGroupBy[height].filter( | ||
(e) => e instanceof Block | ||
)[0]; | ||
const eventBlocks = resultGroupBy[height].filter( | ||
(e) => e instanceof Event | ||
); | ||
const txs = resultGroupBy[height].filter( | ||
(e) => e instanceof Transaction | ||
); | ||
block.events = eventBlocks.length === 0 ? [] : eventBlocks; | ||
block.txs = txs.length === 0 ? [] : txs; | ||
listData.push(block); | ||
} else { | ||
throw Error('Block not found'); | ||
} | ||
} | ||
}); | ||
// handler response | ||
return listData; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hiện tại thì chỉ trả về transaction thành công thôi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chỗ này em chưa có handle gì filter ấy, nên em trả hết ra luôn thôi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ý là mặc định là tx thành công ấy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tức là không có cái filter includeTxFail ấy hả anh