diff --git a/substrate-node/pallets/pallet-smart-contract/src/lib.rs b/substrate-node/pallets/pallet-smart-contract/src/lib.rs index 1fd8ce53b..30f896702 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/lib.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/lib.rs @@ -322,10 +322,12 @@ pub mod pallet { SolutionProviderApproved(u64, bool), /// A Service contract is created ServiceContractCreated(types::ServiceContract), + /// A Service contract metadata is set + ServiceContractMetadataSet(types::ServiceContract), + /// A Service contract fees are set + ServiceContractFeesSet(types::ServiceContract), /// A Service contract is approved - ServiceContractApproved { - service_contract_id: u64, - }, + ServiceContractApproved(types::ServiceContract), /// A Service contract is canceled ServiceContractCanceled { service_contract_id: u64, @@ -333,7 +335,7 @@ pub mod pallet { }, /// A Service contract is billed ServiceContractBilled { - service_contract_id: u64, + service_contract: types::ServiceContract, bill: types::ServiceContractBill, amount: BalanceOf, }, @@ -1941,7 +1943,10 @@ impl Pallet { } // Update service contract in map after modification - ServiceContracts::::insert(service_contract_id, service_contract); + ServiceContracts::::insert(service_contract_id, service_contract.clone()); + + // Trigger event for service contract metadata setting + Self::deposit_event(Event::ServiceContractMetadataSet(service_contract)); Ok(().into()) } @@ -1982,7 +1987,10 @@ impl Pallet { } // Update service contract in map after modification - ServiceContracts::::insert(service_contract_id, service_contract); + ServiceContracts::::insert(service_contract_id, service_contract.clone()); + + // Trigger event for service contract fees setting + Self::deposit_event(Event::ServiceContractFeesSet(service_contract)); Ok(().into()) } @@ -2022,17 +2030,16 @@ impl Pallet { // Change contract state to approved and emit event service_contract.state = types::ServiceContractState::ApprovedByBoth; - // Trigger event for service contract approval - Self::deposit_event(Event::ServiceContractApproved { - service_contract_id, - }); // Initialize billing time let now = >::get().saturated_into::() / 1000; service_contract.last_bill = now; } // Update service contract in map after modification - ServiceContracts::::insert(service_contract_id, service_contract); + ServiceContracts::::insert(service_contract_id, service_contract.clone()); + + // Trigger event for service contract approval + Self::deposit_event(Event::ServiceContractApproved(service_contract)); Ok(().into()) } @@ -2163,11 +2170,19 @@ impl Pallet { }; // Make consumer pay for service contract bill - Self::_service_contract_pay_bill(service_contract_id, service_contract_bill)?; + let amount = + Self::_service_contract_pay_bill(service_contract_id, service_contract_bill.clone())?; // Update contract in list after modification service_contract.last_bill = now; - ServiceContracts::::insert(service_contract_id, service_contract); + ServiceContracts::::insert(service_contract_id, service_contract.clone()); + + // Trigger event for service contract billing + Self::deposit_event(Event::ServiceContractBilled { + service_contract, + bill: service_contract_bill, + amount, + }); Ok(().into()) } @@ -2177,7 +2192,7 @@ impl Pallet { fn _service_contract_pay_bill( service_contract_id: u64, bill: types::ServiceContractBill, - ) -> DispatchResultWithPostInfo { + ) -> Result, DispatchErrorWithPostInfo> { let service_contract = ServiceContracts::::get(service_contract_id) .ok_or(Error::::ServiceContractNotExists)?; let amount = service_contract.calculate_bill_cost_tft::(bill.clone())?; @@ -2212,19 +2227,12 @@ impl Pallet { ExistenceRequirement::KeepAlive, )?; - // Trigger event for service contract billing - Self::deposit_event(Event::ServiceContractBilled { - service_contract_id, - bill, - amount, - }); - log::info!( "bill successfully payed by consumer for service contract with id {:?}", service_contract_id, ); - Ok(().into()) + Ok(amount) } pub fn _change_billing_frequency(frequency: u64) -> DispatchResultWithPostInfo { diff --git a/substrate-node/pallets/pallet-smart-contract/src/tests.rs b/substrate-node/pallets/pallet-smart-contract/src/tests.rs index 7733bd640..e1c4a45e7 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/tests.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/tests.rs @@ -2436,6 +2436,7 @@ fn test_service_contract_create_same_account_fails() { #[test] fn test_service_contract_set_metadata_works() { new_test_ext().execute_with(|| { + run_to_block(1, None); create_service_consumer_contract(); assert_ok!(SmartContractModule::service_contract_set_metadata( @@ -2450,6 +2451,15 @@ fn test_service_contract_set_metadata_works() { service_contract, SmartContractModule::service_contracts(1).unwrap(), ); + + let our_events = System::events(); + assert_eq!(!our_events.is_empty(), true); + assert_eq!( + our_events.last().unwrap(), + &record(MockEvent::SmartContractModule( + SmartContractEvent::ServiceContractMetadataSet(service_contract) + )), + ); }); } @@ -2506,6 +2516,7 @@ fn test_service_contract_set_metadata_too_long_fails() { #[test] fn test_service_contract_set_fees_works() { new_test_ext().execute_with(|| { + run_to_block(1, None); create_service_consumer_contract(); assert_ok!(SmartContractModule::service_contract_set_fees( @@ -2522,6 +2533,15 @@ fn test_service_contract_set_fees_works() { service_contract, SmartContractModule::service_contracts(1).unwrap(), ); + + let our_events = System::events(); + assert_eq!(!our_events.is_empty(), true); + assert_eq!( + our_events.last().unwrap(), + &record(MockEvent::SmartContractModule( + SmartContractEvent::ServiceContractFeesSet(service_contract) + )), + ); }); } @@ -2588,6 +2608,17 @@ fn test_service_contract_approve_works() { SmartContractModule::service_contracts(1).unwrap(), ); + let our_events = System::events(); + assert_eq!(!our_events.is_empty(), true); + assert_eq!( + our_events.last().unwrap(), + &record(MockEvent::SmartContractModule(SmartContractEvent::< + TestRuntime, + >::ServiceContractApproved( + service_contract.clone() + ))), + ); + // Consumer approves assert_ok!(SmartContractModule::service_contract_approve( Origin::signed(bob()), @@ -2608,9 +2639,9 @@ fn test_service_contract_approve_works() { our_events.last().unwrap(), &record(MockEvent::SmartContractModule(SmartContractEvent::< TestRuntime, - >::ServiceContractApproved { - service_contract_id: 1, - })), + >::ServiceContractApproved( + service_contract + ))), ); }); } @@ -2817,7 +2848,7 @@ fn test_service_contract_bill_works() { &record(MockEvent::SmartContractModule(SmartContractEvent::< TestRuntime, >::ServiceContractBilled { - service_contract_id: 1, + service_contract, bill: bill_1, amount: billed_amount_1, })), @@ -2863,7 +2894,7 @@ fn test_service_contract_bill_works() { &record(MockEvent::SmartContractModule(SmartContractEvent::< TestRuntime, >::ServiceContractBilled { - service_contract_id: 1, + service_contract, bill: bill_2, amount: billed_amount_2, })),