cat_gateway/service/common/objects/cardano/
registration_info.rsuse poem_openapi::{types::Example, Object, Union};
use crate::service::{
api::cardano::types::{Nonce, PaymentAddress, PublicVotingInfo, TxId},
common::objects::cardano::hash::Hash,
utilities::as_hex_string,
};
#[derive(Object)]
struct Delegation {
#[oai(validator(min_length = "66", max_length = "66", pattern = "0x[0-9a-f]{64}"))]
voting_key: String,
#[oai(validator(minimum(value = "0"), maximum(value = "9223372036854775807")))]
power: i64,
}
#[derive(Object)]
struct Delegations {
#[oai(validator(max_items = "100"))]
delegations: Vec<Delegation>,
}
#[derive(Object)]
struct DirectVoter {
#[oai(validator(min_length = "66", max_length = "66", pattern = "0x[0-9a-f]{64}"))]
voting_key: String,
}
#[derive(Union)]
#[oai(discriminator_name = "type", one_of = true)]
enum VotingInfo {
Direct(DirectVoter),
Delegated(Delegations),
}
#[derive(Object)]
#[oai(example = true)]
pub(crate) struct RegistrationInfo {
#[oai(validator(min_length = "2", max_length = "116", pattern = "0x[0-9a-f]*"))]
rewards_address: String,
tx_hash: Hash,
#[oai(validator(minimum(value = "0"), maximum(value = "9223372036854775807")))]
nonce: Nonce,
voting_info: VotingInfo,
}
impl RegistrationInfo {
#[allow(dead_code)]
pub(crate) fn new(
tx_hash: TxId, rewards_address: &PaymentAddress, voting_info: PublicVotingInfo,
nonce: Nonce,
) -> Self {
let voting_info = match voting_info {
PublicVotingInfo::Direct(voting_key) => {
VotingInfo::Direct(DirectVoter {
voting_key: as_hex_string(voting_key.bytes()),
})
},
PublicVotingInfo::Delegated(delegations) => {
VotingInfo::Delegated(Delegations {
delegations: delegations
.into_iter()
.map(|(voting_key, power)| {
Delegation {
voting_key: as_hex_string(voting_key.bytes()),
power,
}
})
.collect(),
})
},
};
Self {
tx_hash: tx_hash.into(),
rewards_address: as_hex_string(rewards_address),
nonce,
voting_info,
}
}
}
impl Example for RegistrationInfo {
#[allow(clippy::expect_used)]
fn example() -> Self {
Self {
rewards_address: "0xe0f9722f71d23654387ec1389fe253d380653f4f7e7305a80cf5c4dfa1"
.to_string(),
tx_hash: hex::decode(
"27551498616e8da138780350a7cb8c18ef72cb01b0a6d40c785d095bcc8b1973",
)
.expect("Invalid hex")
.into(),
nonce: 11_623_850,
voting_info: VotingInfo::Delegated(Delegations {
delegations: vec![Delegation {
voting_key:
"0xb16f03d67e95ddd321df4bee8658901eb183d4cb5623624ff5edd7fe54f8e857"
.to_string(),
power: 1,
}],
}),
}
}
}