Skip to content

Commit

Permalink
feat: improve service contract events (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter authored Jan 2, 2023
1 parent 817a145 commit 2464bae
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
52 changes: 30 additions & 22 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,20 @@ 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,
cause: types::Cause,
},
/// A Service contract is billed
ServiceContractBilled {
service_contract_id: u64,
service_contract: types::ServiceContract,
bill: types::ServiceContractBill,
amount: BalanceOf<T>,
},
Expand Down Expand Up @@ -1957,7 +1959,10 @@ impl<T: Config> Pallet<T> {
}

// Update service contract in map after modification
ServiceContracts::<T>::insert(service_contract_id, service_contract);
ServiceContracts::<T>::insert(service_contract_id, service_contract.clone());

// Trigger event for service contract metadata setting
Self::deposit_event(Event::ServiceContractMetadataSet(service_contract));

Ok(().into())
}
Expand Down Expand Up @@ -1998,7 +2003,10 @@ impl<T: Config> Pallet<T> {
}

// Update service contract in map after modification
ServiceContracts::<T>::insert(service_contract_id, service_contract);
ServiceContracts::<T>::insert(service_contract_id, service_contract.clone());

// Trigger event for service contract fees setting
Self::deposit_event(Event::ServiceContractFeesSet(service_contract));

Ok(().into())
}
Expand Down Expand Up @@ -2038,17 +2046,16 @@ impl<T: Config> Pallet<T> {
// 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 = <timestamp::Pallet<T>>::get().saturated_into::<u64>() / 1000;
service_contract.last_bill = now;
}

// Update service contract in map after modification
ServiceContracts::<T>::insert(service_contract_id, service_contract);
ServiceContracts::<T>::insert(service_contract_id, service_contract.clone());

// Trigger event for service contract approval
Self::deposit_event(Event::ServiceContractApproved(service_contract));

Ok(().into())
}
Expand Down Expand Up @@ -2179,11 +2186,19 @@ impl<T: Config> Pallet<T> {
};

// 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::<T>::insert(service_contract_id, service_contract);
ServiceContracts::<T>::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())
}
Expand All @@ -2193,7 +2208,7 @@ impl<T: Config> Pallet<T> {
fn _service_contract_pay_bill(
service_contract_id: u64,
bill: types::ServiceContractBill,
) -> DispatchResultWithPostInfo {
) -> Result<BalanceOf<T>, DispatchErrorWithPostInfo> {
let service_contract = ServiceContracts::<T>::get(service_contract_id)
.ok_or(Error::<T>::ServiceContractNotExists)?;
let amount = service_contract.calculate_bill_cost_tft::<T>(bill.clone())?;
Expand Down Expand Up @@ -2228,19 +2243,12 @@ impl<T: Config> Pallet<T> {
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 {
Expand Down
41 changes: 36 additions & 5 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
)),
);
});
}

Expand Down Expand Up @@ -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(
Expand All @@ -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)
)),
);
});
}

Expand Down Expand Up @@ -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(
RuntimeOrigin::signed(bob()),
Expand All @@ -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
))),
);
});
}
Expand Down Expand Up @@ -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,
})),
Expand Down Expand Up @@ -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,
})),
Expand Down

0 comments on commit 2464bae

Please sign in to comment.