cw_vault_standard/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! # CosmWasm Vault Standard
3//!
4//! A standard interface for tokenized vaults written in CosmWasm. This repo
5//! contains a set of `ExecuteMsg` and `QueryMsg` variants that should be
6//! implemented by a vault contract that adheres to this standard.
7//!
8//! ## Vault Standard Fundamentals
9//! There are a few things to know about the vault standard:
10//! * Each vault has one specific token that is used for deposits, withdrawals
11//! and accounting. This token is called the `base token`.
12//! * Each vault has a `vault token` that represents the users share in the
13//! vault. The number of vault tokens the user receives should be based on the
14//! the number of base tokens they deposit.
15//!
16//! ## How to create a vault contract that adheres to this standard
17//!
18//! To create a vault contract that adheres to the standard, all you need to do
19//! is import the [VaultStandardExecuteMsg] and [VaultStandardQueryMsg] enums
20//! and use them in the entrypoints of your contracts.
21//!
22//! The [VaultStandardExecuteMsg] and [VaultStandardQueryMsg] enums define a set
23//! of variants that should be enough to cover most vault contract use cases,
24//! and all vaults that adhere to the standard must implement all of the
25//! provided default variants. If however your use case requires additional
26//! variants, please see the section on [how to use
27//! extensions](#how-to-use-extensions).
28//!
29//! ## Description and specification of ExecuteMsg and QueryMsg variants
30//! Please refer to the documentation page for each of the enums
31//! [VaultStandardExecuteMsg] and [VaultStandardQueryMsg] for a complete
32//! description of each variant.
33//!
34//! ## How to use Extensions
35//!
36//! If the standard set of `ExecuteMsg` and `QueryMsg` variants are not enough
37//! for your use case, you can include additional ones by defining an extension.
38//! The preferred way to do this is by creating a new enum that extends the
39//! exported [VaultStandardExecuteMsg] and [VaultStandardQueryMsg] enums. For
40//! example:
41//!
42//! ```ignore
43//! pub enum MyExtensionExecuteMsg {
44//! MyVariant1 { ... },
45//! MyVariant2 { ... },
46//! }
47//! ```
48//! This enum can then be included in an enum with all the Extensions that your
49//! vault uses and then be passed in as the generic argument `T` to
50//! [`VaultStandardExecuteMsg<T>`]. For example:
51//!
52//! ```ignore
53//! pub enum ExtensionExecuteMsg {
54//! MyExtension(MyExtensionExecuteMsg),
55//! Lockup(LockupExecuteMsg),
56//! }
57//!
58//! pub type ExecuteMsg = VaultStandardExecuteMsg<ExtensionExecuteMsg>;
59//! ```
60//!
61//! Now you can use the `ExecuteMsg` enum in your contract entrypoints instead
62//! of the default [VaultStandardExecuteMsg] enum.
63//!
64//! ## Included Extensions
65//!
66//! The following extensions are included in this repo:
67//! * [Lockup](crate::extensions::lockup)
68//! * [ForceUnlock](crate::extensions::force_unlock)
69//! * [Keeper](crate::extensions::keeper)
70//! * [Cw4626](crate::extensions::cw4626)
71//!
72//! Each of these extensions are available in this repo via cargo features. To
73//! use them, you can import the crate with a feature flag like this:
74//!
75//! ```toml
76//! cw-vault-standard = { version = "0.2.0", features = ["lockup", "force_unlock"] }
77//! ```
78//!
79//! A short description of each extension can be found below.
80//!
81//! ### Lockup
82//! The lockup extension can be used to create vaults where the vault tokens are
83//! not immediately reedemable. Instead of normally calling the
84//! [`VaultStandardExecuteMsg::Redeem`] variant, the user has to call the
85//! `Unlock` variant on the Lockup extension `ExecuteMsg` and wait for a
86//! specified period of time before they can withdraw their base tokens via the
87//! `WithdrawUnlocked` variant.
88//!
89//! ### ForceUnlock
90//! The force unlock extension can be used to create a vault that also
91//! implements the `Lockup` extension, but where some whitelisted addresses are
92//! allowed to call the `ForceUnlock` variant on the extension `ExecuteMsg` and
93//! immediately unlock the vault tokens of the specified user. This is useful if
94//! the vault is used with leverage and a liquidator needs to be able to
95//! liquidate the tokens locked in the vault.
96//!
97//! ### Keeper
98//! The keeper extension can be used to add functionality for either whitelisted
99//! addresses or anyone to act as a "keeper" for the vault and call functions to
100//! perform jobs that need to be done to keep the vault running.
101//!
102//! ### Cw4626
103//! The Cw4626 extension is the only extension provided with in this repo that
104//! does not extend the default [`VaultStandardExecuteMsg`] and
105//! [`VaultStandardQueryMsg`] enums by putting its variants inside of the
106//! [`VaultStandardExecuteMsg::VaultExtension`] variant. Instead it adds more
107//! variants at the top level, namely the variants from the [CW20
108//! standard](https://github.com/CosmWasm/cw-plus/tree/main/packages/cw20). This
109//! is inspired by the [ERC-4626 standard on
110//! Ethereum](https://ethereum.org/en/developers/docs/standards/tokens/erc-4626/)
111//! and allows the vault to, instead of using a Cosmos native token as the vault
112//! token, have the vault contract be it's own vault token by also implementing
113//! the CW20 standard. This is useful if you are writing a vault on a chain that
114//! does not yet have the [TokenFactory
115//! module](https://github.com/CosmWasm/token-factory) available and can
116//! therefore not issue a Cosmos native token as the vault token.
117
118/// Module containing some pre-defined vault standard extensions.
119pub mod extensions;
120/// Module containing the vault standard ExecutMsg and QueryMsg enums, as well
121/// as QueryMsg response types.
122pub mod msg;
123
124/// Module containing a helper struct for interacting with a vault contract.
125pub mod helper;
126
127pub use helper::*;
128pub use msg::*;
129
130/// The version of the vault standard.
131pub const VERSION: &str = env!("CARGO_PKG_VERSION");