Skip to content

Commit

Permalink
remove NeutronMsg and use protos (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyleu authored Oct 15, 2024
1 parent d756db2 commit 3e8eb90
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ cw-storage-plus = "2.0.0"
cw2 = "2.0.0"
getset = "0.1.3"
itertools = "0.13.0"
neutron-sdk = "0.11.0"
schemars = "0.8.16"
serde = { version = "1.0.207", default-features = false, features = ["derive"] }
serde_json = "1.0.125"
sha2 = "0.10.8"
thiserror = "1.0.63"
# TODO: replace neutron-sdk and cosmos-sdk-proto for neutron-std when we can test with 2.0 (neutron-std adds cosmwasm_2_0 feature)
neutron-sdk = "0.11.0"
cosmos-sdk-proto = { version = "0.20.0", default-features = false }
schemars = "0.8.16"
serde = { version = "1.0.207", default-features = false, features = ["derive"] }
serde_json = "1.0.125"
sha2 = "0.10.8"
thiserror = "1.0.63"

# our contracts
valence-authorization = { path = "contracts/authorization", features = ["library"] }
Expand Down
3 changes: 2 additions & 1 deletion contracts/authorization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ cw-ownable = { workspace = true }
valence-authorization-utils = { workspace = true }
valence-processor-utils = { workspace = true }
valence-polytone-utils = { workspace = true }
neutron-sdk = { workspace = true }
cw-utils = { workspace = true }
serde_json = { workspace = true }
neutron-sdk = { workspace = true }
cosmos-sdk-proto = { workspace = true }

[dev-dependencies]
neutron-test-tube = { workspace = true }
Expand Down
133 changes: 89 additions & 44 deletions contracts/authorization/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cosmos_sdk_proto::{cosmos::base::v1beta1::Coin, traits::MessageExt};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
coins, from_json, to_json_binary, Addr, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Empty, Env,
MessageInfo, Order, Response, StdResult, Storage, Uint128, Uint64, WasmMsg,
MessageInfo, Order, Response, StdResult, Storage, Uint64, WasmMsg,
};
use cw_ownable::{assert_owner, get_ownership, initialize_owner, is_owner};
use cw_storage_plus::Bound;
use cw_utils::Expiration;
use neutron_sdk::bindings::msg::NeutronMsg;
use neutron_sdk::proto_types::osmosis::tokenfactory::v1beta1::{MsgBurn, MsgCreateDenom, MsgMint};
use valence_authorization_utils::{
authorization::{
Authorization, AuthorizationInfo, AuthorizationMode, AuthorizationState, PermissionType,
Expand Down Expand Up @@ -80,7 +81,7 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::UpdateOwnership(action) => update_ownership(deps, env, info, action),
ExecuteMsg::OwnerAction(owner_msg) => {
Expand Down Expand Up @@ -165,7 +166,7 @@ fn update_ownership(
env: Env,
info: MessageInfo,
action: cw_ownable::Action,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
if let cw_ownable::Action::TransferOwnership { new_owner, .. } = &action {
if FIRST_OWNERSHIP.load(deps.storage)? {
assert_owner(deps.storage, &info.sender)?;
Expand All @@ -179,18 +180,15 @@ fn update_ownership(
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

fn add_sub_owner(deps: DepsMut, sub_owner: String) -> Result<Response<NeutronMsg>, ContractError> {
fn add_sub_owner(deps: DepsMut, sub_owner: String) -> Result<Response, ContractError> {
SUB_OWNERS.save(deps.storage, deps.api.addr_validate(&sub_owner)?, &Empty {})?;

Ok(Response::new()
.add_attribute("action", "add_sub_owner")
.add_attribute("sub_owner", sub_owner))
}

fn remove_sub_owner(
deps: DepsMut,
sub_owner: String,
) -> Result<Response<NeutronMsg>, ContractError> {
fn remove_sub_owner(deps: DepsMut, sub_owner: String) -> Result<Response, ContractError> {
SUB_OWNERS.remove(deps.storage, deps.api.addr_validate(&sub_owner)?);

Ok(Response::new()
Expand All @@ -202,7 +200,7 @@ fn add_external_domains(
mut deps: DepsMut,
env: Env,
external_domains: Vec<ExternalDomainInfo>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let mut messages = vec![];
// Save all external domains
for domain in external_domains {
Expand All @@ -222,7 +220,7 @@ fn create_authorizations(
deps: DepsMut,
env: Env,
authorizations: Vec<AuthorizationInfo>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let mut tokenfactory_msgs = vec![];

for each_authorization in authorizations {
Expand All @@ -242,8 +240,10 @@ fn create_authorizations(
// execute the authorization
if let AuthorizationMode::Permissioned(permission_type) = &authorization.mode {
// We will always create the token if it's permissioned
let create_token_msg = NeutronMsg::submit_create_denom(authorization.label.clone());
tokenfactory_msgs.push(create_token_msg);
tokenfactory_msgs.push(create_denom_msg(
env.contract.address.to_string(),
authorization.label.clone(),
));

// Full denom of the token that will be created
let denom =
Expand All @@ -254,15 +254,25 @@ fn create_authorizations(
PermissionType::WithCallLimit(call_limits) => {
for (addr, limit) in call_limits {
deps.api.addr_validate(addr.as_str())?;
let mint_msg = NeutronMsg::submit_mint_tokens(&denom, *limit, addr);
let mint_msg = mint_msg(
env.contract.address.to_string(),
addr.to_string(),
limit.u128(),
denom.clone(),
);
tokenfactory_msgs.push(mint_msg);
}
}
// If it has no call limit we will mint 1 token for each address which will be used to verify if they can execute the authorization via a query
PermissionType::WithoutCallLimit(addrs) => {
for addr in addrs {
deps.api.addr_validate(addr.as_str())?;
let mint_msg = NeutronMsg::submit_mint_tokens(&denom, Uint128::one(), addr);
let mint_msg = mint_msg(
env.contract.address.to_string(),
addr.to_string(),
1,
denom.clone(),
);
tokenfactory_msgs.push(mint_msg);
}
}
Expand All @@ -285,7 +295,7 @@ fn modify_authorization(
expiration: Option<Expiration>,
max_concurrent_executions: Option<u64>,
priority: Option<Priority>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let mut authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
Expand Down Expand Up @@ -314,10 +324,7 @@ fn modify_authorization(
Ok(Response::new().add_attribute("action", "modify_authorization"))
}

fn disable_authorization(
deps: DepsMut,
label: String,
) -> Result<Response<NeutronMsg>, ContractError> {
fn disable_authorization(deps: DepsMut, label: String) -> Result<Response, ContractError> {
let mut authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
Expand All @@ -331,10 +338,7 @@ fn disable_authorization(
Ok(Response::new().add_attribute("action", "disable_authorization"))
}

fn enable_authorization(
deps: DepsMut,
label: String,
) -> Result<Response<NeutronMsg>, ContractError> {
fn enable_authorization(deps: DepsMut, label: String) -> Result<Response, ContractError> {
let mut authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
Expand All @@ -353,19 +357,20 @@ fn mint_authorizations(
env: Env,
label: String,
mints: Vec<Mint>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
ContractError::Authorization(AuthorizationErrorReason::DoesNotExist(label.clone()))
})?;

let token_factory_msgs = match authorization.mode {
let tokenfactory_msgs = match authorization.mode {
AuthorizationMode::Permissioned(_) => Ok(mints.iter().map(|mint| {
NeutronMsg::submit_mint_tokens(
build_tokenfactory_denom(env.contract.address.as_str(), &label),
mint.amount,
mint_msg(
env.contract.address.to_string(),
mint.address.clone(),
mint.amount.u128(),
build_tokenfactory_denom(env.contract.address.as_str(), &label),
)
})),
AuthorizationMode::Permissionless => Err(ContractError::Authorization(
Expand All @@ -375,10 +380,10 @@ fn mint_authorizations(

Ok(Response::new()
.add_attribute("action", "mint_authorizations")
.add_messages(token_factory_msgs))
.add_messages(tokenfactory_msgs))
}

fn pause_processor(deps: DepsMut, domain: Domain) -> Result<Response<NeutronMsg>, ContractError> {
fn pause_processor(deps: DepsMut, domain: Domain) -> Result<Response, ContractError> {
let execute_msg_binary = to_json_binary(&ProcessorExecuteMsg::AuthorizationModuleAction(
AuthorizationMsg::Pause {},
))?;
Expand All @@ -390,7 +395,7 @@ fn pause_processor(deps: DepsMut, domain: Domain) -> Result<Response<NeutronMsg>
.add_attribute("action", "pause_processor"))
}

fn resume_processor(deps: DepsMut, domain: Domain) -> Result<Response<NeutronMsg>, ContractError> {
fn resume_processor(deps: DepsMut, domain: Domain) -> Result<Response, ContractError> {
let execute_msg_binary = to_json_binary(&ProcessorExecuteMsg::AuthorizationModuleAction(
AuthorizationMsg::Resume {},
))?;
Expand All @@ -409,7 +414,7 @@ fn insert_messages(
queue_position: u64,
priority: Priority,
messages: Vec<ProcessorMessage>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
Expand Down Expand Up @@ -474,7 +479,7 @@ fn evict_messages(
domain: Domain,
queue_position: u64,
priority: Priority,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let execute_msg_binary = to_json_binary(&ProcessorExecuteMsg::AuthorizationModuleAction(
AuthorizationMsg::EvictMsgs {
queue_position,
Expand All @@ -495,7 +500,7 @@ fn send_msgs(
label: String,
ttl: Option<Expiration>,
messages: Vec<ProcessorMessage>,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let authorization = AUTHORIZATIONS
.load(deps.storage, label.clone())
.map_err(|_| {
Expand Down Expand Up @@ -570,11 +575,7 @@ fn send_msgs(
.add_attribute("authorization_label", authorization.label))
}

fn retry_msgs(
deps: DepsMut,
env: Env,
execution_id: u64,
) -> Result<Response<NeutronMsg>, ContractError> {
fn retry_msgs(deps: DepsMut, env: Env, execution_id: u64) -> Result<Response, ContractError> {
let mut callback_info = PROCESSOR_CALLBACKS
.load(deps.storage, execution_id)
.map_err(|_| ContractError::ExecutionIDNotFound { execution_id })?;
Expand Down Expand Up @@ -661,7 +662,7 @@ fn retry_bridge_creation(
deps: DepsMut,
env: Env,
domain_name: String,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let mut external_domain = EXTERNAL_DOMAINS.load(deps.storage, domain_name.clone())?;

let msg = match external_domain.connector {
Expand Down Expand Up @@ -710,7 +711,7 @@ fn process_processor_callback(
info: MessageInfo,
execution_id: u64,
execution_result: ExecutionResult,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
let mut callback = PROCESSOR_CALLBACKS.load(deps.storage, execution_id)?;

// Check that the sender is the one that should send the callback
Expand Down Expand Up @@ -751,7 +752,7 @@ fn process_processor_callback(
let msg = match callback.execution_result {
ExecutionResult::Success | ExecutionResult::PartiallyExecuted(_, _) => {
// If the operation was executed or partially executed, the token will be burned
NeutronMsg::submit_burn_tokens(denom, Uint128::one()).into()
burn_msg(env.contract.address.to_string(), 1, denom)
}
_ => {
// Otherwise, the tokens will be sent back
Expand All @@ -777,7 +778,7 @@ fn process_polytone_callback(
env: Env,
info: MessageInfo,
callback_msg: CallbackMessage,
) -> Result<Response<NeutronMsg>, ContractError> {
) -> Result<Response, ContractError> {
// We will only process callbacks from messages initiated by the authorization contract
if callback_msg.initiator != env.contract.address {
return Err(ContractError::Unauthorized(
Expand Down Expand Up @@ -1104,3 +1105,47 @@ pub fn store_inprocess_callback(

Ok(())
}

fn create_denom_msg(sender: String, subdenom: String) -> CosmosMsg {
let msg_create_denom = MsgCreateDenom { sender, subdenom };
// TODO: Change to AnyMsg instead of Stargate when we can test with CW 2.0 (They are the same, just a rename)
#[allow(deprecated)]
CosmosMsg::Stargate {
type_url: "/osmosis.tokenfactory.v1beta1.MsgCreateDenom".to_string(),
value: Binary::from(msg_create_denom.to_bytes().unwrap()),
}
}

fn mint_msg(sender: String, recipient: String, amount: u128, denom: String) -> CosmosMsg {
let msg_mint = MsgMint {
sender,
amount: Some(Coin {
denom,
amount: amount.to_string(),
}),
mint_to_address: recipient,
};
// TODO: Change to AnyMsg instead of Stargate when we can test with CW 2.0 (They are the same, just a rename)
#[allow(deprecated)]
CosmosMsg::Stargate {
type_url: "/osmosis.tokenfactory.v1beta1.MsgMint".to_string(),
value: Binary::from(msg_mint.to_bytes().unwrap()),
}
}

fn burn_msg(sender: String, amount: u128, denom: String) -> CosmosMsg {
let msg_burn = MsgBurn {
sender,
amount: Some(Coin {
denom,
amount: amount.to_string(),
}),
burn_from_address: "".to_string(),
};
// TODO: Change to AnyMsg instead of Stargate when we can test with CW 2.0 (They are the same, just a rename)
#[allow(deprecated)]
CosmosMsg::Stargate {
type_url: "/osmosis.tokenfactory.v1beta1.MsgBurn".to_string(),
value: Binary::from(msg_burn.to_bytes().unwrap()),
}
}
5 changes: 2 additions & 3 deletions contracts/authorization/src/domain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cosmwasm_std::{to_json_binary, Binary, CosmosMsg, DepsMut, Storage, Uint64, WasmMsg};
use neutron_sdk::bindings::msg::NeutronMsg;
use valence_authorization_utils::{
authorization::{ActionsConfig, Authorization},
callback::PolytoneCallbackMsg,
Expand All @@ -18,7 +17,7 @@ pub fn add_domain(
deps: DepsMut,
callback_receiver: String,
domain: &ExternalDomainInfo,
) -> Result<CosmosMsg<NeutronMsg>, ContractError> {
) -> Result<CosmosMsg, ContractError> {
let external_domain = domain.to_external_domain_validated(deps.api)?;

if EXTERNAL_DOMAINS.has(deps.storage, external_domain.name.clone()) {
Expand Down Expand Up @@ -78,7 +77,7 @@ pub fn create_msg_for_processor_or_bridge(
execute_msg: Binary,
domain: &Domain,
callback_request: Option<CallbackRequest>,
) -> Result<CosmosMsg<NeutronMsg>, ContractError> {
) -> Result<CosmosMsg, ContractError> {
// If the domain is the main domain we will use the processor on the main domain, otherwise we will use polytone to send it to the processor on the external domain
match domain {
Domain::Main => {
Expand Down
Loading

0 comments on commit 3e8eb90

Please sign in to comment.