Skip to content

Commit

Permalink
Fix deserialization during upgrade (#6561)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 11, 2024
1 parent fdec217 commit 1342737
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions backend/libraries/chat_events/src/message_content_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ impl From<CompletedCryptoTransaction> for CompletedCryptoTransactionInternal {
mod nns {
use super::*;
use ic_ledger_types::AccountIdentifier;
use serde::Deserializer;
use types::nns::{CryptoAccount, Tokens};

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -825,9 +826,9 @@ mod nns {
pub ledger: CanisterId,
#[serde(rename = "k", alias = "token")]
pub token: Cryptocurrency,
#[serde(rename = "a", alias = "amount")]
#[serde(rename = "a", alias = "amount", deserialize_with = "deserialize_amount")]
pub amount: u64,
#[serde(rename = "e", alias = "fee")]
#[serde(rename = "e", alias = "fee", deserialize_with = "deserialize_amount")]
pub fee: u64,
#[serde(rename = "f", alias = "from")]
pub from: CryptoAccountInternal,
Expand All @@ -843,6 +844,27 @@ mod nns {
pub block_index: u64,
}

fn deserialize_amount<'de, D: Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
let amount = AmountCombined::deserialize(d)?;
Ok(amount.into())
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum AmountCombined {
Old { e8s: u64 },
New(u64),
}

impl From<AmountCombined> for u64 {
fn from(value: AmountCombined) -> Self {
match value {
AmountCombined::Old { e8s } => e8s,
AmountCombined::New(a) => a,
}
}
}

impl From<CompletedCryptoTransactionInternal> for types::nns::CompletedCryptoTransaction {
fn from(value: CompletedCryptoTransactionInternal) -> Self {
Self {
Expand Down

0 comments on commit 1342737

Please sign in to comment.