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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
19 changes: 5 additions & 14 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ soroban-sdk = "21.0.0"
[dev-dependencies]
soroban-sdk = { version = "21.0.0", features = ["testutils"] }
proptest = "1.4.0"
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "remittance_bench"
harness = false

[features]
prop = []

[profile.release]
opt-level = "z"
overflow-checks = true
debug = 0
strip = "symbols"
debug-assertions = false
panic = "abort"
codegen-units = 1
lto = true

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
152 changes: 152 additions & 0 deletions contracts/benches/remittance_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, BatchSize};
use soroban_sdk::{testutils::{Address as _, Ledger}, Address, Env, String, Symbol, Vec, symbol_short};
use gpay_remit_contracts::remittance_hub::{RemittanceHubContract, RemittanceHubContractClient, Asset, EscrowRequest};

fn setup_env_with_token() -> (Env, RemittanceHubContractClient<'static>, Address, Address, Address) {
let env = Env::default();
env.mock_all_auths();

// Register the contract
let contract_id = env.register_contract(None, RemittanceHubContract);
let client = RemittanceHubContractClient::new(&env, &contract_id);

// Reset budget to unlimited for benchmarking
env.budget().reset_unlimited();

let admin = Address::generate(&env);
let oracle = Address::generate(&env);

// Initialize the hub
client.init_hub(&admin, &oracle, &oracle, &3600);

// Register a token
let token_admin = Address::generate(&env);
let token_id = env.register_stellar_asset_contract(token_admin.clone());

(env, client, admin, oracle, token_id)
}

fn bench_send_remittance(c: &mut Criterion) {
c.bench_function("send_remittance", |b| {
b.iter_batched(|| {
let (env, client, _admin, _oracle, _token) = setup_env_with_token();
let from = Address::generate(&env);
let to = Address::generate(&env);
(from, to, client)
}, |(from, to, client)| {
client.send_remittance(&from, &to, black_box(&100), black_box(&symbol_short!("USD")));
}, BatchSize::SmallInput)
});
}

fn bench_batch_create_escrows(c: &mut Criterion) {
for size in [1, 5, 10].iter() {
c.bench_function(&format!("batch_create_escrows_size_{}", size), |b| {
b.iter_batched(|| {
let (env, client, _admin, _oracle, _token) = setup_env_with_token();
let sender = Address::generate(&env);
let recipient = Address::generate(&env);
let issuer = Address::generate(&env);

let asset = Asset {
code: String::from_str(&env, "USDC"),
issuer: issuer.clone(),
};

let mut requests = Vec::new(&env);
for _ in 0..*size {
requests.push_back(EscrowRequest {
recipient: recipient.clone(),
amount: 100,
asset: asset.clone(),
expiration_timestamp: 10000,
});
}
env.ledger().with_mut(|li| li.timestamp = 5000);
(sender, requests, client)
}, |(sender, requests, client)| {
client.batch_create_escrows(&sender, black_box(&requests));
}, BatchSize::SmallInput)
});
}
}

fn bench_batch_deposit(c: &mut Criterion) {
for size in [1, 5, 10].iter() {
c.bench_function(&format!("batch_deposit_size_{}", size), |b| {
b.iter_batched(|| {
let (env, client, _admin, _oracle, token_id) = setup_env_with_token();
let sender = Address::generate(&env);
let recipient = Address::generate(&env);
let issuer = Address::generate(&env);

let asset = Asset {
code: String::from_str(&env, "USDC"),
issuer: issuer.clone(),
};

let mut requests = Vec::new(&env);
for _ in 0..*size {
requests.push_back(EscrowRequest {
recipient: recipient.clone(),
amount: 100,
asset: asset.clone(),
expiration_timestamp: 10000,
});
}
env.ledger().with_mut(|li| li.timestamp = 5000);

// Mint tokens to sender
let token_client = soroban_sdk::token::StellarAssetClient::new(&env, &token_id);
token_client.mint(&sender, &1000000);

let ids = client.batch_create_escrows(&sender, &requests);
(sender, ids, token_id, client)
}, |(sender, escrow_ids, token_id, client)| {
client.batch_deposit(&sender, black_box(&escrow_ids), black_box(&token_id));
}, BatchSize::SmallInput)
});
}
}

