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

cw1-whitelist-ng: Contract implementation in terms of semantical structures #499

Merged
merged 10 commits into from
Oct 21, 2021
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions contracts/cw1-whitelist-ng/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib"
integration-test = "test --test integration"
schema = "run --example schema"
40 changes: 40 additions & 0 deletions contracts/cw1-whitelist-ng/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "cw1-whitelist-ng"
version = "0.10.0"
authors = ["Bartłomiej Kuras <bartk@confio.gmbh>"]
edition = "2018"
description = "Implementation of an proxy contract using a whitelist"
license = "Apache-2.0"
repository = "https://github.com/CosmWasm/cw-plus"
homepage = "https://cosmwasm.com"
documentation = "https://docs.cosmwasm.com"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []
test-utils = []
# generate multitest interfaces
multitest = ["cw-multi-test", "anyhow"]

[dependencies]
cw0 = { path = "../../packages/cw0", version = "0.10.0" }
cw1 = { path = "../../packages/cw1", version = "0.10.0" }
cw2 = { path = "../../packages/cw2", version = "0.10.0" }
cosmwasm-std = { version = "1.0.0-beta", features = ["staking"] }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.10.0" }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.23" }
cw-multi-test = { path = "../../packages/multi-test", version = "0.10.0", optional = true }
anyhow = { version = "1", optional = true }

[dev-dependencies]
anyhow = "1"
assert_matches = "1"
cosmwasm-schema = { version = "1.0.0-beta" }
cw-multi-test = { path = "../../packages/multi-test", version = "0.10.0" }
derivative = "2"
14 changes: 14 additions & 0 deletions contracts/cw1-whitelist-ng/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CW1-Whitelist: A minimal CosmWasm proxy contract
Copyright (C) 2020 Confio OÜ

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
44 changes: 44 additions & 0 deletions contracts/cw1-whitelist-ng/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# CW1 Whitelist

This may be the simplest implementation of CW1, a whitelist of addresses.
It contains a set of admins that are defined upon creation.
Any of those admins may `Execute` any message via the contract,
per the CW1 spec.

To make this slighly less minimalistic, you can allow the admin set
to be mutable or immutable. If it is mutable, then any admin may
(a) change the admin set and (b) freeze it (making it immutable).

While largely an example contract for CW1, this has various real-world use-cases,
such as a common account that is shared among multiple trusted devices,
or trading an entire account (used as 1 of 1 mutable). Most of the time,
this can be used as a framework to build your own,
more advanced cw1 implementations.

## Allowing Custom Messages

By default, this doesn't support `CustomMsg` in order to be fully generic
among blockchains. However, all types are Generic over `T`, and this is only
fixed in `handle`. You can import this contract and just redefine your `handle`
function, setting a different parameter to `ExecuteMsg`, and you can produce
a chain-specific message.

## Running this contract

You will need Rust 1.44.1+ with `wasm32-unknown-unknown` target installed.

You can run unit tests on this via:

`cargo test`

Once you are happy with the content, you can compile it to wasm via:

```
RUSTFLAGS='-C link-arg=-s' cargo wasm
cp ../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm .
ls -l cw1_whitelist.wasm
sha256sum cw1_whitelist.wasm
```

Or for a production-ready (optimized) build, run a build command in the
the repository root: https://github.com/CosmWasm/cw-plus#compiling.
24 changes: 24 additions & 0 deletions contracts/cw1-whitelist-ng/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};

use cw1_whitelist_ng::msg::*;

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema_with_title(&schema_for!(Cw1ExecMsg), &out_dir, "Cw1ExecMsg");
export_schema_with_title(&schema_for!(WhitelistExecMsg), &out_dir, "WhitelistExecMsg");
export_schema_with_title(&schema_for!(Cw1QueryMsg), &out_dir, "Cw1QueryMsg");
export_schema_with_title(
&schema_for!(WhitelistQueryMsg),
&out_dir,
"WhitelistQueryMsg",
);
export_schema(&schema_for!(AdminListResponse), &out_dir);
}
20 changes: 20 additions & 0 deletions contracts/cw1-whitelist-ng/schema/admin_list_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AdminListResponse",
"type": "object",
"required": [
"admins",
"mutable"
],
"properties": {
"admins": {
"type": "array",
"items": {
"type": "string"
}
},
"mutable": {
"type": "boolean"
}
}
}