Skip to content

Commit

Permalink
BigInt clean up (#48)
Browse files Browse the repository at this point in the history
* Remove BigInt from Ethers

* Remove BigInt.t in favor of bigint
  • Loading branch information
DZakh authored Jun 27, 2024
1 parent dd76173 commit 0822112
Show file tree
Hide file tree
Showing 44 changed files with 202 additions and 215 deletions.
6 changes: 3 additions & 3 deletions codegenerator/cli/src/config_parsing/entity_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ impl RescriptType {
match self {
RescriptType::Int => "int".to_string(),
RescriptType::Float => "GqlDbCustomTypes.Float.t".to_string(),
RescriptType::BigInt => "Ethers.BigInt.t".to_string(),
RescriptType::BigInt => "bigint".to_string(),
RescriptType::BigDecimal => "BigDecimal.t".to_string(),
RescriptType::Address => "Ethers.ethAddress".to_string(),
RescriptType::String => "string".to_string(),
Expand Down Expand Up @@ -871,7 +871,7 @@ impl RescriptType {
match self {
RescriptType::Int => "S.int".to_string(),
RescriptType::Float => "GqlDbCustomTypes.Float.schema".to_string(),
RescriptType::BigInt => "Ethers.BigInt.schema".to_string(),
RescriptType::BigInt => "BigInt.schema".to_string(),
RescriptType::BigDecimal => "BigDecimal.schema".to_string(),
RescriptType::Address => "Ethers.ethAddressSchema".to_string(),
RescriptType::String => "S.string".to_string(),
Expand Down Expand Up @@ -904,7 +904,7 @@ impl RescriptType {
match self {
RescriptType::Int => "0".to_string(),
RescriptType::Float => "0.0".to_string(),
RescriptType::BigInt => "Ethers.BigInt.zero".to_string(), //TODO: Migrate to RescriptCore on ReScript migration
RescriptType::BigInt => "BigInt.zero".to_string(), //TODO: Migrate to RescriptCore on ReScript migration
RescriptType::BigDecimal => "BigDecimal.zero".to_string(),
RescriptType::Address => "TestHelpers_MockAddresses.defaultAddress".to_string(),
RescriptType::String => "\"foo\"".to_string(),
Expand Down
9 changes: 3 additions & 6 deletions codegenerator/cli/src/config_parsing/event_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {

assert_eq!(
parsed_rescript_string.to_string(),
String::from("(string, Ethers.BigInt.t)")
String::from("(string, bigint)")
)
}

Expand All @@ -136,10 +136,7 @@ mod tests {
user_address_res_type.to_string(),
"Ethers.ethAddress".to_string()
);
assert_eq!(
amount_uint256_res_type.to_string(),
"Ethers.BigInt.t".to_string()
);
assert_eq!(amount_uint256_res_type.to_string(), "bigint".to_string());
assert_eq!(
tuple_bool_string_res_type.to_string(),
"(bool, Ethers.ethAddress)".to_string()
Expand All @@ -152,7 +149,7 @@ mod tests {
);
assert_eq!(
amount_uint256_res_type.get_default_value_rescript(),
"Ethers.BigInt.zero".to_string()
"BigInt.zero".to_string()
);
assert_eq!(
tuple_bool_string_res_type.get_default_value_rescript(),
Expand Down
6 changes: 3 additions & 3 deletions codegenerator/cli/src/rescript_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl RescriptTypeIdent {
RescriptTypeIdent::Unit => "unit".to_string(),
RescriptTypeIdent::Int => "int".to_string(),
RescriptTypeIdent::Float => "GqlDbCustomTypes.Float.t".to_string(),
RescriptTypeIdent::BigInt => "Ethers.BigInt.t".to_string(),
RescriptTypeIdent::BigInt => "bigint".to_string(),
RescriptTypeIdent::Address => "Ethers.ethAddress".to_string(),
RescriptTypeIdent::String => "string".to_string(),
RescriptTypeIdent::ID => "id".to_string(),
Expand Down Expand Up @@ -289,7 +289,7 @@ impl RescriptTypeIdent {
RescriptTypeIdent::Unit => "S.unit".to_string(),
RescriptTypeIdent::Int => "S.int".to_string(),
RescriptTypeIdent::Float => "GqlDbCustomTypes.Float.schema".to_string(),
RescriptTypeIdent::BigInt => "Ethers.BigInt.schema".to_string(),
RescriptTypeIdent::BigInt => "BigInt.schema".to_string(),
RescriptTypeIdent::Address => "Ethers.ethAddressSchema".to_string(),
RescriptTypeIdent::String => "S.string".to_string(),
RescriptTypeIdent::ID => "S.string".to_string(),
Expand Down Expand Up @@ -355,7 +355,7 @@ impl RescriptTypeIdent {
RescriptTypeIdent::Unit => "()".to_string(),
RescriptTypeIdent::Int => "0".to_string(),
RescriptTypeIdent::Float => "0.0".to_string(),
RescriptTypeIdent::BigInt => "Ethers.BigInt.zero".to_string(), // TODO: Migrate to RescriptCore on ReScript migration
RescriptTypeIdent::BigInt => "BigInt.zero".to_string(), // TODO: Migrate to RescriptCore on ReScript migration
RescriptTypeIdent::Address => "TestHelpers_MockAddresses.defaultAddress".to_string(),
RescriptTypeIdent::String => "\"foo\"".to_string(),
RescriptTypeIdent::ID => "\"my_id\"".to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Types

module EntityIdsMap = {
type t = Js.Dict.t<Set.t<Types.id>>
type t = dict<Set.t<Types.id>>

let makeEmptyEntityIdsMap = (): t => {
Entities.allTables->Belt.Array.map(table => (table.tableName, Set.make()))->Js.Dict.fromArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ let addEventToRawEvents = (
let eventId = EventUtils.packEventIndex(~logIndex, ~blockNumber)
let rawEvent: TablesStatic.RawEvents.t = {
chainId,
eventId: eventId->Ethers.BigInt.toString,
eventId: eventId->BigInt.toString,
blockNumber,
logIndex,
transactionIndex,
Expand All @@ -183,7 +183,7 @@ let addEventToRawEvents = (
},
}

let eventIdStr = eventId->Ethers.BigInt.toString
let eventIdStr = eventId->BigInt.toString

inMemoryStore.rawEvents->InMemoryTable.set({chainId, eventId: eventIdStr}, rawEvent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module LoadActionMap = {

type key = string
type value<'entity> = array<loadAction<'entity>>
type t<'entity> = Js.Dict.t<value<'entity>>
type t<'entity> = dict<value<'entity>>
let empty: unit => t<'entity> = Js.Dict.empty
let getIds = (map: t<'entity>) => map->Js.Dict.keys
let entries: t<'entity> => array<(key, value<'entity>)> = Js.Dict.entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open Belt
/**
A raw js binding to allow deleting from a dict. Used in store delete operation
*/
let deleteDictKey: (Js.Dict.t<'a>, string) => unit = %raw(`
let deleteDictKey: (dict<'a>, string) => unit = %raw(`
function(dict, key) {
delete dict[key]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type configYaml = {
syncSource: aliasSyncSource,
startBlock: int,
confirmedBlockThreshold: int,
contracts: Js.Dict.t<contract>,
contracts: dict<contract>,
}

let mapChainConfigToConfigYaml: Config.chainConfig => configYaml = chainConfig => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ exception UndefinedContractAddress(Ethers.ethAddress)
// protecting static addresses from de-registration.

type mapping = {
nameByAddress: Js.Dict.t<contractName>,
addressesByName: Js.Dict.t<Belt.Set.String.t>,
nameByAddress: dict<contractName>,
addressesByName: dict<Belt.Set.String.t>,
}

let addAddress = (map: mapping, ~name: string, ~address: Ethers.ethAddress) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type interfaceAndAbi = {
}
type t = {
contractAddressMapping: ContractAddressingMap.mapping,
contractNameInterfaceMapping: Js.Dict.t<interfaceAndAbi>,
contractNameInterfaceMapping: dict<interfaceAndAbi>,
}

let make = (
Expand Down
40 changes: 20 additions & 20 deletions codegenerator/cli/templates/static/codegen/src/EventUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ type eventIndex = {
// takes blockNumber, logIndex and packs them into a number with
//32 bits, 16 bits and 16 bits respectively
let packEventIndex = (~blockNumber, ~logIndex) => {
let blockNumber = blockNumber->Ethers.BigInt.fromInt
let logIndex = logIndex->Ethers.BigInt.fromInt
let blockNumber = Ethers.BigInt.Bitwise.shift_left(blockNumber, 16->Ethers.BigInt.fromInt)
let blockNumber = blockNumber->BigInt.fromInt
let logIndex = logIndex->BigInt.fromInt
let blockNumber = BigInt.Bitwise.shift_left(blockNumber, 16->BigInt.fromInt)

blockNumber->Ethers.BigInt.Bitwise.logor(logIndex)
blockNumber->BigInt.Bitwise.logor(logIndex)
}

//Currently not used but keeping in utils
//using @live flag for dead code analyser
@live
let packMultiChainEventIndex = (~timestamp, ~chainId, ~blockNumber, ~logIndex) => {
let timestamp = timestamp->Ethers.BigInt.fromInt
let chainId = chainId->Ethers.BigInt.fromInt
let blockNumber = blockNumber->Ethers.BigInt.fromInt
let logIndex = logIndex->Ethers.BigInt.fromInt
let timestamp = timestamp->BigInt.fromInt
let chainId = chainId->BigInt.fromInt
let blockNumber = blockNumber->BigInt.fromInt
let logIndex = logIndex->BigInt.fromInt

let timestamp = Ethers.BigInt.Bitwise.shift_left(timestamp, 48->Ethers.BigInt.fromInt)
let chainId = Ethers.BigInt.Bitwise.shift_left(chainId, 16->Ethers.BigInt.fromInt)
let blockNumber = Ethers.BigInt.Bitwise.shift_left(blockNumber, 16->Ethers.BigInt.fromInt)
let timestamp = BigInt.Bitwise.shift_left(timestamp, 48->BigInt.fromInt)
let chainId = BigInt.Bitwise.shift_left(chainId, 16->BigInt.fromInt)
let blockNumber = BigInt.Bitwise.shift_left(blockNumber, 16->BigInt.fromInt)

timestamp
->Ethers.BigInt.Bitwise.logor(chainId)
->Ethers.BigInt.Bitwise.logor(blockNumber)
->Ethers.BigInt.Bitwise.logor(logIndex)
->BigInt.Bitwise.logor(chainId)
->BigInt.Bitwise.logor(blockNumber)
->BigInt.Bitwise.logor(logIndex)
}

//Currently not used but keeping in utils
//using @live flag for dead code analyser
@live
let unpackEventIndex = (packedEventIndex: Ethers.BigInt.t) => {
let blockNumber = packedEventIndex->Ethers.BigInt.Bitwise.shift_right(16->Ethers.BigInt.fromInt)
let logIndexMask = 65535->Ethers.BigInt.fromInt
let logIndex = packedEventIndex->Ethers.BigInt.Bitwise.logand(logIndexMask)
let unpackEventIndex = (packedEventIndex: bigint) => {
let blockNumber = packedEventIndex->BigInt.Bitwise.shift_right(16->BigInt.fromInt)
let logIndexMask = 65535->BigInt.fromInt
let logIndex = packedEventIndex->BigInt.Bitwise.logand(logIndexMask)
{
blockNumber: blockNumber->Ethers.BigInt.toString->Belt.Int.fromString->Belt.Option.getUnsafe,
logIndex: logIndex->Ethers.BigInt.toString->Belt.Int.fromString->Belt.Option.getUnsafe,
blockNumber: blockNumber->BigInt.toString->Belt.Int.fromString->Belt.Option.getUnsafe,
logIndex: logIndex->BigInt.toString->Belt.Int.fromString->Belt.Option.getUnsafe,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Belt

type t<'key, 'val> = {
dict: Js.Dict.t<'val>,
dict: dict<'val>,
hash: 'key => string,
}

Expand Down
6 changes: 3 additions & 3 deletions codegenerator/cli/templates/static/codegen/src/LazyLoader.res
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type asyncMap<'a> = {
// How long to wait before cancelling a load request
_timeoutMillis: int,
// The promises we return to callers. We satisfy them asynchronously.
externalPromises: Js.Dict.t<promise<'a>>,
externalPromises: dict<promise<'a>>,
// The handled used to populate the external promises once we have loaded their data.
resolvers: Js.Dict.t<(. 'a) => unit>,
resolvers: dict<(. 'a) => unit>,
// The keys currently being loaded
inProgress: Belt.MutableSet.Int.t,
// Keys for items that we have not started loading yet.
Expand Down Expand Up @@ -54,7 +54,7 @@ let make = (
metadata,
}

let deleteKey: (Js.Dict.t<'a>, string) => unit = (_obj, _k) => %raw(`delete _obj[_k]`)
let deleteKey: (dict<'a>, string) => unit = (_obj, _k) => %raw(`delete _obj[_k]`)

// If something takes longer than this to load, reject the promise and try again
let timeoutAfter = timeoutMillis =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type registeredEvent<'eventArgs, 'loaderReturn> = {
}

type t = {
loaderHandlers: Js.Dict.t<registeredLoaderHandler<unknown, unknown>>,
contractRegisters: Js.Dict.t<contractRegister<unknown>>,
loaderHandlers: dict<registeredLoaderHandler<unknown, unknown>>,
contractRegisters: dict<contractRegister<unknown>>,
}

let make = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type rec t = {
}

// Constructors
@new @module external fromBigInt: Ethers.BigInt.t => t = "bignumber.js"
@new @module external fromBigInt: bigint => 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"
Expand Down
50 changes: 24 additions & 26 deletions codegenerator/cli/templates/static/codegen/src/bindings/BigInt.res
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
@genType.import(("./OpaqueTypes.ts", "GenericBigInt"))
type t

module Misc = {
%%private(
@inline
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<int> => b->toString->Belt.Int.fromString
@val external fromInt: int => bigint = "BigInt"
@val external fromStringUnsafe: string => bigint = "BigInt"
let fromString = str => unsafeToOption(() => str->fromStringUnsafe)
@send external toString: bigint => string = "toString"
let toInt = (b: bigint): option<int> => 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")
let add = (a: bigint, b: bigint): bigint => %raw("a + b")
let sub = (a: bigint, b: bigint): bigint => %raw("a - b")
let mul = (a: bigint, b: bigint): bigint => %raw("a * b")
let div = (a: bigint, b: bigint): bigint => %raw("b > 0n ? a / b : 0n")
let pow = (a: bigint, b: bigint): bigint => %raw("a ** b")
let mod = (a: bigint, b: bigint): bigint => %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")
let eq = (a: bigint, b: bigint): bool => %raw("a === b")
let neq = (a: bigint, b: bigint): bool => %raw("a !== b")
let gt = (a: bigint, b: bigint): bool => %raw("a > b")
let gte = (a: bigint, b: bigint): bool => %raw("a >= b")
let lt = (a: bigint, b: bigint): bool => %raw("a < b")
let lte = (a: bigint, b: bigint): 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 shift_left = (a: bigint, b: bigint): bigint => %raw("a << b")
let shift_right = (a: bigint, b: bigint): bigint => %raw("a >> b")
let logor = (a: bigint, b: bigint): bigint => %raw("a | b")
let logand = (a: bigint, b: bigint): bigint => %raw("a & b")
}

let zero = fromInt(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ module Misc = {
}
}

module BigInt = BigInt

type abi

let makeHumanReadableAbi = (abiArray: array<string>): abi => abiArray->Obj.magic
Expand Down Expand Up @@ -232,11 +230,11 @@ module JsonRpcProvider = {
external getBlockNumber: t => promise<int> = "getBlockNumber"

type block = {
_difficulty: BigInt.t,
_difficulty: bigint,
difficulty: int,
extraData: ethAddress,
gasLimit: BigInt.t,
gasUsed: BigInt.t,
gasLimit: bigint,
gasUsed: bigint,
hash: string,
miner: ethAddress,
nonce: int,
Expand Down
Loading

0 comments on commit 0822112

Please sign in to comment.