Skip to content

Commit

Permalink
Merge pull request #16 from CosmWasm/cw20-escrow
Browse files Browse the repository at this point in the history
Cw20 escrow
  • Loading branch information
ethanfrey authored Jun 28, 2020
2 parents 96eb943 + e512cdd commit 7b2d37f
Show file tree
Hide file tree
Showing 20 changed files with 1,483 additions and 59 deletions.
42 changes: 42 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ workflows:
test:
jobs:
- contract_cw20_base
- contract_cw20_escrow
- package_cw20
- lint

Expand Down Expand Up @@ -49,6 +50,47 @@ jobs:
- target
key: cargocache-cw20-base-rust:1.44.0-{{ checksum "~/project/Cargo.lock" }}

contract_cw20_escrow:
docker:
- image: rust:1.44.0
working_directory: ~/project/contracts/cw20-escrow
steps:
- checkout:
path: ~/project
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
- restore_cache:
keys:
- cargocache-cw20-escrow-rust:1.44.0-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Add wasm32 target
command: rustup target add wasm32-unknown-unknown
- run:
name: Unit Tests
env: RUST_BACKTRACE=1
command: cargo unit-test --locked
- run:
name: Build Wasm
command: cargo wasm-debug --locked
- run:
name: Build and run schema generator
command: cargo schema --locked
- run:
name: Ensure checked-in schemas are up-to-date
command: |
CHANGES_IN_REPO=$(git status --porcelain)
if [[ -n "$CHANGES_IN_REPO" ]]; then
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
git status && git --no-pager diff
exit 1
fi
- save_cache:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-cw20-escrow-rust:1.44.0-{{ checksum "~/project/Cargo.lock" }}

package_cw20:
docker:
- image: rust:1.44.0
Expand Down
49 changes: 31 additions & 18 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions contracts/cw20-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cw20 = { path = "../../packages/cw20", version = "0.1.0" }
cosmwasm-std = { version = "0.9.0-alpha3" }
cosmwasm-storage = { version = "0.9.0-alpha3" }
cosmwasm-std = { version = "0.9.1" }
cosmwasm-storage = { version = "0.9.1" }
schemars = "0.7"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
snafu = { version = "0.6.3" }

[dev-dependencies]
# we only need to enable this if we use integration tests
#cosmwasm-vm = { version = "0.9.0-alpha3", default-features = false }
cosmwasm-schema = { version = "0.9.0-alpha3" }
#cosmwasm-vm = { version = "0.9.1", default-features = false }
cosmwasm-schema = { version = "0.9.1" }
36 changes: 1 addition & 35 deletions contracts/cw20-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,4 @@ pub mod msg;
pub mod state;

#[cfg(all(target_arch = "wasm32", feature = "entrypoint"))]
mod wasm {
use super::contract;
use cosmwasm_std::{
do_handle, do_init, do_query, ExternalApi, ExternalQuerier, ExternalStorage,
};

#[no_mangle]
extern "C" fn init(env_ptr: u32, msg_ptr: u32) -> u32 {
do_init(
&contract::init::<ExternalStorage, ExternalApi, ExternalQuerier>,
env_ptr,
msg_ptr,
)
}

#[no_mangle]
extern "C" fn handle(env_ptr: u32, msg_ptr: u32) -> u32 {
do_handle(
&contract::handle::<ExternalStorage, ExternalApi, ExternalQuerier>,
env_ptr,
msg_ptr,
)
}

#[no_mangle]
extern "C" fn query(msg_ptr: u32) -> u32 {
do_query(
&contract::query::<ExternalStorage, ExternalApi, ExternalQuerier>,
msg_ptr,
)
}

// Other C externs like cosmwasm_vm_version_1, allocate, deallocate are available
// automatically because we `use cosmwasm_std`.
}
cosmwasm_std::create_entry_points!(contract);
6 changes: 6 additions & 0 deletions contracts/cw20-escrow/.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 --features backtraces"
integration-test = "test --test integration"
schema = "run --example schema"
22 changes: 22 additions & 0 deletions contracts/cw20-escrow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "cw20-escrow"
version = "0.1.0"
authors = ["Ethan Frey <ethanfrey@users.noreply.github.com>"]
edition = "2018"

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

[features]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cw20 = { path = "../../packages/cw20", version = "0.1.0" }
cosmwasm-std = { version = "0.9.1", features = ["iterator"] }
cosmwasm-storage = { version = "0.9.1", features = ["iterator"] }
schemars = "0.7"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
snafu = { version = "0.6.3" }

[dev-dependencies]
cosmwasm-schema = { version = "0.9.1" }
15 changes: 15 additions & 0 deletions contracts/cw20-escrow/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CosmWasm-Plus: A collection of production-quality CosmWasm contracts
Copyright (C) 2020 Confio OÜ

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
54 changes: 54 additions & 0 deletions contracts/cw20-escrow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# CW20 Escrow

This is an escrow meta-contract that allows multiple users to
create independent escrows. Each escrow has a sender, recipient,
and arbiter. It also has a unique id (for future calls to reference it)
and an optional timeout.

The basic function is the sender creates an escrow with funds.
The arbiter may at any time decide to release the funds to either
the intended recipient or the original sender (but no one else),
and if it passes with optional timeout, anyone can refund the locked
tokens to the original sender.

We also add a function called "top_up", which allows anyone to add more
funds to the contract at any time.

## Token types

This contract is meant not just to be functional, but also to work as a simple
example of an CW20 "Receiver". And demonstrate how the same calls can be fed
native tokens (via typical `HandleMsg` route), or cw20 tokens (via `Receiver` interface).

Both `create` and `top_up` can be called directly (with a payload of native tokens),
or from a cw20 contract using the [Receiver Interface](../../packages/cw20/README.md#receiver).
This means we can load the escrow with any number of native or cw20 tokens (or a mix),
allow of which get released when the arbiter decides.

## Running this contract

You will need Rust 1.41+ 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/cw20_escrow.wasm .
ls -l cw20_escrow.wasm
sha256sum cw20_escrow.wasm
```

Or for a production-ready (compressed) build, run the following from the
repository root (not currently working with this monorepo...)

```
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="cosmwasm_plus_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.8.0 ./contracts/cw20-base
mv contract.wasm cw20_escrow.wasm
```
20 changes: 20 additions & 0 deletions contracts/cw20-escrow/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::env::current_dir;
use std::fs::create_dir_all;

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

use cw20_escrow::msg::{DetailsResponse, HandleMsg, InitMsg, ListResponse, QueryMsg, ReceiveMsg};

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!(InitMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(ReceiveMsg), &out_dir);
export_schema(&schema_for!(DetailsResponse), &out_dir);
export_schema(&schema_for!(ListResponse), &out_dir);
}
Loading

0 comments on commit 7b2d37f

Please sign in to comment.