Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decoding of contract log events #317

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3954,7 +3954,8 @@ pub fn events_from_summary(
address,
events,
} => Ok(Event::ContractInterrupted(ContractInterrupted {
contract_address: address.into(),
contract_address: address.into(),
contract_logs_raw: events.iter().map(|e| e.as_ref().to_vec()).collect(),
})),
ContractTraceElement::Resumed {
address,
Expand Down Expand Up @@ -5061,15 +5062,70 @@ pub struct ChainUpdatePayload {

#[derive(SimpleObject, serde::Serialize, serde::Deserialize)]
pub struct ContractInterrupted {
contract_address: ContractAddress,
// eventsAsHex("Returns the first _n_ elements from the list." first: Int "Returns the elements
// in the list that come after the specified cursor." after: String "Returns the last _n_
// elements from the list." last: Int "Returns the elements in the list that come before the
// specified cursor." before: String): StringConnection events("Returns the first _n_
// elements from the list." first: Int "Returns the elements in the list that come after the
// specified cursor." after: String "Returns the last _n_ elements from the list." last: Int
// "Returns the elements in the list that come before the specified cursor." before: String):
// StringConnection
contract_address: ContractAddress,
// All logged events by the smart contract during this section of the transaction execution.
contract_logs_raw: Vec<Vec<u8>>,
}

#[ComplexObject]
impl ContractInterrupted {
async fn events_as_hex(&self) -> ApiResult<connection::Connection<String, String>> {
let mut connection = connection::Connection::new(true, true);

self.contract_logs_raw.iter().enumerate().for_each(|(index, log)| {
connection.edges.push(connection::Edge::new(index.to_string(), hex::encode(log)));
});

// Nice-to-have: pagination info but not used at front-end currently.

Ok(connection)
}

async fn events<'a>(
&self,
ctx: &Context<'a>,
) -> ApiResult<connection::Connection<String, String>> {
let pool = get_pool(ctx)?;

let row = sqlx::query!(
"
SELECT
contracts.module_reference as module_reference,
name as contract_name,
schema as display_schema
FROM contracts
JOIN smart_contract_modules ON smart_contract_modules.module_reference = \
contracts.module_reference
WHERE index = $1 AND sub_index = $2
",
self.contract_address.index.0 as i64,
self.contract_address.sub_index.0 as i64
)
.fetch_optional(pool)
.await?
.ok_or(ApiError::NotFound)?;

let opt_event_schema = row
.display_schema
.as_ref()
.and_then(|schema| VersionedModuleSchema::new(schema, &None).ok())
lassemand marked this conversation as resolved.
Show resolved Hide resolved
.and_then(|versioned_schema| {
versioned_schema.get_event_schema(&row.contract_name).ok()
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
});

let mut connection = connection::Connection::new(true, true);

for (index, log) in self.contract_logs_raw.iter().enumerate() {
let decoded_log =
decode_value_with_schema(opt_event_schema.as_ref(), "event", log, "contract_log");
lassemand marked this conversation as resolved.
Show resolved Hide resolved

connection.edges.push(connection::Edge::new(index.to_string(), decoded_log));
}

// Nice-to-have: pagination info but not used at front-end currently.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the inline comment to TODO. I feel it is important to know why pagination is not handled here at the moment when reading the code.

lassemand marked this conversation as resolved.
Show resolved Hide resolved

Ok(connection)
}
}

#[derive(SimpleObject, serde::Serialize, serde::Deserialize)]
Expand All @@ -5086,7 +5142,7 @@ pub struct ContractUpdated {
amount: Amount,
receive_name: String,
version: ContractVersion,
// All logged events by the smart contract during the transaction execution.
// All logged events by the smart contract during this section of the transaction execution.
contract_logs_raw: Vec<Vec<u8>>,
input_parameter: Vec<u8>,
}
Expand Down
Loading