#smart-contracts #platform #ethereum #blockchain #chain #deployment #testing

dumb_contract

A Rust crate for writing, testing, and deploying smart contracts on various blockchain platforms

1 unstable release

0.1.0 Aug 31, 2024

#160 in #chain

MIT license

38KB
208 lines

Dumb Contract Crate

A versatile Rust crate for writing, testing, and deploying smart contracts across various blockchain platforms, including Ethereum, Solana, and Binance Smart Chain (BSC). This crate is designed with a focus on safety, efficiency, and ease of use, making it an excellent choice for both beginners and experienced developers.

Features

  • Multi-Blockchain Support: Easily interact with Ethereum, Solana, and Binance Smart Chain (BSC).
  • Unified API: Consistent API for deploying, calling, and querying smart contracts across different blockchains.
  • Asynchronous Operations: Full async support for non-blocking contract interactions.
  • Custom Error Handling: Detailed error types for precise error tracking and debugging.
  • Testing Utilities: Built-in tools for unit testing your smart contracts locally.

Installation

To include this crate in your Rust project, add the following line to your Cargo.toml file:

[dependencies]
smart_contract_crate = "0.1.0"

Getting Started

Example: Deploying and Interacting with a Smart Contract on Binance Smart Chain

This example demonstrates how to deploy a smart contract on Binance Smart Chain and interact with it using the smart_contract_crate.

use smart_contract_crate::platforms::binance_smart_chain::BinanceSmartChainContract;
use smart_contract_crate::contract::SmartContract;
use tokio;

#[tokio::main]
async fn main() {
    // Initialize a new Binance Smart Chain contract instance
    let contract = BinanceSmartChainContract::new(
        "0x1234567890abcdef1234567890abcdef12345678", // Replace with your contract address
        "https://bsc-dataseed.binance.org/",          // Binance Smart Chain node URL
    )
    .await
    .expect("Failed to create contract");

    // Deploy the contract
    contract.deploy().expect("Failed to deploy contract");

    // Call a function on the smart contract
    let result = contract.call("some_function", &[]).expect("Failed to call function");
    println!("Function call result: {}", result);

    // Query the smart contract
    let query_result = contract.query("some_query").expect("Failed to query contract");
    println!("Query result: {}", query_result);
}

Platform-Specific Implementations

Ethereum

The Ethereum implementation allows you to deploy, call, and query smart contracts on the Ethereum blockchain.

use smart_contract_crate::platforms::ethereum::EthereumContract;
use smart_contract_crate::contract::SmartContract;
use tokio;

#[tokio::main]
async fn main() {
    let contract = EthereumContract::new(
        "0x1234567890abcdef1234567890abcdef12345678", // Ethereum contract address
        "https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID", // Infura node URL
    )
    .await
    .expect("Failed to create contract");

    contract.deploy().expect("Failed to deploy contract");
    let result = contract.call("some_function", &[]).expect("Failed to call function");
    println!("Function call result: {}", result);

    let query_result = contract.query("some_query").expect("Failed to query contract");
    println!("Query result: {}", query_result);
}

Solana

The Solana implementation allows you to interact with programs on the Solana blockchain.

use smart_contract_crate::platforms::solana::SolanaContract;
use solana_client::rpc_client::RpcClient;
use solana_sdk::signer::keypair::Keypair;
use smart_contract_crate::contract::SmartContract;

fn main() {
    let client = RpcClient::new("https://devnet.solana.com"); // Solana RPC URL
    let payer = Keypair::new();
    let contract = SolanaContract::new("program_id", client, payer);

    contract.deploy().expect("Failed to deploy contract");

    let result = contract.call("some_function", &[]).expect("Failed to call function");
    println!("Function call result: {}", result);

    let query_result = contract.query("some_query").expect("Failed to query contract");
    println!("Query result: {}", query_result);
}

Error Handling

The smart_contract_crate provides a robust error handling mechanism to capture and manage various types of errors that can occur during smart contract operations.

Example: Handling Errors Gracefully

use smart_contract_crate::error::ContractError;

fn handle_error() -> Result<(), ContractError> {
    // Example error handling
    let contract_result = some_contract_function();

    match contract_result {
        Ok(_) => println!("Contract function executed successfully."),
        Err(ContractError::NetworkError(e)) => eprintln!("Network error occurred: {}", e),
        Err(ContractError::InvalidAddress(e)) => eprintln!("Invalid contract address: {}", e),
        Err(e) => eprintln!("An unexpected error occurred: {}", e),
    }

    Ok(())
}

Testing Your Smart Contracts

Testing is an essential part of the smart contract development process. This crate provides utilities to help you write and run tests for your smart contracts.

Example: Unit Tests

#[cfg(test)]
mod tests {
    use super::*;
    use smart_contract_crate::platforms::ethereum::EthereumContract;
    use smart_contract_crate::platforms::solana::SolanaContract;
    use smart_contract_crate::platforms::binance_smart_chain::BinanceSmartChainContract;

    #[test]
    fn test_ethereum_deploy() {
        let contract = EthereumContract {
            // Initialize with test data
        };
        assert!(contract.deploy().is_ok());
    }

    #[test]
    fn test_solana_deploy() {
        let contract = SolanaContract {
            // Initialize with test data
        };
        assert!(contract.deploy().is_ok());
    }

    #[test]
    fn test_bsc_deploy() {
        let contract = BinanceSmartChainContract {
            // Initialize with test data
        };
        assert!(contract.deploy().is_ok());
    }
}

Supported Platforms

  • Ethereum: Deploy and interact with smart contracts on the Ethereum blockchain.
  • Solana: Deploy and interact with programs on the Solana blockchain.
  • Binance Smart Chain (BSC): Deploy and interact with Ethereum-compatible smart contracts on BSC.

Contribution

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Further Documentation

For more detailed information, including API documentation and additional examples, please refer to the full documentation.

Dependencies

~73MB
~1.5M SLoC