Skip to content

Commit

Permalink
Fix deserialization error resulting from Blockscout omitting "Optimiz…
Browse files Browse the repository at this point in the history
…ationRuns" field when optimization was not used (#23)

Blockscout seems to omit the "OptimizationRuns" field in the
"getSourceCode" response when the optimizer has not been used, resulting
in a serde deserialization error in the current implementation.
See for example the response for [this
contract](https://eth.blockscout.com/api?apikey=test&module=contract&action=getsourcecode&address=0xDef1C0ded9bec7F1a1670819833240f027b25EfF).

To avoid this I added a default (0) to the respective serde model field.
  • Loading branch information
ckoopmann authored Jan 3, 2024
1 parent 472118d commit e37a7da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub struct Metadata {
pub optimization_used: u64,

/// The number of optimizations performed.
#[serde(deserialize_with = "deserialize_stringified_u64", alias = "OptimizationRuns")]
#[serde(deserialize_with = "deserialize_stringified_u64", alias = "OptimizationRuns", default)]
pub runs: u64,

/// The constructor arguments the contract was deployed with.
Expand Down
24 changes: 23 additions & 1 deletion tests/it/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn can_fetch_contract_abi() {

#[tokio::test]
#[serial]
async fn can_fetch_contract_source_code_from_blockscout() {
async fn can_fetch_deposit_contract_source_code_from_blockscout() {
let client = Client::builder()
.with_url("https://eth.blockscout.com")
.unwrap()
Expand All @@ -54,6 +54,28 @@ async fn can_fetch_contract_source_code_from_blockscout() {
assert_eq!(item.abi().unwrap(), serde_json::from_str(DEPOSIT_CONTRACT_ABI).unwrap());
}

#[tokio::test]
#[serial]
async fn can_fetch_other_contract_source_code_from_blockscout() {
let client = Client::builder()
.with_url("https://eth.blockscout.com")
.unwrap()
.with_api_url("https://eth.blockscout.com/api")
.unwrap()
.with_api_key("test")
.build()
.unwrap();
let meta = client
.contract_source_code("0xDef1C0ded9bec7F1a1670819833240f027b25EfF".parse().unwrap())
.await
.unwrap();

assert_eq!(meta.items.len(), 1);
let item = &meta.items[0];
assert!(matches!(item.source_code, SourceCodeMetadata::SourceCode(_)));
assert_eq!(item.source_code.sources().len(), 1);
}

#[tokio::test]
#[serial]
async fn can_fetch_contract_source_code() {
Expand Down

0 comments on commit e37a7da

Please sign in to comment.