Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feature: very permissive etherscan gwei deser #2327

Merged
merged 2 commits into from
Apr 10, 2023

Conversation

prestwich
Copy link
Collaborator

Extremely permissive deserializer for etherscan gas units

Motivation

More complete fix to #2326 #2325

Solution

Introduce a deser that performs the following in order:

  • checks for a JSON number, and scales it by WEI_PER_GWEI
  • checks for a string, then tries to parse it as a float number of gwei

change the etherscan response object to have U256 responses that are ALWAYS in wei rather than in f64 gwei

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@prestwich prestwich added the enhancement New feature or request label Apr 5, 2023
@prestwich prestwich self-assigned this Apr 5, 2023
Copy link
Contributor

@shekohex shekohex left a comment

Choose a reason for hiding this comment

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

Looks good!

Comment on lines +33 to +35
// This function is used to deserialize a string or number into a U256 with an
// amount of gwei. If the contents is a number, deserialize it. If the contents
// is a string, attempt to deser as first a decimal f64 then a decimal U256.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// This function is used to deserialize a string or number into a U256 with an
// amount of gwei. If the contents is a number, deserialize it. If the contents
// is a string, attempt to deser as first a decimal f64 then a decimal U256.
// This function deserializes a string or number into a U256 with an amount of gwei.
// If the content is a number, it is deserialized. If the content is a string, we attempt to deserialize it first // as a decimal f64 and then as a decimal U256.

use serde::{de, Deserialize, Deserializer};
use std::{collections::HashMap, str::FromStr};

const WEI_PER_GWEI: u64 = 1_000_000_000;
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe use super::GWEI_TO_WEI instead?

Suggested change
const WEI_PER_GWEI: u64 = 1_000_000_000;

}

match StringOrInt::deserialize(deserializer)? {
StringOrInt::Number(i) => Ok(U256::from(i) * WEI_PER_GWEI),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
StringOrInt::Number(i) => Ok(U256::from(i) * WEI_PER_GWEI),
StringOrInt::Number(i) => Ok(U256::from(i) * super::GWEI_TO_WEI),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

doesn't exist in this package, that's in the middleware, not in the etherscan

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see, gotcha!

@prestwich prestwich merged commit 36a2037 into master Apr 10, 2023
@prestwich prestwich deleted the prestwich/etherscan-gwei branch April 10, 2023 18:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants