Skip to content

Commit

Permalink
All IBC code minus sending message runs in test for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jun 27, 2023
1 parent d92eafd commit 9c23a06
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions contracts/provider/external-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ impl ExternalStakingContract<'_> {
let tx_id = self.next_tx_id(ctx.deps.storage)?;

// Save tx
#[allow(clippy::redundant_clone)]
let new_tx = Tx::InFlightRemoteUnstaking {
id: tx_id,
amount: amount.amount,
Expand All @@ -297,29 +296,32 @@ impl ExternalStakingContract<'_> {
};
self.pending_txs.save(ctx.deps.storage, tx_id, &new_tx)?;

#[allow(unused_mut)]
let mut resp = Response::new()
.add_attribute("action", "unstake")
.add_attribute("amount", amount.amount.to_string());
.add_attribute("amount", amount.amount.to_string())
.add_attribute("owner", ctx.info.sender);

// add ibc packet if we are ibc enabled (skip in tests)
let channel = IBC_CHANNEL.load(ctx.deps.storage)?;
let packet = ProviderPacket::Unstake {
validator,
unstake: amount,
tx_id,
};
let msg = IbcMsg::SendPacket {
channel_id: channel.endpoint.channel_id,
data: to_binary(&packet)?,
timeout: packet_timeout(&ctx.env),
};
// send packet if we are ibc enabled (skip in tests)
#[cfg(not(test))]
{
let channel = IBC_CHANNEL.load(ctx.deps.storage)?;
let packet = ProviderPacket::Unstake {
validator,
unstake: amount,
tx_id,
};
let msg = IbcMsg::SendPacket {
channel_id: channel.endpoint.channel_id,
data: to_binary(&packet)?,
timeout: packet_timeout(&ctx.env),
};
resp = resp.add_message(msg);
}

// put this later so compiler doens't complain about mut in test mode
resp = resp.add_attribute("owner", ctx.info.sender);
#[cfg(test)]
{
let _ = msg;
}

Ok(resp)
}
Expand Down Expand Up @@ -981,7 +983,6 @@ pub mod cross_staking {
.save(ctx.deps.storage, (&owner, &msg.validator), &stake_lock)?;

// Save tx
#[allow(clippy::redundant_clone)]
let new_tx = Tx::InFlightRemoteStaking {
id: tx_id,
amount: amount.amount,
Expand All @@ -992,22 +993,26 @@ pub mod cross_staking {

let mut resp = Response::new();

let channel = IBC_CHANNEL.load(ctx.deps.storage)?;
let packet = ProviderPacket::Stake {
validator: msg.validator,
stake: amount.clone(),
tx_id,
};
let msg = IbcMsg::SendPacket {
channel_id: channel.endpoint.channel_id,
data: to_binary(&packet)?,
timeout: packet_timeout(&ctx.env),
};
// add ibc packet if we are ibc enabled (skip in tests)
#[cfg(not(test))]
{
let channel = IBC_CHANNEL.load(ctx.deps.storage)?;
let packet = ProviderPacket::Stake {
validator: msg.validator,
stake: amount.clone(),
tx_id,
};
let msg = IbcMsg::SendPacket {
channel_id: channel.endpoint.channel_id,
data: to_binary(&packet)?,
timeout: packet_timeout(&ctx.env),
};
resp = resp.add_message(msg);
}
#[cfg(test)]
{
let _ = msg;
}

resp = resp
.add_attribute("action", "receive_virtual_stake")
Expand Down

0 comments on commit 9c23a06

Please sign in to comment.