Skip to content

Commit

Permalink
feat: agency
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Feb 16, 2024
1 parent 29e2099 commit 12ca23d
Show file tree
Hide file tree
Showing 25 changed files with 492 additions and 33 deletions.
33 changes: 30 additions & 3 deletions integration-tests/src/client/deferred.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use candid::{Encode, Nat, Principal};
use did::deferred::{Contract, ContractRegistration, DeferredResult, TokenInfo};
use did::deferred::{Agency, Contract, ContractRegistration, DeferredResult, TokenInfo};
use did::ID;
use dip721::{GenericValue, NftError, TokenIdentifier, TokenMetadata};

Expand All @@ -21,12 +21,16 @@ impl<'a> DeferredClient<'a> {
Self { env }
}

pub fn register_contract(&self, data: ContractRegistration) -> DeferredResult<ID> {
pub fn register_contract(
&self,
caller: Principal,
data: ContractRegistration,
) -> DeferredResult<ID> {
let contract_id: DeferredResult<ID> = self
.env
.update(
self.env.deferred_id,
admin(),
caller,
"register_contract",
Encode!(&data).unwrap(),
)
Expand Down Expand Up @@ -207,4 +211,27 @@ impl<'a> DeferredClient<'a> {
)
.unwrap()
}

pub fn admin_register_agency(&self, wallet: Principal, agency: Agency) {
let _: () = self
.env
.update(
self.env.deferred_id,
admin(),
"admin_register_agency",
Encode!(&wallet, &agency).unwrap(),
)
.unwrap();
}

pub fn remove_agency(&self, wallet: Principal) -> DeferredResult<()> {
self.env
.update(
self.env.deferred_id,
wallet,
"remove_agency",
Encode!(&wallet).unwrap(),
)
.unwrap()
}
}
24 changes: 18 additions & 6 deletions integration-tests/tests/inspect/deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ fn test_should_inspect_update_contract_property() {
};

