Skip to content

Commit 4aa316f

Browse files
committed
runtime-sdk/modules/rofl-market: Decrement instances count on cancel
1 parent eb8e61e commit 4aa316f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

runtime-sdk/modules/rofl-market/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl<Cfg: Config> Module<Cfg> {
677677
return Ok(());
678678
}
679679

680-
let provider = state::get_provider(body.provider).ok_or(Error::ProviderNotFound)?;
680+
let mut provider = state::get_provider(body.provider).ok_or(Error::ProviderNotFound)?;
681681
let mut instance =
682682
state::get_instance(body.provider, body.id).ok_or(Error::InstanceNotFound)?;
683683
Self::ensure_caller_is_instance_admin(&instance)?;
@@ -694,6 +694,14 @@ impl<Cfg: Config> Module<Cfg> {
694694
// We can also directly remove the instance.
695695
state::remove_instance(body.provider, body.id);
696696

697+
// Update provider metadata.
698+
provider.instances_count = provider
699+
.instances_count
700+
.checked_sub(1)
701+
.ok_or(Error::InvalidArgument)?;
702+
provider.updated_at = ctx.now();
703+
state::set_provider(provider);
704+
697705
CurrentState::with(|state| {
698706
state.emit_event(Event::InstanceRemoved {
699707
provider: body.provider,

runtime-sdk/modules/rofl-market/src/test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,33 @@ fn test_instance_accept_timeout() {
11991199
assert_eq!(balance, 0);
12001200
let balance = Accounts::get_balance(keys::charlie::address(), Denomination::NATIVE).unwrap();
12011201
assert_eq!(balance, 1_000_000);
1202+
1203+
// Instance should be removed.
1204+
let instances: Vec<types::Instance> = signer_alice
1205+
.query(
1206+
&ctx,
1207+
"roflmarket.Instances",
1208+
types::ProviderQuery {
1209+
provider: keys::alice::address(),
1210+
},
1211+
)
1212+
.unwrap();
1213+
assert_eq!(instances.len(), 0, "instance should be removed");
1214+
1215+
// Provider should have no instances.
1216+
let provider: types::Provider = signer_alice
1217+
.query(
1218+
&ctx,
1219+
"roflmarket.Provider",
1220+
types::ProviderQuery {
1221+
provider: keys::alice::address(),
1222+
},
1223+
)
1224+
.unwrap();
1225+
assert_eq!(
1226+
provider.instances_count, 0,
1227+
"provider should have no instances"
1228+
);
12021229
}
12031230

12041231
#[test]

0 commit comments

Comments
 (0)