Skip to content

Commit c161843

Browse files
committed
Lending-market v2: Implement authenticating viewing key
1 parent 007d30f commit c161843

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

contracts/lending/market_v2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ shade-protocol = { version = "0.1.0", path = "../../../packages/shade_protocol",
2020
schemars = "0.8"
2121
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
2222
thiserror = "1"
23+
query_auth = { path = "../../query_auth" }
2324

2425
[dev-dependencies]
2526
anyhow = "1"

contracts/lending/market_v2/src/contract.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use shade_protocol::{
55
from_binary, to_binary, Addr, Binary, Coin as StdCoin, Decimal, Deps, DepsMut, Env,
66
MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Timestamp, Uint128, WasmMsg,
77
},
8-
contract_interfaces::snip20::Snip20ReceiveMsg,
8+
contract_interfaces::{query_auth::helpers::authenticate_vk, snip20::Snip20ReceiveMsg},
99
query_authentication::viewing_keys,
1010
utils::{asset::Contract, Query},
1111
};
1212

1313
use crate::{
1414
error::ContractError,
15-
msg::{AuthQueryMsg, ExecuteMsg, InstantiateMsg, QueryMsg, ReceiveMsg, TotalDebtResponse},
15+
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, ReceiveMsg, TotalDebtResponse},
1616
state::{debt, Config, CONFIG, VIEWING_KEY},
1717
};
1818

@@ -737,11 +737,53 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
737737
QueryMsg::TokensBalance {
738738
account,
739739
viewing_key,
740-
} => to_binary(&query::tokens_balance(deps, env, account)?)?,
741-
QueryMsg::Withdrawable { account } => to_binary(&query::withdrawable(deps, env, account)?)?,
742-
QueryMsg::Borrowable { account } => to_binary(&query::borrowable(deps, env, account)?)?,
743-
QueryMsg::CreditLine { account } => {
744-
let account = deps.api.addr_validate(&account)?;
740+
} => {
741+
let config = CONFIG.load(deps.storage)?;
742+
authenticate_vk(
743+
account.clone(),
744+
viewing_key,
745+
&deps.querier,
746+
&config.query_auth,
747+
)?;
748+
to_binary(&query::tokens_balance(deps, env, account.to_string())?)?
749+
}
750+
QueryMsg::Withdrawable {
751+
account,
752+
viewing_key,
753+
} => {
754+
let config = CONFIG.load(deps.storage)?;
755+
authenticate_vk(
756+
account.clone(),
757+
viewing_key,
758+
&deps.querier,
759+
&config.query_auth,
760+
)?;
761+
to_binary(&query::withdrawable(deps, env, account.to_string())?)?
762+
}
763+
QueryMsg::Borrowable {
764+
account,
765+
viewing_key,
766+
} => {
767+
let config = CONFIG.load(deps.storage)?;
768+
authenticate_vk(
769+
account.clone(),
770+
viewing_key,
771+
&deps.querier,
772+
&config.query_auth,
773+
)?;
774+
to_binary(&query::borrowable(deps, env, account.to_string())?)?
775+
}
776+
QueryMsg::CreditLine {
777+
account,
778+
viewing_key,
779+
} => {
780+
let config = CONFIG.load(deps.storage)?;
781+
authenticate_vk(
782+
account.clone(),
783+
viewing_key,
784+
&deps.querier,
785+
&config.query_auth,
786+
)?;
745787
to_binary(&query::credit_line(deps, env, account)?)?
746788
}
747789
QueryMsg::Configuration {} => to_binary(&query::config(deps, env)?)?,

contracts/lending/market_v2/src/msg.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,16 @@ pub enum QueryMsg {
123123
TotalDebt {},
124124
/// Returns TokensBalanceResponse
125125
#[returns(TokensBalanceResponse)]
126-
TokensBalance {
127-
account: String,
128-
viewing_key: String,
129-
},
126+
TokensBalance { account: Addr, viewing_key: String },
130127
/// Returns the amount that the given account can withdraw
131128
#[returns(Coin)]
132-
Withdrawable {
133-
account: String,
134-
viewing_key: String,
135-
},
129+
Withdrawable { account: Addr, viewing_key: String },
136130
/// Returns the amount that the given account can borrow
137131
#[returns(Coin)]
138-
Borrowable {
139-
account: String,
140-
viewing_key: String,
141-
},
132+
Borrowable { account: Addr, viewing_key: String },
142133
/// Returns CreditLineResponse
143134
#[returns(lending_utils::credit_line::CreditLineResponse)]
144-
CreditLine {
145-
account: String,
146-
viewing_key: String,
147-
},
135+
CreditLine { account: Addr, viewing_key: String },
148136
}
149137

150138
impl Query for QueryMsg {

contracts/lending/market_v2/src/state.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

44
use shade_protocol::{
5-
c_std::{Addr, ContractInfo, Decimal, Uint128},
5+
c_std::{Addr, Decimal, Uint128},
66
secret_storage_plus::Item,
7+
utils::asset::Contract,
78
};
89

910
use lending_utils::{interest::ValidatedInterest, token::Token};
@@ -38,11 +39,11 @@ pub struct Config {
3839
/// Address of Oracle's contract
3940
pub price_oracle: String,
4041
/// Address of Credit Agency
41-
pub credit_agency: ContractInfo,
42+
pub credit_agency: Contract,
4243
/// Address of oracle
43-
pub oracle: ContractInfo,
44+
pub oracle: Contract,
4445
/// Address of auth query contract
45-
pub query_auth: ContractInfo,
46+
pub query_auth: Contract,
4647
pub reserve_factor: Decimal,
4748
}
4849

0 commit comments

Comments
 (0)