Skip to content

Commit

Permalink
Use block height for id on contract import (#209)
Browse files Browse the repository at this point in the history
* Use block height for id on contract import

* Fix typo

* Update codegenerator/cli/src/hbs_templating/contract_import_templates.rs

Co-authored-by: Jono Prest <[email protected]>

---------

Co-authored-by: Jono Prest <[email protected]>
  • Loading branch information
DZakh and JonoPrest authored Sep 17, 2024
1 parent c46f91a commit 20bae84
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@ impl Event {
}
.to_string();

let second_part = match is_fuel {
true => format!("${{{event_var_name}.transaction.id}}"),
false => format!("${{{event_var_name}.block.number}}"),
let block_number_field = match is_fuel {
true => "height",
false => "number",
};

format!(
"`${{{event_var_name}.chainId}}_{second_part}_${{{event_var_name}.logIndex{}}}`",
"`${{{event_var_name}.chainId}}_${{{event_var_name}.block.{block_number_field}}}_${{{event_var_name}.logIndex{}}}`",
to_string_code
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ module HeightQuery = {
//height to be set in loop
let height = ref(0)

//Retry if the heigth is 0 (expect height to be greater)
//Retry if the height is 0 (expect height to be greater)
while height.contents <= 0 {
let res = await HyperFuelJsonApi.getArchiveHeight(~serverUrl)
Logging.debug({"msg": "querying height", "response": res})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ module LogsQuery = {
switch returnedObj
->(Utils.magic: 'a => Js.Dict.t<unknown>)
->Utils.Dict.dangerouslyGetNonOption(fieldName) {
| Some(_) => ()
| None => acc->Array.push(prefix ++ "." ++ fieldName)->ignore
| Some(_) => ()
| None => acc->Array.push(prefix ++ "." ++ fieldName)->ignore
}
})
}

//Note this function can throw an error
let convertEvent = (event: HyperSyncClient.ResponseTypes.event, ~nonOptionalBlockFieldNames, ~nonOptionalTransactionFieldNames): logsQueryPageItem => {
let convertEvent = (
event: HyperSyncClient.ResponseTypes.event,
~nonOptionalBlockFieldNames,
~nonOptionalTransactionFieldNames,
): logsQueryPageItem => {
let missingParams = []
missingParams->addMissingParams(Types.Log.fieldNames, event.log, ~prefix="log")
missingParams->addMissingParams(nonOptionalBlockFieldNames, event.block, ~prefix="block")
Expand Down Expand Up @@ -150,12 +154,17 @@ module LogsQuery = {
}
}

let convertResponse = (res: HyperSyncClient.ResponseTypes.eventResponse, ~nonOptionalBlockFieldNames, ~nonOptionalTransactionFieldNames): queryResponse<
logsQueryPage,
> => {
let convertResponse = (
res: HyperSyncClient.ResponseTypes.eventResponse,
~nonOptionalBlockFieldNames,
~nonOptionalTransactionFieldNames,
): queryResponse<logsQueryPage> => {
try {
let {nextBlock, archiveHeight, rollbackGuard} = res
let items = res.data->Array.map(item => item->convertEvent(~nonOptionalBlockFieldNames, ~nonOptionalTransactionFieldNames))
let items =
res.data->Array.map(item =>
item->convertEvent(~nonOptionalBlockFieldNames, ~nonOptionalTransactionFieldNames)
)
let page: logsQueryPage = {
items,
nextBlock,
Expand Down Expand Up @@ -191,7 +200,12 @@ module LogsQuery = {
})
)

let query = makeRequestBody(~fromBlock, ~toBlockInclusive=toBlock, ~addressesWithTopics, ~fieldSelection)
let query = makeRequestBody(
~fromBlock,
~toBlockInclusive=toBlock,
~addressesWithTopics,
~fieldSelection,
)

let hyperSyncClient = CachedClients.getClient(serverUrl)

Expand All @@ -216,7 +230,7 @@ module HeightQuery = {
//height to be set in loop
let height = ref(0)

//Retry if the heigth is 0 (expect height to be greater)
//Retry if the height is 0 (expect height to be greater)
while height.contents <= 0 {
let res = await HyperSyncJsonApi.getArchiveHeight(~serverUrl)
switch res {
Expand Down

0 comments on commit 20bae84

Please sign in to comment.