diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index 7a3ef76..dc52c50 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -4,3 +4,9 @@ members = ["utility_contracts", "price_oracle", "resource-token", "common", "set [workspace.dependencies] soroban-sdk = "23.2.4" + +[profile.release] +opt-level = "z" +lto = true +codegen-units = 1 +panic = "abort" diff --git a/contracts/common/Cargo.toml b/contracts/common/Cargo.toml index 02ab4f5..473060b 100644 --- a/contracts/common/Cargo.toml +++ b/contracts/common/Cargo.toml @@ -10,8 +10,7 @@ doctest = false [dependencies] soroban-sdk = { workspace = true } -wee_alloc = { version = "0.4.5", optional = true } [features] default = [] -wasm = ["wee_alloc"] +wasm = [] diff --git a/contracts/common/src/lib.rs b/contracts/common/src/lib.rs index f23be53..5103cc2 100644 --- a/contracts/common/src/lib.rs +++ b/contracts/common/src/lib.rs @@ -1,9 +1,6 @@ #![no_std] extern crate alloc; -#[cfg(all(target_arch = "wasm32", feature = "wasm"))] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; pub mod errors; pub mod graceful_degradation; diff --git a/contracts/fees/Cargo.toml b/contracts/fees/Cargo.toml index 2875239..6b5e545 100644 --- a/contracts/fees/Cargo.toml +++ b/contracts/fees/Cargo.toml @@ -10,7 +10,6 @@ doctest = false [dependencies] soroban-sdk = { workspace = true } -wee_alloc = "0.4.5" [dev-dependencies] soroban-sdk = { workspace = true, features = ["testutils"] } diff --git a/contracts/fees/src/lib.rs b/contracts/fees/src/lib.rs index 54bbf3e..60ee5b9 100644 --- a/contracts/fees/src/lib.rs +++ b/contracts/fees/src/lib.rs @@ -1,7 +1,6 @@ #![no_std] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + use soroban_sdk::xdr::ToXdr; use soroban_sdk::{ diff --git a/contracts/oracle-aggregator/Cargo.toml b/contracts/oracle-aggregator/Cargo.toml index 227dfd9..df528ab 100644 --- a/contracts/oracle-aggregator/Cargo.toml +++ b/contracts/oracle-aggregator/Cargo.toml @@ -9,7 +9,6 @@ crate-type = ["lib", "cdylib"] doctest = false [dependencies] -wee_alloc = "0.4.5" soroban-sdk = { workspace = true } [dev-dependencies] diff --git a/contracts/oracle-aggregator/src/lib.rs b/contracts/oracle-aggregator/src/lib.rs index 2e3982b..6bf4ab1 100644 --- a/contracts/oracle-aggregator/src/lib.rs +++ b/contracts/oracle-aggregator/src/lib.rs @@ -28,8 +28,7 @@ //! value (or a conservative constant on cold start) and flags it via //! `used_fallback`, so consumers can react rather than trust a silent value. -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + pub mod adapter; mod aggregation; diff --git a/contracts/price_oracle/Cargo.toml b/contracts/price_oracle/Cargo.toml index 41fac43..1fb4360 100644 --- a/contracts/price_oracle/Cargo.toml +++ b/contracts/price_oracle/Cargo.toml @@ -9,7 +9,6 @@ crate-type = ["lib", "cdylib"] doctest = false [dependencies] -wee_alloc = "0.4.5" soroban-sdk = { workspace = true } [dev-dependencies] diff --git a/contracts/price_oracle/src/lib.rs b/contracts/price_oracle/src/lib.rs index d724d3f..75a2d70 100644 --- a/contracts/price_oracle/src/lib.rs +++ b/contracts/price_oracle/src/lib.rs @@ -1,7 +1,6 @@ #![no_std] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + use soroban_sdk::xdr::ToXdr; use soroban_sdk::{ diff --git a/contracts/settlement/Cargo.toml b/contracts/settlement/Cargo.toml index 50d1495..bbcd560 100644 --- a/contracts/settlement/Cargo.toml +++ b/contracts/settlement/Cargo.toml @@ -11,7 +11,6 @@ doctest = false [dependencies] soroban-sdk = { workspace = true } utility-contracts-common = { path = "../common", features = ["wasm"] } -wee_alloc = "0.4.5" [dev-dependencies] soroban-sdk = { workspace = true, features = ["testutils"] } diff --git a/contracts/settlement/src/lib.rs b/contracts/settlement/src/lib.rs index 8921367..1febd74 100644 --- a/contracts/settlement/src/lib.rs +++ b/contracts/settlement/src/lib.rs @@ -1,7 +1,6 @@ #![no_std] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + mod constants; mod conversion; diff --git a/contracts/utility_contracts/Cargo.toml b/contracts/utility_contracts/Cargo.toml index c907657..eb113fc 100644 --- a/contracts/utility_contracts/Cargo.toml +++ b/contracts/utility_contracts/Cargo.toml @@ -9,7 +9,6 @@ crate-type = ["lib", "cdylib"] doctest = false [dependencies] -wee_alloc = "0.4.5" soroban-sdk = { workspace = true } [dev-dependencies] diff --git a/contracts/utility_contracts/src/lib.rs b/contracts/utility_contracts/src/lib.rs index 877971e..245feb2 100644 --- a/contracts/utility_contracts/src/lib.rs +++ b/contracts/utility_contracts/src/lib.rs @@ -1,8 +1,7 @@ #![no_std] extern crate alloc; -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + use soroban_sdk::xdr::ToXdr; use soroban_sdk::{ diff --git a/scripts/deploy.sh b/scripts/deploy.sh deleted file mode 100755 index 3dadabb..0000000 --- a/scripts/deploy.sh +++ /dev/null @@ -1,488 +0,0 @@ -#!/bin/bash - -################################################################################ -# Utility-Protocol Contract Deployment Script -# -# This script pulls the Stellar Docker image and deploys the Utility contract -# to either testnet or mainnet with a single command. -# -# Usage: -# ./deploy.sh --network testnet|--network mainnet [--key ] -# -# Examples: -# ./deploy.sh --network testnet -# ./deploy.sh --network mainnet --key "SCRETKEY..." -# -################################################################################ - -set -e # Exit on error - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -BLUE='\033[0;34m' -NC='\033[0m' # No Color - -# Configuration -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" -CONTRACTS_DIR="$PROJECT_ROOT/contracts/utility_contracts" -DOCKER_IMAGE="stellar/quickstart:latest" -CONTAINER_NAME="stellar-deploy" - -# Default values -NETWORK="" -SECRET_KEY="" -CONTRACT_WASM="" -CONTRACT_ID="" - -################################################################################ -# Helper Functions -################################################################################ - -print_info() { - echo -e "${BLUE}ℹ️ $1${NC}" -} - -print_success() { - echo -e "${GREEN}✅ $1${NC}" -} - -print_warning() { - echo -e "${YELLOW}⚠️ $1${NC}" -} - -print_error() { - echo -e "${RED}❌ $1${NC}" -} - -print_step() { - echo -e "\n${GREEN}▶️ Step $1: $2${NC}\n" -} - -usage() { - cat << EOF -Utility-Protocol Contract Deployment Script - -Usage: - $0 --network [--key ] - -Options: - --network, -n Target network (testnet or mainnet) [REQUIRED] - --key, -k Secret key for deployment account (optional, will prompt if not provided) - --help, -h Show this help message - -Examples: - $0 --network testnet - $0 -n testnet -k "SCRETKEY..." - $0 --network mainnet --key "SCRETKEY..." - -Requirements: - - Docker installed and running - - Bash shell - - Internet connection - -EOF - exit 1 -} - -check_requirements() { - print_step "1" "Checking requirements" - - # Check Docker - if ! command -v docker &> /dev/null; then - print_error "Docker is not installed" - echo "Please install Docker from: https://docs.docker.com/get-docker/" - exit 1 - fi - - print_success "Docker is installed ($(docker --version))" - - # Check if Docker daemon is running - if ! docker info &> /dev/null; then - print_error "Docker daemon is not running" - echo "Please start Docker Desktop or the Docker service" - exit 1 - fi - - print_success "Docker daemon is running" - - # Check if Rust/Cargo is installed (for building contract) - if command -v cargo &> /dev/null; then - print_success "Rust is installed ($(cargo --version))" - else - print_warning "Rust is not installed" - echo "The contract needs to be pre-built or you need to install Rust" - echo "Install Rust from: https://rustup.rs/" - fi - - # Check if jq is installed (for JSON parsing) - if command -v jq &> /dev/null; then - print_success "jq is installed" - else - print_warning "jq is not installed" - echo "Some features may not work without jq" - echo "Install with: brew install jq (macOS) or apt-get install jq (Linux)" - fi -} - -pull_docker_image() { - print_step "2" "Pulling Stellar Docker image" - - print_info "Pulling $DOCKER_IMAGE..." - - if docker pull $DOCKER_IMAGE; then - print_success "Successfully pulled $DOCKER_IMAGE" - else - print_error "Failed to pull Docker image" - exit 1 - fi -} - -build_contract() { - print_step "3" "Building smart contract" - - cd "$CONTRACTS_DIR" - - # Check if wasm file exists - WASM_FILE="$CONTRACTS_DIR/target/wasm32-unknown-unknown/release/utility_contracts.wasm" - - if [ ! -f "$WASM_FILE" ]; then - print_info "Contract not built yet, building now..." - - # Install Soroban target if needed - if ! rustup target list --installed | grep -q "wasm32-unknown-unknown"; then - print_info "Installing wasm32-unknown-unknown target..." - rustup target add wasm32-unknown-unknown - fi - - # Build the contract - print_info "Building contract in release mode..." - cargo build --target wasm32-unknown-unknown --release - - if [ ! -f "$WASM_FILE" ]; then - print_error "Failed to build contract" - exit 1 - fi - - print_success "Contract built successfully" - else - print_success "Contract already built at $WASM_FILE" - fi - - CONTRACT_WASM="$WASM_FILE" - cd "$PROJECT_ROOT" -} - -setup_stellar_container() { - print_step "4" "Setting up Stellar container" - - # Stop existing container if running - if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then - print_info "Stopping existing container..." - docker stop $CONTAINER_NAME || true - docker rm $CONTAINER_NAME || true - fi - - # Determine network configuration - local rpc_url - local horizon_url - local friendbot_url - - if [ "$NETWORK" == "testnet" ]; then - rpc_url="https://soroban-testnet.stellar.org" - horizon_url="https://horizon-testnet.stellar.org" - friendbot_url="https://friendbot.stellar.org" - print_info "Configuring for Stellar Testnet" - elif [ "$NETWORK" == "mainnet" ]; then - rpc_url="https://soroban-rpc.stellar.org" - horizon_url="https://horizon.stellar.org" - friendbot_url="" - print_warning "Configuring for Stellar Mainnet (real money!)" - else - print_error "Invalid network: $NETWORK" - exit 1 - fi - - # Start container - print_info "Starting Stellar container..." - docker run -d \ - --name $CONTAINER_NAME \ - -p 8000:8000 \ - -e NETWORK=$NETWORK \ - -e RPC_URL=$rpc_url \ - -e HORIZON_URL=$horizon_url \ - -e FRIENDBOT_URL=$friendbot_url \ - $DOCKER_IMAGE - - sleep 5 # Wait for container to start - - # Verify container is running - if docker ps | grep -q $CONTAINER_NAME; then - print_success "Stellar container started successfully" - print_info "Container name: $CONTAINER_NAME" - print_info "Network: $NETWORK" - else - print_error "Failed to start Stellar container" - exit 1 - fi -} - -get_or_create_keypair() { - print_step "5" "Setting up deployment account" - - if [ -z "$SECRET_KEY" ]; then - print_info "No secret key provided, generating new keypair..." - - # Generate keypair using container - KEYPAIR_OUTPUT=$(docker exec $CONTAINER_NAME stellar-keys generate 2>/dev/null || echo "") - - if [ -z "$KEYPAIR_OUTPUT" ]; then - # Fallback: use openssl to generate random key - print_warning "stellar-keys not available, generating random key" - SECRET_KEY=$(openssl rand -hex 32) - print_warning "Using randomly generated key (not cryptographically secure for production)" - else - # Parse keypair output - PUBLIC_KEY=$(echo "$KEYPAIR_OUTPUT" | grep "Public Key" | awk '{print $3}') - SECRET_KEY=$(echo "$KEYPAIR_OUTPUT" | grep "Secret Key" | awk '{print $3}') - fi - - print_success "Generated new keypair" - print_info "Public Key: $PUBLIC_KEY" - else - print_info "Using provided secret key" - - # Extract public key from secret key (simplified) - # In production, use proper key derivation - PUBLIC_KEY="G$(echo $SECRET_KEY | cut -c2- | head -c 55)" - fi - - # Fund account if testnet - if [ "$NETWORK" == "testnet" ]; then - print_info "Funding account from Friendbot..." - - FUND_RESPONSE=$(curl -s "$FRIENDBOT_URL?addr=$PUBLIC_KEY" || echo "") - - if echo "$FUND_RESPONSE" | grep -q "hash"; then - TX_HASH=$(echo "$FUND_RESPONSE" | grep -o '"hash":"[^"]*"' | cut -d'"' -f4) - print_success "Account funded successfully" - print_info "Transaction: $TX_HASH" - else - print_warning "Friendbot funding may have failed or account already funded" - fi - else - print_warning "Mainnet deployment - ensure account has sufficient XLM balance" - fi -} - -install_soroban_cli() { - print_step "6" "Installing Soroban CLI" - - # Check if soroban CLI is already installed - if command -v soroban &> /dev/null; then - print_success "Soroban CLI is already installed ($(soroban --version))" - return - fi - - print_info "Installing Soroban CLI via Docker..." - - # Create wrapper script - cat > /tmp/soroban << 'EOF' -#!/bin/bash -docker run --rm -i stellar/quickstart:latest soroban "$@" -EOF - - chmod +x /tmp/soroban - - # Try to move to common binary directory - if sudo mv /tmp/soroban /usr/local/bin/soroban 2>/dev/null; then - print_success "Soroban CLI installed to /usr/local/bin/soroban" - else - print_warning "Could not install to system path" - print_info "You can use: docker run --rm -i stellar/quickstart:latest soroban " - SOROBAN_CMD="docker run --rm -i $DOCKER_IMAGE soroban" - fi -} - -deploy_contract() { - print_step "7" "Deploying smart contract" - - print_info "Preparing to deploy contract to $NETWORK..." - print_info "Contract: $CONTRACT_WASM" - - # Deploy using Docker - print_info "Uploading contract Wasm..." - - # For actual deployment, you would use: - # soroban contract upload --source-account --network $NETWORK --wasm $CONTRACT_WASM - - # Simulated deployment for demo - print_info "Executing deployment transaction..." - - # In production, replace with actual deployment commands - # Example: - # DEPLOY_OUTPUT=$(docker exec $CONTAINER_NAME soroban contract deploy \ - # --source-account "$SECRET_KEY" \ - # --network "$NETWORK" \ - # --wasm "/app/utility_contracts.wasm" \ - # 2>&1) - - # Simulate successful deployment - CONTRACT_ID="CB7PSJZALNWNX7NLOAM6LOEL4OJZMFPQZJMIYO522ZSACYWXTZIDEDSS" - - print_success "Contract deployed successfully!" - print_info "Contract ID: $CONTRACT_ID" - - # Save deployment info - DEPLOY_INFO="$PROJECT_ROOT/deployment-info.json" - cat > "$DEPLOY_INFO" << EOF -{ - "contract_id": "$CONTRACT_ID", - "network": "$NETWORK", - "deployed_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")", - "deployer_account": "$PUBLIC_KEY", - "wasm_hash": "$(sha256sum "$CONTRACT_WASM" | cut -d' ' -f1)", - "container_name": "$CONTAINER_NAME" -} -EOF - - print_success "Deployment info saved to: $DEPLOY_INFO" -} - -verify_deployment() { - print_step "8" "Verifying deployment" - - print_info "Verifying contract on $NETWORK..." - - # Construct explorer URL - local explorer_url - if [ "$NETWORK" == "testnet" ]; then - explorer_url="https://stellar.expert/explorer/testnet/contract/$CONTRACT_ID" - else - explorer_url="https://stellar.expert/explorer/public/contract/$CONTRACT_ID" - fi - - print_success "Deployment verified!" - print_info "View on block explorer: $explorer_url" - - # Quick verification check - print_info "Waiting for contract to be available..." - sleep 10 - - # In production, verify contract is callable - # curl -X POST "$RPC_URL" -H "Content-Type: application/json" \ - # -d "{\"jsonrpc\":\"2.0\",\"method\":\"getContractData\",\"params\":{...},\"id\":1}" -} - -display_summary() { - print_step "9" "Deployment Summary" - - cat << EOF - -╔═══════════════════════════════════════════════════════════╗ -║ ║ -║ 🎉 UTILITY PROTOCOL DEPLOYMENT COMPLETE 🎉 ║ -║ ║ -╠═══════════════════════════════════════════════════════════╣ -║ ║ -║ Network: $NETWORK -║ Contract ID: $CONTRACT_ID -║ Deployer Account: $PUBLIC_KEY -║ Container Name: $CONTAINER_NAME -║ ║ -║ Block Explorer: -║ $(printf "%-55s" "$explorer_url") -║ ║ -║ Next Steps: -║ 1. Register a meter: -║ node meter-simulator/src/index.js register -║ ║ -║ 2. View contract on explorer: -║ Open the URL above in your browser -║ ║ -║ 3. Monitor transactions: -║ docker logs -f $CONTAINER_NAME -║ ║ -╚═══════════════════════════════════════════════════════════╝ - -EOF -} - -cleanup() { - print_info "\nCleaning up..." - - # Optionally stop container - read -p "Do you want to stop the Stellar container? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then - docker stop $CONTAINER_NAME - print_success "Container stopped" - fi -} - -################################################################################ -# Main Script -################################################################################ - -# Parse command line arguments -while [[ $# -gt 0 ]]; do - case $1 in - --network|-n) - NETWORK="$2" - shift 2 - ;; - --key|-k) - SECRET_KEY="$2" - shift 2 - ;; - --help|-h) - usage - ;; - *) - print_error "Unknown option: $1" - usage - ;; - esac -done - -# Validate network parameter -if [ -z "$NETWORK" ]; then - print_error "Network is required" - usage -fi - -if [[ "$NETWORK" != "testnet" && "$NETWORK" != "mainnet" ]]; then - print_error "Network must be 'testnet' or 'mainnet'" - usage -fi - -# Warn if mainnet -if [ "$NETWORK" == "mainnet" ]; then - print_warning "⚠️ DEPLOYING TO MAINNET - REAL MONEY WILL BE USED ⚠️" - read -p "Are you sure you want to continue? (yes/no): " -r - echo - if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then - print_info "Deployment cancelled" - exit 0 - fi -fi - -# Run deployment steps -check_requirements -pull_docker_image -build_contract -setup_stellar_container -get_or_create_keypair -install_soroban_cli -deploy_contract -verify_deployment -display_summary - -# Trap cleanup on exit -trap cleanup EXIT - -print_success "\n🚀 Deployment completed successfully!" - -exit 0