Skip to content

Commit e3de440

Browse files
committed
Make an option to make addresses lower case
1 parent 2d65055 commit e3de440

File tree

14 files changed

+95
-18
lines changed

14 files changed

+95
-18
lines changed

codegenerator/cli/npm/envio/src/Address.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ external unsafeFromString: string => t = "%identity"
1010
module Evm = {
1111
@module("viem")
1212
external fromStringOrThrow: string => t = "getAddress"
13+
14+
// NOTE: We could use a regex for this instead, not sure if it is faster/slower than viem's 'isAddress' function
15+
// `/^0x[a-fA-F0-9]{40}$/`
16+
@module("viem") @private
17+
external isAddress: string => bool = "isAddress"
18+
19+
// Validate that the string is a proper address but return a lowercased value
20+
let fromStringLowercaseOrThrow = string => {
21+
// NOTE: We could use a regex for this and make this function more strict, so it only accepts lower case addresses as input
22+
// eg this regex: `/^0x[a-f0-9]{40}$/`
23+
if (isAddress(string)) {
24+
unsafeFromString(string->Js.String2.toLowerCase)
25+
} else {
26+
Js.Exn.raiseError(
27+
`Address "${string}" is invalid. Expected a 20-byte hex string starting with 0x.`,
28+
)
29+
}
30+
}
31+
32+
let fromAddressLowercaseOrThrow = address =>
33+
address->toString->fromStringLowercaseOrThrow
34+
1335
// Reassign since the function might be used in the handler code
1436
// and we don't want to have a "viem" import there. It's needed to keep "viem" a dependency
1537
// of generated code instead of adding it to the indexer project dependencies.

codegenerator/cli/npm/envio/src/sources/HyperSyncClient.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,16 @@ type t = {
448448
}
449449

450450
@module("@envio-dev/hypersync-client") @scope("HypersyncClient") external make: cfg => t = "new"
451-
let make = (~url, ~apiToken, ~httpReqTimeoutMillis, ~maxNumRetries) =>
451+
let make = (
452+
~url,
453+
~apiToken,
454+
~httpReqTimeoutMillis,
455+
~maxNumRetries,
456+
~enableChecksumAddresses=true,
457+
) =>
452458
make({
453459
url,
454-
enableChecksumAddresses: true,
460+
enableChecksumAddresses,
455461
bearerToken: apiToken,
456462
httpReqTimeoutMillis,
457463
maxNumRetries,

codegenerator/cli/src/cli_args/init_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ pub mod evm {
171171
field_selection: None,
172172
raw_events: None,
173173
preload_handlers: Some(true),
174+
lowercase_addresses: None,
174175
})
175176
}
176177

codegenerator/cli/src/config_parsing/graph_migration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ pub async fn generate_config_from_subgraph_id(
285285
field_selection: None,
286286
raw_events: None,
287287
preload_handlers: Some(true),
288+
lowercase_addresses: None,
288289
};
289290
let mut networks: Vec<Network> = vec![];
290291

codegenerator/cli/src/config_parsing/human_config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ pub mod evm {
209209
description = "Makes handlers run twice to enable preload optimisations. Removes handlerWithLoader API, since it's not needed. (recommended, default: false)"
210210
)]
211211
pub preload_handlers: Option<bool>,
212+
#[serde(skip_serializing_if = "Option::is_none")]
213+
#[schemars(
214+
description = "If true, Ethereum addresses are kept lowercase (no checksum) across the indexer (default: false)"
215+
)]
216+
pub lowercase_addresses: Option<bool>,
212217
}
213218

214219
impl Display for HumanConfig {

codegenerator/cli/src/config_parsing/system_config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ pub struct SystemConfig {
414414
pub enable_raw_events: bool,
415415
pub preload_handlers: bool,
416416
pub human_config: HumanConfig,
417+
pub lowercase_addresses: bool,
417418
}
418419

419420
//Getter methods for system config
@@ -721,6 +722,7 @@ impl SystemConfig {
721722
field_selection,
722723
enable_raw_events: evm_config.raw_events.unwrap_or(false),
723724
preload_handlers: evm_config.preload_handlers.unwrap_or(false),
725+
lowercase_addresses: evm_config.lowercase_addresses.unwrap_or(false),
724726
human_config,
725727
})
726728
}
@@ -858,6 +860,7 @@ impl SystemConfig {
858860
field_selection: FieldSelection::fuel(),
859861
enable_raw_events: fuel_config.raw_events.unwrap_or(false),
860862
preload_handlers: fuel_config.preload_handlers.unwrap_or(false),
863+
lowercase_addresses: false,
861864
human_config,
862865
})
863866
}
@@ -2085,6 +2088,7 @@ mod test {
20852088
field_selection: None,
20862089
raw_events: None,
20872090
preload_handlers: None,
2091+
lowercase_addresses: None,
20882092
};
20892093

