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(leaser): migrate leases in batches #99

Merged
merged 6 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions contracts/leaser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leaser"
version = "0.3.0"
version = "0.4.0"
edition.workspace = true
authors.workspace = true
license.workspace = true
Expand Down Expand Up @@ -33,9 +33,10 @@ thiserror = { workspace = true }
serde = { workspace = true, features = ["derive"] }

[dev-dependencies]
lease = { workspace = true, features = ["testing"]}
lease = { workspace = true, features = ["testing"] }
finance = { workspace = true, features = ["testing"] }
currency = { workspace = true, features = ["testing"] }
platform = { workspace = true, features = ["testing"] }
schema = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
Expand Down
16 changes: 12 additions & 4 deletions contracts/leaser/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ pub fn execute(
currency,
max_ltd,
),
ExecuteMsg::MigrateLeases { new_code_id } => {
SingleUserAccess::check_owner_access(deps.storage, &info.sender)
.and_then(move |()| leaser::try_migrate_leases(deps.storage, new_code_id.u64()))
}
ExecuteMsg::MigrateLeases {
new_code_id,
max_leases,
} => SingleUserAccess::check_owner_access(deps.storage, &info.sender).and_then(move |()| {
leaser::try_migrate_leases(deps.storage, new_code_id.u64(), max_leases)
}),
ExecuteMsg::MigrateLeasesCont {
key: next_customer,
max_leases,
} => SingleUserAccess::check_owner_access(deps.storage, &info.sender).and_then(move |()| {
leaser::try_migrate_leases_cont(deps.storage, next_customer, max_leases)
}),
}
.map(response::response_only_messages)
}
Expand Down
5 changes: 5 additions & 0 deletions contracts/leaser/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::TryFromIntError;

use thiserror::Error;

use sdk::cosmwasm_std::StdError;
Expand All @@ -7,6 +9,9 @@ pub enum ContractError {
#[error("[Leaser] [Std] {0}")]
Std(#[from] StdError),

#[error("[Leaser] integer conversion {0}")]
Conversion(#[from] TryFromIntError),

#[error("[Leaser] {0}")]
Finance(#[from] finance::error::Error),

Expand Down
48 changes: 41 additions & 7 deletions contracts/leaser/src/leaser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use finance::{currency::SymbolOwned, liability::Liability, percent::Percent};
use lease::api::{ConnectionParams, DownpaymentCoin, InterestPaymentSpec};
use lpp::{msg::ExecuteMsg, stub::LppRef};
use oracle::stub::OracleRef;
use platform::batch::Batch;
use platform::batch::{Batch, Emit, Emitter};
use platform::message::Response as MessageResponse;
use sdk::cosmwasm_std::{Addr, Deps, StdResult, Storage};

use crate::{
cmd::Quote,
error::ContractError,
migrate::{self},
msg::{ConfigResponse, QuoteResponse},
msg::{ConfigResponse, NbInstances, QuoteResponse},
result::ContractResult,
state::{config::Config, leases::Leases},
};
Expand Down Expand Up @@ -92,24 +92,58 @@ pub(super) fn try_configure(
pub(super) fn try_migrate_leases(
storage: &mut dyn Storage,
new_code_id: u64,
max_leases: NbInstances,
) -> ContractResult<MessageResponse> {
Config::update_lease_code(storage, new_code_id)?;

migrate::migrate_leases(Leases::iter(storage), new_code_id)
.and_then(|batch| update_lpp(storage, new_code_id, batch))
let leases = Leases::iter(storage, None);
migrate::migrate_leases(leases, new_code_id, max_leases)
.and_then(|result| result.try_add_msgs(|msgs| update_lpp_impl(storage, new_code_id, msgs)))
.map(|result| {
MessageResponse::messages_with_events(result.msgs, emit_status(result.next_customer))
})
}

pub(super) fn update_lpp(
pub(super) fn try_migrate_leases_cont(
storage: &mut dyn Storage,
next_customer: Addr,
max_leases: NbInstances,
) -> ContractResult<MessageResponse> {
let lease_code_id = Config::load(storage)?.lease_code_id;

let leases = Leases::iter(storage, Some(next_customer));
migrate::migrate_leases(leases, lease_code_id, max_leases).map(|result| {
MessageResponse::messages_with_events(result.msgs, emit_status(result.next_customer))
})
}

pub(super) fn update_lpp(
storage: &dyn Storage,
new_code_id: u64,
mut batch: Batch,
) -> ContractResult<MessageResponse> {
) -> ContractResult<Batch> {
update_lpp_impl(storage, new_code_id, &mut batch).map(|()| batch)
}

fn update_lpp_impl(
storage: &dyn Storage,
new_code_id: u64,
batch: &mut Batch,
) -> ContractResult<()> {
let lpp = Config::load(storage)?.lpp_addr;
let lpp_update_code = ExecuteMsg::NewLeaseCode {
lease_code_id: new_code_id.into(),
};
batch
.schedule_execute_wasm_no_reply::<_, Nls>(&lpp, lpp_update_code, None)
.map(|()| batch.into())
.map_err(Into::into)
}

fn emit_status(next_customer: Option<Addr>) -> Emitter {
let emitter = Emitter::of_type("migrate-leases");
if let Some(next) = next_customer {
emitter.emit("contunuation-key", next)
} else {
emitter.emit("status", "done")
}
}
Loading