From 93e74135f5f83c14a898fefea9b7c81397af6fe4 Mon Sep 17 00:00:00 2001 From: Fuyao Zhao Date: Mon, 24 Oct 2022 15:56:48 -0700 Subject: [PATCH] chore(sdk): better filter decode api for list of events/resources (#188) --- .circleci/config.yml | 14 +++++++++++--- .github/workflows/release.yaml | 6 +++--- examples/aptos/src/processor.ts | 10 ++++------ sdk/src/aptos/aptos-processor.ts | 8 ++++---- sdk/src/aptos/index.ts | 3 ++- sdk/src/aptos/types.ts | 32 +++++++++++++++++++++++++++----- sdk/src/tests/souffl3.ts | 17 ++++++----------- sdk/templates/aptos/package.json | 15 ++++++++++----- 8 files changed, 67 insertions(+), 38 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d4555e447..f0f0d7688 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,9 +28,17 @@ jobs: command: ./scripts/build-all.sh name: Build All - run: - command: yarn gen && yarn test - name: Test Template - working_directory: sdk/template + command: yarn install && yarn gen && yarn test + name: Test Template - Aptos + working_directory: sdk/templates/aptos + - run: + command: yarn install && yarn gen && yarn test + name: Test Template - EVM + working_directory: sdk/templates/evm + - run: + command: yarn install && yarn test + name: Test Template - RAW + working_directory: sdk/templates/raw # - persist_to_workspace: # root: ~/project # paths: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index bf5bff8de..2c9a46acd 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -26,9 +26,9 @@ jobs: - name: Build run: yarn build working-directory: sdk - - name: Clean template - run: rm -rf node_modules - working-directory: sdk/template + - name: Clean templates node mdules + run: "rm -rf */node_modules" + working-directory: sdk/templates - name: Semantic Release id: semantic uses: cycjimmy/semantic-release-action@v3 diff --git a/examples/aptos/src/processor.ts b/examples/aptos/src/processor.ts index d7952feb3..85bc23e4e 100644 --- a/examples/aptos/src/processor.ts +++ b/examples/aptos/src/processor.ts @@ -23,12 +23,10 @@ SouffleChefCampaign.bind({ network: aptos.AptosNetwork.TEST_NET, startVersion: 6 ctx.meter.Counter('burned').add(1) }) .onTransaction((txn, ctx) => { - if (txn.events) { - for (const event of txn.events) { - if (event && event.type === '0x3::token::DepositEvent') { - ctx.meter.Counter('deposit_token_count').add(Number(event.data.amount)) - } - } + const events = aptos.TYPE_REGISTRY.filterAndDecodeEvents('0x3::token::DepositEvent', txn.events) + for (const event of events) { + // const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance + ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount) } }) diff --git a/sdk/src/aptos/aptos-processor.ts b/sdk/src/aptos/aptos-processor.ts index 5905f4d1f..5777c08fe 100644 --- a/sdk/src/aptos/aptos-processor.ts +++ b/sdk/src/aptos/aptos-processor.ts @@ -9,7 +9,7 @@ import { } from '.' import Long from 'long' -import { EventInstance, DEFAULT_TYPE_REGISTRY } from './types' +import { EventInstance, TYPE_REGISTRY } from './types' import { getChainId } from './network' type IndexConfigure = { @@ -59,7 +59,7 @@ export class AptosBaseProcessor { this.moduleName = moduleName this.configure(options) global.PROCESSOR_STATE.aptosProcessors.push(this) - this.loadTypes(DEFAULT_TYPE_REGISTRY) + this.loadTypes(TYPE_REGISTRY) } // getABI(): MoveModule | undefined { @@ -124,7 +124,7 @@ export class AptosBaseProcessor { txn.events = [] for (const evt of events) { const eventInstance = evt as EventInstance - const decoded = DEFAULT_TYPE_REGISTRY.decodeEvent(eventInstance) + const decoded = TYPE_REGISTRY.decodeEvent(eventInstance) await handler(decoded || eventInstance, ctx) } } @@ -163,7 +163,7 @@ export class AptosBaseProcessor { ) if (tx) { const payload = tx.payload as TransactionPayload_EntryFunctionPayload - const decoded = DEFAULT_TYPE_REGISTRY.decodeFunctionPayload(payload) + const decoded = TYPE_REGISTRY.decodeFunctionPayload(payload) await handler(decoded, ctx) } return ctx.getProcessResult() diff --git a/sdk/src/aptos/index.ts b/sdk/src/aptos/index.ts index 1aa9091d7..be4042bb5 100644 --- a/sdk/src/aptos/index.ts +++ b/sdk/src/aptos/index.ts @@ -1,5 +1,6 @@ export type { Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from 'aptos-sdk/src/generated' -export type { EventInstance, TypedEventInstance, TypeRegistry, TypedEntryFunctionPayload } from './types' +export type { EventInstance, TypedEventInstance, TypedEntryFunctionPayload } from './types' +export { TYPE_REGISTRY, TypeRegistry } from './types' export type { FunctionNameAndCallFilter, EventFilter, CallFilter, ArgumentsFilter } from './aptos-processor' export { AptosBaseProcessor } from './aptos-processor' export { AptosContext } from './context' diff --git a/sdk/src/aptos/types.ts b/sdk/src/aptos/types.ts index 14e988eb6..e607c896f 100644 --- a/sdk/src/aptos/types.ts +++ b/sdk/src/aptos/types.ts @@ -225,13 +225,35 @@ export class TypeRegistry { decodeEvent(event: Event): TypedEventInstance | undefined { return this.decodedInternal(event) as TypedEventInstance } + filterAndDecodeEvents(typePrefix: string, resources: Event[]): TypedEventInstance[] { + return this.filterAndDecodeInternal(typePrefix, resources) as TypedEventInstance[] + } + decodeResource(res: MoveResource): TypedMoveResource | undefined { + return this.decodedInternal(res) + } + filterAndDecodeResources(typePrefix: string, resources: MoveResource[]): TypedMoveResource[] { + return this.filterAndDecodeInternal(typePrefix, resources) + } - decodeResource(event: MoveResource): TypedMoveResource | undefined { - return this.decodedInternal(event) + private filterAndDecodeInternal(typePrefix: string, structs: StructWithTag[]): StructWithType[] { + if (!structs) { + return [] + } + const results: StructWithType[] = [] + for (const resource of structs) { + if (!resource.type.startsWith(typePrefix)) { + continue + } + const result = this.decodedInternal(resource) + if (result) { + results.push(result as StructWithType) + } + } + return results } private decodedInternal(typeStruct: StructWithTag): StructWithType | undefined { - const registry = DEFAULT_TYPE_REGISTRY + const registry = TYPE_REGISTRY // this.loadTypes(registry) // TODO check if module is not loaded @@ -249,7 +271,7 @@ export class TypeRegistry { } decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload { - const registry = DEFAULT_TYPE_REGISTRY + const registry = TYPE_REGISTRY // this.loadTypes(registry) const argumentsTyped: any[] = [] @@ -269,4 +291,4 @@ export class TypeRegistry { } } -export const DEFAULT_TYPE_REGISTRY = new TypeRegistry() +export const TYPE_REGISTRY = new TypeRegistry() diff --git a/sdk/src/tests/souffl3.ts b/sdk/src/tests/souffl3.ts index c34878372..4b3555976 100644 --- a/sdk/src/tests/souffl3.ts +++ b/sdk/src/tests/souffl3.ts @@ -1,8 +1,7 @@ import { SouffleChefCampaign, CandyMachine } from './types/aptos/souffle' import { token } from '../builtin/aptos/0x3' -import { coin, voting } from '../builtin/aptos/0x1' -import { DEFAULT_TYPE_REGISTRY } from '../aptos/types' -import DepositEventInstance = coin.DepositEventInstance +import { voting } from '../builtin/aptos/0x1' +import { TYPE_REGISTRY } from '../aptos/types' SouffleChefCampaign.bind({ startVersion: 3212312 }) .onEntryPullTokenV2((call: SouffleChefCampaign.PullTokenV2Payload, ctx) => { @@ -22,14 +21,10 @@ SouffleChefCampaign.bind({ startVersion: 3212312 }) } ) .onTransaction((txn, ctx) => { - if (txn.events) { - for (const event of txn.events) { - if (event && event.type === '0x3::token::DepositEvent') { - // const typedEvent = this.dec - const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance - ctx.meter.Counter('deposit_token_count').add(depositEventInstance.data_typed.amount) - } - } + const events = TYPE_REGISTRY.filterAndDecodeEvents('0x3::token::DepositEvent', txn.events) + for (const event of events) { + // const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance + ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount) } }) diff --git a/sdk/templates/aptos/package.json b/sdk/templates/aptos/package.json index 9fbb29bac..b781ae336 100644 --- a/sdk/templates/aptos/package.json +++ b/sdk/templates/aptos/package.json @@ -1,15 +1,20 @@ { "name": "template-aptos", "private": true, - "license": "Apache-2.0", "version": "1.0.0", "scripts": { - "compile": "tsc -p .", "test": "jest", - "build": "sentio build" + "gen": "sentio gen", + "build": "sentio build", + "upload": "sentio upload" }, "dependencies": { - "@sentio/sdk": "1.0.0-development" + "@sentio/sdk": "^1.0.0-development" }, - "devDependencies": {} + "devDependencies": { + "@types/jest": "^29.0.0", + "jest": "^29.0.0", + "ts-jest": "^29.0.0", + "typescript": "^4.7.2" + } }