Skip to content

Commit

Permalink
Remove the Optionality to the get_balance. (#2117)
Browse files Browse the repository at this point in the history
* Remove the "Option<_>" for the get_balance because it is against the
principle of an oracle.
  • Loading branch information
MathieuDutSik authored Jun 11, 2024
1 parent 66521fa commit 0ce4f13
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
21 changes: 5 additions & 16 deletions linera-ethereum/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ pub trait EthereumQueries {
/// Gets the balance of the specified address at the specified block number.
/// if no block number is specified then the balance of the latest block is
/// returned.
async fn get_balance(
&self,
address: &str,
block_number: Option<u64>,
) -> Result<U256, Self::Error>;
async fn get_balance(&self, address: &str, block_number: u64) -> Result<U256, Self::Error>;

/// Reads the events of the smart contract.
///
Expand Down Expand Up @@ -129,11 +125,8 @@ pub trait EthereumQueries {
) -> Result<Bytes, Self::Error>;
}

pub(crate) fn get_block_id(block_number: Option<u64>) -> BlockId {
let number = match block_number {
None => BlockNumberOrTag::Latest,
Some(val) => BlockNumberOrTag::Number(val),
};
pub(crate) fn get_block_id(block_number: u64) -> BlockId {
let number = BlockNumberOrTag::Number(block_number);
BlockId::Number(number)
}

Expand All @@ -154,11 +147,7 @@ where
Ok(result.to::<u64>())
}

async fn get_balance(
&self,
address: &str,
block_number: Option<u64>,
) -> Result<U256, Self::Error> {
async fn get_balance(&self, address: &str, block_number: u64) -> Result<U256, Self::Error> {
let address = address.parse::<Address>()?;
let tag = get_block_id(block_number);
Ok(self.request("eth_getBalance", (address, tag)).await?)
Expand Down Expand Up @@ -201,7 +190,7 @@ where
.from(from)
.to(contract_address)
.input(input);
let tag = get_block_id(Some(block));
let tag = get_block_id(block);
Ok(self.request::<_, Bytes>("eth_call", (tx, tag)).await?)
}
}
4 changes: 2 additions & 2 deletions linera-ethereum/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl EthereumQueries for EthereumClient<HttpProvider> {
async fn get_balance(
&self,
address: &str,
block_number: Option<u64>,
block_number: u64,
) -> Result<U256, EthereumServiceError> {
let address = address.parse::<Address>()?;
let block_id = get_block_id(block_number);
Expand Down Expand Up @@ -129,7 +129,7 @@ impl EthereumQueries for EthereumClient<HttpProvider> {
.from(from)
.to(contract_address)
.input(input);
let block_id = get_block_id(Some(block));
let block_id = get_block_id(block);
let eth_call = self.provider.call(&tx).block(block_id);
Ok(eth_call.await?)
}
Expand Down
6 changes: 2 additions & 4 deletions linera-ethereum/tests/ethereum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ async fn test_get_accounts_balance() -> anyhow::Result<()> {
assert_eq!(block_number, block_number_simp);
let target_balance = U256::from_str("10000000000000000000000")?;
for address in addresses {
let balance = ethereum_client
.get_balance(&address, Some(block_number))
.await?;
let balance = ethereum_client.get_balance(&address, block_number).await?;
let balance_simp = ethereum_client_simp
.get_balance(&address, Some(block_number))
.get_balance(&address, block_number)
.await?;
assert_eq!(balance, balance_simp);
assert_eq!(balance, target_balance);
Expand Down

0 comments on commit 0ce4f13

Please sign in to comment.