From 98fc8c88e45483566fd1eaa50f1b70531437fa5a Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Wed, 11 May 2022 07:35:30 -0700 Subject: [PATCH] impl TryFrom for Chain (#1247) --- ethers-core/src/types/chain.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ethers-core/src/types/chain.rs b/ethers-core/src/types/chain.rs index c05c1ed7..2eb5bafc 100644 --- a/ethers-core/src/types/chain.rs +++ b/ethers-core/src/types/chain.rs @@ -2,7 +2,7 @@ use serde::Deserialize; use thiserror::Error; use core::convert::TryFrom; -use std::{default, fmt, str::FromStr}; +use std::{convert::TryInto, default, fmt, str::FromStr}; use crate::types::U256; @@ -128,6 +128,17 @@ impl TryFrom for Chain { } } +impl TryFrom for Chain { + type Error = ParseChainError; + + fn try_from(chain: U256) -> Result { + if chain.bits() > 64 { + return Err(ParseChainError(chain.to_string())) + } + chain.as_u64().try_into() + } +} + impl FromStr for Chain { type Err = ParseChainError; fn from_str(chain: &str) -> Result {