Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
maneva3 committed Nov 6, 2024
1 parent eb71bd0 commit 29b2dfb
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 78 deletions.
25 changes: 12 additions & 13 deletions platform/contracts/admin/src/contracts/impl_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ pub(crate) fn execute(
}

pub(super) fn migrate_contract(address: Addr, migrate: MigrationSpec, batches: Batches) -> Batches {
let post_migration_execute_batch = match migrate.post_migrate_execute_msg {
Some(post_migrate_execute_msg) => execute_contract(
address.clone(),
post_migrate_execute_msg,
batches.post_migration_execute_batch,
),
None => batches.post_migration_execute_batch,
};
let post_migration_execute_batch =
if let Some(post_migrate_execute_msg) = migrate.post_migrate_execute_msg {
execute_contract(
address.clone(),
post_migrate_execute_msg,
batches.post_migration_execute_batch,
)
} else {
batches.post_migration_execute_batch
};

let migration_batch = batches.migration_batch.schedule_execute_reply_on_success(
WasmMsg::Migrate {
Expand Down Expand Up @@ -263,7 +265,8 @@ fn execute_contract(address: Addr, execute_message: String, batch: Batch) -> Bat
})
}

pub(crate) struct Batches {
#[derive(Default)]
pub(super) struct Batches {
migration_batch: Batch,
post_migration_execute_batch: Batch,
}
Expand All @@ -275,8 +278,4 @@ impl Batches {
post_migration_execute_batch,
}
}

pub(crate) fn default() -> Self {
Batches::new(Batch::default(), Batch::default())
}
}
14 changes: 4 additions & 10 deletions platform/contracts/timealarms/src/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use crate::{msg::ExecuteMsg, ContractError};

pub type Result<T> = StdResult<T, ContractError>;

