Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
762 changes: 614 additions & 148 deletions Cargo.lock

Large diffs are not rendered by default.

43 changes: 40 additions & 3 deletions src/admin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use soroban_sdk::{panic_with_error, symbol_short, Address, BytesN, Env};

use crate::mint::TTL_TEMP;
use crate::{ContractError, DataKey};
use crate::{ContractError, DataKey, TransferFeeConfig};

/// Reads the stored admin or panics with `NotInitialized`.
pub(crate) fn read_admin(e: &Env) -> Address {
Expand All @@ -11,10 +11,24 @@ pub(crate) fn read_admin(e: &Env) -> Address {
.unwrap_or_else(|| panic_with_error!(e, ContractError::NotInitialized))
}

/// Initializes the contract with the controlling admin address and the
/// Ed25519 public key used to verify mint signatures.
///
/// # Panics
/// - [`ContractError::AlreadyInitialized`] if the contract was already initialized.
/// - [`ContractError::InvalidAdminPubKey`] if `admin_pubkey` is the all-zero
/// key. An all-zero Ed25519 public key has no known corresponding private
/// key, so accepting it would silently brick every future `mint_wrap` call
/// (no valid signature could ever be produced) while leaving the contract in
/// an "initialized" state. Rejecting it at initialization time prevents this
/// misconfiguration rather than discovering it after deployment.
pub(crate) fn initialize(e: Env, admin: Address, admin_pubkey: BytesN<32>) {
if e.storage().instance().has(&DataKey::Admin) {
panic_with_error!(e, ContractError::AlreadyInitialized);
}
if admin_pubkey == BytesN::from_array(&e, &[0u8; 32]) {
panic_with_error!(e, ContractError::InvalidAdminPubKey);
}
e.storage().instance().set(&DataKey::Admin, &admin);
e.storage()
.instance()
Expand Down Expand Up @@ -49,6 +63,27 @@ pub(crate) fn set_pause(e: Env, paused: bool) {
e.events().publish((symbol_short!("pause"),), paused);
}

/// Admin-only: configure the token-denominated fee charged by `transfer_wrap`.
///
/// An amount of zero enables fee-free transfers without removing the configured
/// token and recipient.
pub(crate) fn set_transfer_fee(e: Env, token: Address, recipient: Address, amount: i128) {
read_admin(&e).require_auth();
if amount < 0 {
panic_with_error!(e, ContractError::InvalidFeeParams);
}
e.storage().instance().set(
&DataKey::TransferFee,
&TransferFeeConfig {
amount,
recipient: recipient.clone(),
token: token.clone(),
},
);
e.events()
.publish((symbol_short!("fee"),), (token, recipient, amount));
}

pub(crate) fn is_paused(e: &Env) -> bool {
e.storage()
.instance()
Expand Down Expand Up @@ -111,8 +146,10 @@ pub(crate) fn upgrade(e: Env, new_wasm_hash: BytesN<32>) {
.set(&DataKey::ContractVersion, &next_version);

// Emit audit event with the requested WASM hash and new version
e.events()
.publish((symbol_short!("upgrade"), next_version), new_wasm_hash.clone());
e.events().publish(
(symbol_short!("upgrade"), next_version),
new_wasm_hash.clone(),
);

// Update the contract WASM with the provided hash
e.deployer().update_current_contract_wasm(new_wasm_hash);
Expand Down
2 changes: 1 addition & 1 deletion src/balance_of_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(test)]

use super::{StellarWrapContract, StellarWrapContractClient};
use soroban_sdk::{Env, Address, testutils::Address as _};
use soroban_sdk::{testutils::Address as _, Address, Env};

#[test]
fn test_balance_of_starts_at_zero() {
Expand Down
34 changes: 17 additions & 17 deletions src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ fn validate_period(e: &Env, period: u64) {
pub(crate) fn set_bridge_relayer(e: &Env, relayer: Address) {
let admin = crate::admin::read_admin(e);
admin.require_auth();
e.storage().instance().set(&DataKey::BridgeRelayer, &relayer);
e.storage().instance().extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);
e.storage()
.instance()
.set(&DataKey::BridgeRelayer, &relayer);
e.storage()
.instance()
.extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);
}

/// Returns the configured bridge relayer address, or None if not set.
Expand All @@ -38,7 +42,9 @@ pub(crate) fn set_chain_status(e: &Env, chain_id: u32, enabled: bool) {
}
let key = DataKey::BridgeChainStatus(chain_id);
e.storage().instance().set(&key, &enabled);
e.storage().instance().extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);
e.storage()
.instance()
.extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);
}

