Skip to content

Commit

Permalink
Add metadata to match
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dfinity committed Jan 17, 2025
1 parent 1672bb8 commit 869baaa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
56 changes: 34 additions & 22 deletions rs/rosetta-api/icp/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
mod state;

use crate::convert::state::State;
use crate::errors::ApiError;
use crate::models::amount::{from_amount, ledgeramount_from_amount};
use crate::models::operation::OperationType;
use crate::models::{self, AccountIdentifier, BlockIdentifier, Operation};
use crate::request::request_result::RequestResult;
use crate::request::transaction_operation_results::TransactionOperationResults;
use crate::request::transaction_results::TransactionResults;
use crate::request::Request;
use crate::request_types::{
ChangeAutoStakeMaturityMetadata, DisburseMetadata, FollowMetadata, KeyMetadata,
MergeMaturityMetadata, NeuronIdentifierMetadata, NeuronInfoMetadata, PublicKeyOrPrincipal,
RegisterVoteMetadata, RequestResultMetadata, SetDissolveTimestampMetadata, SpawnMetadata,
StakeMaturityMetadata, Status, STATUS_COMPLETED,
use crate::{
convert,
convert::state::State,
errors,
errors::ApiError,
models::{
self,
amount::{from_amount, ledgeramount_from_amount},
operation::OperationType,
AccountIdentifier, BlockIdentifier, Operation,
},
request::{
request_result::RequestResult, transaction_operation_results::TransactionOperationResults,
transaction_results::TransactionResults, Request,
},
request_types::{
ChangeAutoStakeMaturityMetadata, DisburseMetadata, FollowMetadata, KeyMetadata,
ListNeuronsMetadata, MergeMaturityMetadata, NeuronIdentifierMetadata, NeuronInfoMetadata,
PublicKeyOrPrincipal, RegisterVoteMetadata, RequestResultMetadata,
SetDissolveTimestampMetadata, SpawnMetadata, StakeMaturityMetadata, Status,
STATUS_COMPLETED,
},
transaction_id::TransactionIdentifier,
};
use crate::transaction_id::TransactionIdentifier;
use crate::{convert, errors};
use dfn_protobuf::ProtoBuf;
use ic_crypto_tree_hash::Path;
use ic_ledger_canister_blocks_synchronizer::blocks::HashedBlock;
use ic_ledger_core::block::BlockType;
use ic_ledger_hash_of::HashOf;
use ic_types::messages::{HttpCanisterUpdate, HttpReadState};
use ic_types::{CanisterId, PrincipalId};
use ic_types::{
messages::{HttpCanisterUpdate, HttpReadState},
CanisterId, PrincipalId,
};
use icp_ledger::{
Block, BlockIndex, Operation as LedgerOperation, SendArgs, Subaccount, TimeStamp, Tokens,
Transaction,
};
use on_wire::{FromWire, IntoWire};
use rosetta_core::convert::principal_id_from_public_key;
use serde_json::map::Map;
use serde_json::{from_value, Number, Value};
use std::convert::{TryFrom, TryInto};
use serde_json::{from_value, map::Map, Number, Value};
use std::{
convert::{TryFrom, TryInto},
fs::metadata,
};

/// This module converts from ledger_canister data structures to Rosetta data
/// structures
Expand Down Expand Up @@ -271,7 +282,8 @@ pub fn operations_to_requests(
}
OperationType::ListNeurons => {
validate_neuron_management_op()?;
state.list_neurons(account)?;
let ListNeuronsMetadata { page_number } = o.metadata.clone().try_into()?;
state.list_neurons(account, page_number)?;
}
OperationType::Burn | OperationType::Mint => {
let msg = format!("Unsupported operation type: {:?}", o.type_);
Expand Down
4 changes: 3 additions & 1 deletion rs/rosetta-api/icp/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ impl Request {
neuron_index: *neuron_index,
controller: controller.map(PublicKeyOrPrincipal::Principal),
}),
Request::ListNeurons(ListNeurons { .. }) => Ok(RequestType::ListNeurons),
Request::ListNeurons(ListNeurons { page_number, .. }) => Ok(RequestType::ListNeurons {
page_number: page_number.unwrap_or_default(),
}),
Request::Follow(Follow {
neuron_index,
controller,
Expand Down
24 changes: 23 additions & 1 deletion rs/rosetta-api/icp/src/request_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,23 @@ impl TryFrom<NeuronInfoMetadata> for ObjectMap {
}
}

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Deserialize, Serialize)]
pub struct ListNeuronsMetadata {
page_number: Option<u64>,
}

impl TryFrom<Option<ObjectMap>> for ListNeuronsMetadata {
type Error = ApiError;
fn try_from(o: Option<ObjectMap>) -> Result<Self, Self::Error> {
serde_json::from_value(serde_json::Value::Object(o.unwrap_or_default())).map_err(|e| {
ApiError::internal_error(format!(
"Could not parse LIST_NEURONS operation metadata from metadata JSON object: {}",
e
))
})
}
}

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Deserialize, Serialize)]
pub struct FollowMetadata {
pub topic: i32,
Expand Down Expand Up @@ -1406,7 +1423,12 @@ impl TransactionBuilder {
amount: None,
related_operations: None,
coin_change: None,
metadata: None,
metadata: Some(
ListNeuronsMetadata {
page_number: *page_number,
}
.try_into()?,
),
});
Ok(())
}
Expand Down

0 comments on commit 869baaa

Please sign in to comment.