From da3ac99b134035a1616a50e229e5c9e602fc203a Mon Sep 17 00:00:00 2001 From: nujabes403 Date: Mon, 18 Dec 2023 21:18:49 +0900 Subject: [PATCH] fix: fix near-contract-standards event emission --- .../src/fungible_token/events.ts | 21 ++++++++++++------- .../src/non_fungible_token/events.ts | 11 +++++++--- packages/near-contract-standards/src/util.ts | 3 +++ 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 packages/near-contract-standards/src/util.ts diff --git a/packages/near-contract-standards/src/fungible_token/events.ts b/packages/near-contract-standards/src/fungible_token/events.ts index 96a613c0..862d0ecb 100644 --- a/packages/near-contract-standards/src/fungible_token/events.ts +++ b/packages/near-contract-standards/src/fungible_token/events.ts @@ -18,19 +18,24 @@ import { NearEvent } from "../event"; import { Option } from "../non_fungible_token/utils"; import { AccountId, Balance } from "near-sdk-js"; +import { toSnakeCase } from "../util"; export type Nep141EventKind = FtMint[] | FtTransfer[] | FtBurn[]; export class Nep141Event extends NearEvent { - version: string; - event_kind: Nep141EventKind; - - constructor(version: string, event_kind: Nep141EventKind) { - super(); - this.version = version; - this.event_kind = event_kind; - } + standard: string; + version: string; + event: string; + data: Nep141EventKind; + + constructor(version: string, event_kind: Nep141EventKind) { + super(); + this.standard = "nep141" + this.version = version + this.event = toSnakeCase(event_kind[0].constructor.name) + this.data = event_kind } +} /** Data to log for an FT mint event. To log this event, call [`.emit()`](FtMint::emit). */ export class FtMint { diff --git a/packages/near-contract-standards/src/non_fungible_token/events.ts b/packages/near-contract-standards/src/non_fungible_token/events.ts index f8d5e810..c94d374b 100644 --- a/packages/near-contract-standards/src/non_fungible_token/events.ts +++ b/packages/near-contract-standards/src/non_fungible_token/events.ts @@ -16,17 +16,22 @@ import { AccountId } from "near-sdk-js"; import { NearEvent } from "../event"; import { TokenId } from "./token"; +import { toSnakeCase } from "../util"; export type Nep171EventKind = NftMint[] | NftTransfer[] | NftBurn[]; export class Nep171Event extends NearEvent { + standard: string; version: string; - event_kind: Nep171EventKind; + event: string; + data: Nep171EventKind; constructor(version: string, event_kind: Nep171EventKind) { super(); - this.version = version; - this.event_kind = event_kind; + this.standard = "nep171" + this.version = version + this.event = toSnakeCase(event_kind[0].constructor.name) + this.data = event_kind } } diff --git a/packages/near-contract-standards/src/util.ts b/packages/near-contract-standards/src/util.ts new file mode 100644 index 00000000..6878f2d9 --- /dev/null +++ b/packages/near-contract-standards/src/util.ts @@ -0,0 +1,3 @@ +export const toSnakeCase = (str: string) => { + return str.replace(/[A-Z]/g, (letter, index) => { return index == 0 ? letter.toLowerCase() : '_'+ letter.toLowerCase();}); +} \ No newline at end of file