-
Notifications
You must be signed in to change notification settings - Fork 98
feat(cwgrants): Implement x/cwgrants module #527
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
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
45a6d85
add protos
fdymylja b7022de
impl module API
fdymylja 40e6a4f
add codec
fdymylja 8910607
add errors
fdymylja e267e48
add msg implementation
fdymylja 60199f4
add genesis validation + test
fdymylja 0ec2f1f
add keys
fdymylja cfb6aae
add keeper and keeper testing
fdymylja 0816749
add msg and query server
fdymylja 69765eb
add cw type wrapper
fdymylja 6b0bd1a
add ante handler logic for grants
fdymylja 9d4c712
do wiring
fdymylja fabbc72
add cwgrant contract
fdymylja 023f34e
finalise testing
fdymylja 2972709
add more safety test case
fdymylja b4d6e98
add cwgrant wasm
fdymylja 5c0f264
lint
fdymylja 60fcee5
PR feedback: check if genesis granter is CW contract
fdymylja d080e6c
adjust genesis test
fdymylja d37eb2f
add UnregisterAsGranterMsg
fdymylja adcafa0
implement UnregisterAsGranter
fdymylja 66301fb
rename to CWFees
fdymylja d009356
move test contract to appropriate package
fdymylja 70b9d88
add to upgrade handler!
fdymylja 98bb046
add cw fees adr
fdymylja 529cd80
lint
fdymylja 1708c80
Merge branch 'main' into fd/cw_grants
fdymylja e6f2ef2
apply gas limits for request for grants
fdymylja d7396b9
update ADR
fdymylja 3f4c618
Merge branch 'main' into fd/cw_grants
fdymylja 6c3505b
merge main
fdymylja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| [package] | ||
| name = "cwgrant" | ||
| version = "0.1.0" | ||
| authors = ["Frojdi"] | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [profile.release] | ||
| opt-level = 3 | ||
| debug = false | ||
| rpath = false | ||
| lto = true | ||
| debug-assertions = false | ||
| codegen-units = 1 | ||
| panic = 'abort' | ||
| incremental = false | ||
| overflow-checks = true | ||
|
|
||
| [features] | ||
| # for more explicit tests, cargo test --features=backtraces | ||
| backtraces = ["cosmwasm-std/backtraces"] | ||
| # use library feature to disable all instantiate/execute/query exports | ||
| library = [] | ||
|
|
||
| [package.metadata.scripts] | ||
| optimize = """docker run --rm -v "$(pwd)":/code \ | ||
| --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \ | ||
| --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ | ||
| cosmwasm/optimizer:0.15.0 | ||
| """ | ||
|
|
||
| [dependencies] | ||
| cosmwasm-schema = "1.5.0" | ||
| cosmwasm-std = { version = "1.5.0", features = [ | ||
| "cosmwasm_1_3", | ||
| # Enable this if you only deploy to chains that have CosmWasm 1.4 or higher | ||
| # "cosmwasm_1_4", | ||
| ] } | ||
| cw-storage-plus = "1.1.0" | ||
| cw2 = "1.1.1" | ||
| schemars = "0.8.15" | ||
| serde = { version = "1.0.189", default-features = false, features = ["derive"] } | ||
| thiserror = { version = "1.0.49" } | ||
|
|
||
| [dev-dependencies] | ||
| cw-multi-test = "0.17.0" |
This file contains hidden or 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,2 @@ | ||
| 87a239f88d13f5d6f4a170d95f281563a91e01dcaae4a699ef1981dfef24115b cwgrant-aarch64.wasm | ||
| 72cdb4ffa538ff9365328a65427469c603a87007c3155b52515bf728a9b02c95 cwgrant.wasm |
This file contains hidden or 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 @@ | ||
| 92df70ecf8e205915633d93114aefd0340e65a72de4390ea2e14c27161271408 /target/wasm32-unknown-unknown/release/cwgrant.wasm | ||
Binary file not shown.
This file contains hidden or 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,57 @@ | ||
| use cosmwasm_std::{DepsMut, Empty, Env, MessageInfo, Response, entry_point}; | ||
| use crate::errors::ContractError; | ||
| use crate::msgs::{CwGrant, InstantiateMsg, SudoMsg}; | ||
| use crate::state::GRANTS; | ||
|
|
||
| #[cfg_attr(not(feature = "library"), entry_point)] | ||
| pub fn instantiate( | ||
| deps: DepsMut, | ||
| _env: Env, | ||
| _: MessageInfo, | ||
| msg: InstantiateMsg, | ||
| ) -> Result<Response, ContractError> { | ||
| for addr in &msg.grants { | ||
| let addr = deps.api.addr_validate(addr)?; | ||
| GRANTS.save(deps.storage, &addr, &Empty{})? | ||
| } | ||
| Ok(Response::default()) | ||
| } | ||
| #[cfg_attr(not(feature = "library"), entry_point)] | ||
| pub fn sudo( | ||
| deps: DepsMut, | ||
| _env: Env, | ||
| msg: SudoMsg, | ||
| ) -> Result<Response, ContractError> { | ||
| return match msg { | ||
| SudoMsg::CwGrant(grant) => { | ||
| sudo_grant(deps, grant) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn sudo_grant(deps: DepsMut, msg: CwGrant) -> Result<Response, ContractError> { | ||
| // in order to pay the fees all message senders need to be | ||
| // in the grants list. | ||
| for m in &msg.msgs { | ||
| let sender = deps.api.addr_validate(&m.sender)?; | ||
| if !GRANTS.has(deps.storage, &sender) { | ||
| return Err(ContractError::Unauthorized {}) | ||
| } | ||
| } | ||
|
|
||
| Ok(Response::default()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use cosmwasm_std::to_json_binary; | ||
| use crate::msgs::{CwGrant, SudoMsg}; | ||
|
|
||
| #[test] | ||
| fn encoding() { | ||
| println!("{}", to_json_binary(&SudoMsg::CwGrant(CwGrant{ | ||
| fee_requested: vec![], | ||
| msgs: vec![], | ||
| })).unwrap()); | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| use cosmwasm_std::StdError; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum ContractError { | ||
| #[error("{0}")] | ||
| Std(#[from] StdError), | ||
|
|
||
| #[error("Unauthorized")] | ||
| Unauthorized {}, | ||
| } |
This file contains hidden or 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,4 @@ | ||
| mod msgs; | ||
| mod contract; | ||
| mod errors; | ||
| mod state; |
This file contains hidden or 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,29 @@ | ||
| use cosmwasm_schema::{cw_serde}; | ||
| use cosmwasm_std::{Binary, Coin}; | ||
|
|
||
| #[cw_serde] | ||
| pub struct InstantiateMsg { | ||
| pub grants: Vec<String> | ||
| } | ||
|
|
||
| #[cw_serde] | ||
| pub enum SudoMsg { | ||
| CwGrant(CwGrant) | ||
| } | ||
| #[cw_serde] | ||
| pub struct CwGrant { | ||
| pub fee_requested: Vec<Coin>, | ||
| pub msgs: Vec<CwGrantMessage>, | ||
| } | ||
| #[cw_serde] | ||
| pub struct CwGrantMessage { | ||
| pub sender: String, | ||
| pub type_url: String, | ||
| pub msg: Binary, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| #[test] | ||
| fn build() {} | ||
| } |
This file contains hidden or 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,4 @@ | ||
| use cosmwasm_std::{Addr, Empty}; | ||
| use cw_storage_plus::Map; | ||
|
|
||
| pub const GRANTS: Map<&Addr, Empty> = Map::new("grants"); |
This file contains hidden or 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,64 @@ | ||
| --- | ||
|
|
||
| # ADR-009: Introduction of CWFees Module | ||
|
|
||
| Date: [Insert Date] | ||
|
fdymylja marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Status | ||
|
|
||
| [Insert Status: Proposed, Accepted, etc.] | ||
|
fdymylja marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Context | ||
|
|
||
| The introduction of the CWFees module marks a significant advancement in how transaction fees are handled within the | ||
| ecosystem. Previously confined to entities registered under the standard x/feegrant module, the `FeeGranter` role within | ||
| a transaction's `AuthInfo.Fee` can now be assumed by a CosmWasm contract. This transition from a static, | ||
| binary-implemented fee grant logic to a dynamic, contract-based approach enhances flexibility, eliminating the need for | ||
| chain upgrades for modifications in fee grant logic. | ||
|
|
||
| ## Decision | ||
|
|
||
| We have expanded the capabilities of the CWFees module by introducing two key message entry points: `RegisterAsGranter` | ||
| and `UnregisterAsGranter`, exclusively accessible to CosmWasm contracts. This enables a contract, for example during its | ||
| instantiation phase, to declare itself as a fee granter by issuing a `StargateMsg` containing a `RegisterAsGranter` | ||
| message. Once registered, the system acknowledges the contract as an eligible fee granter. | ||
|
|
||
| In the event of a transaction, the user can designate a registered CosmWasm contract as the `Tx.AuthInfo.Fee.Granter`. | ||
| The system, in turn, invokes this contract via sudo, providing it with critical information encapsulated in a structured | ||
| JSON format: | ||
|
|
||
| ```golang | ||
| type SudoMsg struct { | ||
| CWGrant *CWGrant `json:"cw_grant"` | ||
| } | ||
|
|
||
| type CWGrant struct { | ||
| FeeRequested wasmVmTypes.Coins `json:"fee_requested"` | ||
| Msgs []CWGrantMessage `json:"msgs"` | ||
| } | ||
|
|
||
| type CWGrantMessage struct { | ||
| Sender string `json:"sender"` | ||
| TypeUrl string `json:"type_url"` | ||
| Msg []byte `json:"msg"` | ||
| } | ||
| ``` | ||
|
|
||
| This detailed information empowers the contract to make an informed decision regarding the grant request. If the contract | ||
| consents (i.e., no errors are returned), the runtime itself handles the transfer of fees from the contract to the | ||
| auth collector. Conversely, if the contract opts to decline the grant, it must issue an error response. Overall, the contract | ||
| is not required to do anything besides signaling it accepts or refuses the grant (no coin moving is required!). | ||
|
|
||
| ## Consequences | ||
|
|
||
| ### Positive | ||
| 1. Grants developers enhanced control over the application of fee grants. | ||
| 2. Enables transactions where contracts absorb the gas costs for users, creating a gas-less experience. | ||
| 3. Supports the development of diverse incentive models. | ||
| 4. Accommodates the use of multiple coin types for fee payments. | ||
|
|
||
| ### Negative | ||
| 1. Introduces a layer of complexity to the system's architecture. | ||
|
|
||
| ### Security Considerations | ||
| - To mitigate potential risks, the gas usage within a CWGrant will be capped, preventing a CWFees contract from incurring excessive gas consumption. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.