fn bench_batch_release(c: &mut Criterion) {
for size in [1, 5, 10].iter() {
c.bench_function(&format!("batch_release_size_{}", size), |b| {
b.iter_batched(|| {
let (env, client, _admin, _oracle, token_id) = setup_env_with_token();
let sender = Address::generate(&env);
let recipient = Address::generate(&env);
let issuer = Address::generate(&env);

let asset = Asset {
code: String::from_str(&env, "USDC"),
issuer: issuer.clone(),
};

let mut requests = Vec::new(&env);
for _ in 0..*size {
requests.push_back(EscrowRequest {
recipient: recipient.clone(),
amount: 100,
asset: asset.clone(),
expiration_timestamp: 10000,
});
}
env.ledger().with_mut(|li| li.timestamp = 5000);

// Mint tokens to sender
let token_client = soroban_sdk::token::StellarAssetClient::new(&env, &token_id);
token_client.mint(&sender, &1000000);

let ids = client.batch_create_escrows(&sender, &requests);
client.batch_deposit(&sender, &ids, &token_id);
(recipient, ids, token_id, client)
}, |(recipient, escrow_ids, token_id, client)| {
client.batch_release(&recipient, black_box(&escrow_ids), black_box(&token_id));
}, BatchSize::SmallInput)
});
}
}

criterion_group!(benches, bench_send_remittance, bench_batch_create_escrows, bench_batch_deposit, bench_batch_release);
criterion_main!(benches);
8 changes: 4 additions & 4 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![no_std]

mod aml;
mod kyc;
mod oracle;
pub mod aml;
pub mod kyc;
pub mod oracle;
pub mod payment_escrow;
pub mod rate_limit;
mod remittance_hub;
pub mod remittance_hub;
pub mod upgradeable;

pub use aml::MockAmlOracleContract;
Expand Down
9 changes: 5 additions & 4 deletions contracts/src/remittance_hub.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::aml::{self, AmlConfig, AmlScreeningResult, AmlStatus};

use crate::oracle::{self as oracle_mod, CachedRate, OracleConfig};
use crate::rate_limit::{self as rate_limit_mod, FunctionType};
use crate::rate_limit::{self, FunctionType};
use crate::upgradeable;
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, symbol_short, Address, BytesN, Env, String, Symbol,
};


#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
Expand Down Expand Up @@ -1029,7 +1027,7 @@ impl RemittanceHubContract {
Some(a) => a,
None => return Ok(()), // No admin set yet, skip rate limiting
};
let allowed = rate_limit_mod::check_rate_limit(env, caller, function_type, &admin);
let allowed = rate_limit::check_rate_limit(env, caller, function_type, &admin);
if allowed {
Ok(())
} else {
Expand Down Expand Up @@ -1095,6 +1093,8 @@ impl RemittanceHubContract {
upgradeable::migrate(&env, &admin)
}



// ── Analytics ──────────────────────────────────────────────────

pub fn get_metric(env: Env, metric_type: MetricType, timestamp: u64, is_weekly: bool) -> i128 {
Expand Down Expand Up @@ -1126,6 +1126,7 @@ impl RemittanceHubContract {
.persistent()
.get(&DataKey::Admin)
.ok_or(RemittanceError::Unauthorized)?;

if caller != stored_admin {
return Err(RemittanceError::Unauthorized);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 794876926236461
"lo": 314970889163398
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.10.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 94061507327297
"lo": 7167175642279
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.100.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 927336209107696
"lo": 111852279558089
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.101.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 715157565648685
"lo": 100369389564203
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.102.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 161440231597829
"lo": 491056683234059
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.103.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 797715941244230
"lo": 560400642780196
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.104.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 778232436353525
"lo": 877493170744173
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.105.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 65984093604124
"lo": 338327635228447
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.106.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 646146842544408
"lo": 656245671034412
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.107.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 658207015249401
"lo": 162943068328952
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.108.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 66075818541745
"lo": 177986654722703
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.109.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 66196674812287
"lo": 480974821921668
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.11.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 292604871925748
"lo": 900194438157482
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.110.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 678122791933968
"lo": 158246690306612
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.111.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 112701100182948
"lo": 769630099644590
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.112.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 219732332700518
"lo": 476738456193033
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.113.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 592093981213
"lo": 190518558996875
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/test_snapshots/fuzz_calculate_fees.114.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"data": {
"i128": {
"hi": 0,
"lo": 191041894640951
"lo": 425060057078700
}
}
}
Expand Down
Loading
Loading