Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve api #6

Merged
merged 6 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions contracts/tfi-factory/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,19 @@
],
"properties": {
"token": {
"type": "object",
"required": [
"contract_addr"
],
"properties": {
"contract_addr": {
"$ref": "#/definitions/Addr"
}
}
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"native_token"
"native"
],
"properties": {
"native_token": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
}
"native": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
24 changes: 4 additions & 20 deletions contracts/tfi-factory/schema/pair_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,19 @@
],
"properties": {
"token": {
"type": "object",
"required": [
"contract_addr"
],
"properties": {
"contract_addr": {
"$ref": "#/definitions/Addr"
}
}
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"native_token"
"native"
],
"properties": {
"native_token": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
}
"native": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
24 changes: 4 additions & 20 deletions contracts/tfi-factory/schema/pairs_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,19 @@
],
"properties": {
"token": {
"type": "object",
"required": [
"contract_addr"
],
"properties": {
"contract_addr": {
"$ref": "#/definitions/Addr"
}
}
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"native_token"
"native"
],
"properties": {
"native_token": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
}
"native": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
24 changes: 4 additions & 20 deletions contracts/tfi-factory/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,19 @@
],
"properties": {
"token": {
"type": "object",
"required": [
"contract_addr"
],
"properties": {
"contract_addr": {
"$ref": "#/definitions/Addr"
}
}
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"native_token"
"native"
],
"properties": {
"native_token": {
"type": "object",
"required": [
"denom"
],
"properties": {
"denom": {
"type": "string"
}
}
"native": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
57 changes: 20 additions & 37 deletions contracts/tfi-factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response,
StdError, StdResult, SubMsg, WasmMsg,
attr, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdError,
StdResult, SubMsg, WasmMsg,
};

use crate::querier::query_liquidity_token;
use crate::response::MsgInstantiateContractResponse;
use crate::state::{pair_key, read_pairs, Config, TmpPairInfo, CONFIG, PAIRS, TMP_PAIR_INFO};

use protobuf::Message;
use tfi::asset::{AssetInfo, PairInfo, PairInfoRaw};
use tfi::asset::{AssetInfo, PairInfo};
use tfi::factory::{
ConfigResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, PairsResponse, QueryMsg,
};
Expand All @@ -24,7 +24,7 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> StdResult<Response> {
let config = Config {
owner: deps.api.addr_canonicalize(info.sender.as_str())?,
owner: info.sender,
token_code_id: msg.token_code_id,
pair_code_id: msg.pair_code_id,
};
Expand Down Expand Up @@ -63,15 +63,14 @@ pub fn execute_update_config(
let mut config: Config = CONFIG.load(deps.storage)?;

// permission check
if deps.api.addr_canonicalize(info.sender.as_str())? != config.owner {
if info.sender != config.owner {
return Err(StdError::generic_err("unauthorized"));
}

if let Some(owner) = owner {
// validate address format
let _ = deps.api.addr_validate(&owner)?;

config.owner = deps.api.addr_canonicalize(&owner)?;
let owner = deps.api.addr_validate(&owner)?;
config.owner = owner;
}

if let Some(token_code_id) = token_code_id {
Expand Down Expand Up @@ -100,12 +99,8 @@ pub fn execute_create_pair(
asset_infos: [AssetInfo; 2],
) -> StdResult<Response> {
let config: Config = CONFIG.load(deps.storage)?;
let raw_infos = [
asset_infos[0].to_raw(deps.api)?,
asset_infos[1].to_raw(deps.api)?,
];

let pair_key = pair_key(&raw_infos);
let pair_key = pair_key(&asset_infos);
if let Ok(Some(_)) = PAIRS.may_load(deps.storage, &pair_key) {
return Err(StdError::generic_err("Pair already exists"));
}
Expand All @@ -114,7 +109,7 @@ pub fn execute_create_pair(
deps.storage,
&TmpPairInfo {
pair_key,
asset_infos: raw_infos,
asset_infos: asset_infos.clone(),
},
)?;

Expand Down Expand Up @@ -154,15 +149,15 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Response> {
StdError::parse_err("MsgInstantiateContractResponse", "failed to parse data")
})?;

let pair_contract = res.get_contract_address();
let liquidity_token = query_liquidity_token(deps.as_ref(), Addr::unchecked(pair_contract))?;
let pair_contract = deps.api.addr_validate(res.get_contract_address())?;
let liquidity_token = query_liquidity_token(deps.as_ref(), pair_contract.clone())?;

PAIRS.save(
deps.storage,
&tmp_pair_info.pair_key,
&PairInfoRaw {
liquidity_token: deps.api.addr_canonicalize(liquidity_token.as_str())?,
contract_addr: deps.api.addr_canonicalize(pair_contract)?,
&PairInfo {
liquidity_token: liquidity_token.clone(),
contract_addr: pair_contract.clone(),
asset_infos: tmp_pair_info.asset_infos,
},
)?;
Expand All @@ -171,7 +166,7 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Response> {
messages: vec![],
submessages: vec![],
attributes: vec![
attr("pair_contract_addr", pair_contract),
attr("pair_contract_addr", pair_contract.as_str()),
attr("liquidity_token_addr", liquidity_token.as_str()),
],
data: None,
Expand All @@ -192,7 +187,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let state: Config = CONFIG.load(deps.storage)?;
let resp = ConfigResponse {
owner: deps.api.addr_humanize(&state.owner)?.to_string(),
owner: state.owner.into(),
token_code_id: state.token_code_id,
pair_code_id: state.pair_code_id,
};
Expand All @@ -201,29 +196,17 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
}

pub fn query_pair(deps: Deps, asset_infos: [AssetInfo; 2]) -> StdResult<PairInfo> {
let pair_key = pair_key(&[
asset_infos[0].to_raw(deps.api)?,
asset_infos[1].to_raw(deps.api)?,
]);
let pair_info: PairInfoRaw = PAIRS.load(deps.storage, &pair_key)?;
pair_info.to_normal(deps.api)
let pair_key = pair_key(&asset_infos);
let pair_info: PairInfo = PAIRS.load(deps.storage, &pair_key)?;
Ok(pair_info)
}

pub fn query_pairs(
deps: Deps,
start_after: Option<[AssetInfo; 2]>,
limit: Option<u32>,
) -> StdResult<PairsResponse> {
let start_after = if let Some(start_after) = start_after {
Some([
start_after[0].to_raw(deps.api)?,
start_after[1].to_raw(deps.api)?,
])
} else {
None
};

let pairs: Vec<PairInfo> = read_pairs(deps.storage, deps.api, start_after, limit)?;
let pairs: Vec<PairInfo> = read_pairs(deps.storage, start_after, limit)?;
let resp = PairsResponse { pairs };

Ok(resp)
Expand Down
26 changes: 8 additions & 18 deletions contracts/tfi-factory/src/mock_querier.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{
from_slice, to_binary, Api, Coin, ContractResult, Empty, OwnedDeps, Querier, QuerierResult,
from_slice, to_binary, Coin, ContractResult, Empty, OwnedDeps, Querier, QuerierResult,
QueryRequest, SystemError, SystemResult, WasmQuery,
};
use cosmwasm_storage::to_length_prefixed;
use std::collections::HashMap;
use tfi::asset::{AssetInfoRaw, PairInfo, PairInfoRaw};
use tfi::asset::{AssetInfo, PairInfo};

/// mock_dependencies is a drop-in replacement for cosmwasm_std::testing::mock_dependencies
/// this uses our CustomQuerier.
Expand Down Expand Up @@ -69,7 +68,7 @@ impl WasmMockQuerier {
match &request {
QueryRequest::Wasm(WasmQuery::Raw { contract_addr, key }) => {
let key: &[u8] = key.as_slice();
let prefix_pair_info = to_length_prefixed(b"pair_info").to_vec();
let prefix_pair_info = b"pair_info".to_vec();

if key.to_vec() == prefix_pair_info {
let pair_info: PairInfo = match self.tfi_pair_querier.pairs.get(contract_addr) {
Expand All @@ -82,21 +81,12 @@ impl WasmMockQuerier {
}
};

let api: MockApi = MockApi::default();
SystemResult::Ok(ContractResult::from(to_binary(&PairInfoRaw {
contract_addr: api
.addr_canonicalize(pair_info.contract_addr.as_str())
.unwrap(),
liquidity_token: api
.addr_canonicalize(pair_info.liquidity_token.as_str())
.unwrap(),
SystemResult::Ok(ContractResult::from(to_binary(&PairInfo {
contract_addr: pair_info.contract_addr.clone(),
liquidity_token: pair_info.liquidity_token,
asset_infos: [
AssetInfoRaw::NativeToken {
denom: "uusd".to_string(),
},
AssetInfoRaw::NativeToken {
denom: "uusd".to_string(),
},
AssetInfo::Native("uusd".to_string()),
AssetInfo::Native("uusd".to_string()),
],
})))
} else {
Expand Down
8 changes: 4 additions & 4 deletions contracts/tfi-factory/src/querier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use cosmwasm_std::{Addr, Binary, Deps, QueryRequest, StdResult, WasmQuery};
use tfi::asset::PairInfoRaw;
use tfi::asset::PairInfo;

pub fn query_liquidity_token(deps: Deps, contract_addr: Addr) -> StdResult<Addr> {
// load pair_info form the pair contract
let pair_info: PairInfoRaw = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Raw {
let pair_info: PairInfo = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Raw {
contract_addr: contract_addr.to_string(),
key: Binary::from("\u{0}\u{9}pair_info".as_bytes()),
key: Binary::from("pair_info".as_bytes()),
}))?;

deps.api.addr_humanize(&pair_info.liquidity_token)
Ok(pair_info.liquidity_token)
}
Loading