|
| 1 | +const CONTRACT_NAME: &str = "eris-ginkou-usdc-proxy"; |
| 2 | +const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + error::{ContractError, ContractResult, CustomResult}, |
| 6 | + ginkou::Ginkou, |
| 7 | + msg::{CallbackMsg, Config, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, ReceiveMsg}, |
| 8 | + state::CONFIG, |
| 9 | +}; |
| 10 | +use astroport::asset::{native_asset_info, token_asset_info, AssetInfoExt, PairInfo}; |
| 11 | + |
| 12 | +#[cfg(not(feature = "library"))] |
| 13 | +use cosmwasm_std::entry_point; |
| 14 | +use cosmwasm_std::{ |
| 15 | + from_binary, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, |
| 16 | + StdResult, Storage, WasmMsg, |
| 17 | +}; |
| 18 | +use cw2::set_contract_version; |
| 19 | +use cw20::Cw20ReceiveMsg; |
| 20 | + |
| 21 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 22 | +pub fn instantiate( |
| 23 | + deps: DepsMut, |
| 24 | + _env: Env, |
| 25 | + _info: MessageInfo, |
| 26 | + msg: InstantiateMsg, |
| 27 | +) -> ContractResult { |
| 28 | + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; |
| 29 | + |
| 30 | + CONFIG.save( |
| 31 | + deps.storage, |
| 32 | + &Config { |
| 33 | + ginkou: Ginkou { |
| 34 | + contract: deps.api.addr_validate(&msg.ginkou)?, |
| 35 | + usdc_denom: msg.usdc_denom, |
| 36 | + musdc_addr: deps.api.addr_validate(&msg.musdc_addr)?, |
| 37 | + }, |
| 38 | + }, |
| 39 | + )?; |
| 40 | + Ok(Response::default()) |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 44 | +pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> ContractResult { |
| 45 | + match msg { |
| 46 | + ExecuteMsg::Receive(cw20_msg) => receive(deps, env, info, cw20_msg), |
| 47 | + ExecuteMsg::Swap { |
| 48 | + offer_asset, |
| 49 | + to, |
| 50 | + .. |
| 51 | + } => { |
| 52 | + offer_asset.assert_sent_native_token_balance(&info)?; |
| 53 | + let config = CONFIG.load(deps.storage)?; |
| 54 | + |
| 55 | + if offer_asset.info != native_asset_info(config.ginkou.usdc_denom.clone()) { |
| 56 | + return Err(ContractError::ExpectingUsdcToken(offer_asset.info.to_string())); |
| 57 | + } |
| 58 | + |
| 59 | + Ok(Response::new() |
| 60 | + .add_attribute("action", "virtual-swap") |
| 61 | + .add_message(config.ginkou.deposit_usdc(offer_asset.amount.u128())?) |
| 62 | + .add_message(send_to( |
| 63 | + deps, |
| 64 | + &env, |
| 65 | + to.unwrap_or(info.sender.to_string()), |
| 66 | + token_asset_info(config.ginkou.musdc_addr), |
| 67 | + )?)) |
| 68 | + }, |
| 69 | + ExecuteMsg::Callback(callback_msg) => callback(deps, env, info, callback_msg), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn receive(deps: DepsMut, env: Env, info: MessageInfo, cw20_msg: Cw20ReceiveMsg) -> ContractResult { |
| 74 | + match from_binary(&cw20_msg.msg)? { |
| 75 | + ReceiveMsg::Swap { |
| 76 | + to, |
| 77 | + .. |
| 78 | + } => { |
| 79 | + let config = CONFIG.load(deps.storage)?; |
| 80 | + |
| 81 | + if info.sender != config.ginkou.musdc_addr { |
| 82 | + return Err(ContractError::ExpectingMusdcToken(info.sender.into())); |
| 83 | + } |
| 84 | + |
| 85 | + Ok(Response::new() |
| 86 | + .add_attribute("action", "virtual-swap_receive") |
| 87 | + .add_message(config.ginkou.withdraw_msg(cw20_msg.amount)?) |
| 88 | + .add_message(send_to( |
| 89 | + deps, |
| 90 | + &env, |
| 91 | + to.unwrap_or(info.sender.to_string()), |
| 92 | + native_asset_info(config.ginkou.usdc_denom), |
| 93 | + )?)) |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn send_to( |
| 99 | + deps: DepsMut, |
| 100 | + env: &Env, |
| 101 | + to: String, |
| 102 | + asset_info: astroport::asset::AssetInfo, |
| 103 | +) -> CustomResult<CosmosMsg> { |
| 104 | + let receiver = deps.api.addr_validate(&to)?; |
| 105 | + Ok(CosmosMsg::Wasm(WasmMsg::Execute { |
| 106 | + contract_addr: env.contract.address.to_string(), |
| 107 | + msg: to_binary(&ExecuteMsg::Callback(CallbackMsg::SendTo { |
| 108 | + to: receiver, |
| 109 | + asset_info, |
| 110 | + }))?, |
| 111 | + funds: vec![], |
| 112 | + })) |
| 113 | +} |
| 114 | + |
| 115 | +fn callback( |
| 116 | + deps: DepsMut, |
| 117 | + env: Env, |
| 118 | + info: MessageInfo, |
| 119 | + callback_msg: CallbackMsg, |
| 120 | +) -> ContractResult { |
| 121 | + if env.contract.address != info.sender { |
| 122 | + return Err(ContractError::CallbackOnlyCalledByContract {}); |
| 123 | + } |
| 124 | + |
| 125 | + match callback_msg { |
| 126 | + CallbackMsg::SendTo { |
| 127 | + to: receiver, |
| 128 | + asset_info: token, |
| 129 | + } => { |
| 130 | + let amount = token.query_pool(&deps.querier, env.contract.address)?; |
| 131 | + let send = token.with_balance(amount).into_msg(receiver)?; |
| 132 | + |
| 133 | + Ok(Response::new().add_attribute("action", "callback-send_to").add_message(send)) |
| 134 | + }, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 139 | +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> { |
| 140 | + match msg { |
| 141 | + QueryMsg::Config {} => to_binary(&get_config(deps.storage)?), |
| 142 | + QueryMsg::Pair {} => to_binary(&query_pair(deps, env)?), |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +pub fn query_pair(deps: Deps, env: Env) -> StdResult<PairInfo> { |
| 147 | + let config = CONFIG.load(deps.storage)?; |
| 148 | + |
| 149 | + Ok(PairInfo { |
| 150 | + asset_infos: vec![ |
| 151 | + native_asset_info(config.ginkou.usdc_denom), |
| 152 | + token_asset_info(config.ginkou.musdc_addr), |
| 153 | + ], |
| 154 | + contract_addr: env.contract.address.clone(), |
| 155 | + liquidity_token: env.contract.address, |
| 156 | + pair_type: astroport::factory::PairType::Custom("virtual".to_string()), |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +fn get_config(store: &dyn Storage) -> StdResult<Config> { |
| 161 | + CONFIG.load(store) |
| 162 | +} |
| 163 | + |
| 164 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 165 | +pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> ContractResult { |
| 166 | + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; |
| 167 | + |
| 168 | + Ok(Response::new() |
| 169 | + .add_attribute("new_contract_name", CONTRACT_NAME) |
| 170 | + .add_attribute("new_contract_version", CONTRACT_VERSION)) |
| 171 | +} |
0 commit comments