Skip to content

Commit

Permalink
Add snapshot function back
Browse files Browse the repository at this point in the history
  • Loading branch information
larry0x committed Jan 8, 2022
1 parent 64129c7 commit dc2245a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions contracts/martian-field/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ fn execute_callback(deps: DepsMut, env: Env, info: MessageInfo, msg: CallbackMsg
CallbackMsg::AssertHealth {
user_addr,
} => callbacks::assert_health(deps, env, user_addr),
CallbackMsg::Snapshot {
user_addr,
} => callbacks::snapshot(deps, env, user_addr),
}
}

Expand Down
22 changes: 20 additions & 2 deletions contracts/martian-field/src/execute_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use cosmwasm_std::{

use cw_asset::{Asset, AssetInfo, AssetList};

use fields_of_mars::martian_field::{Position, State};
use fields_of_mars::martian_field::{Position, Snapshot, State};

use crate::health::compute_health;
use crate::state::{CACHED_USER_ADDR, CONFIG, POSITION, STATE};
use crate::state::{CACHED_USER_ADDR, CONFIG, POSITION, SNAPSHOT, STATE};

static DEFAULT_BOND_UNITS_PER_SHARE_BONDED: Uint128 = Uint128::new(1_000_000);
static DEFAULT_DEBT_UNITS_PER_ASSET_BORROWED: Uint128 = Uint128::new(1_000_000);
Expand Down Expand Up @@ -590,3 +590,21 @@ pub fn assert_health(deps: DepsMut, env: Env, user_addr: Addr) -> StdResult<Resp
.add_attribute("action", "martian_field :: callback :: assert_health")
.add_event(event))
}

pub fn snapshot(deps: DepsMut, env: Env, user_addr: Addr) -> StdResult<Response> {
let config = CONFIG.load(deps.storage)?;
let state = STATE.load(deps.storage)?;
let position = POSITION.load(deps.storage, &user_addr).unwrap_or_default();
let health = compute_health(&deps.querier, &env, &config, &state, &position)?;

let snapshot = Snapshot {
time: env.block.time.seconds(),
height: env.block.height,
position: position.into(),
health,
};

SNAPSHOT.save(deps.storage, &user_addr, &snapshot)?;

Ok(Response::new().add_attribute("action", "martian_field :: callback :: snapshot"))
}
5 changes: 4 additions & 1 deletion contracts/martian-field/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use cosmwasm_std::{Addr};
use cw_storage_plus::{Item, Map};

use fields_of_mars::martian_field::{Config, Position, State};
use fields_of_mars::martian_field::{Config, Position, Snapshot, State};

pub const CONFIG: Item<Config> = Item::new("config");
pub const STATE: Item<State> = Item::new("state");
pub const POSITION: Map<&Addr, Position> = Map::new("position");

pub const CACHED_USER_ADDR: Item<Addr> = Item::new("cached_user_addr");

// TODO: delete this
pub const SNAPSHOT: Map<&Addr, Snapshot> = Map::new("snapshot");
18 changes: 18 additions & 0 deletions packages/fields-of-mars/src/martian_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ pub struct Health {
pub ltv: Option<Decimal>,
}

/// Every time the user invokes `update_position`, we record a snaphot of the position
///
/// This snapshot does have any impact on the contract's normal functioning. Rather it is used by
/// the frontend to calculate PnL. Once we have the infrastructure for calculating PnL off-chain
/// available, we will migrate the contract to delete this
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Snapshot {
pub time: u64,
pub height: u64,
pub position: PositionUnchecked,
pub health: Health,
}

//--------------------------------------------------------------------------------------------------
// Message and response types
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -359,6 +372,11 @@ pub mod msg {
AssertHealth {
user_addr: Addr,
},
/// See the comment on struct `Snapshot`. This callback should be removed at some point
/// after launch when our tx indexing infrastructure is ready
Snapshot {
user_addr: Addr,
},
}

impl CallbackMsg {
Expand Down

0 comments on commit dc2245a

Please sign in to comment.