ethexe-ethereum 2.0.0

Ethereum contract bindings and transaction helpers for ethexe
Documentation
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

use crate::abi::{Gear, utils::*};
use ethexe_common::gear::*;
use gear_core::message::ReplyDetails;
use gear_core_errors::{ReplyCode, SuccessReplyReason};

//                          //
// From Rust types to alloy //
//                          //

impl From<AggregatedPublicKey> for Gear::AggregatedPublicKey {
    fn from(value: AggregatedPublicKey) -> Self {
        Self {
            x: u256_to_uint256(value.x),
            y: u256_to_uint256(value.y),
        }
    }
}

impl From<ChainCommitment> for Gear::ChainCommitment {
    fn from(value: ChainCommitment) -> Self {
        Self {
            transitions: value.transitions.into_iter().map(Into::into).collect(),
            head: value.head.0.into(),
            lastAdvancedEthBlock: value.last_advanced_eth_block.0.into(),
        }
    }
}

impl From<CodeCommitment> for Gear::CodeCommitment {
    fn from(value: CodeCommitment) -> Self {
        Self {
            id: code_id_to_bytes32(value.id),
            valid: value.valid,
        }
    }
}

impl From<ValidatorsCommitment> for Gear::ValidatorsCommitment {
    fn from(value: ValidatorsCommitment) -> Self {
        Self {
            hasAggregatedPublicKey: value.has_aggregated_public_key,
            aggregatedPublicKey: value.aggregated_public_key.into(),
            verifiableSecretSharingCommitment: Bytes::copy_from_slice(
                &value.verifiable_secret_sharing_commitment,
            ),
            validators: value.validators.into_iter().map(|v| v.into()).collect(),
            eraIndex: Uint256::from(value.era_index),
        }
    }
}

impl From<OperatorRewardsCommitment> for Gear::OperatorRewardsCommitment {
    fn from(value: OperatorRewardsCommitment) -> Self {
        Self {
            amount: u256_to_uint256(value.amount),
            root: h256_to_bytes32(value.root),
        }
    }
}

impl From<StakerRewards> for Gear::StakerRewards {
    fn from(value: StakerRewards) -> Self {
        Self {
            vault: value.vault.0.into(),
            amount: u256_to_uint256(value.amount),
        }
    }
}

impl From<StakerRewardsCommitment> for Gear::StakerRewardsCommitment {
    fn from(value: StakerRewardsCommitment) -> Self {
        Self {
            distribution: value.distribution.into_iter().map(Into::into).collect(),
            totalAmount: u256_to_uint256(value.total_amount),
            token: value.token.0.into(),
        }
    }
}

impl From<RewardsCommitment> for Gear::RewardsCommitment {
    fn from(value: RewardsCommitment) -> Self {
        Self {
            operators: value.operators.into(),
            stakers: value.stakers.into(),
            timestamp: u64_to_uint48_lossy(value.timestamp),
        }
    }
}

impl From<BatchCommitment> for Gear::BatchCommitment {
    fn from(value: BatchCommitment) -> Self {
        Self {
            blockHash: value.block_hash.0.into(),
            blockTimestamp: u64_to_uint48_lossy(value.timestamp),
            previousCommittedBatchHash: value.previous_batch.0.into(),
            expiry: value.expiry,
            chainCommitment: value.chain_commitment.into_iter().map(Into::into).collect(),
            codeCommitments: value.code_commitments.into_iter().map(Into::into).collect(),
            rewardsCommitment: value
                .rewards_commitment
                .into_iter()
                .map(Into::into)
                .collect(),
            validatorsCommitment: value
                .validators_commitment
                .into_iter()
                .map(Into::into)
                .collect(),
        }
    }
}

impl From<Message> for Gear::Message {
    fn from(value: Message) -> Self {
        Self {
            id: message_id_to_bytes32(value.id),
            destination: actor_id_to_address_lossy(value.destination),
            payload: value.payload.into(),
            value: value.value,
            replyDetails: value.reply_details.into(),
            call: value.call,
        }
    }
}

impl From<Gear::Timelines> for Timelines {
    fn from(value: Gear::Timelines) -> Self {
        Self {
            era: value.era.to::<u64>(),
            election: value.election.to::<u64>(),
            validation_delay: value.validationDelay.to::<u64>(),
        }
    }
}

impl From<Option<ReplyDetails>> for Gear::ReplyDetails {
    fn from(value: Option<ReplyDetails>) -> Self {
        value
            .unwrap_or(ReplyDetails::new(
                Default::default(),
                ReplyCode::Success(SuccessReplyReason::Auto),
            ))
            .into()
    }
}

impl From<ReplyDetails> for Gear::ReplyDetails {
    fn from(value: ReplyDetails) -> Self {
        let (to, code) = value.into_parts();

        Self {
            to: message_id_to_bytes32(to),
            code: code.to_bytes().into(),
        }
    }
}

impl From<StateTransition> for Gear::StateTransition {
    fn from(value: StateTransition) -> Self {
        Self {
            actorId: actor_id_to_address_lossy(value.actor_id),
            newStateHash: h256_to_bytes32(value.new_state_hash),
            exited: value.exited,
            inheritor: actor_id_to_address_lossy(value.inheritor),
            valueToReceive: value.value_to_receive,
            valueToReceiveNegativeSign: value.value_to_receive_negative_sign,
            valueClaims: value.value_claims.into_iter().map(Into::into).collect(),
            messages: value.messages.into_iter().map(Into::into).collect(),
        }
    }
}

impl From<ValueClaim> for Gear::ValueClaim {
    fn from(value: ValueClaim) -> Self {
        Self {
            messageId: message_id_to_bytes32(value.message_id),
            destination: actor_id_to_address_lossy(value.destination),
            value: value.value,
        }
    }
}

impl From<Gear::GenesisBlockInfo> for GenesisBlockInfo {
    fn from(value: Gear::GenesisBlockInfo) -> Self {
        Self {
            hash: bytes32_to_h256(value.hash),
            number: value.number,
            timestamp: value.timestamp.to::<u64>(),
        }
    }
}

impl From<Gear::ComputationSettings> for ComputationSettings {
    fn from(value: Gear::ComputationSettings) -> Self {
        Self {
            threshold: value.threshold,
            wvara_per_second: value.wvaraPerSecond,
        }
    }
}