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

Feature/token overhaul #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added a wasm exec helper function and implemented allowance functions…
… for cw20
FloppyDisck committed Oct 29, 2024
commit 5d89e197e736d071f23f91c7417f6f7c26b95885
138 changes: 111 additions & 27 deletions src/fungible/cw20.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{AttributeBuilder, Fungible};
use cosmwasm_std::{to_json_binary, Addr, Attribute, CosmosMsg, Deps, StdResult, Uint128, WasmMsg};
use crate::{execute_wasm, AttributeBuilder, Fungible};
use cosmwasm_std::{
to_json_binary, Addr, Attribute, Binary, CosmosMsg, Deps, StdResult, Uint128, WasmMsg,
};
use cw721::Expiration;
use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

@@ -13,44 +16,125 @@ impl Cw20Token {
Self { address }
}

pub fn transfer_from(
/// Send transaction that uses the cw20 Send smart contract interface
pub fn contract_send(
&self,
owner: &Addr,
recipient: &Addr,
amount: Uint128,
contract: impl Into<String>,
amount: impl Into<Uint128>,
msg: Binary,
) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.address.to_string(),
msg: to_json_binary(&cw20_base::msg::ExecuteMsg::TransferFrom {
owner: owner.to_string(),
recipient: recipient.to_string(),
amount,
})?,
funds: vec![],
}))
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::Send {
contract: contract.into(),
msg,
amount: amount.into(),
},
)
}

/// Send from transaction that uses the cw20 Send smart contract interface
pub fn contract_send_from(
&self,
owner: impl Into<String>,
contract: impl Into<String>,
amount: impl Into<Uint128>,
msg: Binary,
) -> StdResult<CosmosMsg> {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::SendFrom {
owner: owner.into(),
contract: contract.into(),
msg,
amount: amount.into(),
},
)
}

/// Standard send from another owner's address
pub fn send_from(
&self,
owner: impl Into<String>,
recipient: impl Into<String>,
amount: impl Into<Uint128>,
) -> StdResult<CosmosMsg> {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::TransferFrom {
owner: owner.into(),
recipient: recipient.into(),
amount: amount.into(),
},
)
}

/// Burn from another owner's address
pub fn burn_from(
&self,
owner: impl Into<String>,
amount: impl Into<Uint128>,
) -> StdResult<CosmosMsg> {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::BurnFrom {
owner: owner.into(),
amount: amount.into(),
},
)
}

pub fn increase_allowance(
&self,
spender: impl Into<String>,
amount: impl Into<Uint128>,
expires: Option<Expiration>,
) -> StdResult<CosmosMsg> {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::IncreaseAllowance {
spender: spender.into(),
amount: amount.into(),
expires,
},
)
}

pub fn decrease_allowance(
&self,
spender: impl Into<String>,
amount: impl Into<Uint128>,
expires: Option<Expiration>,
) -> StdResult<CosmosMsg> {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::DecreaseAllowance {
spender: spender.into(),
amount: amount.into(),
expires,
},
)
}
}

impl Fungible for Cw20Token {
fn send(&self, target: impl Into<String>, amount: impl Into<Uint128>) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.address.to_string(),
msg: to_json_binary(&cw20_base::msg::ExecuteMsg::Transfer {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::Transfer {
recipient: target.into(),
amount: amount.into(),
})?,
funds: vec![],
}))
},
)
}

fn burn(&self, amount: impl Into<Uint128>) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.address.to_string(),
msg: to_json_binary(&cw20_base::msg::ExecuteMsg::Burn {
execute_wasm(
&self.address,
&cw20_base::msg::ExecuteMsg::Burn {
amount: amount.into(),
})?,
funds: vec![],
}))
},
)
}

fn balance(&self, address: impl Into<String>, deps: Deps) -> StdResult<Uint128> {
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod fungible;
pub mod non_fungible;

use cosmwasm_std::{to_json_string, Attribute, Response, StdResult};
use cosmwasm_std::{
to_json_binary, to_json_string, Attribute, CosmosMsg, Response, StdResult, WasmMsg,
};
pub use fungible::*;
pub use non_fungible::*;
use serde::Serialize;

// TODO: implement custom JsonSchema

// TODO: implement batch send for native token
// TODO: implement Auth for native
// TODO: implement Perms for Cw20
@@ -16,6 +16,17 @@ use serde::Serialize;

pub type TokenKey = (u8, String);

pub(crate) fn execute_wasm<T: Serialize + ?Sized>(
contract: impl Into<String>,
msg: &T,
) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract.into(),
msg: to_json_binary(msg)?,
funds: vec![],
}))
}

pub trait AttributeBuilder: Serialize {
fn write_json_attributes(&self, key: &str, resp: &mut Response) -> StdResult<()> {
resp.attributes.push(self.json_attributes(key)?);
24 changes: 11 additions & 13 deletions src/non_fungible/cw721.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::non_fungible::NonFungible;
use crate::AttributeBuilder;
use crate::{execute_wasm, AttributeBuilder};
use cosmwasm_std::{to_json_binary, Addr, Attribute, CosmosMsg, Deps, StdResult, WasmMsg};
use cw721::{NumTokensResponse, OwnerOfResponse, TokensResponse};
use schemars::JsonSchema;
@@ -18,24 +18,22 @@ impl Cw721Token {

impl NonFungible for Cw721Token {
fn send(&self, target: impl Into<String>, token: impl Into<String>) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.address.to_string(),
msg: to_json_binary(&cw721::Cw721ExecuteMsg::TransferNft {
execute_wasm(
&self.address,
&cw721::Cw721ExecuteMsg::TransferNft {
recipient: target.into(),
token_id: token.into(),
})?,
funds: vec![],
}))
},
)
}

fn burn(&self, token: impl Into<String>) -> StdResult<CosmosMsg> {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.address.to_string(),
msg: to_json_binary(&cw721::Cw721ExecuteMsg::Burn {
execute_wasm(
&self.address,
&cw721::Cw721ExecuteMsg::Burn {
token_id: token.into(),
})?,
funds: vec![],
}))
},
)
}

fn owner_of(&self, token: impl Into<String>, deps: Deps) -> StdResult<OwnerOfResponse> {