Skip to content

Commit

Permalink
fix: buyers cannot be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jul 31, 2024
1 parent e9f3b11 commit de46065
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/canisters/deferred.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ A Contract is identified by the following properties
- **value**: the FIAT value of the contract
- **currency**: the currency used to represent the value
- **agency**: the agency which has created the contract
- **sellers**: the contract sellers
- **buyers**: the contract buyers
- **sellers**: the contract sellers. Cannot be empty
- **buyers**: the contract buyers. Cannot be empty
- **is_signed**: if signed the contract tokens can be sold. The token must be signed by custodians (or DAO)
- **type**: the contract type (Sell / Funding)
- **reward**: the reward of EKOKE token given to a NFT buyer
Expand Down
1 change: 1 addition & 0 deletions src/declarations/deferred/deferred.did
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type TokenError = variant {
CannotCloseContract;
ContractNotSigned : nat;
ContractHasNoSeller;
ContractHasNoBuyer;
BadContractExpiration;
ContractHasNoTokens;
TokenIsBurned : nat;
Expand Down
1 change: 1 addition & 0 deletions src/declarations/deferred/deferred.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export type TokenError = { 'ContractAlreadySigned' : bigint } |
{ 'CannotCloseContract' : null } |
{ 'ContractNotSigned' : bigint } |
{ 'ContractHasNoSeller' : null } |
{ 'ContractHasNoBuyer' : null } |
{ 'BadContractExpiration' : null } |
{ 'ContractHasNoTokens' : null } |
{ 'TokenIsBurned' : bigint } |
Expand Down
1 change: 1 addition & 0 deletions src/declarations/deferred/deferred.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const idlFactory = ({ IDL }) => {
'CannotCloseContract' : IDL.Null,
'ContractNotSigned' : IDL.Nat,
'ContractHasNoSeller' : IDL.Null,
'ContractHasNoBuyer' : IDL.Null,
'BadContractExpiration' : IDL.Null,
'ContractHasNoTokens' : IDL.Null,
'TokenIsBurned' : IDL.Nat,
Expand Down
1 change: 1 addition & 0 deletions src/declarations/marketplace/marketplace.did
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type TokenError = variant {
CannotCloseContract;
ContractNotSigned : nat;
ContractHasNoSeller;
ContractHasNoBuyer;
BadContractExpiration;
ContractHasNoTokens;
TokenIsBurned : nat;
Expand Down
1 change: 1 addition & 0 deletions src/declarations/marketplace/marketplace.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export type TokenError = { 'ContractAlreadySigned' : bigint } |
{ 'CannotCloseContract' : null } |
{ 'ContractNotSigned' : bigint } |
{ 'ContractHasNoSeller' : null } |
{ 'ContractHasNoBuyer' : null } |
{ 'BadContractExpiration' : null } |
{ 'ContractHasNoTokens' : null } |
{ 'TokenIsBurned' : bigint } |
Expand Down
1 change: 1 addition & 0 deletions src/declarations/marketplace/marketplace.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const idlFactory = ({ IDL }) => {
'CannotCloseContract' : IDL.Null,
'ContractNotSigned' : IDL.Nat,
'ContractHasNoSeller' : IDL.Null,
'ContractHasNoBuyer' : IDL.Null,
'BadContractExpiration' : IDL.Null,
'ContractHasNoTokens' : IDL.Null,
'TokenIsBurned' : IDL.Nat,
Expand Down
1 change: 1 addition & 0 deletions src/deferred/deferred.did
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type TokenError = variant {
CannotCloseContract;
ContractNotSigned : nat;
ContractHasNoSeller;
ContractHasNoBuyer;
BadContractExpiration;
ContractHasNoTokens;
TokenIsBurned : nat;
Expand Down
1 change: 1 addition & 0 deletions src/deferred/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl Deferred {
caller(),
data.value,
&data.sellers,
&data.buyers,
data.installments,
data.expiration.as_deref(),
)?;
Expand Down
60 changes: 59 additions & 1 deletion src/deferred/src/app/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl Inspect {
caller: Principal,
value: u64,
sellers: &[Seller],
buyers: &[Principal],
installments: u64,
expiration: Option<&str>,
) -> DeferredResult<()> {
Expand All @@ -179,6 +180,10 @@ impl Inspect {
return Err(DeferredError::Token(TokenError::ContractHasNoSeller));
}

if buyers.is_empty() || buyers.iter().any(|buyer| buyer == &Principal::anonymous()) {
return Err(DeferredError::Token(TokenError::ContractHasNoBuyer));
}

// verify value must be multiple of installments
if value % installments != 0 {
return Err(DeferredError::Token(
Expand Down Expand Up @@ -533,6 +538,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -550,6 +556,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -567,6 +574,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -584,6 +592,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -601,6 +610,7 @@ mod test {
principal: Principal::anonymous(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -611,7 +621,51 @@ mod test {
fn test_should_inspect_contract_register_if_sellers_is_empty() {
let caller = crate::utils::caller();
assert!(RolesManager::set_custodians(vec![caller]).is_ok());
assert!(Inspect::inspect_register_contract(caller, 100, &[], 25, None,).is_err());
assert!(Inspect::inspect_register_contract(
caller,
100,
&[],
&[Principal::management_canister()],
25,
None,
)
.is_err());
}

#[test]
fn test_should_inspect_contract_register_if_buyer_is_anonymous() {
let caller = crate::utils::caller();
assert!(RolesManager::set_custodians(vec![caller]).is_ok());
assert!(Inspect::inspect_register_contract(
caller,
100,
&[Seller {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::anonymous()],
25,
None,
)
.is_err());
}

#[test]
fn test_should_inspect_contract_register_if_buyers_is_empty() {
let caller = crate::utils::caller();
assert!(RolesManager::set_custodians(vec![caller]).is_ok());
assert!(Inspect::inspect_register_contract(
caller,
100,
&[Seller {
principal: Principal::management_canister(),
quota: 100,
}],
&[],
25,
None,
)
.is_err());
}

#[test]
Expand All @@ -631,6 +685,7 @@ mod test {
quota: 40,
}
],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -648,6 +703,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
None,
)
Expand All @@ -665,6 +721,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
Some("2078-01-01"),
)
Expand All @@ -676,6 +733,7 @@ mod test {
principal: Principal::management_canister(),
quota: 100,
}],
&[Principal::management_canister()],
25,
Some("2018-01-01"),
)
Expand Down
1 change: 1 addition & 0 deletions src/deferred/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn inspect_message_impl() {
caller(),
data.value,
&data.sellers,
&data.buyers,
data.installments,
data.expiration.as_deref(),
)
Expand Down
2 changes: 2 additions & 0 deletions src/did/src/deferred/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum TokenError {
ContractValueIsNotMultipleOfInstallments,
#[error("the provided contract has no seller")]
ContractHasNoSeller,
#[error("the provided contract has no buyer")]
ContractHasNoBuyer,
#[error("in order to close the contract, all the tokens must be owned by the seller")]
CannotCloseContract,
#[error("the provided contract seller quota sum is not 100")]
Expand Down
1 change: 1 addition & 0 deletions src/marketplace/marketplace.did
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type TokenError = variant {
CannotCloseContract;
ContractNotSigned : nat;
ContractHasNoSeller;
ContractHasNoBuyer;
BadContractExpiration;
ContractHasNoTokens;
TokenIsBurned : nat;
Expand Down

0 comments on commit de46065

Please sign in to comment.