Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Ensure types are compatible with TS interfaces #323

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
32 changes: 16 additions & 16 deletions crates/fuel-streams-core/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ mod tests {

let expected_json = json!({
"consensus": {
"kind": "Genesis",
"chain_config_hash": "0000000000000000000000000000000000000000000000000000000000000000",
"coins_root": "0000000000000000000000000000000000000000000000000000000000000000",
"contracts_root": "0000000000000000000000000000000000000000000000000000000000000000",
"messages_root": "0000000000000000000000000000000000000000000000000000000000000000",
"transactions_root": "0000000000000000000000000000000000000000000000000000000000000000"
"chainConfigHash": "0000000000000000000000000000000000000000000000000000000000000000",
"coinsRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"contractsRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"messagesRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"transactionsRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"type": "Genesis"
},
"header": {
"application_hash": "0000000000000000000000000000000000000000000000000000000000000000",
"consensus_parameters_version": 1,
"da_height": 1000,
"event_inbox_root": "0101010101010101010101010101010101010101010101010101010101010101",
"applicationHash": "0000000000000000000000000000000000000000000000000000000000000000",
"consensusParametersVersion": 1,
"daHeight": 1000,
"eventInboxRoot": "0101010101010101010101010101010101010101010101010101010101010101",
"height": 42,
"id": "0000000000000000000000000000000000000000000000000000000000000000",
"message_outbox_root": "0303030303030303030303030303030303030303030303030303030303030303",
"message_receipt_count": 10,
"prev_root": "0404040404040404040404040404040404040404040404040404040404040404",
"state_transition_bytecode_version": 2,
"messageOutboxRoot": "0303030303030303030303030303030303030303030303030303030303030303",
"messageReceiptCount": 10,
"prevRoot": "0404040404040404040404040404040404040404040404040404040404040404",
"stateTransitionBytecodeVersion": 2,
"time": [0, 0, 0, 0, 101, 44, 62, 128],
"transactions_count": 5,
"transactions_root": "0505050505050505050505050505050505050505050505050505050505050505",
"transactionsCount": 5,
"transactionsRoot": "0505050505050505050505050505050505050505050505050505050505050505",
"version": "V1"
},
"height": 42,
Expand Down
34 changes: 30 additions & 4 deletions crates/fuel-streams-core/src/blocks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::types::*;

// Block type
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
pub consensus: Consensus,
pub header: BlockHeader,
Expand Down Expand Up @@ -37,7 +38,7 @@ impl Block {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct BlockHeight(String);

impl From<FuelCoreBlockHeight> for BlockHeight {
Expand All @@ -61,10 +62,32 @@ impl std::fmt::Display for BlockHeight {

// Consensus enum
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
#[serde(tag = "type")]
pub enum Consensus {
Genesis(Genesis),
PoAConsensus(PoAConsensus),
PoAConsensus(FuelCorePoAConsensus),
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Genesis {
pub chain_config_hash: Bytes32,
pub coins_root: Bytes32,
pub contracts_root: Bytes32,
pub messages_root: Bytes32,
pub transactions_root: Bytes32,
}

impl From<FuelCoreGenesis> for Genesis {
fn from(genesis: FuelCoreGenesis) -> Self {
Self {
chain_config_hash: genesis.chain_config_hash.into(),
coins_root: genesis.coins_root.into(),
contracts_root: genesis.contracts_root.into(),
messages_root: genesis.messages_root.into(),
transactions_root: genesis.transactions_root.into(),
}
}
}

impl Default for Consensus {
Expand All @@ -76,7 +99,9 @@ impl Default for Consensus {
impl From<FuelCoreConsensus> for Consensus {
fn from(consensus: FuelCoreConsensus) -> Self {
match consensus {
FuelCoreConsensus::Genesis(genesis) => Consensus::Genesis(genesis),
FuelCoreConsensus::Genesis(genesis) => {
Consensus::Genesis(genesis.into())
}
FuelCoreConsensus::PoA(poa) => Consensus::PoAConsensus(poa),
_ => panic!("Unknown consensus type: {:?}", consensus),
}
Expand All @@ -92,6 +117,7 @@ pub enum BlockVersion {

// Header type
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockHeader {
pub application_hash: Bytes32,
pub consensus_parameters_version: u32,
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-streams-core/src/fuel_core_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub use fuel_core_types::{
blockchain::{
block::Block as FuelCoreBlock,
consensus::{
poa::PoAConsensus,
poa::PoAConsensus as FuelCorePoAConsensus,
Consensus as FuelCoreConsensus,
Genesis,
Genesis as FuelCoreGenesis,
},
header::BlockHeader as FuelCoreBlockHeader,
primitives::BlockId as FuelCoreBlockId,
Expand Down
5 changes: 4 additions & 1 deletion crates/fuel-streams-core/src/inputs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::types::*;

// Input enum
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
#[serde(tag = "type")]
pub enum Input {
Contract(InputContract),
Coin(InputCoin),
Expand Down Expand Up @@ -105,6 +105,7 @@ impl Default for Input {

// InputCoin type
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputCoin {
pub amount: u64,
pub asset_id: AssetId,
Expand All @@ -119,6 +120,7 @@ pub struct InputCoin {

// InputContract type
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputContract {
pub balance_root: Bytes32,
pub contract_id: Bytes32,
Expand All @@ -141,6 +143,7 @@ impl From<&FuelCoreInputContract> for InputContract {

// InputMessage type
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputMessage {
pub amount: u64,
pub data: HexString,
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-streams-core/src/logs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::types::*;
/// A convenient aggregate type to represent a Fuel logs to allow users
/// think about them agnostic of receipts.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
#[serde(tag = "type")]
pub enum Log {
WithoutData {
id: ContractId,
Expand Down
5 changes: 5 additions & 0 deletions crates/fuel-streams-core/src/outputs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,31 @@ impl From<&FuelCoreOutput> for Output {
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoinOutput {
pub amount: u64,
pub asset_id: AssetId,
pub to: Address,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChangeOutput {
pub amount: u64,
pub asset_id: AssetId,
pub to: Address,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VariableOutput {
pub amount: u64,
pub asset_id: AssetId,
pub to: Address,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OutputContract {
pub balance_root: Bytes32,
pub input_index: u16,
Expand All @@ -94,6 +98,7 @@ impl From<&FuelCoreOutputContract> for OutputContract {
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContractCreated {
contract_id: ContractId,
state_root: Bytes32,
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-streams-core/src/receipts/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use crate::types::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Receipt {
pub amount: Option<u64>,
pub asset_id: Option<AssetId>,
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-streams-core/src/transactions/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use fuel_core_types::fuel_tx;
use crate::types::*;

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
pub id: Bytes32,
pub kind: TransactionKind,
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-streams-core/src/utxos/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Utxo {
pub utxo_id: FuelCoreUtxoId,
pub sender: Option<Address>,
Expand Down
Loading