Skip to content

[cosmos] Implement set fee governance instruction #423

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

Merged
merged 14 commits into from
Dec 13, 2022
Merged

[cosmos] Implement set fee governance instruction #423

merged 14 commits into from
Dec 13, 2022

Conversation

jayantk
Copy link
Contributor

@jayantk jayantk commented Dec 9, 2022

Add the set fee governance instruction to the cosmos contract. I thought this would be a good warmup but the change sprawled a bit because I needed to add governance instructions, serialization, etc. I'll leave some comments inline on anything nonobvious.

@jayantk jayantk changed the title [wip] Implement set fee in cosmos contract Implement set fee in cosmos contract Dec 9, 2022
emitter: msg.pyth_emitter,
pyth_emitter_chain: msg.pyth_emitter_chain,
}]),
chain_id: msg.chain_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added governance fields to ConfigInfo and the initialization message.

Note that fee_denom below seems to be needed to determine which coin the fees are paid in. I don't totally understand what this does, but I'm copying from wormhole here https://github.com/wormhole-foundation/wormhole/blob/main/cosmwasm/contracts/wormhole/src/contract.rs#L74

Copy link
Collaborator

@ali-behjati ali-behjati Dec 13, 2022

Choose a reason for hiding this comment

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

We should think a little bit about it and we should ask WH people how they handled it. Basically in cosmos there might be more than 1 native coin, as in terra there were both LUNA and UST (iirc: uluna and ust).

So the question is that do we want to have only one accepted coin per each chain?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Due to the nature of cosmwasm I think it might be better if we have multiple Coins and define a separate governance instruction for CW contracts that can set the fee to multiple coins.

Note: I don't know how many chains have multiple native coins. It is worth looking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wormhole only supports one coin (uluna). We can support multiple, but let me add that in a separate pr. (I think we'll need more governance instructions if we go that route.)

@@ -397,7 +513,7 @@ mod test {
vaa.emitter_address = vec![1u8];
vaa.emitter_chain = 3;

assert_eq!(verify_vaa_sender(&config_info, &vaa), Ok(()));
assert_eq!(verify_vaa_from_data_source(&config_info, &vaa), Ok(()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(this is a rename for clarity, since there are now two different VAA senders)

pub pyth_emitter: Binary,
pub pyth_emitter_chain: u16,
pub wormhole_contract: HumanAddr,
pub pyth_emitter: Binary,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to this PR, but can we change the Inst..Msg to get a list of emitter addresses and chains instead of 1?

}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
SubmitVaa { data: Binary },
UpdatePriceFeeds { data: Binary },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to this PR, but would be nice if we add UpdatePriceFeedsIfNecessary in another PR too.

pub valid_time_period: Duration,

// The fee to pay, denominated in fee_denom (typically, the chain's native token)
pub fee: u128,
Copy link
Collaborator

@ali-behjati ali-behjati Dec 13, 2022

Choose a reason for hiding this comment

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

Why not storing Coin here? Coin has both fee and denom and also it's native to the cosmwasm api has_coins or transferring all get Coin type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, in general, instead of u128 it's better to use cosmwasm Uint128 which handles json serde properly.

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've been copying the wormhole code for all of this stuff. Let me circle back on this (and the supporting multiple coin types) after implementing the actual payment mechanics, which will help me understand how this works.

Copy link
Collaborator

@ali-behjati ali-behjati left a comment

Choose a reason for hiding this comment

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

This is great! Sorry that the work is more than what you expected. I left couple of comments.

Also I investigated the tilt problem and that is because of uint128. Apparently in cosmwasm you cannot use it at all in Ser/De (more info here). So you need to use Uint128 here to fix the initialization issue. I tested it and it worked. Other values should be int and only fee should become a string.

pub valid_time_period: Duration,

// The fee to pay, denominated in fee_denom (typically, the chain's native token)
pub fee: u128,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, in general, instead of u128 it's better to use cosmwasm Uint128 which handles json serde properly.

emitter: msg.pyth_emitter,
pyth_emitter_chain: msg.pyth_emitter_chain,
}]),
chain_id: msg.chain_id,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Due to the nature of cosmwasm I think it might be better if we have multiple Coins and define a separate governance instruction for CW contracts that can set the fee to multiple coins.

Note: I don't know how many chains have multiple native coins. It is worth looking.

data_sources: HashSet::from([PythDataSource {
owner: info.sender,
wormhole_contract: deps.api.addr_validate(msg.wormhole_contract.as_ref())?,
data_sources: HashSet::from([PythDataSource {
emitter: msg.pyth_emitter,
pyth_emitter_chain: msg.pyth_emitter_chain,
}]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

In other contracts, we use chain_id from Wormhole, isn't it possible here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

After seeing the tests I realized why you are doing it.

In cosmwasm 1.0.0, there is a way to mock contract queries (it wasn't there in the past for 0.16 in Classic Terra). Please look at this:
https://docs.rs/cosmwasm-std/1.0.0/cosmwasm_std/testing/struct.MockQuerier.html


// The fee to pay, denominated in fee_denom (typically, the chain's native token)
pub fee: u128,
pub fee_denom: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to this PR, in other networks we decided to only store the price feed and ignore other fields, as storage is expensive.

@@ -1,6 +1,6 @@
# Pyth CosmWasm
l# Pyth CosmWasm
Copy link
Collaborator

Choose a reason for hiding this comment

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

This l is added by mistake here.

return Err(format!("Invalid governance module {module_num}",).into());
}

// TODO: check endianness
Copy link
Collaborator

Choose a reason for hiding this comment

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

wdym here? It seems you are checking it. And yeah we ser/de everything big endian (because EVM is a big indian VM).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the EVM thing is exactly what i was wondering. thanks.

let action_type: u8 = bytes.read_u8()?;
let target_chain_id: u16 = bytes.read_u16::<BigEndian>()?;

let action: Result<GovernanceAction, String> = match action_type {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nitpick: maybe have a separate enum for this?

Ok(buf)
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the lines below are unncessary.

updated_config.fee = (val as u128)
.checked_mul(
10_u128
.checked_pow(expo as u32)
Copy link
Collaborator

@ali-behjati ali-behjati Dec 13, 2022

Choose a reason for hiding this comment

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

nitpick: Can we do a checked conversion here instead of expo as u32? as will truncate the value.

@ali-behjati ali-behjati changed the title Implement set fee in cosmos contract Implement set fee governance instruction in cosmos contract Dec 13, 2022
@ali-behjati ali-behjati changed the title Implement set fee governance instruction in cosmos contract [cosmos] Implement set fee governance instruction Dec 13, 2022
Copy link
Collaborator

@ali-behjati ali-behjati left a comment

Choose a reason for hiding this comment

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

Awesome!

@jayantk jayantk merged commit 024d73a into main Dec 13, 2022
@jayantk jayantk deleted the cosmos branch December 13, 2022 16:46
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.

2 participants