declare

pub fn declare(contract_name: ByteArray, fee_settings: FeeSettings, nonce: Option<felt252>) -> Result<DeclareResult, ScriptCommandError>

Declares a contract and returns DeclareResult.

  • contract_name - name of a contract as Cairo string. It is a name of the contract (part after mod keyword) e.g. "HelloStarknet".
  • fee_settings - fee settings for the transaction, see `FeeSettingsTrait.
  • nonce - nonce for declare transaction. If not provided, nonce will be set automatically.
use sncast_std::{declare, FeeSettingsTrait};

fn main() {
    let fee_settings = FeeSettingsTrait::max_fee(9999999);

    let result = declare("HelloStarknet", fee_settings, Option::None).expect('declare failed');

    println!("declare result: {}", result);
    println!("debug declare result: {:?}", result);
}

Returned Type

  • If the contract has not been declared, DeclareResult::Success is returned containing respective transaction hash.
  • If the contract has already been declared, DeclareResult::AlreadyDeclared is returned.

Getting the Class Hash

Both variants contain class_hash of the declared contract. Import DeclareResultTrait to access it.

pub trait DeclareResultTrait {
    fn class_hash(self: @DeclareResult) -> @ClassHash;
}

Structures Used by the Command

#[derive(Drop, Copy, Debug, Serde)]
pub enum DeclareResult {
    Success: DeclareTransactionResult,
    AlreadyDeclared: AlreadyDeclaredResult,
}

#[derive(Drop, Copy, Debug, Serde)]
pub struct DeclareTransactionResult {
    pub class_hash: ClassHash,
    pub transaction_hash: felt252,
}

#[derive(Drop, Copy, Debug, Serde)]
pub struct AlreadyDeclaredResult {
    pub class_hash: ClassHash,
}