From 6edd0ee79a4d4c66ca02295e5e607198102b6a48 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Wed, 19 Jun 2024 10:38:44 +0400 Subject: [PATCH] [V1] Remove EventSummary from init template (#34) * Remove EventSummary from init template * Revert the code which I shouldn't remove --- .../cli/src/config_parsing/human_config.rs | 1 + .../javascript/src/EventHandlers.js.hbs | 48 ++----------- .../javascript/test/Test.js.hbs | 40 +---------- .../rescript/src/EventHandlers.res.hbs | 41 ++--------- .../rescript/test/Test.res.hbs | 39 +---------- .../shared/schema.graphql.hbs | 16 +---- .../typescript/src/EventHandlers.ts.hbs | 68 ++++--------------- .../typescript/test/Test.ts.hbs | 38 +---------- .../static/codegen/src/bindings/BigInt.res | 58 ++++++++++++++++ .../static/codegen/src/bindings/Ethers.res | 51 +------------- 10 files changed, 92 insertions(+), 308 deletions(-) create mode 100644 codegenerator/cli/templates/static/codegen/src/bindings/BigInt.res diff --git a/codegenerator/cli/src/config_parsing/human_config.rs b/codegenerator/cli/src/config_parsing/human_config.rs index b3bdabe75..c30458e68 100644 --- a/codegenerator/cli/src/config_parsing/human_config.rs +++ b/codegenerator/cli/src/config_parsing/human_config.rs @@ -32,6 +32,7 @@ pub mod evm { pub name: String, #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub ecosystem: Option, #[serde(skip_serializing_if = "Option::is_none")] pub schema: Option, diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs index 01cbe0fde..84d273ded 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs @@ -1,51 +1,16 @@ /* - *Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features* + * Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features */ - const { +const { {{#each imported_contracts as |contract|}} {{contract.name.capitalized}}Contract, {{/each}} } = require("generated"); - -const GLOBAL_EVENTS_SUMMARY_KEY = "GlobalEventsSummary"; - -module.exports = { - GLOBAL_EVENTS_SUMMARY_KEY, -}; - -const INITIAL_EVENTS_SUMMARY = { - id: GLOBAL_EVENTS_SUMMARY_KEY, {{#each imported_contracts as |contract|}} {{#each contract.imported_events as |event|}} - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0), - {{/each}} -{{/each}} -}; - -{{#each imported_contracts as |contract|}} - {{#each contract.imported_events as |event|}} - {{contract.name.capitalized}}Contract.{{event.name.capitalized - }}.loader(({event, context}) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); - - {{contract.name.capitalized}}Contract.{{event.name.capitalized - }}.handler(({event, context}) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - - const currentSummaryEntity = summary ?? INITIAL_EVENTS_SUMMARY; - - const nextSummaryEntity = { - ...currentSummaryEntity, - {{contract.name.uncapitalized}}_{{event.name.capitalized - }}Count: currentSummaryEntity.{{contract.name.uncapitalized - }}_{{event.name.capitalized - }}Count + BigInt(1), - }; - const {{contract.name.uncapitalized - }}_{{event.name.capitalized - }}Entity = { +{{contract.name.capitalized}}Contract.{{event.name.capitalized}}.handler(({event, context}) => { + const entity = { id: event.transactionHash + event.logIndex.toString(), {{#each event.params as |param|}} {{param.entity_key.uncapitalized @@ -58,13 +23,10 @@ const INITIAL_EVENTS_SUMMARY = { {{/if}} , {{/each}} - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, }; - context.EventsSummary.set(nextSummaryEntity); context.{{contract.name.capitalized - }}_{{event.name.capitalized}}.set({{contract.name.uncapitalized - }}_{{event.name.capitalized}}Entity); + }}_{{event.name.capitalized}}.set(entity); }); {{/each}} {{/each}} diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs index 3dc1c8e94..1b65e23df 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs @@ -3,29 +3,13 @@ const assert = require("assert"); const { TestHelpers } = require("generated"); const { MockDb, {{contract.name.capitalized}}, Addresses } = TestHelpers; - -const { GLOBAL_EVENTS_SUMMARY_KEY } = require("../src/EventHandlers"); {{/with}} -const MOCK_EVENTS_SUMMARY_ENTITY = { - id: GLOBAL_EVENTS_SUMMARY_KEY, - {{#each imported_contracts as | contract |}} - {{#each contract.codegen_events as | event |}} - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0), - {{/each}} - {{/each}} -}; - {{#with imported_contracts.[0] as | contract |}} {{#with contract.codegen_events.[0] as | event |}} describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} event tests", () => { // Create mock db - const mockDbInitial = MockDb.createMockDb(); - - // Add mock EventsSummaryEntity to mock db - const mockDbFinal = mockDbInitial.entities.EventsSummary.set( - MOCK_EVENTS_SUMMARY_ENTITY - ); + const mockDb = MockDb.createMockDb(); // Creating mock {{contract.name.capitalized}} contract {{event.name.capitalized}} event const mock{{contract.name.capitalized}}{{event.name.capitalized}}Event = {{contract.name.capitalized}}.{{event.name.capitalized}}.createMockEvent({ @@ -47,7 +31,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even // Processing the event const mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({ event: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event, - mockDb: mockDbFinal, + mockDb, }); it("{{contract.name.capitalized}}_{{event.name.capitalized}}Entity is created correctly", () => { @@ -65,7 +49,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even {{#each event.params as |param|}} {{param.param_name.uncapitalized}}: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event.params.{{param.param_name.uncapitalized}}, {{/each}} - eventsSummary: "GlobalEventsSummary", }; // Asserting that the entity in the mock database is the same as the expected entity assert.deepEqual( @@ -74,25 +57,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even "Actual {{contract.name.capitalized}}{{event.name.capitalized}}Entity should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}Entity" ); }); - - it("EventsSummaryEntity is updated correctly", () => { - // Getting the actual entity from the mock database - let actualEventsSummaryEntity = mockDbUpdated.entities.EventsSummary.get( - GLOBAL_EVENTS_SUMMARY_KEY - ); - - // Creating the expected entity - const expectedEventsSummaryEntity = { - ...MOCK_EVENTS_SUMMARY_ENTITY, - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: MOCK_EVENTS_SUMMARY_ENTITY.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count + BigInt(1), - }; - // Asserting that the entity in the mock database is the same as the expected entity - assert.deepEqual( - actualEventsSummaryEntity, - expectedEventsSummaryEntity, - "Actual EventsSummaryEntity should be the same as the expected EventsSummaryEntity" - ); - }); }); {{/with}} {{/with}} diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs index 046626e72..0260b8146 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs @@ -1,41 +1,13 @@ -let globalEventsSummaryKey = "GlobalEventsSummary" - -module BigInt = Ethers.BigInt - -let initialEventsSummary: Types.eventsSummaryEntity = { - id: globalEventsSummaryKey, +/* + * Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features + */ {{#each imported_contracts as |contract|}} {{#each contract.imported_events as |event|}} - {{contract.name.uncapitalized}}_{{event.name.capitalized - }}Count: BigInt.fromInt(0), - {{/each}} -{{/each}} -} - -{{#each imported_contracts as |contract|}} - {{#each contract.imported_events as |event|}} - Handlers.{{contract.name.capitalized}}Contract.{{event.name.capitalized - }}.loader(({event, context}) => { - context.eventsSummary.load(globalEventsSummaryKey) -}) Handlers.{{contract.name.capitalized }}Contract.{{event.name.capitalized }}.handler(({event, context}) => { - let summary = context.eventsSummary.get(globalEventsSummaryKey) - - let currentSummaryEntity = summary->Belt.Option.getWithDefault(initialEventsSummary) - - let nextSummaryEntity = { - ...currentSummaryEntity, - {{contract.name.uncapitalized}}_{{event.name.capitalized - }}Count: currentSummaryEntity.{{contract.name.uncapitalized - }}_{{event.name.capitalized - }}Count->BigInt.add(BigInt.fromInt(1)), - } - - let {{contract.name.uncapitalized - }}_{{event.name.capitalized}}Entity: Types.{{contract.name.uncapitalized + let entity: Types.{{contract.name.uncapitalized }}_{{event.name.capitalized }}Entity = { id: event.transactionHash ++ event.logIndex->Belt.Int.toString, @@ -52,13 +24,10 @@ Handlers.{{contract.name.capitalized {{/if}} , {{/each}} - eventsSummary: globalEventsSummaryKey, } - context.eventsSummary.set(nextSummaryEntity) context.{{contract.name.uncapitalized - }}_{{event.name.capitalized}}.set({{contract.name.uncapitalized - }}_{{event.name.capitalized}}Entity) + }}_{{event.name.capitalized}}.set(entity) }) {{/each}} {{/each}} diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs index 0be002cb0..57a9c51c9 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs @@ -3,27 +3,11 @@ open Mocha open Belt open TestHelpers -let globalEventsSummaryKey = "GlobalEventsSummary" - -module BigInt = Ethers.BigInt - -let mockEventsSummaryEntity: Types.eventsSummaryEntity = { - id: globalEventsSummaryKey, - {{#each imported_contracts as | contract |}} - {{#each contract.codegen_events as | event |}} - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt.fromInt(0), - {{/each}} - {{/each}} -} - {{#with imported_contracts.[0] as | contract |}} {{#with contract.codegen_events.[0] as | event |}} describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} event tests", () => { // Create mock db - let mockDbInitial = MockDb.createMockDb() - - // Add mock EventsSummaryEntity to mock db - let mockDbFinal = mockDbInitial.entities.eventsSummary.set(mockEventsSummaryEntity) + let mockDb = MockDb.createMockDb() // Creating mock {{contract.name.capitalized}} contract {{event.name.capitalized}} event let mock{{contract.name.capitalized}}{{event.name.capitalized}}Event = {{contract.name.capitalized}}.{{event.name.capitalized}}.createMockEvent({ @@ -45,7 +29,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even // Processing the event let mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({ event: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event, - mockDb: mockDbFinal, + mockDb, }) it("{{contract.name.capitalized}}_{{event.name.capitalized}}Entity is created correctly", () => { @@ -61,7 +45,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even {{#each event.params as |param|}} {{param.param_name.uncapitalized}}: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event.params.{{param.param_name.uncapitalized}}{{#if param.is_eth_address}}->Ethers.ethAddressToString{{/if}}, {{/each}} - eventsSummary: globalEventsSummaryKey, } //Assert the expected {{contract.name.capitalized}} {{event.name.capitalized}} entity Assert.deep_equal( @@ -70,24 +53,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even ~message="Actual {{contract.name.capitalized}}_{{event.name.capitalized}}Entity should be the same as the expected {{contract.name.capitalized}}_{{event.name.capitalized}}Entity", ) }) - - it("EventsSummaryEntity is updated correctly", () => { - // Getting the actual entity from the mock database - let actualEventsSummaryEntity = - mockDbUpdated.entities.eventsSummary.get(globalEventsSummaryKey)->Option.getExn - - // Creating the expected entity - let expectedEventsSummaryEntity: Types.eventsSummaryEntity = { - ...mockEventsSummaryEntity, - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: mockEventsSummaryEntity.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count->BigInt.add(BigInt.fromInt(1)), - } - // Asserting that the entity in the mock database is the same as the expected entity - Assert.deep_equal( - actualEventsSummaryEntity, - expectedEventsSummaryEntity, - ~message="Actual EventsSummaryEntity should be the same as the expected EventsSummaryEntity", - ) - }) }) {{/with}} {{/with}} diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/shared/schema.graphql.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/shared/schema.graphql.hbs index fc9f41402..880dfd046 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/shared/schema.graphql.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/shared/schema.graphql.hbs @@ -1,23 +1,11 @@ -type EventsSummary { - id: ID! {{#each imported_contracts as |contract|}} {{#each contract.imported_events as |event|}} - {{contract.name.uncapitalized}}_{{event.name.capitalized - }}: [{{contract.name.capitalized}}_{{event.name.capitalized - }}!]! @derivedFrom(field: "eventsSummary") - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt! - {{/each}} -{{/each}} -} -{{#each imported_contracts as |contract|}} - {{#each contract.imported_events as |event|}} - type {{contract.name.capitalized}}_{{event.name.capitalized}} { id: ID! {{#each event.params as |param|}} {{param.entity_key.uncapitalized}}: {{param.graphql_type}} {{/each}} - eventsSummary: String! } + {{/each}} -{{/each}} +{{/each}} \ No newline at end of file diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs index 32767ec98..6de4a5e9d 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs @@ -1,72 +1,32 @@ /* - *Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features* + * Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features */ import { {{#each imported_contracts as |contract|}} {{contract.name.capitalized}}Contract, - {{#each contract.imported_events as |event|}} - {{contract.name.capitalized}}_{{event.name.capitalized}}Entity, - {{/each}} +{{#each contract.imported_events as |event|}} + {{contract.name.capitalized}}_{{event.name.capitalized}}Entity, {{/each}} -EventsSummaryEntity -} from "generated"; - -export const GLOBAL_EVENTS_SUMMARY_KEY = "GlobalEventsSummary"; - -const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = { - id: GLOBAL_EVENTS_SUMMARY_KEY, -{{#each imported_contracts as |contract|}} - {{#each contract.imported_events as |event|}} - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0), - {{/each}} {{/each}} -}; - +} from "generated"; {{#each imported_contracts as |contract|}} {{#each contract.imported_events as |event|}} - {{contract.name.capitalized}}Contract.{{event.name.capitalized - }}.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); - - {{contract.name.capitalized}}Contract.{{event.name.capitalized - }}.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; - - const nextSummaryEntity = { - ...currentSummaryEntity, - {{contract.name.uncapitalized}}_{{event.name.capitalized - }}Count: currentSummaryEntity.{{contract.name.uncapitalized - }}_{{event.name.capitalized - }}Count + BigInt(1), - }; - const {{contract.name.uncapitalized - }}_{{event.name.capitalized}}Entity: {{contract.name.capitalized - }}_{{event.name.capitalized - }}Entity = { +{{contract.name.capitalized}}Contract.{{event.name.capitalized}}.handler(({ event, context }) => { + const entity: {{contract.name.capitalized}}_{{event.name.capitalized}}Entity = { id: event.transactionHash + event.logIndex.toString(), {{#each event.params as |param|}} - {{param.entity_key.uncapitalized - }}: event.params.{{param.event_key.uncapitalized}}{{#if - param.tuple_param_accessor_indexes - }} - {{#each param.tuple_param_accessor_indexes as |index|}} - [{{index}}] - {{/each}} - {{/if}} - , + {{param.entity_key.uncapitalized}}: event.params.{{param.event_key.uncapitalized}}{{#if + param.tuple_param_accessor_indexes + }} + {{#each param.tuple_param_accessor_indexes as |index|}} + [{{index}}] + {{/each}} + {{/if}}, {{/each}} - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, }; - context.EventsSummary.set(nextSummaryEntity); - context.{{contract.name.capitalized - }}_{{event.name.capitalized}}.set({{contract.name.uncapitalized - }}_{{event.name.capitalized}}Entity); + context.{{contract.name.capitalized}}_{{event.name.capitalized}}.set(entity); }); {{/each}} {{/each}} diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs index 8685a3119..76f8f0a75 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs @@ -3,35 +3,17 @@ import assert from "assert"; import { TestHelpers, - EventsSummaryEntity, {{contract.name.capitalized}}_{{event.name.capitalized}}Entity } from "generated"; const { MockDb, {{contract.name.capitalized}}, Addresses } = TestHelpers; - -import { GLOBAL_EVENTS_SUMMARY_KEY } from "../src/EventHandlers"; {{/with}} {{/with}} - -const MOCK_EVENTS_SUMMARY_ENTITY: EventsSummaryEntity = { - id: GLOBAL_EVENTS_SUMMARY_KEY, - {{#each imported_contracts as | contract |}} - {{#each contract.codegen_events as | event |}} - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0), - {{/each}} - {{/each}} -}; - {{#with imported_contracts.[0] as | contract |}} {{#with contract.codegen_events.[0] as | event |}} describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} event tests", () => { // Create mock db - const mockDbInitial = MockDb.createMockDb(); - - // Add mock EventsSummaryEntity to mock db - const mockDbFinal = mockDbInitial.entities.EventsSummary.set( - MOCK_EVENTS_SUMMARY_ENTITY - ); + const mockDb = MockDb.createMockDb(); // Creating mock {{contract.name.capitalized}} contract {{event.name.capitalized}} event const mock{{contract.name.capitalized}}{{event.name.capitalized}}Event = {{contract.name.capitalized}}.{{event.name.capitalized}}.createMockEvent({ @@ -53,7 +35,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even // Processing the event const mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({ event: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event, - mockDb: mockDbFinal, + mockDb, }); it("{{contract.name.capitalized}}_{{event.name.capitalized}}Entity is created correctly", () => { @@ -71,26 +53,10 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even {{#each event.params as |param|}} {{param.param_name.uncapitalized}}: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event.params.{{param.param_name.uncapitalized}}, {{/each}} - eventsSummary: "GlobalEventsSummary", }; // Asserting that the entity in the mock database is the same as the expected entity assert.deepEqual(actual{{contract.name.capitalized}}{{event.name.capitalized}}Entity, expected{{contract.name.capitalized}}{{event.name.capitalized}}Entity, "Actual {{contract.name.capitalized}}{{event.name.capitalized}}Entity should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}Entity"); }); - - it("EventsSummaryEntity is updated correctly", () => { - // Getting the actual entity from the mock database - let actualEventsSummaryEntity = mockDbUpdated.entities.EventsSummary.get( - GLOBAL_EVENTS_SUMMARY_KEY - ); - - // Creating the expected entity - const expectedEventsSummaryEntity: EventsSummaryEntity = { - ...MOCK_EVENTS_SUMMARY_ENTITY, - {{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: MOCK_EVENTS_SUMMARY_ENTITY.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count + BigInt(1), - }; - // Asserting that the entity in the mock database is the same as the expected entity - assert.deepEqual(actualEventsSummaryEntity, expectedEventsSummaryEntity, "Actual {{contract.name.capitalized}}{{event.name.capitalized}}Entity should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}Entity"); - }); }); {{/with}} {{/with}} diff --git a/codegenerator/cli/templates/static/codegen/src/bindings/BigInt.res b/codegenerator/cli/templates/static/codegen/src/bindings/BigInt.res new file mode 100644 index 000000000..8a235d87e --- /dev/null +++ b/codegenerator/cli/templates/static/codegen/src/bindings/BigInt.res @@ -0,0 +1,58 @@ +@genType.import(("./OpaqueTypes.ts", "GenericBigInt")) +type t + +module Misc = { + let unsafeToOption: (unit => 'a) => option<'a> = unsafeFunc => { + try { + unsafeFunc()->Some + } catch { + | Js.Exn.Error(_obj) => None + } + } +} + +// constructors and methods +@val external fromInt: int => t = "BigInt" +@val external fromStringUnsafe: string => t = "BigInt" +let fromString = str => Misc.unsafeToOption(() => str->fromStringUnsafe) +@send external toString: t => string = "toString" +let toInt = (b: t): option => b->toString->Belt.Int.fromString + +//silence unused var warnings for raw bindings +@@warning("-27") +// operation +let add = (a: t, b: t): t => %raw("a + b") +let sub = (a: t, b: t): t => %raw("a - b") +let mul = (a: t, b: t): t => %raw("a * b") +let div = (a: t, b: t): t => %raw("b > 0n ? a / b : 0n") +let pow = (a: t, b: t): t => %raw("a ** b") +let mod = (a: t, b: t): t => %raw("b > 0n ? a % b : 0n") + +// comparison +let eq = (a: t, b: t): bool => %raw("a === b") +let neq = (a: t, b: t): bool => %raw("a !== b") +let gt = (a: t, b: t): bool => %raw("a > b") +let gte = (a: t, b: t): bool => %raw("a >= b") +let lt = (a: t, b: t): bool => %raw("a < b") +let lte = (a: t, b: t): bool => %raw("a <= b") + +module Bitwise = { + let shift_left = (a: t, b: t): t => %raw("a << b") + let shift_right = (a: t, b: t): t => %raw("a >> b") + let logor = (a: t, b: t): t => %raw("a | b") + let logand = (a: t, b: t): t => %raw("a & b") +} + +let zero = fromInt(0) + +let schema = + S.string + ->S.setName("BigInt") + ->S.transform((. s) => { + parser: (. string) => + switch string->fromString { + | Some(bigInt) => bigInt + | None => s.fail(. "The string is not valid BigInt") + }, + serializer: (. bigint) => bigint->toString, + }) diff --git a/codegenerator/cli/templates/static/codegen/src/bindings/Ethers.res b/codegenerator/cli/templates/static/codegen/src/bindings/Ethers.res index becff0def..cabb76d0a 100644 --- a/codegenerator/cli/templates/static/codegen/src/bindings/Ethers.res +++ b/codegenerator/cli/templates/static/codegen/src/bindings/Ethers.res @@ -11,56 +11,7 @@ module Misc = { } } -module BigInt = { - @genType.import(("./OpaqueTypes.ts", "GenericBigInt")) - type t - - // constructors and methods - @val external fromInt: int => t = "BigInt" - @val external fromStringUnsafe: string => t = "BigInt" - let fromString = str => Misc.unsafeToOption(() => str->fromStringUnsafe) - @send external toString: t => string = "toString" - let toInt = (b: t): option => b->toString->Belt.Int.fromString - - //silence unused var warnings for raw bindings - @@warning("-27") - // operation - let add = (a: t, b: t): t => %raw("a + b") - let sub = (a: t, b: t): t => %raw("a - b") - let mul = (a: t, b: t): t => %raw("a * b") - let div = (a: t, b: t): t => %raw("b > 0n ? a / b : 0n") - let pow = (a: t, b: t): t => %raw("a ** b") - let mod = (a: t, b: t): t => %raw("b > 0n ? a % b : 0n") - - // comparison - let eq = (a: t, b: t): bool => %raw("a === b") - let neq = (a: t, b: t): bool => %raw("a !== b") - let gt = (a: t, b: t): bool => %raw("a > b") - let gte = (a: t, b: t): bool => %raw("a >= b") - let lt = (a: t, b: t): bool => %raw("a < b") - let lte = (a: t, b: t): bool => %raw("a <= b") - - module Bitwise = { - let shift_left = (a: t, b: t): t => %raw("a << b") - let shift_right = (a: t, b: t): t => %raw("a >> b") - let logor = (a: t, b: t): t => %raw("a | b") - let logand = (a: t, b: t): t => %raw("a & b") - } - - let zero = fromInt(0) - - let schema = - S.string - ->S.setName("Ethers.BigInt") - ->S.transform((. s) => { - parser: (. string) => - switch string->fromString { - | Some(bigInt) => bigInt - | None => s.fail(. "The string is not valid BigInt") - }, - serializer: (. bigint) => bigint->toString, - }) -} +module BigInt = BigInt type abi