NAC_Blockchain/nac-daemon/src/contract.rs

38 lines
1.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// nac-daemon/src/contract.rs
// NAC 合约模块 - Charter 合约部署和调用
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractDeployRequest {
pub source: String,
pub deployer: String,
pub init_args: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractDeployResult {
pub contract_address: String,
pub tx_hash: String,
pub bytecode_size: usize,
pub gas_used: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractCallRequest {
pub contract_address: String,
pub function_name: String,
pub args: Vec<String>,
pub caller: String,
}
/// 生成合约地址(基于部署者地址和 nonce
pub fn generate_contract_address(deployer: &str, nonce: u64) -> String {
let mut hasher = Keccak256::new();
hasher.update(deployer.as_bytes());
hasher.update(&nonce.to_le_bytes());
let result = hasher.finalize();
format!("NAC_CTR_{}", hex::encode(&result[..14]))
}