Skip to content

Commit

Permalink
chore(aptos): add onResourceChange to AptosResourceProcessor (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedragon authored Sep 19, 2024
1 parent a291b3e commit eb6cb83
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
32 changes: 20 additions & 12 deletions packages/sdk/src/aptos/aptos-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,27 @@ export class AptosPlugin extends Plugin {
})
for (const handler of aptosProcessor.resourcesHandlers) {
const handlerId = handlers.aptosResourceHandlers.push(handler.handler) - 1
accountConfig.moveIntervalConfigs.push({
intervalConfig: {
handlerId: handlerId,
minutes: 0,
minutesInterval: handler.timeIntervalInMinutes,
slot: 0,
slotInterval: handler.versionInterval,
if (handler.timeIntervalInMinutes || handler.versionInterval) {
accountConfig.moveIntervalConfigs.push({
intervalConfig: {
handlerId: handlerId,
minutes: 0,
minutesInterval: handler.timeIntervalInMinutes,
slot: 0,
slotInterval: handler.versionInterval,
fetchConfig: undefined
},
type: handler.type || '',
ownerType: MoveOwnerType.ADDRESS,
fetchConfig: undefined
},
type: handler.type || '',
ownerType: MoveOwnerType.ADDRESS,
fetchConfig: undefined
})
})
} else if (handler.type) {
// on resource change
accountConfig.moveResourceChangeConfigs.push({
handlerId,
type: handler.type
})
}
}
config.accountConfigs.push(accountConfig)
}
Expand Down
41 changes: 35 additions & 6 deletions packages/sdk/src/aptos/aptos-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class AptosBaseProcessor {
}

public onResourceChange(
handler: (changes: ResourceChange[], ctx: AptosContext) => Promise<ProcessResult>,
handler: (changes: ResourceChange[], ctx: AptosResourcesContext) => PromiseOrVoid,
type: string
): this {
const processor = this
Expand All @@ -235,13 +235,13 @@ export class AptosBaseProcessor {
throw new ServerError(Status.INVALID_ARGUMENT, 'resource is null')
}
const resources = data.resources as ResourceChange[]
const ctx = new AptosContext(
processor.moduleName,
const timestamp = Number(data.timestampMicros)

const ctx = new AptosResourcesContext(
processor.config.network,
processor.config.address,
BigInt(data.version),
null,
0,
data.version,
timestamp,
processor.config.baseLabels
)
await handler(resources, ctx)
Expand Down Expand Up @@ -356,6 +356,35 @@ export class AptosResourcesProcessor {
fetchConfig
)
}

public onResourceChange(
handler: (changes: ResourceChange[], ctx: AptosResourcesContext) => PromiseOrVoid,
typeOrPrefix: string
): this {
const processor = this
this.resourcesHandlers.push({
fetchConfig: DEFAULT_RESOURCE_FETCH_CONFIG,
handler: async function (data) {
const timestamp = Number(data.timestampMicros)

if (!data.resources || !data.version) {
throw new ServerError(Status.INVALID_ARGUMENT, 'resource is null')
}
const resources = data.resources as ResourceChange[]
const ctx = new AptosResourcesContext(
processor.config.network,
processor.config.address,
data.version,
timestamp,
processor.config.baseLabels
)
await handler(resources, ctx)
return ctx.stopAndGetResult()
},
type: typeOrPrefix
})
return this
}
}

function configure(options: AptosBindOptions): IndexConfigure {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/aptos/tests/aptos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('Test Aptos Example', () => {
test('check configuration ', async () => {
const config = await service.getConfig({})
expect(config.contractConfigs).length(5)

expect(config.accountConfigs).length(6)
})

test('Check souffl3 transaction dispatch', async () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/src/aptos/tests/souffl3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ voting.bind().onEventCreateProposalEvent((evt, ctx) => {
ctx.meter.Gauge('size').record(evt.data_decoded.metadata.data.length)
})

AptosResourcesProcessor.bind({ address: '0x1' }).onTimeInterval((resources, ctx) => {
ctx.meter.Counter('onTimer').add(1)
}, 10000)
AptosResourcesProcessor.bind({ address: '0x1' })
.onTimeInterval((resources, ctx) => {
ctx.meter.Counter('onTimer').add(1)
}, 10000)
.onResourceChange((resources, ctx) => {
ctx.meter.Counter('onResourceChange').add(resources.length)
}, '0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>')

aptos_account.bind().onEntryCreateAccount(
(call, ctx) => {
Expand Down

0 comments on commit eb6cb83

Please sign in to comment.