|
| 1 | +#![allow(clippy::enum_variant_names)] |
| 2 | +#![allow(missing_docs)] |
| 3 | + |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use async_trait::async_trait; |
| 7 | +use ethers::providers::Middleware; |
| 8 | +use ethers_contract::builders::ContractCall; |
| 9 | +use hyperlane_core::{ |
| 10 | + Announcement, ChainResult, ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain, |
| 11 | + HyperlaneProvider, SignedType, TxOutcome, ValidatorAnnounce, H160, H256, U256, |
| 12 | +}; |
| 13 | +use tracing::{instrument, trace}; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + interfaces::i_validator_announce::IValidatorAnnounce as TronValidatorAnnounceInternal, |
| 17 | + TronProvider, |
| 18 | +}; |
| 19 | + |
| 20 | +/// A reference to a ValidatorAnnounce contract on some Tron chain |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct TronValidatorAnnounce { |
| 23 | + contract: Arc<TronValidatorAnnounceInternal<TronProvider>>, |
| 24 | + domain: HyperlaneDomain, |
| 25 | + provider: Arc<TronProvider>, |
| 26 | +} |
| 27 | + |
| 28 | +impl TronValidatorAnnounce { |
| 29 | + /// Create a reference to a ValidatoAnnounce contract at a specific Tron |
| 30 | + /// address on some chain |
| 31 | + pub fn new(provider: TronProvider, locator: &ContractLocator) -> Self { |
| 32 | + let provider = Arc::new(provider); |
| 33 | + Self { |
| 34 | + contract: Arc::new(TronValidatorAnnounceInternal::new( |
| 35 | + locator.address, |
| 36 | + provider.clone(), |
| 37 | + )), |
| 38 | + domain: locator.domain.clone(), |
| 39 | + provider, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns a ContractCall that processes the provided message. |
| 44 | + /// If the provided tx_gas_limit is None, gas estimation occurs. |
| 45 | + async fn announce_contract_call( |
| 46 | + &self, |
| 47 | + announcement: SignedType<Announcement>, |
| 48 | + ) -> ChainResult<ContractCall<TronProvider, bool>> { |
| 49 | + let serialized_signature: [u8; 65] = announcement.signature.into(); |
| 50 | + let tx = self.contract.announce( |
| 51 | + announcement.value.validator.into(), |
| 52 | + announcement.value.storage_location, |
| 53 | + serialized_signature.into(), |
| 54 | + ); |
| 55 | + Ok(tx) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl HyperlaneChain for TronValidatorAnnounce { |
| 60 | + fn domain(&self) -> &HyperlaneDomain { |
| 61 | + &self.domain |
| 62 | + } |
| 63 | + |
| 64 | + fn provider(&self) -> Box<dyn HyperlaneProvider> { |
| 65 | + Box::new(self.provider.clone()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl HyperlaneContract for TronValidatorAnnounce { |
| 70 | + fn address(&self) -> H256 { |
| 71 | + self.contract.address().into() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[async_trait] |
| 76 | +impl ValidatorAnnounce for TronValidatorAnnounce { |
| 77 | + async fn get_announced_storage_locations( |
| 78 | + &self, |
| 79 | + validators: &[H256], |
| 80 | + ) -> ChainResult<Vec<Vec<String>>> { |
| 81 | + let storage_locations = self |
| 82 | + .contract |
| 83 | + .get_announced_storage_locations( |
| 84 | + validators.iter().map(|v| H160::from(*v).into()).collect(), |
| 85 | + ) |
| 86 | + .call() |
| 87 | + .await?; |
| 88 | + Ok(storage_locations) |
| 89 | + } |
| 90 | + |
| 91 | + #[instrument(ret, skip(self))] |
| 92 | + async fn announce_tokens_needed( |
| 93 | + &self, |
| 94 | + announcement: SignedType<Announcement>, |
| 95 | + chain_signer: H256, |
| 96 | + ) -> Option<U256> { |
| 97 | + let Ok(contract_call) = self.announce_contract_call(announcement).await else { |
| 98 | + trace!("Unable to get announce contract call"); |
| 99 | + return None; |
| 100 | + }; |
| 101 | + |
| 102 | + let chain_signer_h160 = ethers::types::H160::from(chain_signer); |
| 103 | + let Ok(balance) = Middleware::get_balance(&self.provider, chain_signer_h160, None).await |
| 104 | + else { |
| 105 | + trace!("Unable to query balance"); |
| 106 | + return None; |
| 107 | + }; |
| 108 | + |
| 109 | + let Some(max_cost) = contract_call.tx.max_cost() else { |
| 110 | + trace!("Unable to get announce max cost"); |
| 111 | + return None; |
| 112 | + }; |
| 113 | + Some(max_cost.saturating_sub(balance).into()) |
| 114 | + } |
| 115 | + |
| 116 | + #[instrument(err, ret, skip(self))] |
| 117 | + async fn announce(&self, announcement: SignedType<Announcement>) -> ChainResult<TxOutcome> { |
| 118 | + let contract_call = self.announce_contract_call(announcement).await?; |
| 119 | + self.provider.send_and_wait(&contract_call).await |
| 120 | + } |
| 121 | +} |
0 commit comments