Add a BigDecimal type that can be used in the indexer#15
Conversation
| @@ -0,0 +1,111 @@ | |||
| import { expect } from 'chai'; | |||
| // import { BigDecimal } from 'generated';/// For some reason this doesn't work? | |||
There was a problem hiding this comment.
I tried litrally everything under the sun to try get this to work, for some reason I couldn't :/
Anyway.
We should probably put this in a @envio/bigdecimal npm package or something rather. But this is working for now.
There was a problem hiding this comment.
Any ideas what is going wrong here @JonoPrest or @DZakh ?
There was a problem hiding this comment.
It would need to be exported from index.js and type exported from index.d.ts in the generated code if you want to import straight from generated.
|
|
||
| type EntityWithFields { | ||
| id: ID! | ||
| bigDecimal: BigDecimal! |
| switch handlerContext.entityWithFields.get(testEntity1.id) { | ||
| | Some(entity) => Assert.equal(entity.bigDecimal->BigDecimal.toString, "123.456") | ||
| | None => Assert.fail("Entity should exist") | ||
| } | ||
| switch handlerContext.entityWithFields.get(testEntity2.id) { | ||
| | Some(entity) => Assert.equal(entity.bigDecimal->BigDecimal.toString, "654.321") | ||
| | None => Assert.fail("Entity should exist") | ||
| } |
There was a problem hiding this comment.
Tests that they are read and written to the DB correctly.
Also they use numeric in postgres - so they appear to work completely as expected.
| export class BigDecimal { | ||
| private value: BigNumber; | ||
|
|
||
| constructor(value: BigNumber) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| static fromBigInt(value: BigInt): BigDecimal { | ||
| return new BigDecimal(new BigNumber(value as any)); | ||
| } |
There was a problem hiding this comment.
Should we maybe write it in ReScript to have the same API for both ReScript and TS codebases?
There was a problem hiding this comment.
I actually had this before:
@genType.import(("bignumber.js", "BigNumber"))
type t
// Constructors
@new @module external _fromBigInt: Ethers.BigInt.t => t = "bignumber.js"
@genType let fromBigInt = _fromBigInt
@new @module external _fromFloat: float => t = "bignumber.js"
@genType let fromFloat = _fromFloat
@new @module external _fromInt: int => t = "bignumber.js"
@genType let fromInt = _fromInt
@new @module external _fromStringUnsafe: string => t = "bignumber.js"
@genType let fromStringUnsafe = _fromStringUnsafe
@new @module external _fromString: string => option<t> = "bignumber.js"
@genType let fromString = _fromString
// Methods
@send external _toString: t => string = "toString"
@genType let toString = _toString
@send external _toFixed: t => string = "toFixed"
@genType let toFixed = _toFixed
let toInt = (b: t): option<int> => b->toString->Belt.Int.fromString
// Arithmetic Operations
@send external _plus: (t, t) => t = "plus"
@genType let plus = _plus
@send external _minus: (t, t) => t = "minus"
@genType let minus = _minus
@send external _times: (t, t) => t = "multipliedBy"
@genType let times = _times
@send external _div: (t, t) => t = "dividedBy"
@genType let div = _div
// @send external _pow: (t, int) => t = "toExponential"
// @genType let pow = _pow
// @send external _mod: (t, t) => t = "modulo"
// @genType let mod = _mod
// Comparison
@send external _equals: (t, t) => bool = "isEqualTo"
@genType let equals = _equals
let notEquals: (t, t) => bool = (a, b) => !_equals(a, b)
@send external _gt: (t, t) => bool = "isGreaterThan"
@genType let gt = _gt
@send external _gte: (t, t) => bool = "isGreaterThanOrEqualTo"
@genType let gte = _gte
@send external _lt: (t, t) => bool = "isLessThan"
@genType let lt = _lt
@send external _lte: (t, t) => bool = "isLessThanOrEqualTo"
@genType let lte = _lteBut it looked a bit messy. Maybe you have another idea? I couldn't get the gentype to work on 'external' bindings.
There was a problem hiding this comment.
type rec t = private {
gt: t => bool,
get: t => bool,
}
There was a problem hiding this comment.
We can have the API written in TS style, but do it in ReScript
There was a problem hiding this comment.
I like the suggestion of binging as records. Then docs/templates look the same in ts/res and js.
There was a problem hiding this comment.
Also... rather make this a js file, then add a declaration file and genType.import should point to the js file. Otherwise JS users can't use this module.
There was a problem hiding this comment.
But first prize is just create the api directly in rescript
There was a problem hiding this comment.
js users can use bignumber.js directly?
Will do the 'record' based interface 👍
| RescriptType::BigDecimal => { | ||
| "// default value not required since BigDecimal doesn't exist on contracts for contract import" | ||
| .to_string() |
There was a problem hiding this comment.
Solidity smart contracts never have BigDecimals, so this isn't required.
| RescriptType::BigDecimal => { | ||
| "// default value not required since BigDecimal doesn't exist on contracts for contract import" | ||
| .to_string() |
There was a problem hiding this comment.
Ok cool maybe don't do a line comment for this in case someone tries to use it later on. Then at least it can be inlined/nested and not break the editor expectations.
| RescriptType::BigDecimal => { | |
| "// default value not required since BigDecimal doesn't exist on contracts for contract import" | |
| .to_string() | |
| RescriptType::BigDecimal => { | |
| "/*default value not required since BigDecimal doesn't exist on contracts for contract import*/" | |
| .to_string() |
There was a problem hiding this comment.
I agree, but it'll break anyway since it'll expect the value :)
| @@ -0,0 +1,47 @@ | |||
| @genType.import(("./BigDecimal.ts", "BigDecimal")) // This feature might not work in future versions of gentype: https://rescript-lang.org/docs/manual/latest/typescript-integration#deprecated-features | |||
There was a problem hiding this comment.
It will work 🙂 I've chatted with Cristiano and CometKim and the feature will be kept for rescript 12
| SimpleNftContract, | ||
| NftCollectionEntity, | ||
| UserEntity, | ||
| // BigDecimal, /// For some reason this doesn't work? |
There was a problem hiding this comment.
@JasoonS see my comment above, this needs to be exported from index.js and index.d.ts to get these directly from generated as a package
JonoPrest
left a comment
There was a problem hiding this comment.
Hey @JasoonS, thanks this is awesome! Can ask if I we can merge 1r, 2r and 3r into main first and then rebase on top of that?
There will be some conflicts with the postgres types etc. I'm happy to help with those. But will be much easier than the other way around. Let me know if you want to give those a review first. Dmitry has reviewed and happy 👍🏼
Sure, will switch to record interface and craete an |
…imal.res Co-authored-by: Dmitry Zakharov <dzakh.dev@gmail.com>
5cfb184 to
141a7e3
Compare
| @@ -0,0 +1,38 @@ | |||
| @genType.import(("bignumber.js", "default")) | |||
There was a problem hiding this comment.
Turns out there already were typescript types for this in the library... Was doing extra work for nothing
| @new @module external fromBigInt: Ethers.BigInt.t => t = "bignumber.js" | ||
| @new @module external fromFloat: float => t = "bignumber.js" | ||
| @new @module external fromInt: int => t = "bignumber.js" | ||
| @new @module external fromStringUnsafe: string => t = "bignumber.js" | ||
| @new @module external fromString: string => option<t> = "bignumber.js" |
There was a problem hiding this comment.
These are not shared with typescript and javascript.
They should use new BigDecimal(<a value of any suitable type>)
There was a problem hiding this comment.
I'm thinking maybe since it's just a binding without creating our own interface maybe we should use send bindings instead of record? What do you think @DZakh?
Sorry to mess you around Jason. I thought if we were creating our own class/types for interfacing with this it would be a good idea but since it's just bindings I would suggest we use idiomatic rescript bindings.
There was a problem hiding this comment.
I agree that @send bindings are more idiomatic and flexible in ReScript since they allow for the handling of function overloaded definitions.
DZakh
left a comment
There was a problem hiding this comment.
The .d.ts file looks wrong. Everything else is good 👍
| {{/each}} | ||
| } from "./src/Enum.gen"; | ||
|
|
||
| import {default as $$t} from 'bignumber.js'; |
There was a problem hiding this comment.
Ah, no copy paste error 👍
There was a problem hiding this comment.
Should be:
| import {default as $$t} from 'bignumber.js'; | |
| import {default as BigDecimal} from 'bignumber.js'; |
Now I'm confused how it is working currently. I guess typscript took the types from the index.js file rather.
|
The failing test worked locally for me. I'll check again after meeting |
56681ee to
0aee6e0
Compare
There was a problem hiding this comment.
Denham - this is how to use it in typescript.
| }; | ||
| context.NftCollection.set(nftCollection); | ||
|
|
||
| context.EntityWithFields.set({ id: "testingBigDecimalWorks", bigDecimal: new BigDecimal(123.456) }) |
There was a problem hiding this comment.
This line shows using a BigDecimal @DenhamPreen
The workflow was pinned to a v1.x-era SHA using Discord.js v11 / snekfetch, which does not truncate the `details` payload. When E2E tests failed, the action tried to send an error body over Discord's 2000-char limit, Discord returned 400 Bad Request, and snekfetch threw unhandled — so no one got the failure notification. Upstream fix: issue #15 (closed 2025-06-24), shipped in the v2.x rewrite. v2.1.1+ also downgrades webhook errors to notices, so a broken notification no longer crashes the step. Pinned to SHA c259727 (v2.2.1). Co-authored-by: Claude <noreply@anthropic.com>

No description provided.