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

tg-valset: Returning created rewards contract address #261

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion contracts/tgrade-valset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ protobuf = { version = "2", features = ["with-bytes"] }

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0-beta" }
cw-multi-test = { version = "0.10.0" }
cw-multi-test = "0.10.0"
tg4-engagement = { path = "../tg4-engagement", version = "0.5.0-alpha" }
tg4-stake = { path = "../tg4-stake", version = "0.5.0-alpha" }
# we enable multitest feature only for tests
tg-bindings-test = { path = "../../packages/bindings-test", version = "0.5.0-alpha" }
derivative = "2"
anyhow = "1"
assert_matches = "1.5"

27 changes: 20 additions & 7 deletions contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tg_utils::Duration;

use crate::error::ContractError;
use crate::msg::{
ConfigResponse, EpochResponse, ExecuteMsg, InstantiateMsg, JailingPeriod,
ConfigResponse, EpochResponse, ExecuteMsg, InstantiateMsg, InstantiateResponse, JailingPeriod,
ListActiveValidatorsResponse, ListValidatorResponse, OperatorResponse, QueryMsg,
RewardsDistribution, RewardsInstantiateMsg, ValidatorMetadata, ValidatorResponse,
};
Expand Down Expand Up @@ -603,14 +603,18 @@ fn calculate_diff(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
REWARDS_INIT_REPLY_ID => rewards_instantiate_reply(deps, msg),
REWARDS_INIT_REPLY_ID => rewards_instantiate_reply(deps, env, msg),
_ => Err(ContractError::UnrecognisedReply(msg.id)),
}
}

pub fn rewards_instantiate_reply(deps: DepsMut, msg: Reply) -> Result<Response, ContractError> {
pub fn rewards_instantiate_reply(
deps: DepsMut,
env: Env,
msg: Reply,
) -> Result<Response, ContractError> {
let id = msg.id;
let res: MsgInstantiateContractResponse = Message::parse_from_bytes(
msg.result
Expand All @@ -634,9 +638,18 @@ pub fn rewards_instantiate_reply(deps: DepsMut, msg: Reply) -> Result<Response,
Ok(config)
})?;

let resp = Response::new()
.add_attribute("action", "tgrade-valset_instantiation")
.add_attribute("rewards_contract", res.get_contract_address());
let response = InstantiateResponse {
rewards_contract: Addr::unchecked(res.get_contract_address()),
};

let mut resp = MsgInstantiateContractResponse::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you don't need to add this info. This is added by x/wasm. Just encode your contract info. The following is sufficient:

let data = InstantiateResponse {
    rewards_contract: Addr::unchecked(res.get_contract_address()),
};
let resp = Response::new().set_data(to_binary(&data));
Ok(resp)

The contract doesn't have full access. It is run in a sandbox and it's output (data, events) are processed before shown to consumers.

resp.set_contract_address(env.contract.address.to_string());
resp.set_data(to_binary(&response)?.to_vec());

let resp = Response::new().set_data(
resp.write_to_bytes()
.map_err(|err| ContractError::Proto(err.to_string()))?,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky. We have three nested levels of data here.


Ok(resp)
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/tgrade-valset/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub enum ContractError {
#[error("{0}")]
AdminError(#[from] AdminError),

#[error("{0}")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed, we don't need to proto encode.

Proto(String),

#[error("Operator is already registered, cannot change Tendermint pubkey")]
OperatorRegistered {},

Expand Down
6 changes: 6 additions & 0 deletions contracts/tgrade-valset/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ pub enum RewardsDistribution {
DistributeFunds {},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct InstantiateResponse {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<nit> This would better be called InstantiateReplyResponse, or perhaps ReplyInstantiateResponse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It is response for instantiate (from the caller point of view). Internally it comes from the reply function but it is only because of how we model responding, but semantically it is just async handler for the subcall, which is not relevant to whoever triggered initial instantiation (there would not be any other response from instantiation than this one).

Copy link
Contributor

@maurolacy maurolacy Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I've just realised they are the same, while working on the multitest executor (see CosmWasm/cw-plus#515).

Copy link
Contributor

@maurolacy maurolacy Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the standard MsgInstantiateContractResponse then, by the way?

It's defined in the recent cw-plus 0.10.1. And under parse_reply... :-/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different. It is not MsgInstantiateContractResponse, which is data returned by x/wasm.

This is the JSON data the contract returns to the runtime. (To be parsed by the Go module)

pub rewards_contract: Addr,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down