Skip to content

Commit

Permalink
chore(sdk-aptos): used xxx_decoded instead of xxx_typed (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Jan 29, 2023
1 parent 451b9b8 commit 55a92f5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
10 changes: 5 additions & 5 deletions examples/aptos/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const accountTracker = AccountEventTracker.register('pull')

coin.bind({ network: AptosNetwork.MAIN_NET }).onEventWithdrawEvent((evt, ctx) => {
if (evt.guid.account_address === '0x9c5382a5aa6cd92f38ffa50bd8ec2879833997116499cc5bcd6d4688a962e330') {
ctx.meter.Counter('air_dropped').add(evt.data_typed.amount)
ctx.meter.Counter('air_dropped').add(evt.data_decoded.amount)
}
ctx.meter.Counter('evt_cnt').add(1)
})

SouffleChefCampaign.bind({ network: AptosNetwork.TEST_NET, startVersion: 6604913 })
.onEntryPullTokenV2((call, ctx) => {
ctx.meter.Counter('call_num').add(1)
ctx.meter.Counter('pulled').add(call.arguments_typed[3])
ctx.meter.Counter('pulled').add(call.arguments_decoded[3])

accountTracker.trackEvent(ctx, { distinctId: ctx.transaction.sender })
})
Expand All @@ -27,16 +27,16 @@ SouffleChefCampaign.bind({ network: AptosNetwork.TEST_NET, startVersion: 6604913
const events = TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('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)
ctx.meter.Counter('deposit_token_count').add(event.data_decoded.amount)
}
})

CandyMachine.bind({ network: AptosNetwork.TEST_NET, startVersion: 6604913 }).onEntryPullToken((call, ctx) => {
ctx.meter.Counter('pulled').add(call.arguments_typed[2], { coin: call.type_arguments[0] })
ctx.meter.Counter('pulled').add(call.arguments_decoded[2], { coin: call.type_arguments[0] })
})

token
.bind({ network: AptosNetwork.TEST_NET, startVersion: 282159141 })
.onEventWithdrawEvent((evt: token.WithdrawEventInstance, ctx) => {
ctx.meter.Counter('with_draw').add(evt.data_typed.amount, { token: evt.data_typed.id.token_data_id.name })
ctx.meter.Counter('with_draw').add(evt.data_decoded.amount, { token: evt.data_decoded.id.token_data_id.name })
})
4 changes: 4 additions & 0 deletions packages/sdk-aptos/src/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ function generateStructs(module: MoveModule, struct: MoveStruct, events: Set<str
eventPayload = `
export interface ${struct.name}Instance extends
TypedEventInstance<${struct.name}${genericStringAny}> {
/** @deprecated use {@link data_decoded} instead */
data_typed: ${struct.name}${genericStringAny}
data_decoded: ${struct.name}${genericStringAny}
type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]
}
`
Expand Down Expand Up @@ -345,7 +347,9 @@ function generateCallArgsStructs(module: MoveModule, func: MoveFunction) {
return `
export interface ${camelFuncName}Payload${genericString}
extends TypedEntryFunctionPayload<[${fields.join(',')}]> {
/** @deprecated use {@link arguments_decoded} instead */
arguments_typed: [${fields.join(',')}],
arguments_decoded: [${fields.join(',')}],
type_arguments: [${func.generic_type_params.map((_) => 'string').join(', ')}]
}
`
Expand Down
21 changes: 17 additions & 4 deletions packages/sdk-aptos/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ export type EventInstance = Event & {
}

export type TypedEventInstance<T> = EventInstance & {
// Typed data converted from ABI
// undefined if there is converting error, usually because the ABI/data
// mismatch
/**
* @deprecated use {@link data_decoded} instead
*/
data_typed: T

/**
* decoded data using ABI, undefined if there is decoding error, usually because the ABI/data mismatch
*/
data_decoded: T

type_arguments: string[]
}

// Don't use intermedidate type to make IDE happier
// Don't use intermediate type to make IDE happier
export type TypedEntryFunctionPayload<T extends Array<any>> = TransactionPayload_EntryFunctionPayload & {
/**
* @deprecated use {@link arguments_decoded} instead
*/
arguments_typed: T

/**
* decoded argument data using ABI, undefined if there is decoding error, usually because the ABI/data mismatch
*/
arguments_decoded: T
}

export type TypedMoveResource<T> = MoveResource & {
Expand Down
12 changes: 5 additions & 7 deletions packages/sdk-aptos/src/tests/souffl3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const accountTracker = AccountEventTracker.register('pull')
SouffleChefCampaign.bind({ startVersion: 3212312n })
.onEntryPullTokenV2((call: SouffleChefCampaign.PullTokenV2Payload, ctx) => {
ctx.meter.Counter('call_num').add(1)
ctx.meter.Counter('pulled').add(call.arguments_typed[3])
ctx.meter.Counter('pulled').add(call.arguments_decoded[3])
})
.onEventPullTokenEvent((evt, ctx) => {
ctx.meter.Counter('burned').add(1)
Expand All @@ -28,7 +28,7 @@ SouffleChefCampaign.bind({ startVersion: 3212312n })
const events = TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('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)
ctx.meter.Counter('deposit_token_count').add(event.data_decoded.amount)
}
})

Expand All @@ -37,14 +37,12 @@ CandyMachine.bind().onEntryPullToken((call: CandyMachine.PullTokenPayload, ctx)
})

token.bind().onEventDepositEvent((evt: token.DepositEventInstance, ctx) => {
ctx.meter.Gauge('version').record(evt.data_typed.id.property_version)
ctx.meter.Counter('deposit').add(evt.data_typed.amount, { token: evt.data_typed.id.token_data_id.name })
ctx.meter.Gauge('version').record(evt.data_decoded.id.property_version)
ctx.meter.Counter('deposit').add(evt.data_decoded.amount, { token: evt.data_decoded.id.token_data_id.name })
})

voting.bind().onEventCreateProposalEvent((evt, ctx) => {
// console.log(evt)
evt.data_typed.expiration_secs + evt.data_typed.expiration_secs
ctx.meter.Gauge('size').record(evt.data_typed.metadata.data.length)
ctx.meter.Gauge('size').record(evt.data_decoded.metadata.data.length)
})

AptosAccountProcessor.bind({ address: '0x1' }).onTimeInterval((resources, ctx) => {
Expand Down
13 changes: 11 additions & 2 deletions packages/sdk-aptos/src/type-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ export class TypeRegistry {
console.error('Decoding error for ', JSON.stringify(typeStruct), e)
return undefined
}
return { ...typeStruct, data_typed: dataTyped, type_arguments: typeArguments } as StructWithType<T>
return {
...typeStruct,
data_typed: dataTyped,
data_decoded: dataTyped,
type_arguments: typeArguments,
} as StructWithType<T>
}

decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
Expand All @@ -180,7 +185,11 @@ export class TypeRegistry {
return payload
}

return { ...payload, arguments_typed: argumentsTyped } as TypedEntryFunctionPayload<any>
return {
...payload,
arguments_typed: argumentsTyped,
arguments_decoded: argumentsTyped,
} as TypedEntryFunctionPayload<any>
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Logger extends NamedResultDescriptor {
return new Logger(this.ctx, name)
}

log(level: LogLevel, message: any, attributes: Attributes = {}) {
protected log(level: LogLevel, message: any, attributes: Attributes = {}) {
if (typeof message !== 'string' && !(message instanceof String)) {
message = JSON.stringify(message)
}
Expand Down

0 comments on commit 55a92f5

Please sign in to comment.