Skip to content

Commit

Permalink
Merge pull request #3 from jpine3528/develop
Browse files Browse the repository at this point in the history
Add foundry asset
  • Loading branch information
taha-abbasi authored Oct 24, 2022
2 parents 32879cc + 81b371b commit d984c95
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 8 deletions.
118 changes: 112 additions & 6 deletions contracts/multiswap-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use cosmwasm_std::{
use cw_storage_plus::Bound;

use multiswap::{
AddLiquidityEvent, AddSignerEvent, BridgeSwapEvent, BridgeWithdrawSignedEvent, Liquidity,
MigrateMsg, MultiswapExecuteMsg, MultiswapQueryMsg, RemoveLiquidityEvent, RemoveSignerEvent,
TransferOwnershipEvent,
AddFoundryAssetEvent, AddLiquidityEvent, AddSignerEvent, BridgeSwapEvent,
BridgeWithdrawSignedEvent, Liquidity, MigrateMsg, MultiswapExecuteMsg, MultiswapQueryMsg,
RemoveFoundryAssetEvent, RemoveLiquidityEvent, RemoveSignerEvent, TransferOwnershipEvent,
};

use crate::error::{self, ContractError};
use crate::msg::InstantiateMsg;
use crate::state::{LIQUIDITIES, OWNER, SIGNERS};
use crate::state::{FOUNDRY_ASSETS, LIQUIDITIES, OWNER, SIGNERS};
use cw_utils::Event;
// use crate::state::{APPROVES, BALANCES, MINTER, TOKENS};

Expand Down Expand Up @@ -54,6 +54,10 @@ pub fn execute(
}
MultiswapExecuteMsg::AddSigner { signer } => execute_add_signer(env, signer),
MultiswapExecuteMsg::RemoveSigner { signer } => execute_remove_signer(env, signer),
MultiswapExecuteMsg::AddFoundryAsset { token } => execute_add_foundry_asset(env, token),
MultiswapExecuteMsg::RemoveFoundryAsset { token } => {
execute_remove_foundry_asset(env, token)
}
MultiswapExecuteMsg::AddLiquidity { token, amount } => {
execute_add_liquidity(env, token, amount)
}
Expand Down Expand Up @@ -150,6 +154,56 @@ pub fn execute_remove_signer(env: ExecuteEnv, signer: String) -> Result<Response
Ok(rsp)
}

pub fn execute_add_foundry_asset(
env: ExecuteEnv,
token: String,
) -> Result<Response, ContractError> {
let ExecuteEnv {
mut deps,
env,
info,
} = env;

if info.sender != OWNER.load(deps.storage)? {
return Err(ContractError::Unauthorized {});
}

let mut rsp = Response::default();
FOUNDRY_ASSETS.save(deps.storage, token.as_str(), &token.to_string())?;

let event = AddFoundryAssetEvent {
from: info.sender.as_str(),
token: token.as_str(),
};
event.add_attributes(&mut rsp);
Ok(rsp)
}

pub fn execute_remove_foundry_asset(
env: ExecuteEnv,
token: String,
) -> Result<Response, ContractError> {
let ExecuteEnv {
mut deps,
env,
info,
} = env;

if info.sender != OWNER.load(deps.storage)? {
return Err(ContractError::Unauthorized {});
}

let mut rsp = Response::default();
FOUNDRY_ASSETS.remove(deps.storage, token.as_str());

let event = RemoveFoundryAssetEvent {
from: info.sender.as_str(),
token: token.as_str(),
};
event.add_attributes(&mut rsp);
Ok(rsp)
}

