Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions rs/rosetta-api/icrc1/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ pub enum OperationType {
Approve,
Fee,
FeeCollector,
Pause,
Unpause,
Deactivate,
}

#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -427,3 +430,42 @@ impl TryFrom<ObjectMap> for FeeCollectorMetadata {
.context("Could not parse FeeCollectorMetadata from JSON object")
}
}

#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
pub struct ManagementActionMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub caller: Option<Principal>,

#[serde(skip_serializing_if = "Option::is_none")]
pub mthd: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}

impl TryFrom<ManagementActionMetadata> for ObjectMap {
type Error = anyhow::Error;
fn try_from(d: ManagementActionMetadata) -> Result<ObjectMap, Self::Error> {
match serde_json::to_value(d) {
Ok(v) => match v {
serde_json::Value::Object(ob) => Ok(ob),
_ => anyhow::bail!(
"Could not convert ManagementActionMetadata to ObjectMap. Expected type Object but received: {:?}",
v
),
},
Err(err) => anyhow::bail!(
"Could not convert ManagementActionMetadata to ObjectMap: {:?}",
err
),
}
}
}

impl TryFrom<ObjectMap> for ManagementActionMetadata {
type Error = anyhow::Error;
fn try_from(o: ObjectMap) -> anyhow::Result<Self> {
serde_json::from_value(serde_json::Value::Object(o))
.context("Could not parse ManagementActionMetadata from JSON object")
}
}
130 changes: 124 additions & 6 deletions rs/rosetta-api/icrc1/src/common/utils/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::common::storage::types::{IcrcOperation, RosettaBlock};
use crate::common::types::{FeeCollectorMetadata, FeeMetadata, FeeSetter};
use crate::common::types::{
FeeCollectorMetadata, FeeMetadata, FeeSetter, ManagementActionMetadata,
};
use crate::{
AppState, MultiTokenAppState,
common::{
Expand Down Expand Up @@ -156,6 +158,9 @@ pub fn rosetta_core_operations_to_icrc1_operation(
Transfer,
Approve,
FeeCollector,
Pause,
Unpause,
Deactivate,
}

// A builder which helps depict the icrc1 Operation and allows for an arbitrary order of rosetta_core Operations
Expand All @@ -172,6 +177,7 @@ pub fn rosetta_core_operations_to_icrc1_operation(
fee_collector: Option<Account>,
caller: Option<Principal>,
mthd: Option<String>,
reason: Option<String>,
}

impl IcrcOperationBuilder {
Expand All @@ -189,6 +195,7 @@ pub fn rosetta_core_operations_to_icrc1_operation(
fee_collector: None,
caller: None,
mthd: None,
reason: None,
}
}

Expand Down Expand Up @@ -252,8 +259,13 @@ pub fn rosetta_core_operations_to_icrc1_operation(
self
}

pub fn with_reason(mut self, reason: Option<String>) -> Self {
self.reason = reason;
self
}

pub fn build(self) -> anyhow::Result<crate::common::storage::types::IcrcOperation> {
Ok(match self.icrc_operation.context("Icrc Operation type needs to be of type Mint, Burn, Transfer or Approve")? {
Ok(match self.icrc_operation.context("Icrc Operation type must be one of Mint, Burn, Transfer, Approve, FeeCollector, Pause, Unpause, or Deactivate")? {
IcrcOperation::Mint => {
if self.from.is_some() {
bail!("From AccountIdentifier field is not allowed for Mint operation")
Expand Down Expand Up @@ -300,6 +312,21 @@ pub fn rosetta_core_operations_to_icrc1_operation(
caller: self.caller,
mthd: self.mthd,
},
IcrcOperation::Pause => crate::common::storage::types::IcrcOperation::Pause{
caller: self.caller,
mthd: self.mthd,
reason: self.reason,
},
IcrcOperation::Unpause => crate::common::storage::types::IcrcOperation::Unpause{
caller: self.caller,
mthd: self.mthd,
reason: self.reason,
},
IcrcOperation::Deactivate => crate::common::storage::types::IcrcOperation::Deactivate{
caller: self.caller,
mthd: self.mthd,
reason: self.reason,
},
})
}
}
Expand Down Expand Up @@ -417,6 +444,39 @@ pub fn rosetta_core_operations_to_icrc1_operation(
.with_caller(fc_metadata.caller)
.with_mthd(fc_metadata.mthd)
}
OperationType::Pause => {
let metadata = operation
.metadata
.context("metadata should be set for pause operations")?;
let ma_metadata = ManagementActionMetadata::try_from(metadata)?;
icrc1_operation_builder
.with_icrc_operation(IcrcOperation::Pause)
.with_caller(ma_metadata.caller)
.with_mthd(ma_metadata.mthd)
.with_reason(ma_metadata.reason)
}
OperationType::Unpause => {
let metadata = operation
.metadata
.context("metadata should be set for unpause operations")?;
let ma_metadata = ManagementActionMetadata::try_from(metadata)?;
icrc1_operation_builder
.with_icrc_operation(IcrcOperation::Unpause)
.with_caller(ma_metadata.caller)
.with_mthd(ma_metadata.mthd)
.with_reason(ma_metadata.reason)
}
OperationType::Deactivate => {
let metadata = operation
.metadata
.context("metadata should be set for deactivate operations")?;
let ma_metadata = ManagementActionMetadata::try_from(metadata)?;
icrc1_operation_builder
.with_icrc_operation(IcrcOperation::Deactivate)
.with_caller(ma_metadata.caller)
.with_mthd(ma_metadata.mthd)
.with_reason(ma_metadata.reason)
}
};
}
icrc1_operation_builder.build()
Expand Down Expand Up @@ -670,10 +730,68 @@ pub fn icrc1_operation_to_rosetta_core_operations(
),
));
}
crate::common::storage::types::IcrcOperation::Pause { .. }
| crate::common::storage::types::IcrcOperation::Unpause { .. }
| crate::common::storage::types::IcrcOperation::Deactivate { .. } => {
panic!("ICRC-124 Rosetta support not yet implemented")
crate::common::storage::types::IcrcOperation::Pause {
caller,
mthd,
reason,
} => {
operations.push(rosetta_core::objects::Operation::new(
0,
OperationType::Pause.to_string(),
None,
None,
None,
Some(
ManagementActionMetadata {
caller,
mthd,
reason,
}
.try_into()?,
),
));
}
crate::common::storage::types::IcrcOperation::Unpause {
caller,
mthd,
reason,
} => {
operations.push(rosetta_core::objects::Operation::new(
0,
OperationType::Unpause.to_string(),
None,
None,
None,
Some(
ManagementActionMetadata {
caller,
mthd,
reason,
}
.try_into()?,
),
));
}
crate::common::storage::types::IcrcOperation::Deactivate {
caller,
mthd,
reason,
} => {
operations.push(rosetta_core::objects::Operation::new(
0,
OperationType::Deactivate.to_string(),
None,
None,
None,
Some(
ManagementActionMetadata {
caller,
mthd,
reason,
}
.try_into()?,
),
));
}
};

Expand Down
Loading