-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instantiate2 create pair message
- Loading branch information
Showing
20 changed files
with
1,228 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use cosmwasm_std::Instantiate2AddressError; | ||
use cosmwasm_std::StdError; | ||
|
||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum ContractError { | ||
#[error("{0}")] | ||
Std(#[from] StdError), | ||
|
||
#[error("{0}")] | ||
Instantiate2AddressError(#[from] Instantiate2AddressError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::helpers::generate_salt; | ||
use crate::msg::ExecuteMsg; | ||
use crate::state::{INFINITY_GLOBAL, SENDER_COUNTER}; | ||
use crate::ContractError; | ||
|
||
use cosmwasm_std::{to_binary, DepsMut, Env, MessageInfo, WasmMsg}; | ||
use infinity_global::load_global_config; | ||
use infinity_pair::msg::InstantiateMsg as InfinityPairInstantiateMsg; | ||
use sg_std::Response; | ||
|
||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
deps: DepsMut, | ||
env: Env, | ||
info: MessageInfo, | ||
msg: ExecuteMsg, | ||
) -> Result<Response, ContractError> { | ||
match msg { | ||
ExecuteMsg::CreatePair { | ||
pair_immutable, | ||
pair_config, | ||
} => { | ||
let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; | ||
let global_config = load_global_config(&deps.querier, &infinity_global)?; | ||
|
||
let response = Response::new().add_message(WasmMsg::Instantiate { | ||
admin: Some(env.contract.address.into()), | ||
code_id: global_config.infinity_pair_code_id, | ||
label: "Infinity Pair".to_string(), | ||
msg: to_binary(&InfinityPairInstantiateMsg { | ||
infinity_global: infinity_global.to_string(), | ||
pair_immutable, | ||
pair_config, | ||
})?, | ||
funds: info.funds, | ||
}); | ||
|
||
Ok(response) | ||
}, | ||
ExecuteMsg::CreatePair2 { | ||
pair_immutable, | ||
pair_config, | ||
} => { | ||
let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; | ||
let global_config = load_global_config(&deps.querier, &infinity_global)?; | ||
|
||
let counter = | ||
SENDER_COUNTER.may_load(deps.storage, info.sender.clone())?.unwrap_or_default(); | ||
let salt = generate_salt(&info.sender, counter); | ||
SENDER_COUNTER.save(deps.storage, info.sender.clone(), &(counter + 1))?; | ||
|
||
let response = Response::new().add_message(WasmMsg::Instantiate2 { | ||
admin: Some(env.contract.address.into()), | ||
code_id: global_config.infinity_pair_code_id, | ||
label: "Infinity Pair".to_string(), | ||
msg: to_binary(&InfinityPairInstantiateMsg { | ||
infinity_global: infinity_global.to_string(), | ||
pair_immutable, | ||
pair_config, | ||
})?, | ||
funds: info.funds, | ||
salt, | ||
}); | ||
|
||
Ok(response) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::ContractError; | ||
|
||
use cosmwasm_std::{instantiate2_address, Addr, Binary, Deps, Env}; | ||
use sha2::{Digest, Sha256}; | ||
|
||
pub fn generate_salt(sender: &Addr, counter: u64) -> Binary { | ||
let mut hasher = Sha256::new(); | ||
hasher.update(sender.as_bytes()); | ||
hasher.update(&counter.to_be_bytes()); | ||
hasher.finalize().to_vec().into() | ||
} | ||
|
||
pub fn generate_instantiate_2_addr( | ||
deps: Deps, | ||
env: &Env, | ||
sender: &Addr, | ||
counter: u64, | ||
code_id: u64, | ||
) -> Result<(Addr, Binary), ContractError> { | ||
let code_res = deps.querier.query_wasm_code_info(code_id)?; | ||
|
||
let salt = generate_salt(&sender, counter); | ||
|
||
// predict the contract address | ||
let addr_raw = instantiate2_address( | ||
&code_res.checksum, | ||
&deps.api.addr_canonicalize(env.contract.address.as_str())?, | ||
&salt, | ||
)?; | ||
|
||
let addr = deps.api.addr_humanize(&addr_raw)?; | ||
|
||
Ok((addr, salt)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::msg::InstantiateMsg; | ||
use crate::state::INFINITY_GLOBAL; | ||
|
||
use cosmwasm_std::StdError; | ||
use cosmwasm_std::{DepsMut, Env, MessageInfo}; | ||
use cw2::set_contract_version; | ||
use sg_std::Response; | ||
|
||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
|
||
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); | ||
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
msg: InstantiateMsg, | ||
) -> Result<Response, StdError> { | ||
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | ||
|
||
INFINITY_GLOBAL.save(deps.storage, &deps.api.addr_validate(&msg.infinity_global)?)?; | ||
|
||
Ok(Response::new() | ||
.add_attribute("action", "instantiate") | ||
.add_attribute("contract_name", CONTRACT_NAME) | ||
.add_attribute("contract_version", CONTRACT_VERSION)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,10 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, StdResult}; | ||
use cosmwasm_std::{StdError, WasmMsg}; | ||
use cw2::set_contract_version; | ||
use cw_storage_plus::Item; | ||
use infinity_global::load_global_config; | ||
use infinity_pair::msg::InstantiateMsg as InfinityPairInstantiateMsg; | ||
use infinity_pair::state::{PairConfig, PairImmutable}; | ||
use sg_std::Response; | ||
pub mod execute; | ||
pub mod helpers; | ||
pub mod instantiate; | ||
pub mod msg; | ||
pub mod query; | ||
pub mod state; | ||
|
||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
mod error; | ||
|
||
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); | ||
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
pub const INFINITY_GLOBAL: Item<Addr> = Item::new("g"); | ||
|
||
#[cw_serde] | ||
pub struct InstantiateMsg { | ||
/// The address of the infinity global contract | ||
pub infinity_global: String, | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
msg: InstantiateMsg, | ||
) -> Result<Response, StdError> { | ||
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | ||
|
||
INFINITY_GLOBAL.save(deps.storage, &deps.api.addr_validate(&msg.infinity_global)?)?; | ||
|
||
Ok(Response::new() | ||
.add_attribute("action", "instantiate") | ||
.add_attribute("contract_name", CONTRACT_NAME) | ||
.add_attribute("contract_version", CONTRACT_VERSION)) | ||
} | ||
|
||
#[cw_serde] | ||
pub enum ExecuteMsg { | ||
CreatePair { | ||
/// The immutable parameters of the pair | ||
pair_immutable: PairImmutable<String>, | ||
/// The user configurable parameters of the pair | ||
pair_config: PairConfig<String>, | ||
}, | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
deps: DepsMut, | ||
env: Env, | ||
info: MessageInfo, | ||
msg: ExecuteMsg, | ||
) -> Result<Response, StdError> { | ||
match msg { | ||
ExecuteMsg::CreatePair { | ||
pair_immutable, | ||
pair_config, | ||
} => { | ||
let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; | ||
let global_config = load_global_config(&deps.querier, &infinity_global)?; | ||
|
||
let response = Response::new().add_message(WasmMsg::Instantiate { | ||
admin: Some(env.contract.address.into()), | ||
code_id: global_config.infinity_pair_code_id, | ||
label: "InfinityPair".to_string(), | ||
msg: to_binary(&InfinityPairInstantiateMsg { | ||
infinity_global: infinity_global.to_string(), | ||
pair_immutable, | ||
pair_config, | ||
})?, | ||
funds: info.funds, | ||
}); | ||
|
||
Ok(response) | ||
}, | ||
} | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult<Binary> { | ||
unimplemented!() | ||
} | ||
pub use crate::error::ContractError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use cosmwasm_schema::{cw_serde, QueryResponses}; | ||
use cosmwasm_std::{Addr, Binary}; | ||
use infinity_pair::state::{PairConfig, PairImmutable}; | ||
|
||
#[cw_serde] | ||
pub struct InstantiateMsg { | ||
/// The address of the infinity global contract | ||
pub infinity_global: String, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum ExecuteMsg { | ||
CreatePair { | ||
/// The immutable parameters of the pair | ||
pair_immutable: PairImmutable<String>, | ||
/// The user configurable parameters of the pair | ||
pair_config: PairConfig<String>, | ||
}, | ||
CreatePair2 { | ||
/// The immutable parameters of the pair | ||
pair_immutable: PairImmutable<String>, | ||
/// The user configurable parameters of the pair | ||
pair_config: PairConfig<String>, | ||
}, | ||
} | ||
|
||
#[cw_serde] | ||
pub struct NextPairResponse { | ||
pub sender: Addr, | ||
pub pair: Addr, | ||
pub salt: Binary, | ||
} | ||
|
||
#[cw_serde] | ||
#[derive(QueryResponses)] | ||
pub enum QueryMsg { | ||
#[returns(NextPairResponse)] | ||
NextPair { | ||
sender: String, | ||
}, | ||
} |
Oops, something went wrong.