20902094
let project_paths = ParsedProjectPaths::new(".", "generated", "config.yaml").unwrap();
@@ -2131,6 +2135,7 @@ mod test {
21312135
field_selection: None,
21322136
raw_events: None,
21332137
preload_handlers: None,
2138+
lowercase_addresses: None,
21342139
};
21352140

21362141
let system_config_with_output = SystemConfig::from_human_config(

codegenerator/cli/src/hbs_templating/codegen_templates.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ impl NetworkConfigTemplate {
11231123
format!(
11241124
"NetworkSources.evm(~chain, ~contracts=[{contracts_code}], ~hyperSync={hyper_sync_code}, \
11251125
~allEventSignatures=[{all_event_signatures}]->Belt.Array.concatMany, \
1126-
~shouldUseHypersyncClientDecoder={is_client_decoder}, ~rpcs=[{rpcs}])"
1126+
~shouldUseHypersyncClientDecoder={is_client_decoder}, ~rpcs=[{rpcs}], ~lowercaseAddresses={})",
1127+
if config.lowercase_addresses { "true" } else { "false" }
11271128
),
11281129
deprecated_sync_source_code,
11291130
)
@@ -1278,6 +1279,7 @@ pub struct ProjectTemplate {
12781279
envio_version: String,
12791280
//Used for the package.json reference to handlers in generated
12801281
relative_path_to_root_from_generated: String,
1282+
lowercase_addresses: bool,
12811283
}
12821284

12831285
impl ProjectTemplate {
@@ -1384,6 +1386,7 @@ impl ProjectTemplate {
13841386
envio_version: get_envio_version()?,
13851387
//Used for the package.json reference to handlers in generated
13861388
relative_path_to_root_from_generated,
1389+
lowercase_addresses: cfg.lowercase_addresses,
13871390
})
13881391
}
13891392
}

codegenerator/cli/templates/dynamic/codegen/src/ConfigYAML.res.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type configYaml = {
2727
startBlock: int,
2828
confirmedBlockThreshold: int,
2929
contracts: dict<contract>,
30+
lowercaseAddresses: bool,
3031
}
3132

3233
let publicConfig = ChainMap.fromArrayUnsafe([
@@ -60,7 +61,8 @@ let publicConfig = ChainMap.fromArrayUnsafe([
6061
confirmedBlockThreshold: {{chain_config.network_config.confirmed_block_threshold}},
6162
syncSource: {{chain_config.deprecated_sync_source_code}},
6263
startBlock: {{chain_config.network_config.start_block}},
63-
contracts
64+
contracts,
65+
lowercaseAddresses: {{#if ../lowercase_addresses}}true{{else}}false{{/if}}
6466
}
6567
)
6668
},

codegenerator/cli/templates/dynamic/codegen/src/RegisterHandlers.res.hbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ let registerContractHandlers = (
3535
addresses: [
3636
{{#each contract.addresses as | address |}}
3737
{{#if ../../../is_evm_ecosystem}}
38+
{{#if ../../../../lowercase_addresses}}
39+
"{{address}}"->Address.Evm.fromStringLowercaseOrThrow
40+
{{else}}
3841
"{{address}}"->Address.Evm.fromStringOrThrow
42+
{{/if}}
3943
{{else}}
4044
"{{address}}"->Address.unsafeFromString
4145
{{/if}},
@@ -71,6 +75,7 @@ let registerContractHandlers = (
7175
~isUnorderedMultichainMode={{is_unordered_multichain_mode}},
7276
~chains,
7377
~enableRawEvents={{enable_raw_events}},
78+
~lowercaseAddresses={{lowercase_addresses}},
7479
{{#if chain_config.is_fuel}}
7580
~ecosystem=Fuel,
7681
{{/if}}

codegenerator/cli/templates/static/codegen/src/Config.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type t = {
128128
persistence: Persistence.t,
129129
addContractNameToContractNameMapping: dict<string>,
130130
maxAddrInPartition: int,
131+
lowercaseAddresses: bool,
131132
}
132133

133134
let make = (
@@ -138,6 +139,7 @@ let make = (
138139
~enableRawEvents=false,
139140
~persistence=codegenPersistence,
140141
~ecosystem=Evm,
142+
~lowercaseAddresses=false,
141143
) => {
142144
let chainMap =
143145
chains
@@ -172,6 +174,7 @@ let make = (
172174
ecosystem,
173175
addContractNameToContractNameMapping,
174176
maxAddrInPartition: Env.maxAddrInPartition,
177+
lowercaseAddresses,
175178
}
176179
}
177180

0 commit comments

Comments
 (0)