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

tgrade-valset: Interface for distribution rewards by external contract #198

Merged
merged 9 commits into from
Oct 8, 2021

Conversation

hashedone
Copy link
Contributor

@hashedone hashedone commented Oct 1, 2021

Closes #138

  • Instantiate rewards distribution contract on init
  • Emit event with information about distribution contract on contract instantiation
  • Allow to query for distribution contract
  • Distribute rewards by external contract
  • Tests

@hashedone hashedone force-pushed the 138-distributing-rewards-using-engagement branch from 1d9c411 to 84c2f62 Compare October 1, 2021 12:58
@hashedone hashedone marked this pull request as draft October 1, 2021 13:04
@hashedone hashedone force-pushed the 138-distributing-rewards-using-engagement branch 3 times, most recently from 52de51d to d0a2ef0 Compare October 5, 2021 10:51
@hashedone

This comment has been minimized.

@hashedone hashedone force-pushed the 138-distributing-rewards-using-engagement branch 2 times, most recently from 7c45ebb to 4e8ef65 Compare October 6, 2021 14:45
@hashedone hashedone force-pushed the 138-distributing-rewards-using-engagement branch from 2b5446b to 1f73104 Compare October 7, 2021 08:55
@hashedone hashedone marked this pull request as ready for review October 7, 2021 08:55
Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

In general, this looks quite good and thorough.

The logic for handling the response (MsgInstantiateContract) would be good to pull out as a general utility helper and then see if we use protobuf, prost, or just hard-code a decoding (as there are 2 simple types and we may need that to avoid code bloat). (Created CosmWasm/cw-plus#480 and put it in the backlog)

Other than that, I just think there are a few events and a message that could be optional (only emitted when non-empty). Please ping when you fix those so I can approve and merge

syntax = "proto3";

// MsgInstantiateContractResponse defines the Msg/InstantiateContract response type.
message MsgInstantiateContractResponse {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to pull this out and add the autogen.
Honestly, I find prost easier to work with than the protobuf package.

We could merge like this, but it would be good to pull this into a common helpers package. Eventually optimising for minimum code-size, dependency bloat. I think I have this in another contract or two somewhere. And others ask for it. Something that could be added to cw-plus (as cw-proto package?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it could be. It is just making maintenance easier (no risk of committing not regenerated message)

Copy link
Contributor

Choose a reason for hiding this comment

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

those proto types are a stable part of the 1.0 api. they will not change.


```json
{
"_contract_addr": "valset_addr"
Copy link
Contributor

Choose a reason for hiding this comment

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

missing comma

contracts/tgrade-valset/examples/schema.rs Show resolved Hide resolved
contracts/tgrade-valset/src/msg.rs Show resolved Hide resolved
contracts/tgrade-valset/src/contract.rs Show resolved Hide resolved
.map(|vi| ValidatorUpdate {
pubkey: vi.validator_pubkey.clone(),
power: vi.power,
.map(|vi| {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this with unzip to do 2 at once.


pub fn rewards_instantiate_reply(deps: DepsMut, msg: Reply) -> Result<Response, ContractError> {
let id = msg.id;
let res: MsgInstantiateContractResponse = Message::parse_from_bytes(
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be a good logic to pull out into a helper. In addition to the proto logic.

The only time we need to use this struct is handling SubMsg where we want to get info from another contract.
(Pulling into cw-plus can be a separate PR and shouldn't block this one)


// diff between empty and vals must be removals
let diff = calculate_diff(empty, vals.clone());
assert_eq!(diff.diffs.len(), 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

These updates look fine and fair to remove this length check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actual reason I did it is that they are only making failure messages worse. You got info test failed, but you don't have diff output, so it may be harder to reason about what happened (you would probably add prints and rerun test...). It would fail anyway line below ;)

pub fn withdraw_validation_reward(&mut self, executor: &str) -> AnyResult<AppResponse> {
self.app.execute_contract(
Addr::unchecked(executor),
self.rewards_contract.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

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

nice one, letting the tests interact with the rewards contract, just like the main contract.

I was surprised how clean those tests were, but this is very nice abstraction for the suite.

let denom = self.token.clone();
let admin = Addr::unchecked(&self.admin);
let recipient = self.valset.to_string();
self.app.init_modules(move |router, api, storage| {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very good use of the API.

Question: does it make sense to provide some easier approach for this?
I thought of (maybe did???) add a SudoMsg::Bank to allow minting in some version of multi-test. Can you see if you can use that here? We should make this normal use case easy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I spend like couple hours to find this function (its name isn't helping here). I would actually just make the .custom_message on app which sends sudo message directly, so it would just look like:

self.app.custom_message(TgradeMsg::MintTokens {
  denom,
  amount: amount.into(),
  recipient,
}).unwrap();

Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

one optimization missing (don't send update messages on no change). Otherwise, good to merge.

@ethanfrey ethanfrey merged commit e13303d into main Oct 8, 2021
@ethanfrey ethanfrey deleted the 138-distributing-rewards-using-engagement branch October 8, 2021 13:06
})
.collect();
.unzip();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏼

err: "Missing reply data".to_owned(),
})?
.as_slice(),
)
Copy link
Contributor

@maurolacy maurolacy Oct 8, 2021

Choose a reason for hiding this comment

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

Very nice. Only question is, do we really need to use protobuf here, or this could be handled with serde and Deserialize / from_binary?

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 protobuf coming from wasmd.
And yes, you do need to parse it.
We never need to create it (it happens when we dispatch a message and check data on reply).

We should document this better here CosmWasm/wasmd#623 and then add helpers here CosmWasm/cw-plus#480

(No rush on these... they are lower in the backlog but good to flag this as an issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tgrade-valset: Consider using cw2222 distribution mechanism
3 participants