Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve service contract events #563

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -322,18 +322,20 @@ pub mod pallet {
SolutionProviderApproved(u64, bool),
/// A Service contract is created
ServiceContractCreated(types::ServiceContract),
/// A Service contract metadata is set
ServiceContractMetadataSet(types::ServiceContract),
renauter marked this conversation as resolved.
Show resolved Hide resolved
/// A Service contract fees are set
ServiceContractFeesSet(types::ServiceContract),
renauter marked this conversation as resolved.
Show resolved Hide resolved
/// A Service contract is approved
ServiceContractApproved {
service_contract_id: u64,
},
ServiceContractApproved(types::ServiceContract),
renauter marked this conversation as resolved.
Show resolved Hide resolved
/// 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 @@ -1941,7 +1943,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 @@ -1982,7 +1987,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 @@ -2022,17 +2030,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 @@ -2163,11 +2170,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 @@ -2177,7 +2192,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 @@ -2212,19 +2227,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(
Origin::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