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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/dispute_resolution_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
soroban-sdk = { workspace = true }
delivery_contract = { path = "../delivery_contract" }
escrow_contract = { path = "../escrow_contract" }
identity_reputation_contract = { path = "../identity_reputation_contract" }
shared_types = { path = "../shared_types" }

[dev-dependencies]
Expand Down
39 changes: 37 additions & 2 deletions contracts/dispute_resolution_contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use soroban_sdk::{
};
use shared_types::{DeliveryId, SwiftChainError};
use delivery_contract::{DeliveryContractClient, DeliveryStatus};
use identity_reputation_contract::IdentityReputationContractClient;

const DISPUTE_REPUTATION_PENALTY: u32 = 10;

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -32,6 +35,7 @@ pub enum DataKey {
Admin(Address),
DeliveryContract,
EscrowContract,
IdentityReputationContract,
DisputeTimeLimit,
Dispute(DeliveryId),
}
Expand Down Expand Up @@ -94,6 +98,23 @@ impl DisputeResolutionContract {
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized))
}

pub fn set_identity_reputation_contract(env: Env, caller: Address, reputation_contract: Address) {
caller.require_auth();
if !Self::is_admin(env.clone(), caller.clone()) {
panic_with_error!(&env, SwiftChainError::Unauthorized);
}
env.storage()
.instance()
.set(&DataKey::IdentityReputationContract, &reputation_contract);
}

pub fn get_identity_reputation_contract(env: Env) -> Address {
env.storage()
.instance()
.get(&DataKey::IdentityReputationContract)
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized))
}

pub fn get_dispute_time_limit(env: Env) -> u64 {
env.storage()
.instance()
Expand Down Expand Up @@ -218,6 +239,21 @@ impl DisputeResolutionContract {
env.storage().persistent().set(&dispute_key, &dispute);
env.storage().persistent().extend_ttl(&dispute_key, 518400, 518400);

let delivery_contract_addr = Self::get_delivery_contract(env.clone());
let delivery_client = DeliveryContractClient::new(&env, &delivery_contract_addr);
let delivery = delivery_client.get_delivery(&delivery_id);
let driver = delivery
.driver
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::ProviderNotFound));

let reputation_addr = Self::get_identity_reputation_contract(env.clone());
let reputation_client = IdentityReputationContractClient::new(&env, &reputation_addr);
reputation_client.decrease_reputation(
&env.current_contract_address(),
&driver,
&DISPUTE_REPUTATION_PENALTY,
);

let escrow_addr = Self::get_escrow_contract(env.clone());

use soroban_sdk::IntoVal;
Expand All @@ -234,7 +270,7 @@ impl DisputeResolutionContract {

env.events().publish(
(Symbol::new(&env, "dispute_resolved_refund"), delivery_id),
(caller, delivery_id),
(caller, delivery_id, driver, DISPUTE_REPUTATION_PENALTY),
);
}

Expand Down Expand Up @@ -340,4 +376,3 @@ impl DisputeResolutionContract {

#[cfg(test)]
mod test;

1 change: 1 addition & 0 deletions contracts/escrow_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
soroban-sdk = { workspace = true }
shared_types = { path = "../shared_types" }
settlement_contract = { path = "../settlement_contract" }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
59 changes: 45 additions & 14 deletions contracts/escrow_contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, panic_with_error, token, Address, Env,
Symbol,
};
use settlement_contract::SettlementContractClient;

pub mod constants {
pub const ESCROW_TTL_THRESHOLD: u32 = 518400;
Expand Down Expand Up @@ -50,6 +51,35 @@ fn calculate_fee(amount: i128, platform_fee_bps: u32) -> i128 {
amount.saturating_mul(platform_fee_bps as i128) / 10_000
}

fn get_settlement_contract(env: &Env) -> Option<Address> {
env.storage().instance().get(&DataKey::SettlementContract)
}

fn payout_driver(env: &Env, token: &Address, driver: &Address, amount: i128) {
if amount <= 0 {
return;
}

if let Some(settlement_addr) = get_settlement_contract(env) {
let settlement_client = SettlementContractClient::new(env, &settlement_addr);
if let Some(preferred_asset) = settlement_client.get_driver_preference(driver) {
if preferred_asset != token.clone() {
settlement_client.execute_settlement_swap(
&env.current_contract_address(),
token,
&preferred_asset,
driver,
&amount,
&0i128,
);
return;
}
}
}

token::Client::new(env, token).transfer(&env.current_contract_address(), driver, &amount);
}

fn save_escrow(env: &Env, delivery_id: u64, record: &EscrowRecord) {
let key = escrow_key(delivery_id);
env.storage().persistent().set(&key, record);
Expand Down Expand Up @@ -79,6 +109,7 @@ fn load_escrow(env: &Env, delivery_id: u64) -> EscrowRecord {
#[derive(Clone)]
enum DataKey {
PendingAdmin,
SettlementContract,
}

#[contracterror]
Expand Down Expand Up @@ -189,6 +220,18 @@ impl EscrowContract {
load_protocol_config(&env).protocol_version
}

pub fn set_settlement_contract(env: Env, admin: Address, settlement_contract: Address) {
admin.require_auth();
require_admin(&env, &admin);
env.storage()
.instance()
.set(&DataKey::SettlementContract, &settlement_contract);
}

pub fn get_settlement_contract(env: Env) -> Option<Address> {
get_settlement_contract(&env)
}

pub fn propose_admin(env: Env, current_admin: Address, new_admin: Address) {
current_admin.require_auth();
let stored_admin: Address = env
Expand Down Expand Up @@ -302,13 +345,7 @@ impl EscrowContract {
let platform_fee = calculate_fee(record.amount, platform_fee_bps);
let driver_amount = record.amount.saturating_sub(platform_fee);

if driver_amount > 0 {
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.driver,
&driver_amount,
);
}
payout_driver(&env, &record.token, &record.driver, driver_amount);

if platform_fee > 0 {
let admin: Address = env
Expand Down Expand Up @@ -398,13 +435,7 @@ impl EscrowContract {
let platform_fee = calculate_fee(record.amount, platform_fee_bps);
let driver_amount = record.amount.saturating_sub(platform_fee);

if driver_amount > 0 {
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
&record.driver,
&driver_amount,
);
}
payout_driver(&env, &record.token, &record.driver, driver_amount);

if platform_fee > 0 {
let admin: Address = env
Expand Down
2 changes: 1 addition & 1 deletion contracts/settlement_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]
path = "lib.rs"

[dependencies]
Expand Down
Loading
Loading