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
3 changes: 2 additions & 1 deletion contracts/delivery_contract/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]

use shared_types::SwiftChainError;
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, panic_with_error, Address, Env, Symbol,
};
Expand Down Expand Up @@ -86,7 +87,7 @@ pub struct DeliveryContract;
impl DeliveryContract {
pub fn init(env: Env, admin: Address, escrow_contract: Address) {
if env.storage().instance().has(&DataKey::Admin) {
panic!("AlreadyInitialized");
panic_with_error!(&env, SwiftChainError::AlreadyInitialized);
}
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage()
Expand Down
2 changes: 1 addition & 1 deletion contracts/delivery_contract/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn test_create_delivery_incrementing_ids_and_persistence() {
}

#[test]
#[should_panic(expected = "AlreadyInitialized")]
#[should_panic]
fn test_double_init() {
let (env, client, admin, _, _) = setup_test();
let escrow = Address::generate(&env);
Expand Down
93 changes: 50 additions & 43 deletions contracts/escrow_contract/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![no_std]

use shared_types::{events, DeliveryStatus, EscrowRecord, EscrowStatus};
use shared_types::{events, DeliveryStatus, EscrowRecord, EscrowStatus, SwiftChainError};
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, panic_with_error, token, Address, Env,
Symbol,
contract, contractimpl, contracttype, panic_with_error, token, Address, Env, Symbol,
};

pub mod constants {
pub const ESCROW_TTL_THRESHOLD: u32 = 518400;
pub const ESCROW_TTL_EXTEND_TO: u32 = 518400;
pub const PROTOCOL_VERSION: u32 = 1;
}

fn require_admin(env: &Env, caller: &Address) {
let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
.unwrap_or_else(|| panic_with_error!(env, SwiftChainError::NotInitialized));
if *caller != stored_admin {
panic!("Unauthorized");
panic_with_error!(env, SwiftChainError::Unauthorized);
}
}

Expand Down Expand Up @@ -51,7 +51,7 @@ fn load_escrow(env: &Env, delivery_id: u64) -> EscrowRecord {
.storage()
.persistent()
.get(&key)
.unwrap_or_else(|| panic_with_error!(env, EscrowError::DeliveryNotFound));
.unwrap_or_else(|| panic_with_error!(env, SwiftChainError::DeliveryNotFound));
env.storage().persistent().extend_ttl(
&key,
constants::ESCROW_TTL_THRESHOLD,
Expand All @@ -65,8 +65,9 @@ fn load_escrow(env: &Env, delivery_id: u64) -> EscrowRecord {
enum DataKey {
Admin,
PendingAdmin,
Token,
PlatformFeeBps,
Amount,
ProtocolVersion,
Escrow(u64),
}

Expand All @@ -89,58 +90,54 @@ pub struct FeeUpdated {

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EscrowState {
Pending,
Released,
Refunded,
Paused,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EscrowRecord {
pub sender: Address,
pub driver: Address,
pub struct ProtocolInitialized {
pub admin: Address,
pub token: Address,
pub amount: i128,
pub status: EscrowState,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DisputeFavour {
Sender,
Driver,
pub platform_fee_bps: u32,
pub protocol_version: u32,
}

#[contract]
pub struct EscrowContract;

#[contractimpl]
impl EscrowContract {
pub fn init(env: Env, admin: Address, amount: i128) {
pub fn init(env: Env, admin: Address, token: Address, platform_fee_bps: u32) {
if env.storage().instance().has(&DataKey::Admin) {
panic!("Already initialized");
panic_with_error!(&env, SwiftChainError::AlreadyInitialized);
}
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage().instance().set(&DataKey::Amount, &amount);
env.storage().instance().set(&DataKey::Token, &token);
env.storage()
.instance()
.set(&DataKey::PlatformFeeBps, &0u32);
.set(&DataKey::PlatformFeeBps, &platform_fee_bps);
env.storage()
.instance()
.set(&DataKey::ProtocolVersion, &constants::PROTOCOL_VERSION);

env.events().publish(
(Symbol::new(&env, "ProtocolInitialized"),),
ProtocolInitialized {
admin,
token,
platform_fee_bps,
protocol_version: constants::PROTOCOL_VERSION,
},
);
}

pub fn update_platform_fee(env: Env, admin: Address, new_fee_bps: u32) {
let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized));
if admin != stored_admin {
panic!("Unauthorized");
panic_with_error!(&env, SwiftChainError::Unauthorized);
}
admin.require_auth();
if new_fee_bps > 1000 {
panic_with_error!(&env, EscrowError::InvalidState);
panic_with_error!(&env, SwiftChainError::InvalidState);
}
let old_fee: u32 = env
.storage()
Expand Down Expand Up @@ -174,11 +171,21 @@ impl EscrowContract {
env.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized")
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized))
}

pub fn get_amount(env: Env) -> i128 {
env.storage().instance().get(&DataKey::Amount).unwrap_or(0)
pub fn get_token(env: Env) -> Address {
env.storage()
.instance()
.get(&DataKey::Token)
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized))
}

pub fn get_protocol_version(env: Env) -> u32 {
env.storage()
.instance()
.get(&DataKey::ProtocolVersion)
.unwrap_or(0)
}

pub fn propose_admin(env: Env, current_admin: Address, new_admin: Address) {
Expand All @@ -187,7 +194,7 @@ impl EscrowContract {
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized));
if stored_admin != current_admin {
panic!("caller is not the admin");
}
Expand All @@ -214,7 +221,7 @@ impl EscrowContract {
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
.unwrap_or_else(|| panic_with_error!(&env, SwiftChainError::NotInitialized));
env.storage().instance().set(&DataKey::Admin, &new_admin);
env.storage().instance().remove(&DataKey::PendingAdmin);
env.storage().instance().extend_ttl(
Expand Down Expand Up @@ -287,7 +294,7 @@ impl EscrowContract {
let contract_balance =
token::Client::new(&env, &record.token).balance(&env.current_contract_address());
if contract_balance < record.amount {
panic_with_error!(&env, EscrowError::InsufficientFunds);
panic_with_error!(&env, SwiftChainError::InsufficientFunds);
}
let platform_fee_bps: u32 = env
.storage()
Expand Down Expand Up @@ -341,7 +348,7 @@ impl EscrowContract {
let contract_balance =
token::Client::new(&env, &record.token).balance(&env.current_contract_address());
if contract_balance < record.amount {
panic_with_error!(&env, EscrowError::InsufficientFunds);
panic_with_error!(&env, SwiftChainError::InsufficientFunds);
}
token::Client::new(&env, &record.token).transfer(
&env.current_contract_address(),
Expand Down Expand Up @@ -437,7 +444,7 @@ impl EscrowContract {
.persistent()
.has(&DataKey::Escrow(delivery_id))
{
panic_with_error!(&env, EscrowError::DeliveryNotFound);
panic_with_error!(&env, SwiftChainError::DeliveryNotFound);
}
load_escrow(&env, delivery_id)
}
Expand Down
13 changes: 13 additions & 0 deletions contracts/escrow_contract/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use shared_types::SwiftChainError;
use soroban_sdk::{
testutils::Address as _,
token::{Client as TokenClient, StellarAssetClient},
Expand All @@ -17,6 +18,18 @@ fn setup_token(env: &Env, admin: &Address) -> Address {
.address()
}

fn init_contract(
env: &Env,
client: &EscrowContractClient,
admin: &Address,
platform_fee_bps: u32,
) -> Address {
let token_admin = Address::generate(env);
let token = setup_token(env, &token_admin);
client.init(admin, &token, &platform_fee_bps);
token
}

fn mint(env: &Env, token: &Address, to: &Address, amount: i128) {
StellarAssetClient::new(env, token).mint(to, &amount);
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/shared_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ crate-type = ["rlib"]

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
Loading