// call register
let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(env
.update::<DeferredResult<()>>(
env.deferred_id,
Expand Down Expand Up @@ -154,7 +156,9 @@ fn test_should_inspect_update_contract_property_is_not_authorized() {
};

// call register
let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(env
.update::<DeferredResult<()>>(
env.deferred_id,
Expand Down Expand Up @@ -199,7 +203,9 @@ fn test_should_inspect_update_contract_property_bad_key() {
};

// call register
let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(env
.update::<DeferredResult<()>>(
env.deferred_id,
Expand Down Expand Up @@ -238,7 +244,9 @@ fn test_should_inspect_update_contract_buyers() {
};

// call register
let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(env
.update::<DeferredResult<()>>(
env.deferred_id,
Expand Down Expand Up @@ -288,7 +296,9 @@ fn test_should_inspect_update_contract_buyers_not_seller() {
};

// call register
let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(env
.update::<DeferredResult<()>>(
env.deferred_id,
Expand Down Expand Up @@ -444,7 +454,9 @@ fn test_should_inspect_burn() {
)],
};

let contract_id = client.register_contract(registration_data).unwrap();
let contract_id = client
.register_contract(admin(), registration_data)
.unwrap();
assert!(client.admin_sign_contract(contract_id.clone()).is_ok());

// transfer token to buyer
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/use_case/buy_marketplace_nft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use did::deferred::{ContractRegistration, ContractType, GenericValue, Seller, ID};
use icrc::icrc1::account::Account;
use integration_tests::actor::{alice, alice_account, bob, charlie, charlie_account};
use integration_tests::actor::{admin, alice, alice_account, bob, charlie, charlie_account};
use integration_tests::client::{DeferredClient, IcrcLedgerClient, MarketplaceClient};
use integration_tests::TestEnv;
use pretty_assertions::{assert_eq, assert_ne};
Expand Down Expand Up @@ -130,7 +130,7 @@ fn setup_contract_marketplace(env: &TestEnv) -> ID {
};
// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();
assert_eq!(contract_id, 0_u64);

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/use_case/increment_contract_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use did::deferred::{ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::alice;
use integration_tests::actor::{admin, alice};
use integration_tests::client::DeferredClient;
use integration_tests::TestEnv;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -28,7 +28,7 @@ fn test_as_seller_i_can_set_the_contract_buyers() {

// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();

// sign contract
Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/use_case/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod buy_marketplace_nft;
mod icrc2_spend;
mod increment_contract_value;
mod register_agency;
mod register_contract_buyers;
mod register_sell_contract;
mod reserve_reward_pool;
Expand Down
68 changes: 68 additions & 0 deletions integration-tests/tests/use_case/register_agency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use candid::Nat;
use did::deferred::{Agency, ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::{alice, bob};
use integration_tests::client::DeferredClient;
use integration_tests::TestEnv;
use pretty_assertions::assert_eq;

#[test]
#[serial_test::serial]
fn test_should_register_agency_and_be_able_to_create_contract() {
let env = TestEnv::init();
let deferred_client = DeferredClient::from(&env);

let registration_data = ContractRegistration {
r#type: ContractType::Sell,
sellers: vec![Seller {
principal: alice(),
quota: 100,
}],
buyers: vec![],
value: 400_000,
currency: "EUR".to_string(),
installments: 400_000 / 100,
properties: vec![(
"contract:address".to_string(),
GenericValue::TextContent("via roma 10".to_string()),
)],
};

// give bob an agency
deferred_client.admin_register_agency(
bob(),
Agency {
name: "Bob's agency".to_string(),
address: "Via Delle Botteghe Scure".to_string(),
city: "Rome".to_string(),
region: "Lazio".to_string(),
zip_code: "00100".to_string(),
country: "Italy".to_string(),
continent: did::deferred::Continent::Europe,
email: "email".to_string(),
website: "website".to_string(),
mobile: "mobile".to_string(),
vat: "vat".to_string(),
agent: "agent".to_string(),
logo: None,
},
);

// call register
let contract_id = deferred_client
.register_contract(bob(), registration_data.clone())
.unwrap();
assert_eq!(contract_id, 0_u64);

// check unsigned contract and signed contracts
let unsigned_contracts = deferred_client.admin_get_unsigned_contracts();
assert_eq!(unsigned_contracts, vec![contract_id.clone()]);
let signed_contract = deferred_client.get_signed_contracts();
assert!(signed_contract.is_empty());

// sign contract
let res = deferred_client.admin_sign_contract(Nat::from(0_u64));
assert!(res.is_ok());

// agency could remove himself
assert!(deferred_client.remove_agency(bob()).is_ok());
}
4 changes: 2 additions & 2 deletions integration-tests/tests/use_case/register_contract_buyers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use did::deferred::{ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::{alice, bob};
use integration_tests::actor::{admin, alice, bob};
use integration_tests::client::DeferredClient;
use integration_tests::TestEnv;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -28,7 +28,7 @@ fn test_as_seller_i_can_set_the_contract_buyers() {

// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();

// sign contract
Expand Down
28 changes: 25 additions & 3 deletions integration-tests/tests/use_case/register_sell_contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use candid::Nat;
use did::deferred::{ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::{alice, bob};
use did::deferred::{Agency, ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::{admin, alice, bob};
use integration_tests::client::DeferredClient;
use integration_tests::TestEnv;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -33,9 +33,27 @@ fn test_as_seller_i_can_register_a_sell_contract() {
)],
};

// register agency for admin
let agency = Agency {
name: "Admin's agency".to_string(),
address: "Via Delle Botteghe Scure".to_string(),
city: "Rome".to_string(),
region: "Lazio".to_string(),
zip_code: "00100".to_string(),
country: "Italy".to_string(),
continent: did::deferred::Continent::Europe,
email: "email".to_string(),
website: "website".to_string(),
mobile: "mobile".to_string(),
vat: "vat".to_string(),
agent: "agent".to_string(),
logo: None,
};
deferred_client.admin_register_agency(admin(), agency.clone());

// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();
assert_eq!(contract_id, 0_u64);

Expand All @@ -49,6 +67,10 @@ fn test_as_seller_i_can_register_a_sell_contract() {
let res = deferred_client.admin_sign_contract(Nat::from(0_u64));
assert!(res.is_ok());

// get contract
let contract = deferred_client.get_contract(&contract_id).unwrap();
assert_eq!(contract.agency.unwrap(), agency);

// check unsigned contract and signed contracts
let unsigned_contracts = deferred_client.admin_get_unsigned_contracts();
assert!(unsigned_contracts.is_empty());
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/use_case/reserve_reward_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use candid::Nat;
use did::deferred::{ContractRegistration, ContractType, Seller};
use dip721::GenericValue;
use icrc::icrc1::account::Account;
use integration_tests::actor::alice;
use integration_tests::actor::{admin, alice};
use integration_tests::client::{DeferredClient, EkokeClient};
use integration_tests::{ekoke_to_picoekoke, TestEnv};

Expand Down Expand Up @@ -33,7 +33,7 @@ fn test_should_reserve_a_reward_pool_on_ekoke() {

// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();
assert_eq!(contract_id, 0_u64);

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/use_case/update_contract_property.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use did::deferred::{ContractRegistration, ContractType, GenericValue, Seller};
use integration_tests::actor::{alice, bob};
use integration_tests::actor::{admin, alice, bob};
use integration_tests::client::DeferredClient;
use integration_tests::TestEnv;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -40,7 +40,7 @@ fn test_should_update_contract_property() {

// call register
let contract_id = deferred_client
.register_contract(registration_data)
.register_contract(admin(), registration_data)
.unwrap();

let res = deferred_client.admin_sign_contract(contract_id.clone());
Expand Down
26 changes: 26 additions & 0 deletions src/declarations/deferred/deferred.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';
import type { IDL } from '@dfinity/candid';

export interface Agency {
'vat' : string,
'region' : string,
'zip_code' : string,
'country' : string,
'agent' : string,
'city' : string,
'logo' : [] | [string],
'name' : string,
'continent' : Continent,
'email' : string,
'website' : string,
'address' : string,
'mobile' : string,
}
export type AllowanceError = { 'AllowanceNotFound' : null } |
{ 'BadSpender' : null } |
{ 'AllowanceChanged' : null } |
Expand All @@ -25,11 +40,19 @@ export type ConfigurationError = { 'AdminsCantBeEmpty' : null } |
{ 'AnonymousAdmin' : null };
export type ConfigurationError_1 = { 'CustodialsCantBeEmpty' : null } |
{ 'AnonymousCustodial' : null };
export type Continent = { 'Africa' : null } |
{ 'Antarctica' : null } |
{ 'Asia' : null } |
{ 'Europe' : null } |
{ 'SouthAmerica' : null } |
{ 'Oceania' : null } |
{ 'NorthAmerica' : null };
export interface Contract {
'id' : bigint,
'value' : bigint,
'type' : ContractType,
'is_signed' : boolean,
'agency' : [] | [Agency],
'properties' : Array<[string, GenericValue]>,
'sellers' : Array<Seller>,
'tokens' : Array<bigint>,
Expand Down Expand Up @@ -252,6 +275,7 @@ export type Vec = Array<
>;
export interface _SERVICE {
'admin_get_unsigned_contracts' : ActorMethod<[], Array<bigint>>,
'admin_register_agency' : ActorMethod<[Principal, Agency], undefined>,
'admin_remove_role' : ActorMethod<[Principal, Role], Result>,
'admin_set_ekoke_canister' : ActorMethod<[Principal], undefined>,
'admin_set_marketplace_canister' : ActorMethod<[Principal], undefined>,
Expand All @@ -262,6 +286,7 @@ export interface _SERVICE {
'burn' : ActorMethod<[bigint], Result_1>,
'custodians' : ActorMethod<[], Array<Principal>>,
'cycles' : ActorMethod<[], bigint>,
'get_agencies' : ActorMethod<[], Array<Agency>>,
'get_contract' : ActorMethod<[bigint], [] | [Contract]>,
'get_signed_contracts' : ActorMethod<[], Array<bigint>>,
'get_token' : ActorMethod<[bigint], [] | [TokenInfo]>,
Expand All @@ -280,6 +305,7 @@ export interface _SERVICE {
'owner_token_identifiers' : ActorMethod<[Principal], Result_4>,
'owner_token_metadata' : ActorMethod<[Principal], Result_5>,
'register_contract' : ActorMethod<[ContractRegistration], Result_6>,
'remove_agency' : ActorMethod<[Principal], Result>,
'seller_increment_contract_value' : ActorMethod<
[bigint, bigint, bigint],
Result
Expand Down
Loading

0 comments on commit 12ca23d

Please sign in to comment.