Skip to content

Commit

Permalink
refactor(katana-primitives): make most fields for account/contract al…
Browse files Browse the repository at this point in the history
…locations optional (dojoengine#1512)
  • Loading branch information
kariy authored Feb 6, 2024
1 parent 4fe87ba commit ad01832
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 112 deletions.
22 changes: 11 additions & 11 deletions crates/katana/primitives/src/genesis/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethers::types::U256;
use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};
use serde::{Deserialize, Serialize};
use starknet::core::serde::unsigned_field_element::UfeHex;
use starknet::core::serde::unsigned_field_element::{UfeHex, UfeHexOption};
use starknet::core::utils::get_contract_address;
use starknet::signers::SigningKey;

Expand Down Expand Up @@ -34,15 +34,15 @@ impl GenesisAllocation {
}

/// Get the contract class hash.
pub fn class_hash(&self) -> ClassHash {
pub fn class_hash(&self) -> Option<ClassHash> {
match self {
Self::Contract(contract) => contract.class_hash,
Self::Account(account) => account.class_hash(),
Self::Account(account) => Some(account.class_hash()),
}
}

/// Get the balance to be allocated to this contract.
pub fn balance(&self) -> U256 {
pub fn balance(&self) -> Option<U256> {
match self {
Self::Contract(contract) => contract.balance,
Self::Account(account) => account.balance(),
Expand Down Expand Up @@ -92,7 +92,7 @@ impl GenesisAccountAlloc {
}
}

pub fn balance(&self) -> U256 {
pub fn balance(&self) -> Option<U256> {
match self {
Self::Account(account) => account.balance,
Self::DevAccount(account) => account.balance,
Expand Down Expand Up @@ -126,10 +126,10 @@ impl GenesisAccountAlloc {
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GenesisContractAlloc {
/// The class hash of the contract.
#[serde_as(as = "UfeHex")]
pub class_hash: ClassHash,
#[serde_as(as = "UfeHexOption")]
pub class_hash: Option<ClassHash>,
/// The amount of the fee token allocated to the contract.
pub balance: U256,
pub balance: Option<U256>,
/// The initial nonce of the contract.
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<FieldElement>,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl DevGenesisAccount {
balance: U256,
) -> (ContractAddress, Self) {
let (addr, mut account) = Self::new(private_key, class_hash);
account.balance = balance;
account.balance = Some(balance);
(addr, account)
}
}
Expand All @@ -184,7 +184,7 @@ pub struct GenesisAccount {
#[serde_as(as = "UfeHex")]
pub class_hash: ClassHash,
/// The amount of the fee token allocated to the account.
pub balance: U256,
pub balance: Option<U256>,
/// The initial nonce of the account.
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<FieldElement>,
Expand All @@ -211,7 +211,7 @@ impl GenesisAccount {
balance: U256,
) -> (ContractAddress, Self) {
let (address, account) = Self::new(public_key, class_hash);
(address, Self { balance, ..account })
(address, Self { balance: Some(balance), ..account })
}
}

Expand Down
Loading

0 comments on commit ad01832

Please sign in to comment.