Skip to content

Commit

Permalink
Lend-market v2: receive snip20 interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ueco-jb committed Nov 21, 2023
1 parent 86a5a5e commit 9da205f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
74 changes: 71 additions & 3 deletions contracts/lending/market_v2/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
use shade_protocol::c_std::entry_point;
use shade_protocol::{
c_std::{
to_binary, Addr, Binary, Coin as StdCoin, Decimal, Deps, DepsMut, Env, MessageInfo, Reply,
Response, StdError, StdResult, SubMsg, Timestamp, Uint128, WasmMsg,
from_binary, to_binary, Addr, Binary, Coin as StdCoin, Decimal, Deps, DepsMut, Env,
MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Timestamp, Uint128, WasmMsg,
},
contract_interfaces::snip20::Snip20ReceiveMsg,
query_authentication::viewing_keys,
utils::{asset::Contract, Query},
};

use crate::{
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, ReceiveMsg},
state::{Config, CONFIG, VIEWING_KEY},
};

Expand Down Expand Up @@ -132,6 +133,73 @@ pub fn execute(
}
}

pub fn receive_snip20_message(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: Snip20ReceiveMsg,
) -> Result<Response, ContractError> {
use ReceiveMsg::*;
// TODO: Result instead of unwrap
match from_binary(&msg.msg.unwrap())? {
Deposit => {
let config = CONFIG.load(deps.storage)?;
if config.ctoken_contract != info.sender {
return Err(ContractError::Unauthorized {});
};
execute::deposit(
deps,
env,
msg.sender,
lending_utils::coin::Coin {
denom: Token::Cw20(
Contract::new(&config.ctoken_contract, &config.ctoken_code_hash).into(),
),
amount: msg.amount,
},
)
}
Repay => {
let config = CONFIG.load(deps.storage)?;
if config.ctoken_contract != info.sender {
return Err(ContractError::Unauthorized {});
};
let sender = deps.api.addr_validate(msg.sender.as_str())?;
execute::repay(
deps,
env,
lending_utils::coin::Coin {
denom: Token::Cw20(
Contract::new(&config.ctoken_contract, &config.ctoken_code_hash).into(),
),
amount: msg.amount,
},
sender,
)
}
RepayTo { account } => {
let config = CONFIG.load(deps.storage)?;
if config.ctoken_contract != info.sender {
return Err(ContractError::Unauthorized {});
};
let account = deps.api.addr_validate(account.as_str())?;
let sender = deps.api.addr_validate(msg.sender.as_str())?;
execute::repay_to(
deps,
env,
sender,
lending_utils::coin::Coin {
denom: Token::Cw20(
Contract::new(&config.ctoken_contract, &config.ctoken_code_hash).into(),
),
amount: msg.amount,
},
account,
)
}
}
}

// Available credit line helpers
mod cr_lending_utils {
use super::*;
Expand Down
7 changes: 7 additions & 0 deletions contracts/lending/market_v2/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ pub enum ExecuteMsg {
Withdraw { amount: Uint128 },
}

#[cw_serde]
pub enum ReceiveMsg {
Deposit,
Repay,
RepayTo { account: String },
}

#[cw_serde]
pub enum CreditAgencyExecuteMsg {
/// Ensures a given account has entered a market. Meant to be called by a specific
Expand Down

0 comments on commit 9da205f

Please sign in to comment.