pub trait TimeAlarms
where
Self: Into<Batch>,
{
pub trait TimeAlarms {
fn add_alarm(self, time: Timestamp) -> Result<Batch>;
}

Expand Down Expand Up @@ -80,12 +77,9 @@ impl<'a> TimeAlarmsStub<'a> {

impl<'a> TimeAlarms for TimeAlarmsStub<'a> {
fn add_alarm(self, time: Timestamp) -> Result<Batch> {
let addr = self.addr().clone();
Ok(self.batch.schedule_execute_no_reply(wasm_execute(
addr,
&ExecuteMsg::AddAlarm { time },
vec![],
)?))
wasm_execute(self.addr(), &ExecuteMsg::AddAlarm { time }, vec![])
.map_err(Into::into)
.map(|msg| self.batch.schedule_execute_no_reply(msg))
}
}

Expand Down
5 changes: 2 additions & 3 deletions platform/packages/platform/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::{coin_legacy::to_cosmwasm_impl, contract::Code, error::Error, result:
pub type ReplyId = u64;

#[must_use]
#[derive(Default, Debug)]
#[derive(Default)]
#[cfg_attr(
any(debug_assertions, test, feature = "testing"),
derive(PartialEq, Eq)
derive(Debug, PartialEq, Eq)
)]
pub struct Batch {
msgs: Vec<SubMsg>,
Expand Down Expand Up @@ -46,7 +46,6 @@ impl Batch {
Self::wasm_exec_msg_no_funds(addr, msg).map(|wasm_msg| self.schedule_no_reply(wasm_msg))
}

// TODO get self by value
pub fn schedule_execute_wasm_no_reply<M, C>(
self,
addr: Addr,
Expand Down
13 changes: 4 additions & 9 deletions protocol/contracts/leaser/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,13 @@ impl MigrationResult {
where
F: FnOnce(Batch) -> ContractResult<Batch>,
{
let (msgs, next_customer) = (self.msgs, self.next_customer);

add_fn(msgs).map(|batch| MigrationResult {
msgs: batch,
next_customer,
add_fn(self.msgs).map(|msgs| Self {
msgs,
next_customer: self.next_customer,
})
}

fn default() -> Self {
Self::new(Batch::default(), None)
}

#[cfg(test)]
fn new(msgs: Batch, next_customer: Option<Addr>) -> Self {
Self {
msgs,
Expand Down
14 changes: 9 additions & 5 deletions protocol/contracts/lpp/src/contract/lender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ where
.ok_or(ContractError::NoDeposit {})?
.withdraw(deps.storage, amount_nlpn)?;

let mut bank = bank::account(&env.contract.address, deps.querier);
bank = bank.send(payment_lpn, lender_addr.clone());
let bank =
bank::account(&env.contract.address, deps.querier).send(payment_lpn, lender_addr.clone());

if let Some(reward) = maybe_reward {
let bank = if let Some(reward) = maybe_reward {
if !reward.is_zero() {
bank = bank.send(reward, lender_addr.clone());
bank.send(reward, lender_addr.clone())
} else {
bank
}
}
} else {
bank
};

let batch: Batch = bank.into();
Ok(MessageResponse::messages_with_events(
Expand Down
2 changes: 1 addition & 1 deletion protocol/contracts/lpp/src/stub/lender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
Lpns: Group,
{
fn open_loan_req(self, amount: Coin<Lpn>) -> Result<Batch> {
let id = self.id().clone();
let id = self.id();
self.batch
.schedule_execute_wasm_reply_on_success_no_funds(
id,
Expand Down
19 changes: 10 additions & 9 deletions protocol/contracts/lpp/src/stub/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ where
type Error = ContractError;

fn try_from(stub: LppLoanImpl<Lpn, Lpns>) -> StdResult<Self, Self::Error> {
let mut batch = Batch::default();

if !stub.repayment.is_zero() {
batch = batch.schedule_execute_wasm_no_reply(
stub.lpp_ref.addr().clone(),
&ExecuteMsg::<Lpns>::RepayLoan(),
Some(stub.repayment),
)?;
}
let batch = (!stub.repayment.is_zero())
.then(|| {
Batch::default().schedule_execute_wasm_no_reply(
stub.lpp_ref.addr().clone(),
&ExecuteMsg::<Lpns>::RepayLoan(),
Some(stub.repayment),
)
})
.transpose()?
.unwrap_or_else(Batch::default);

Ok(Self {
lpp_ref: stub.lpp_ref,
Expand Down
14 changes: 7 additions & 7 deletions protocol/packages/dex/src/impl_/resp_delivery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ where
// Limitations:
// 1. Cannot, if ever we want, handle reply_on_success since a successful delivery would have moved to another state
// 2. Do not support reply_* to the sub-messages
let resp = Batch::default();
resp.schedule_execute_wasm_reply_on_error_no_funds(
myself,
&ForwardToInnerMsg::msg(),
REPLY_ID,
)
.map_err(Into::into)
Batch::default()
.schedule_execute_wasm_reply_on_error_no_funds(
myself,
&ForwardToInnerMsg::msg(),
REPLY_ID,
)
.map_err(Into::into)
}
}

Expand Down
14 changes: 7 additions & 7 deletions tests/Cargo.lock

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

31 changes: 17 additions & 14 deletions tests/src/oracle_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,20 +526,23 @@ fn schedule_alarm(
base: Amount,
quote: Amount,
) -> ContractResult<CwResponse> {
Ok(platform::response::response_only_messages(
platform::message::Response::messages_only({
Batch::default().schedule_execute_wasm_no_reply::<_, BaseC>(
ORACLE_ADDR.load(storage).unwrap(),
&ExecuteMsg::AddPriceAlarm {
alarm: Alarm::new(
price::total_of::<BaseC>(base.into()).is::<Lpn>(quote.into()),
None,
),
},
None,
)?
}),
))
Batch::default()
.schedule_execute_wasm_no_reply::<_, BaseC>(
ORACLE_ADDR.load(storage).unwrap(),
&ExecuteMsg::AddPriceAlarm {
alarm: Alarm::new(
price::total_of::<BaseC>(base.into()).is::<Lpn>(quote.into()),
None,
),
},
None,
)
.map_err(Into::into)
.map(|batch| {
platform::response::response_only_messages(platform::message::Response::messages_only(
batch,
))
})
}

fn execute<const RESCHEDULE: bool, const PRICE_BASE: Amount, const PRICE_QUOTE: Amount>(
Expand Down

0 comments on commit 29b2dfb

Please sign in to comment.