pub fn execute_add_liquidity(
env: ExecuteEnv,
token: String,
Expand All @@ -161,6 +215,10 @@ pub fn execute_add_liquidity(
info,
} = env;

if !is_foundry_asset(deps.storage, token.to_string()) {
return Err(ContractError::Unauthorized {});
}

let mut rsp = Response::default();
LIQUIDITIES.update(
deps.storage,
Expand Down Expand Up @@ -201,6 +259,10 @@ pub fn execute_remove_liquidity(
info,
} = env;

if !is_foundry_asset(deps.storage, token.to_string()) {
return Err(ContractError::Unauthorized {});
}

let mut rsp = Response::default();
LIQUIDITIES.update(
deps.storage,
Expand Down Expand Up @@ -234,6 +296,10 @@ pub fn execute_withdraw_signed(
salt: String,
signature: String,
) -> Result<Response, ContractError> {
if !is_foundry_asset(env.deps.storage, token.to_string()) {
return Err(ContractError::Unauthorized {});
}

let payee_addr = env.deps.api.addr_validate(&payee)?;

// TODO: gets signer from params
Expand All @@ -243,7 +309,7 @@ pub fn execute_withdraw_signed(
// }

// TODO: ensure that the signer is registered on-chain
// if !k.IsSigner(ctx, signer.String()) {
// if is_signer(env.storage, signer.String()) {
// return types.ErrInvalidSigner(k.codespace)
// }

Expand Down Expand Up @@ -342,6 +408,7 @@ pub fn query(deps: Deps, _env: Env, msg: MultiswapQueryMsg) -> StdResult<Binary>
}
MultiswapQueryMsg::AllLiquidity {} => to_binary(&query_all_liquidity(deps)?),
MultiswapQueryMsg::Signers {} => to_binary(&query_signers(deps)?),
MultiswapQueryMsg::FoundryAssets {} => to_binary(&query_foundry_assets(deps)?),
}
}

Expand All @@ -361,10 +428,13 @@ pub fn query_all_liquidity(deps: Deps) -> StdResult<Vec<Liquidity>> {
}

pub fn query_signers(deps: Deps) -> StdResult<Vec<String>> {
// Ok(SIGNERS.may_load(deps.storage)?.unwrap_or_default())
Ok(read_signers(deps.storage, deps.api))
}

pub fn query_foundry_assets(deps: Deps) -> StdResult<Vec<String>> {
Ok(read_foundry_assets(deps.storage, deps.api))
}

const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 10;
pub fn read_liquidities(
Expand Down Expand Up @@ -421,6 +491,42 @@ pub fn read_signers(
.collect::<Vec<String>>();
}

pub fn is_signer(storage: &dyn Storage, signer: String) -> bool {
if let Ok(Some(_)) = SIGNERS.may_load(storage, signer.as_str()) {
return true;
}
return false;
}

pub fn read_foundry_assets(
storage: &dyn Storage,
api: &dyn Api,
// start_after: Option<(String, String)>,
// limit: Option<u32>,
) -> Vec<String> {
let limit = DEFAULT_LIMIT as usize;
// let start = calc_range_start(start_after);
// let start_key = start.map(Bound::exclusive);

return FOUNDRY_ASSETS
.range(storage, None, None, Order::Ascending)
.take(limit)
.map(|item| {
if let Ok((_, it)) = item {
return it;
}
return "".to_string();
})
.collect::<Vec<String>>();
}

pub fn is_foundry_asset(storage: &dyn Storage, foundry_asset: String) -> bool {
if let Ok(Some(_)) = FOUNDRY_ASSETS.may_load(storage, foundry_asset.as_str()) {
return true;
}
return false;
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
Ok(Response::default())
Expand Down
2 changes: 2 additions & 0 deletions contracts/multiswap-base/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub const OWNER: Item<Addr> = Item::new("owner");
pub const LIQUIDITIES: Map<(&str, &Addr), Liquidity> = Map::new("liquidities");
/// Store signers.
pub const SIGNERS: Map<&str, String> = Map::new("signers");
/// Store foundry assets.
pub const FOUNDRY_ASSETS: Map<&str, String> = Map::new("foundry_assets");
28 changes: 28 additions & 0 deletions packages/multiswap/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ impl<'a> Event for RemoveSignerEvent<'a> {
}
}

/// Tracks foundry asset additions
pub struct AddFoundryAssetEvent<'a> {
pub from: &'a str,
pub token: &'a str,
}

impl<'a> Event for AddFoundryAssetEvent<'a> {
fn add_attributes(&self, rsp: &mut Response) {
rsp.attributes.push(attr("action", "add_foundry_asset"));
rsp.attributes.push(attr("token", self.token));
rsp.attributes.push(attr("from", self.from));
}
}

/// Tracks foundry asset removals
pub struct RemoveFoundryAssetEvent<'a> {
pub from: &'a str,
pub token: &'a str,
}

impl<'a> Event for RemoveFoundryAssetEvent<'a> {
fn add_attributes(&self, rsp: &mut Response) {
rsp.attributes.push(attr("action", "remove_foundry_asset"));
rsp.attributes.push(attr("token", self.token));
rsp.attributes.push(attr("from", self.from));
}
}

/// Tracks liquidity additions
pub struct AddLiquidityEvent<'a> {
pub from: &'a str,
Expand Down
5 changes: 3 additions & 2 deletions packages/multiswap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use crate::event::{
AddLiquidityEvent, AddSignerEvent, BridgeSwapEvent, BridgeWithdrawSignedEvent,
RemoveLiquidityEvent, RemoveSignerEvent, TransferOwnershipEvent,
AddFoundryAssetEvent, AddLiquidityEvent, AddSignerEvent, BridgeSwapEvent,
BridgeWithdrawSignedEvent, RemoveFoundryAssetEvent, RemoveLiquidityEvent, RemoveSignerEvent,
TransferOwnershipEvent,
};
pub use crate::msg::{MigrateMsg, MultiswapExecuteMsg};
pub use crate::query::{Liquidity, MultiswapQueryMsg};
Expand Down
6 changes: 6 additions & 0 deletions packages/multiswap/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub enum MultiswapExecuteMsg {
RemoveSigner {
signer: String,
},
AddFoundryAsset {
token: String,
},
RemoveFoundryAsset {
token: String,
},
AddLiquidity {
token: String,
amount: Uint128,
Expand Down
1 change: 1 addition & 0 deletions packages/multiswap/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum MultiswapQueryMsg {
Liquidity { owner: String, token: String },
AllLiquidity {},
Signers {},
FoundryAssets {},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
Expand Down

0 comments on commit d984c95

Please sign in to comment.