Skip to content

Commit

Permalink
feat: Remove types forwarding to interface in sv::messages (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird authored May 7, 2024
1 parent f8c1e55 commit 741b358
Show file tree
Hide file tree
Showing 27 changed files with 356 additions and 373 deletions.
47 changes: 47 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,53 @@

This guide explains what is needed to upgrade contracts when migrating over major releases of `sylvia`. Note that you can also view the [complete CHANGELOG](https://github.com/CosmWasm/sylvia/blob/main/CHANGELOG.md) to understand the differences.


## 1.0.2 ->


### Generics in `sv::messages` not required
```diff
-#[contract]
-#[sv::messages(generic<SomeType1, SomeType2, SomeType3> as Generic
-impl Contract {}
+#[contract]
+#[sv::messages(generic as Generic)]
+impl Contract {}
```

This change is optional, since the generics are still accepted by the parser. Though they are
ignored in the further process.


### CodeId generic over the Contract type
```diff
-let code_id: CodeId<
- SvCustomMsg,
- SvCustomMsg,
- _,
-> = CodeId::store_code(&app);
+let code_id: CodeId<
+ GenericContract<
+ SvCustomMsg,
+ SvCustomMsg,
+ >,
+ _,
+> = CodeId::store_code(&app);
```

### Lifetime ellision in a contract's impl block not supported
```diff
-#[contract]
-impl Cw1SubkeysContract<'_> {
- // [...]
-}
+#[contract]
+impl<'a> Cw1SubkeysContract<'a> {
+ // [...]
+}
```


## 0.9.3 -> 0.10.0

## Multitest proxy
Expand Down
52 changes: 0 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,58 +689,6 @@ where
}
```

### Implement interface

```rust
impl<InstantiateParam, ExecParam, FieldType>
Generic
for crate::contract::GenericContract<
InstantiateParam,
ExecParam,
FieldType,
>
{
type Error = StdError;
type ExecParam = ExecParam;
type QueryParam: SvCustomMsg;
type RetType = SvCustomMsg;

fn generic_exec(
&self,
_ctx: ExecCtx,
_msgs: Vec<CosmosMsg<Self::ExecParam>>,
) -> StdResult<Response> {
Ok(Response::new())
}

fn generic_query(
&self,
_ctx: QueryCtx,
_msg: Self::QueryParam,
) -> StdResult<Self::RetType> {
Ok(SvCustomMsg {})
}
}
```

Now we have to inform Sylvia that the interface implemented for the contract has associated types.
We have to list those types (generics or concrete) next to the interface in the `#[sv::messages]`
attribute:

```rust
#[contract]
#[sv::messages(generic<ExecParam, SvCustomMsg, SvCustomMsg> as Generic)]
impl<InstantiateParam, ExecParam, FieldType>
GenericContract<InstantiateParam, ExecParam, FieldType>
where
for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de,
ExecParam: CustomMsg + DeserializeOwned + 'static,
FieldType: 'static,
{
...
}
```

### Generics in entry_points

Entry points have to be generated with concrete types. Using the `entry_points` macro
Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Cw1SubkeysContract<'a> {
#[sv::error(ContractError)]
#[sv::messages(cw1 as Cw1)]
#[sv::messages(whitelist as Whitelist)]
impl Cw1SubkeysContract<'_> {
impl<'abcd> Cw1SubkeysContract<'abcd> {
pub const fn new() -> Self {
Self {
whitelist: Cw1WhitelistContract::new(),
Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/cw1-whitelist/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Cw1WhitelistContract<'a> {
#[sv::error(ContractError)]
#[sv::messages(cw1 as Cw1)]
#[sv::messages(whitelist as Whitelist)]
impl Cw1WhitelistContract<'_> {
impl<'a> Cw1WhitelistContract<'a> {
pub const fn new() -> Self {
Self {
admins: Map::new("admins"),
Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct Cw20Base<'a> {
#[sv::messages(cw20_allowances as Allowances)]
#[sv::messages(cw20_marketing as Marketing)]
#[sv::messages(cw20_minting as Minting)]
impl Cw20Base<'_> {
impl<'abcd> Cw20Base<'abcd> {
pub const fn new() -> Self {
Self {
token_info: Item::new("token_info"),
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/generic_contract/src/bin/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {

write_api! {
instantiate: InstantiateMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg, SvCustomMsg, SvCustomMsg>,
query: ContractQueryMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg,>,
query: ContractQueryMsg<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg,>,
}
}
32 changes: 17 additions & 15 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub struct GenericContract<
#[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, String>))]
#[contract]
#[sv::messages(cw1 as Cw1: custom(msg, query))]
#[sv::messages(generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as CustomAndGeneric)]
#[sv::messages(generic as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
impl<
InstantiateT,
Expand Down Expand Up @@ -194,7 +194,7 @@ where
#[cfg(test)]
mod tests {
use super::sv::mt::CodeId;
use super::{SvCustomMsg, SvCustomQuery};
use super::{GenericContract, SvCustomMsg, SvCustomQuery};
use crate::contract::sv::mt::GenericContractProxy;
use cw_multi_test::IntoBech32;
use sylvia::multitest::App;
Expand All @@ -204,18 +204,20 @@ mod tests {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
#[allow(clippy::type_complexity)]
let code_id: CodeId<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
GenericContract<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
>,
_,
> = CodeId::store_code(&app);

Expand Down
32 changes: 17 additions & 15 deletions examples/contracts/generic_contract/src/custom_and_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use custom_and_generic::CustomAndGeneric;
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};

use crate::contract::{SvCustomMsg, SvCustomQuery};
use crate::contract::{GenericContract, SvCustomMsg, SvCustomQuery};

impl<
InstantiateT,
Expand All @@ -18,7 +18,7 @@ impl<
MigrateT,
FieldT,
> CustomAndGeneric
for crate::contract::GenericContract<
for GenericContract<
InstantiateT,
Exec1T,
Exec2T,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<

#[cfg(test)]
mod tests {
use super::{SvCustomMsg, SvCustomQuery};
use super::{GenericContract, SvCustomMsg, SvCustomQuery};
use crate::contract::sv::mt::CodeId;
use custom_and_generic::sv::mt::CustomAndGenericProxy;
use cw_multi_test::IntoBech32;
Expand All @@ -114,18 +114,20 @@ mod tests {
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id = CodeId::<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
GenericContract<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
>,
_,
>::store_code(&app);

Expand Down
31 changes: 17 additions & 14 deletions examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::contract::GenericContract;
use cosmwasm_std::{CosmosMsg, Response, StdError, StdResult};
use cw1::{CanExecuteResp, Cw1};
use sylvia::types::{ExecCtx, QueryCtx};
Expand All @@ -16,7 +17,7 @@ impl<
MigrateT,
FieldT,
> Cw1
for crate::contract::GenericContract<
for GenericContract<
InstantiateT,
Exec1T,
Exec2T,
Expand Down Expand Up @@ -50,7 +51,7 @@ impl<
#[cfg(test)]
mod tests {
use crate::contract::sv::mt::CodeId;
use crate::contract::{SvCustomMsg, SvCustomQuery};
use crate::contract::{GenericContract, SvCustomMsg, SvCustomQuery};
use cosmwasm_std::{CosmosMsg, Empty};
use cw1::sv::mt::Cw1Proxy;
use cw_multi_test::IntoBech32;
Expand All @@ -60,18 +61,20 @@ mod tests {
fn proxy_methods() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
let code_id = CodeId::<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
GenericContract<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
>,
_,
>::store_code(&app);

Expand Down
32 changes: 17 additions & 15 deletions examples/contracts/generic_contract/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use generic::Generic;
use serde::Deserialize;
use sylvia::types::{CustomMsg, ExecCtx, QueryCtx, SudoCtx};

use crate::contract::SvCustomMsg;
use crate::contract::{GenericContract, SvCustomMsg};

impl<
InstantiateT,
Expand All @@ -19,7 +19,7 @@ impl<
MigrateT,
FieldT,
> Generic
for crate::contract::GenericContract<
for GenericContract<
InstantiateT,
Exec1T,
Exec2T,
Expand Down Expand Up @@ -117,7 +117,7 @@ where
#[cfg(test)]
mod tests {
use crate::contract::sv::mt::CodeId;
use crate::contract::{SvCustomMsg, SvCustomQuery};
use crate::contract::{GenericContract, SvCustomMsg, SvCustomQuery};
use cosmwasm_std::CosmosMsg;
use cw_multi_test::IntoBech32;
use generic::sv::mt::GenericProxy;
Expand All @@ -128,18 +128,20 @@ mod tests {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg, SvCustomQuery>>::custom(|_, _, _| {});
#[allow(clippy::type_complexity)]
let code_id: CodeId<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
GenericContract<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
String,
>,
_,
> = CodeId::store_code(&app);

Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/generic_iface_on_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl cosmwasm_std::CustomQuery for SvCustomQuery {}

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
#[sv::messages(generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as CustomAndGeneric)]
#[sv::messages(generic as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic as CustomAndGeneric)]
#[sv::messages(cw1 as Cw1: custom(msg, query))]
/// Required if interface returns generic `Response`
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
Expand Down
Loading

0 comments on commit 741b358

Please sign in to comment.