/// Returns whether a target/source chain ID is supported/enabled.
Expand Down Expand Up @@ -91,7 +97,9 @@ pub(crate) fn bridge_wrap_out(
let current_nonce: u64 = e.storage().instance().get(&nonce_key).unwrap_or(0);
let next_nonce = current_nonce + 1;
e.storage().instance().set(&nonce_key, &next_nonce);
e.storage().instance().extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);
e.storage()
.instance()
.extend_ttl(TTL_ONE_YEAR, TTL_ONE_YEAR);

let outbound_req = OutboundBridgeRequest {
nonce: next_nonce,
Expand Down Expand Up @@ -169,17 +177,16 @@ pub(crate) fn bridge_wrap_in(
archetype: archetype.clone(),
period,
fsm: WrapLifecycleFSM::new(WrapState::Active, now),
description: None,
image_url: None,
};

e.storage().persistent().set(&wrap_key, &record);
e.storage()
.persistent()
.extend_ttl(&wrap_key, TTL_ONE_YEAR, TTL_ONE_YEAR);

storage_accounting::add_storage_bytes(
&e,
storage_accounting::estimate_wrap_bytes_new(),
);
storage_accounting::add_storage_bytes(&e, storage_accounting::estimate_wrap_bytes_new());

let count_key = DataKey::WrapCount(recipient.clone());
let current_count: u32 = e.storage().persistent().get(&count_key).unwrap_or(0);
Expand Down Expand Up @@ -268,10 +275,7 @@ pub(crate) fn bridge_wrap_in(
);
}

