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

Generic interfaces on examples #402

Merged
merged 13 commits into from
Aug 6, 2024
5 changes: 4 additions & 1 deletion examples/contracts/custom/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use cosmwasm_std::{CosmosMsg, Empty, Response, StdError, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};

use crate::contract::CustomContract;

impl Cw1 for CustomContract {
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
5 changes: 4 additions & 1 deletion examples/contracts/cw1-subkeys/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{ensure, Addr, Response, StdResult};
use cosmwasm_std::{ensure, Addr, Empty, Response, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};

Expand All @@ -7,6 +7,9 @@ use crate::error::ContractError;

impl Cw1 for Cw1SubkeysContract {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(
&self,
Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/cw1-subkeys/src/whitelist.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Response, StdResult};
use cosmwasm_std::{Empty, Response, StdResult};
use sylvia::types::{ExecCtx, QueryCtx};
use whitelist::responses::AdminListResponse;
use whitelist::Whitelist;
Expand All @@ -8,6 +8,8 @@ use crate::error::ContractError;

impl Whitelist for Cw1SubkeysContract {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;

fn freeze(&self, ctx: ExecCtx) -> Result<Response, Self::Error> {
self.whitelist.freeze(ctx).map_err(From::from)
Expand Down
5 changes: 4 additions & 1 deletion examples/contracts/cw1-whitelist/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, CosmosMsg, Response, StdResult};
use cosmwasm_std::{Addr, CosmosMsg, Empty, Response, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};

Expand All @@ -7,6 +7,9 @@ use crate::error::ContractError;

impl Cw1 for Cw1WhitelistContract {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, ctx: ExecCtx, msgs: Vec<CosmosMsg>) -> Result<Response, ContractError> {
if !self.is_admin(ctx.deps.as_ref(), &ctx.info.sender) {
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-whitelist/src/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::error::ContractError;

impl Whitelist for Cw1WhitelistContract {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;

fn freeze(&self, ctx: ExecCtx) -> Result<Response, ContractError> {
if !self.is_admin(ctx.deps.as_ref(), &ctx.info.sender) {
Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/cw20-base/src/allowances.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Binary, Order, Response, StdError, StdResult, Uint128};
use cosmwasm_std::{Addr, Binary, Empty, Order, Response, StdError, StdResult, Uint128};
use cw20_allowances::responses::{
AllAccountsResponse, AllAllowancesResponse, AllSpenderAllowancesResponse, AllowanceInfo,
AllowanceResponse, SpenderAllowanceInfo,
Expand All @@ -18,6 +18,8 @@ const DEFAULT_LIMIT: u32 = 10;

impl Cw20Allowances for Cw20Base {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;

/// Allows spender to access an additional amount tokens from the owner's (env.sender) account.
/// If expires is Some(), overwrites current allowance expiration with this one.
Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/cw20-base/src/marketing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::contract::Cw20Base;
use crate::error::ContractError;
use crate::validation::verify_logo;
use cosmwasm_std::{Response, StdError, StdResult};
use cosmwasm_std::{Empty, Response, StdError, StdResult};
use cw20_marketing::responses::{DownloadLogoResponse, LogoInfo, MarketingInfoResponse};
use cw20_marketing::{Cw20Marketing, EmbeddedLogo, Logo};
use sylvia::types::{ExecCtx, QueryCtx};

impl Cw20Marketing for Cw20Base {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;

fn update_marketing(
&self,
Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/cw20-base/src/minting.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::contract::{Cw20Base, MinterData};
use crate::error::ContractError;
use cosmwasm_std::{Response, StdResult, Uint128};
use cosmwasm_std::{Empty, Response, StdResult, Uint128};
use cw20_minting::responses::MinterResponse;
use cw20_minting::Cw20Minting;
use sylvia::types::{ExecCtx, QueryCtx};

impl Cw20Minting for Cw20Base {
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;

fn mint(
&self,
Expand Down
5 changes: 4 additions & 1 deletion examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::contract::GenericContract;
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use cosmwasm_std::{CosmosMsg, Empty, Response, StdError, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};

Expand Down Expand Up @@ -33,6 +33,9 @@ impl<
>
{
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
5 changes: 4 additions & 1 deletion examples/contracts/generic_iface_on_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use cosmwasm_std::{CosmosMsg, Empty, Response, StdError, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};

impl Cw1 for crate::contract::NonGenericContract {
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
5 changes: 4 additions & 1 deletion examples/contracts/generics_forwarded/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::schemars::JsonSchema;
use cosmwasm_std::{CosmosMsg, CustomMsg, Response, StdResult};
use cosmwasm_std::{CosmosMsg, CustomMsg, Empty, Response, StdResult};
use cw1::{CanExecuteResp, Cw1};
use serde::de::DeserializeOwned;
use serde::Deserialize;
Expand Down Expand Up @@ -57,6 +57,9 @@ where
FieldT: 'static,
{
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> Result<Response, Self::Error> {
Ok(Response::new())
Expand Down
28 changes: 18 additions & 10 deletions examples/interfaces/cw1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use serde::{Deserialize, Serialize};
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[derive(
Expand All @@ -11,35 +11,43 @@ pub struct CanExecuteResp {
}

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw1 {
type Error: From<StdError>;
type ExecC: CustomMsg;
type QueryC: CustomQuery;
type CosmosCustomMsg: CustomMsg;

/// Execute requests the contract to re-dispatch all these messages with the
/// contract's address as sender. Every implementation has it's own logic to
/// determine in
#[sv::msg(exec)]
fn execute(&self, ctx: ExecCtx, msgs: Vec<CosmosMsg>) -> Result<Response, Self::Error>;
fn execute(
&self,
ctx: ExecCtx<Self::QueryC>,
msgs: Vec<CosmosMsg<Self::CosmosCustomMsg>>,
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Checks permissions of the caller on this proxy.
/// If CanExecute returns true then a call to `Execute` with the same message,
/// from the given sender, before any further state changes, should also succeed.
#[sv::msg(query)]
fn can_execute(
&self,
ctx: QueryCtx,
ctx: QueryCtx<Self::QueryC>,
sender: String,
msg: CosmosMsg,
msg: CosmosMsg<Self::CosmosCustomMsg>,
) -> StdResult<CanExecuteResp>;
}

#[cfg(test)]
mod tests {
use cosmwasm_std::{coins, from_json, to_json_binary, BankMsg};
use cosmwasm_std::{coins, from_json, to_json_binary, BankMsg, Empty};

use crate::sv::{Cw1ExecMsg, Cw1QueryMsg};

#[test]
fn execute() {
let original = super::sv::ExecMsg::Execute {
let original: Cw1ExecMsg<Empty> = super::sv::ExecMsg::Execute {
msgs: vec![BankMsg::Send {
to_address: "receiver".to_owned(),
amount: coins(10, "atom"),
Expand All @@ -55,13 +63,13 @@ mod tests {

#[test]
fn execute_from_json() {
let deserialized = from_json(br#"{"execute": { "msgs": [] }}"#).unwrap();
let deserialized: Cw1ExecMsg<Empty> = from_json(br#"{"execute": { "msgs": [] }}"#).unwrap();
assert_eq!(super::sv::ExecMsg::Execute { msgs: vec![] }, deserialized);
}

#[test]
fn query() {
let original = super::sv::QueryMsg::CanExecute {
let original: Cw1QueryMsg<Empty> = super::sv::QueryMsg::CanExecute {
sender: "sender".to_owned(),
msg: BankMsg::Send {
to_address: "receiver".to_owned(),
Expand All @@ -78,7 +86,7 @@ mod tests {

#[test]
fn query_from_json() {
let deserialized = from_json(
let deserialized: Cw1QueryMsg<Empty> = from_json(
br#"{"can_execute": {
"sender": "address",
"msg": {
Expand Down
33 changes: 17 additions & 16 deletions examples/interfaces/cw20-allowances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,74 @@ use cw_utils::Expiration;
use responses::{
AllAccountsResponse, AllAllowancesResponse, AllSpenderAllowancesResponse, AllowanceResponse,
};
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw20Allowances {
type Error: From<StdError>;
type ExecC: CustomMsg;
type QueryC: CustomQuery;

/// Allows spender to access an additional amount tokens from the owner's (env.sender) account.
/// If expires is Some(), overwrites current allowance expiration with this one.
#[sv::msg(exec)]
fn increase_allowance(
&self,
ctx: ExecCtx,
ctx: ExecCtx<Self::QueryC>,
spender: String,
amount: Uint128,
expires: Option<Expiration>,
) -> Result<Response, Self::Error>;
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Lowers the spender's access of tokens from the owner's (env.sender) account by amount.
/// If expires is Some(), overwrites current allowance expiration with this one.
#[sv::msg(exec)]
fn decrease_allowance(
&self,
ctx: ExecCtx,
ctx: ExecCtx<Self::QueryC>,
spender: String,
amount: Uint128,
expires: Option<Expiration>,
) -> Result<Response, Self::Error>;
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Transfers amount tokens from owner -> recipient
/// if `env.sender` has sufficient pre-approval.
#[sv::msg(exec)]
fn transfer_from(
&self,
ctx: ExecCtx,
ctx: ExecCtx<Self::QueryC>,
owner: String,
recipient: String,
amount: Uint128,
) -> Result<Response, Self::Error>;
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Sends amount tokens from owner -> contract
/// if `env.sender` has sufficient pre-approval.
#[sv::msg(exec)]
fn send_from(
&self,
ctx: ExecCtx,
ctx: ExecCtx<Self::QueryC>,
owner: String,
contract: String,
amount: Uint128,
msg: Binary,
) -> Result<Response, Self::Error>;
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Destroys amount of tokens forever
#[sv::msg(exec)]
fn burn_from(
&self,
ctx: ExecCtx,
ctx: ExecCtx<Self::QueryC>,
owner: String,
amount: Uint128,
) -> Result<Response, Self::Error>;
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Returns how much spender can use from owner account, 0 if unset.
#[sv::msg(query)]
fn allowance(
&self,
ctx: QueryCtx,
ctx: QueryCtx<Self::QueryC>,
owner: String,
spender: String,
) -> StdResult<AllowanceResponse>;
Expand All @@ -80,7 +81,7 @@ pub trait Cw20Allowances {
#[sv::msg(query)]
fn all_allowances(
&self,
ctx: QueryCtx,
ctx: QueryCtx<Self::QueryC>,
owner: String,
start_after: Option<String>,
limit: Option<u32>,
Expand All @@ -90,7 +91,7 @@ pub trait Cw20Allowances {
#[sv::msg(query)]
fn all_spender_allowances(
&self,
ctx: QueryCtx,
ctx: QueryCtx<Self::QueryC>,
spender: String,
start_after: Option<String>,
limit: Option<u32>,
Expand All @@ -100,7 +101,7 @@ pub trait Cw20Allowances {
#[sv::msg(query)]
fn all_accounts(
&self,
ctx: QueryCtx,
ctx: QueryCtx<Self::QueryC>,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<AllAccountsResponse>;
Expand Down
Loading
Loading