Skip to content

Commit

Permalink
Add new query to return all allowances on subkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Aug 24, 2020
1 parent 166efc2 commit 4f8274a
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 5 deletions.
5 changes: 3 additions & 2 deletions contracts/cw1-subkeys/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};

use cw1_subkeys::msg::{HandleMsg, QueryMsg};
use cw1_subkeys::msg::{AllAllowancesResponse, HandleMsg, QueryMsg};
use cw1_subkeys::state::Allowance;
use cw1_whitelist::msg::{AdminListResponse, InitMsg};

Expand All @@ -16,6 +16,7 @@ fn main() {
export_schema(&schema_for!(InitMsg), &out_dir);
export_schema_with_title(&mut schema_for!(HandleMsg), &out_dir, "HandleMsg");
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(Allowance), &out_dir);
export_schema(&schema_for!(AdminListResponse), &out_dir);
export_schema(&schema_for!(AllAllowancesResponse), &out_dir);
export_schema(&schema_for!(Allowance), &out_dir);
}
108 changes: 108 additions & 0 deletions contracts/cw1-subkeys/schema/all_allowances_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AllAllowancesResponse",
"type": "object",
"required": [
"allowances"
],
"properties": {
"allowances": {
"type": "array",
"items": {
"$ref": "#/definitions/AllowanceInfo"
}
}
},
"definitions": {
"AllowanceInfo": {
"type": "object",
"required": [
"balance",
"expires",
"spender"
],
"properties": {
"balance": {
"$ref": "#/definitions/Balance"
},
"expires": {
"$ref": "#/definitions/Expiration"
},
"spender": {
"$ref": "#/definitions/HumanAddr"
}
}
},
"Balance": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
},
"Coin": {
"type": "object",
"required": [
"amount",
"denom"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
}
},
"Expiration": {
"anyOf": [
{
"description": "AtHeight will expire when `env.block.height` >= height",
"type": "object",
"required": [
"at_height"
],
"properties": {
"at_height": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
}
},
{
"description": "AtTime will expire when `env.block.time` >= time",
"type": "object",
"required": [
"at_time"
],
"properties": {
"at_time": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
}
},
{
"description": "Never will never expire. Used to express the empty variant",
"type": "object",
"required": [
"never"
],
"properties": {
"never": {
"type": "object"
}
}
}
]
},
"HumanAddr": {
"type": "string"
},
"Uint128": {
"type": "string"
}
}
}
12 changes: 12 additions & 0 deletions contracts/cw1-subkeys/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
}
}
}
},
{
"description": "Gets all Allowances for this contract Returns AllAllowancesResponse",
"type": "object",
"required": [
"all_allowances"
],
"properties": {
"all_allowances": {
"type": "object"
}
}
}
],
"definitions": {
Expand Down
27 changes: 24 additions & 3 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use schemars::JsonSchema;
use std::fmt;

use cosmwasm_std::{
log, to_binary, Api, BankMsg, Binary, Coin, CosmosMsg, Empty, Env, Extern, HandleResponse,
HumanAddr, InitResponse, Querier, StdError, StdResult, Storage,
log, to_binary, Api, BankMsg, Binary, CanonicalAddr, Coin, CosmosMsg, Empty, Env, Extern,
HandleResponse, HumanAddr, InitResponse, Order, Querier, StdError, StdResult, Storage,
};
use cw0::Expiration;
use cw1_whitelist::{
Expand All @@ -13,7 +13,7 @@ use cw1_whitelist::{
};
use cw2::{set_contract_version, ContractVersion};

use crate::msg::{HandleMsg, QueryMsg};
use crate::msg::{AllAllowancesResponse, AllowanceInfo, HandleMsg, QueryMsg};
use crate::state::{allowances, allowances_read, Allowance};
use std::ops::{AddAssign, Sub};

Expand Down Expand Up @@ -208,6 +208,7 @@ pub fn query<S: Storage, A: Api, Q: Querier>(
match msg {
QueryMsg::AdminList {} => to_binary(&query_admin_list(deps)?),
QueryMsg::Allowance { spender } => to_binary(&query_allowance(deps, spender)?),
QueryMsg::AllAllowances {} => to_binary(&query_all_allowances(deps)?),
}
}

Expand All @@ -223,6 +224,26 @@ pub fn query_allowance<S: Storage, A: Api, Q: Querier>(
Ok(allow)
}

// return a list of all allowances here
pub fn query_all_allowances<S: Storage, A: Api, Q: Querier>(
deps: &Extern<S, A, Q>,
) -> StdResult<AllAllowancesResponse> {
let api = &deps.api;
let res: StdResult<Vec<AllowanceInfo>> = allowances_read(&deps.storage)
.range(None, None, Order::Ascending)
.map(|item| {
item.and_then(|(k, allow)| {
Ok(AllowanceInfo {
spender: api.human_address(&CanonicalAddr::from(k))?,
balance: allow.balance,
expires: allow.expires,
})
})
})
.collect();
Ok(AllAllowancesResponse { allowances: res? })
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
17 changes: 17 additions & 0 deletions contracts/cw1-subkeys/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::fmt;
use cosmwasm_std::{Coin, CosmosMsg, Empty, HumanAddr};
use cw0::Expiration;

use crate::balance::Balance;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum HandleMsg<T = Empty>
Expand Down Expand Up @@ -44,4 +46,19 @@ pub enum QueryMsg {
/// Get the current allowance for the given subkey (how much it can spend)
/// Returns crate::state::Allowance
Allowance { spender: HumanAddr },
/// Gets all Allowances for this contract
/// Returns AllAllowancesResponse
AllAllowances {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct AllAllowancesResponse {
pub allowances: Vec<AllowanceInfo>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct AllowanceInfo {
pub spender: HumanAddr,
pub balance: Balance,
pub expires: Expiration,
}

0 comments on commit 4f8274a

Please sign in to comment.