pub(crate) fn get_outbound_bridge_request(
e: &Env,
nonce: u64,
) -> Option<OutboundBridgeRequest> {
pub(crate) fn get_outbound_bridge_request(e: &Env, nonce: u64) -> Option<OutboundBridgeRequest> {
let key = DataKey::OutboundBridgeRequest(nonce);
e.storage().persistent().get(&key)
}
Expand All @@ -285,11 +289,7 @@ pub(crate) fn get_inbound_bridge_record(
e.storage().persistent().get(&key)
}

pub(crate) fn is_inbound_nonce_processed(
e: &Env,
source_chain: u32,
source_nonce: u64,
) -> bool {
pub(crate) fn is_inbound_nonce_processed(e: &Env, source_chain: u32, source_nonce: u64) -> bool {
let key = DataKey::InboundBridgeProcessed(source_chain, source_nonce);
e.storage().persistent().get(&key).unwrap_or(false)
}
Expand Down
39 changes: 16 additions & 23 deletions src/bridge_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ extern crate std;
use super::*;
use crate::signature::construct_mint_payload;
use ed25519_dalek::{Signer, SigningKey};
use soroban_sdk::{
symbol_short,
testutils::Address as _,
Address, Bytes, BytesN, Env, Symbol,
};
use soroban_sdk::{symbol_short, testutils::Address as _, Address, Bytes, BytesN, Env, Symbol};
use std::panic::{catch_unwind, AssertUnwindSafe};

fn setup_test_env<'a>(env: &'a Env) -> (StellarWrapContractClient<'a>, Address, Address, SigningKey) {
fn setup_test_env<'a>(
env: &'a Env,
) -> (StellarWrapContractClient<'a>, Address, Address, SigningKey) {
let contract_id = env.register_contract(None, StellarWrapContract);
let client = StellarWrapContractClient::new(env, &contract_id);

Expand Down Expand Up @@ -66,15 +64,15 @@ fn test_set_and_check_chain_status() {
let chain_eth = 1u32;
let chain_sol = 900u32;

assert!(!client.is_chain_supported(chain_eth));
assert!(!client.is_chain_supported(chain_sol));
assert!(!client.is_chain_supported(&chain_eth));
assert!(!client.is_chain_supported(&chain_sol));

client.set_chain_status(&chain_eth, &true);
assert!(client.is_chain_supported(chain_eth));
assert!(!client.is_chain_supported(chain_sol));
assert!(client.is_chain_supported(&chain_eth));
assert!(!client.is_chain_supported(&chain_sol));

client.set_chain_status(&chain_eth, &false);
assert!(!client.is_chain_supported(chain_eth));
assert!(!client.is_chain_supported(&chain_eth));
}

#[test]
Expand All @@ -83,7 +81,7 @@ fn test_invalid_chain_zero() {
env.mock_all_auths();

let (client, _admin, _relayer, _key) = setup_test_env(&env);
assert!(!client.is_chain_supported(0));
assert!(!client.is_chain_supported(&0));
}

#[test]
Expand Down Expand Up @@ -122,7 +120,9 @@ fn test_bridge_wrap_out_success() {
assert_eq!(nonce, 1);
assert_eq!(client.get_outbound_nonce(), 1);

let request = client.get_outbound_bridge_request(&nonce).expect("request exists");
let request = client
.get_outbound_bridge_request(&nonce)
.expect("request exists");
assert_eq!(request.nonce, 1);
assert_eq!(request.sender, user);
assert_eq!(request.destination_chain, dest_chain);
Expand Down Expand Up @@ -186,7 +186,7 @@ fn test_bridge_wrap_in_success() {
let data_hash = BytesN::from_array(&env, &[99u8; 32]);
let source_nonce = 101u64;

assert!(!client.is_inbound_nonce_processed(source_chain, source_nonce));
assert!(!client.is_inbound_nonce_processed(&source_chain, &source_nonce));
assert_eq!(client.balance_of(&recipient), 0);

client.bridge_wrap_in(
Expand All @@ -198,7 +198,7 @@ fn test_bridge_wrap_in_success() {
&data_hash,
);

assert!(client.is_inbound_nonce_processed(source_chain, source_nonce));
assert!(client.is_inbound_nonce_processed(&source_chain, &source_nonce));
assert_eq!(client.balance_of(&recipient), 1);

let record = client
Expand Down Expand Up @@ -293,14 +293,7 @@ fn test_bridge_paused_blocks_operations() {
assert!(out_result.is_err());

let in_result = catch_unwind(AssertUnwindSafe(|| {
client.bridge_wrap_in(
&chain_id,
&500u64,
&user,
&period,
&archetype,
&data_hash,
);
client.bridge_wrap_in(&chain_id, &500u64, &user, &period, &archetype, &data_hash);
}));
assert!(in_result.is_err());
}
5 changes: 3 additions & 2 deletions src/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) fn burn_wrap(e: Env, user: Address, period: u64) {

// Find and remove the period from the list
let mut found_index: Option<u32> = None;
for (i, &p) in periods.iter().enumerate() {
for (i, p) in periods.iter().enumerate() {
if p == period {
found_index = Some(i as u32);
break;
Expand All @@ -83,5 +83,6 @@ pub(crate) fn burn_wrap(e: Env, user: Address, period: u64) {
}

// 7. Emit burn event AFTER state mutation
e.events().publish((symbol_short!("burn"), user.clone(), period), user);
e.events()
.publish((symbol_short!("burn"), user.clone(), period), user);
}
8 changes: 7 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ pub enum ContractError {
// Expiration errors
WrapNotExpired = 46,
InvalidExpirationDuration = 47,
// Transfer errors
TransferFeeNotConfigured = 48,
InvalidTransfer = 49,
TransferInProgress = 50,
StorageInvariantViolation = 51,
/// The admin signing key provided to `initialize` is invalid (e.g. all-zero).
InvalidAdminPubKey = 52,
}

2 changes: 1 addition & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum MintEventType {

impl MintEventType {
/// Convert this event type to a Soroban `Symbol`.
pub fn to_symbol(&self, e: &Env) -> Symbol {
pub fn to_symbol(self, e: &Env) -> Symbol {
match self {
MintEventType::Mint => Symbol::new(e, "mint"),
MintEventType::Transition => Symbol::new(e, "trans"),
Expand Down
Loading