Skip to content

Commit

Permalink
Correct escrow transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
mgralinski-bright committed Jul 5, 2023
1 parent d63782b commit 29b7731
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
70 changes: 56 additions & 14 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ pub mod bright_disputes {
defendant_id: AccountId,
escrow: Balance,
) -> Result<DisputeId> {
self.assert_transferred(escrow)?;
let owner_id = ink::env::caller::<ink::env::DefaultEnvironment>();
self.last_dispute_id = self.generate_dispute_id()?;
let dispute = Dispute::create(self.last_dispute_id, owner_link, defendant_id, escrow);
self.update_dispute(dispute);

self.env().transfer(self.env().account_id(), escrow)?;

self.env().emit_event(DisputeRaised {
id: self.last_dispute_id,
owner_id,
Expand All @@ -143,18 +142,17 @@ pub mod bright_disputes {
}

/// Defendant confirms his participation in dispute.
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_defendant(
&mut self,
dispute_id: DisputeId,
defendant_link: String,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
let id = dispute.id();
self.assert_transferred(dispute.escrow())?;
dispute.confirm_defendant(defendant_link)?;
dispute.set_dispute_round(DisputeRound::create(self.env().block_timestamp(), None));
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
dispute.increment_deposit();
self.update_dispute(dispute);

Expand Down Expand Up @@ -230,18 +228,17 @@ pub mod bright_disputes {
}

/// Assigned jure can confirm his participation in dispute
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_jure_participation_in_dispute(
&mut self,
dispute_id: DisputeId,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
self.assert_transferred(dispute.escrow())?;

let caller = ink::env::caller::<ink::env::DefaultEnvironment>();
let mut jure = self.get_jure_or_assert(caller)?;
jure.confirm_participation_in_dispute(dispute_id)?;
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
self.update_jure(jure);

dispute.increment_deposit();
Expand All @@ -250,18 +247,17 @@ pub mod bright_disputes {
}

/// Judge can confirm his participation in dispute
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_judge_participation_in_dispute(
&mut self,
dispute_id: DisputeId,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
self.assert_transferred(dispute.escrow())?;

let caller = ink::env::caller::<ink::env::DefaultEnvironment>();
let mut jure = self.get_jure_or_assert(caller)?;
jure.confirm_participation_in_dispute(dispute_id)?;
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
self.update_jure(jure);

dispute.increment_deposit();
Expand Down Expand Up @@ -428,18 +424,31 @@ pub mod bright_disputes {
}
return Ok(());
}

fn assert_transferred(&self, expected_amount: Balance) -> Result<()> {
let transferred = self.env().transferred_value();
if transferred != expected_amount {
return Err(BrightDisputesError::InvalidEscrowAmount);
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use ink::env::{test::set_caller, DefaultEnvironment};
use ink::env::{
test::{set_caller, set_value_transferred},
DefaultEnvironment,
};

use super::*;

fn create_test_bright_dispute_with_running_dispute() -> BrightDisputes {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
let mut bright_disputes = BrightDisputes::new();

set_value_transferred::<DefaultEnvironment>(10);

// Alice creates a dispute
let dispute_id = bright_disputes
.create_dispute("https://brightinventions.pl/".into(), accounts.bob, 10)
Expand Down Expand Up @@ -488,9 +497,16 @@ pub mod bright_disputes {
let owner_link = "https://brightinventions.pl/";
let escrow_amount: Balance = 15;
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(escrow_amount);

let result =
bright_disputes.create_dispute(owner_link.into(), accounts.bob, escrow_amount);
assert_eq!(result, Ok(1));

// Failed, escrow amount doesn't match the transferred value.
let result =
bright_disputes.create_dispute(owner_link.into(), accounts.bob, escrow_amount + 1);
assert_eq!(result, Err(BrightDisputesError::InvalidEscrowAmount));
}

/// Test if we can create multiple disputes.
Expand All @@ -500,6 +516,7 @@ pub mod bright_disputes {

let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);

// Alice creates first dispute
let result = bright_disputes.create_dispute(
Expand Down Expand Up @@ -527,6 +544,8 @@ pub mod bright_disputes {
let result = bright_disputes.get_dispute(1);
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

set_value_transferred::<DefaultEnvironment>(10);

bright_disputes
.create_dispute("https://brightinventions.pl/1".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand All @@ -542,6 +561,8 @@ pub mod bright_disputes {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
let mut bright_disputes = BrightDisputes::new();

set_value_transferred::<DefaultEnvironment>(10);

bright_disputes
.create_dispute("https://brightinventions.pl/1".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -573,6 +594,7 @@ pub mod bright_disputes {
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

// Create dispute
set_value_transferred::<DefaultEnvironment>(10);
bright_disputes
.create_dispute("https://brightinventions.pl".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -616,6 +638,7 @@ pub mod bright_disputes {
let result = bright_disputes.confirm_defendant(1, defendant_link.into());
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

set_value_transferred::<DefaultEnvironment>(10);
let dispute_id = bright_disputes
.create_dispute("".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -671,6 +694,7 @@ pub mod bright_disputes {
fn update_defendant_description() {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);

let mut bright_disputes = BrightDisputes::new();

Expand Down Expand Up @@ -766,6 +790,7 @@ pub mod bright_disputes {

// Switch to "PickingJuriesAndJudge" state.
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);
bright_disputes
.process_dispute_round(dispute_id)
.expect("Failed to process dispute round!");
Expand All @@ -776,14 +801,31 @@ pub mod bright_disputes {

let judge = dispute.judge().expect("Judge was not assigned!");

// Success
// Fail to confirm judge, invalid escrow
set_caller::<DefaultEnvironment>(judge);
set_value_transferred::<DefaultEnvironment>(5);
let result = bright_disputes.confirm_judge_participation_in_dispute(dispute_id);
assert_eq!(result, Err(BrightDisputesError::InvalidEscrowAmount));

// Success, confirm judge
set_caller::<DefaultEnvironment>(judge);
set_value_transferred::<DefaultEnvironment>(10);
let result = bright_disputes.confirm_judge_participation_in_dispute(dispute_id);
assert_eq!(result, Ok(()));

// Success
// Confirm juries
for jure in &dispute.juries() {
set_caller::<DefaultEnvironment>(*jure);

// Fail to confirm jure, invalid escrow
set_value_transferred::<DefaultEnvironment>(5);
assert_eq!(
bright_disputes.confirm_jure_participation_in_dispute(dispute_id),
Err(BrightDisputesError::InvalidEscrowAmount)
);

// Success
set_value_transferred::<DefaultEnvironment>(10);
assert_eq!(
bright_disputes.confirm_jure_participation_in_dispute(dispute_id),
Ok(())
Expand Down
1 change: 1 addition & 0 deletions contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum BrightDisputesError {
NotAuthorized,
InvalidDisputeState,
InvalidAction,
InvalidEscrowAmount,

JureAlreadyVoted,
JureAlreadyAdded,
Expand Down
5 changes: 3 additions & 2 deletions tests/bright_disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use scale::Encode as _;

#[allow(dead_code)]
pub const CODE_HASH: [u8; 32] = [
60, 75, 158, 148, 77, 75, 179, 135, 170, 224, 43, 109, 89, 129, 53, 230, 24, 241, 3, 222, 76,
185, 154, 22, 106, 57, 124, 73, 78, 217, 57, 70,
206, 40, 233, 94, 177, 91, 101, 22, 171, 11, 5, 89, 43, 242, 9, 87, 188, 185, 138, 211, 4, 63,
59, 135, 90, 206, 90, 4, 10, 167, 122, 113,
];

#[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
Expand Down Expand Up @@ -68,6 +68,7 @@ pub enum BrightDisputesError {
NotAuthorized(),
InvalidDisputeState(),
InvalidAction(),
InvalidEscrowAmount(),
JureAlreadyVoted(),
JureAlreadyAdded(),
JureAlreadyRegistered(),
Expand Down
8 changes: 4 additions & 4 deletions tests/bright_disputes_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rand::RngCore as _;
use crate::{
bright_disputes,
bright_disputes::{DisputeResult, DisputeState, RoundState},
helpers::{create_new_connection, create_new_connections},
helpers::{alephs, create_new_connection, create_new_connections},
};

async fn connect_and_deploy() -> Result<(SignedConnection, bright_disputes::Instance)> {
Expand All @@ -29,7 +29,7 @@ async fn test_dispute_success() -> Result<()> {

// Create a dispute
contract
.create_dispute(&owner_conn, "".into(), defendant, 10)
.create_dispute(&owner_conn, "".into(), defendant, alephs(0))
.await?;
let dispute_id = contract.get_last_dispute_id(&owner_conn).await??;
assert!(dispute_id == 1u32);
Expand Down Expand Up @@ -80,7 +80,7 @@ async fn test_dispute_success() -> Result<()> {
.confirm_jure_participation_in_dispute(conn, dispute_id)
.await?;
}

// Confirm judge participation
contract
.confirm_judge_participation_in_dispute(judge_conn, dispute_id)
Expand Down Expand Up @@ -128,7 +128,7 @@ async fn test_dispute_rounds() -> Result<()> {

// Create a dispute
contract
.create_dispute(&owner_conn, "".into(), defendant, 10)
.create_dispute(&owner_conn, "".into(), defendant, alephs(0))
.await?;
let dispute_id = contract.get_last_dispute_id(&owner_conn).await??;
assert!(dispute_id == 1u32);
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ pub async fn create_new_connections(num_of_new_connections: u8) -> Result<Vec<Si
Ok(connections)
}

fn alephs(n: u128) -> aleph_client::Balance {
pub fn alephs(n: u128) -> aleph_client::Balance {
n * 1_000_000_000_000
}

0 comments on commit 29b7731

Please sign in to comment.