Skip to content

Commit

Permalink
Merge pull request #55 from confio/rename-dso-token
Browse files Browse the repository at this point in the history
Rename dso-token to trusted-token
  • Loading branch information
ueco-jb authored Oct 26, 2021
2 parents bffa83d + 0528547 commit aca8af8
Show file tree
Hide file tree
Showing 30 changed files with 113 additions and 70 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ workflows:
jobs:
- contract_tfi_factory
- contract_tfi_pair
- contract_dso_token
- contract_trusted_token
- package_tfi
build:
jobs:
Expand Down Expand Up @@ -94,10 +94,10 @@ jobs:
- target
key: cargocache-tfi-pair-rust:1.51.0-{{ checksum "~/project/Cargo.lock" }}

contract_dso_token:
contract_trusted_token:
docker:
- image: rust:1.51.0
working_directory: ~/project/contracts/dso-token
working_directory: ~/project/contracts/trusted-token
steps:
- checkout:
path: ~/project
Expand All @@ -106,7 +106,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version
- restore_cache:
keys:
- cargocache-dso-token-rust:1.51.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-trusted-token-rust:1.51.0-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Unit Tests
environment:
Expand All @@ -128,7 +128,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-dso-token-rust:1.51.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-trusted-token-rust:1.51.0-{{ checksum "~/project/Cargo.lock" }}

package_tfi:
docker:
Expand Down
52 changes: 26 additions & 26 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ incremental = false
codegen-units = 1
incremental = false

[profile.release.package.dso-token]
opt-level = 3
debug = false
debug-assertions = false
[profile.release.package.trusted-token]
codegen-units = 1
incremental = false
overflow-checks = true

[profile.release]
rpath = false
Expand Down
4 changes: 2 additions & 2 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Once you have these, upload all three codes and record the code ids. I will call

Note that you must use `cw20-base` (or similar) for the token in `TOKEN_ID`, as the
[`InstantiateMsg` is fixed](https://github.com/confio/tfi/blob/main/packages/tfi/src/token.rs#L7-L15).
This is used for *liquidity tokens*. You can use any custom (whitelist / dso) token contract for the trading pairs.
This is used for *liquidity tokens*. You can use any custom (whitelist / trusted) token contract for the trading pairs.

## Instantiate the Factory

Expand Down Expand Up @@ -247,4 +247,4 @@ with this request to the AMM. The final result will work like sending the native
"msg": "eyJzd2FwIjp7Im1heF9zcHJlYWQiOiIwLjI1In19Cg"
}
}
```
```
8 changes: 4 additions & 4 deletions contracts/tfi-factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tfi-factory"
version = "0.2.0"
version = "0.2.1"
authors = ["Terraform Labs, PTE."]
edition = "2018"
description = "A tfi factory contract - auto pair contract generator and also directory for all pairs"
Expand All @@ -23,7 +23,7 @@ crate-type = ["cdylib", "rlib"]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
tfi = { path = "../../packages/tfi", default-features = false, version = "0.2.0"}
tfi = { path = "../../packages/tfi", default-features = false, version = "0.2.1"}
cosmwasm-std = "=1.0.0-beta"
cw-storage-plus = "=0.10"
schemars = "0.8.1"
Expand All @@ -36,8 +36,8 @@ cosmwasm-schema = "=1.0.0-beta"
cosmwasm-storage = "=1.0.0-beta"
anyhow = { version = "1", features = ["backtrace"] }
cw-multi-test = "=0.10"
tfi-pair = { path = "../tfi-pair", version = "0.2.0" }
dso-token = { path = "../dso-token", version = "0.2.0" }
tfi-pair = { path = "../tfi-pair", version = "0.2.1" }
trusted-token = { path = "../trusted-token", version = "0.2.1" }
derivative = "2"
cw4-group = { version = "=0.10", features = ["library"] }
cw4 = "=0.10"
Expand Down
10 changes: 5 additions & 5 deletions contracts/tfi-factory/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn pair_not_whitelisted() {
.provide_liquidity(&pair, &lp, 2000, 6000)
.unwrap_err();

assert_error(err, dso_token::error::ContractError::Unauthorized {});
assert_error(err, trusted_token::error::ContractError::Unauthorized {});
}

/// Failure test showing up, that it is impossible to provide liquidity nor swap with pair by
Expand Down Expand Up @@ -123,11 +123,11 @@ fn actors_not_whitelisted() {
let err = suite
.increase_allowance(&cash.addr(), &lp, &pair, 6000)
.unwrap_err();
assert_error(err, dso_token::error::ContractError::Unauthorized {});
assert_error(err, trusted_token::error::ContractError::Unauthorized {});

// As non-member liquidity provider is not allowed to provide liquidity
let err = suite.provide_liquidity(&pair, &lp, 2000, 6000).unwrap_err();
assert_error(err, dso_token::error::ContractError::Unauthorized {});
assert_error(err, trusted_token::error::ContractError::Unauthorized {});

// Even if lp is later added, he would need to increase allowance first, as previous attempt
// failed
Expand All @@ -140,8 +140,8 @@ fn actors_not_whitelisted() {

// Non whitelisted members has no swap rights
let err = suite.swap_btc(&pair, &trader, 1000).unwrap_err();
assert_error(err, dso_token::error::ContractError::Unauthorized {});
assert_error(err, trusted_token::error::ContractError::Unauthorized {});

let err = suite.swap_cash(&pair, &trader, 1000).unwrap_err();
assert_error(err, dso_token::error::ContractError::Unauthorized {});
assert_error(err, trusted_token::error::ContractError::Unauthorized {});
}
12 changes: 6 additions & 6 deletions contracts/tfi-factory/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ fn contract_cw20() -> Box<dyn Contract<Empty>> {

fn contract_token() -> Box<dyn Contract<Empty>> {
Box::new(ContractWrapper::new(
dso_token::contract::execute,
dso_token::contract::instantiate,
dso_token::contract::query,
trusted_token::contract::execute,
trusted_token::contract::instantiate,
trusted_token::contract::query,
))
}

Expand All @@ -76,7 +76,7 @@ fn contract_group() -> Box<dyn Contract<Empty>> {
/// Testing environment with:
/// * single native token "btc"
/// * single cw4-group used as whitelist
/// * single dso-token "cash" using internal group
/// * single trusted-token "cash" using internal group
/// * single tfi-factory
/// * number of actors which are just address initialized with some "btc" and "cash"
///
Expand All @@ -94,7 +94,7 @@ pub struct Suite {
pub actors: Vec<Addr>,
/// cw4 whitelist contract address
pub whitelist: Cw4Contract,
/// dso-token cash contract address
/// trusted-token cash contract address
pub cash: Cw20Contract,
/// tfi-factory contract address
pub factory: Addr,
Expand Down Expand Up @@ -414,7 +414,7 @@ impl Config {
app.instantiate_contract(
cw20_id,
owner.clone(),
&dso_token::msg::InstantiateMsg {
&trusted_token::msg::InstantiateMsg {
name: "Cash Token".to_owned(),
symbol: "CASH".to_owned(),
decimals: 9,
Expand Down
6 changes: 3 additions & 3 deletions contracts/tfi-pair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tfi-pair"
version = "0.2.0"
version = "0.2.1"
authors = ["Terraform Labs, PTE."]
edition = "2018"
description = "A tfi pair contract"
Expand All @@ -24,7 +24,7 @@ backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
integer-sqrt = "0.1.5"
tfi = { path = "../../packages/tfi", default-features = false, version = "0.2.0"}
tfi = { path = "../../packages/tfi", default-features = false, version = "0.2.1"}
cw2 = "=0.10"
cw20 = "=0.10"
cosmwasm-std = "=1.0.0-beta"
Expand All @@ -38,6 +38,6 @@ cosmwasm-schema = "=1.0.0-beta"
cosmwasm-storage = "=1.0.0-beta"
cw20-base = { version = "=0.10", features = ["library"] }
cw-multi-test = { version = "=0.10" }
tfi-mocks = { path = "../../packages/mocks", version = "0.2.0"}
tfi-mocks = { path = "../../packages/mocks", version = "0.2.1"}
derivative = "2"
anyhow = { version = "1", features = ["backtrace"] }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dso-token"
version = "0.2.0"
name = "trusted-token"
version = "0.2.1"
authors = ["Ethan Frey <ethanfrey@users.noreply.github.com>"]
edition = "2018"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# DSO Token
# Trusted Token

This is a cw20-based token with whitelisting, to be used in the context of a DSO.
This is a cw20-based token with whitelisting, to be used in the context of a Trusted Circle.

It can be used to provide one side of a trading pair in an AMM setting.

Only whitelisted users will be able to add liquidity to the token, and trade it.

## Differences between standard cw20 and dso-token
## Differences between standard cw20 and trusted-token

### Instantiation

Expand All @@ -27,6 +27,53 @@ pub struct InstantiateMsg {
New field is the address of cw4 group contract. Only addresses being members of
this group would be able to trade this token.

### Execution

We override the `execute` method and check that all addresses (sender, recipient, owner if sending on someone else's behalf)
are members of the whitelist contract before dispatching to the standard `cw20-base` action. This looks like:

```rust
pub fn execute() {
let res = match msg {
ExecuteMsg::Transfer { recipient, amount } => {
verify_sender_and_addresses_on_whitelist(deps.as_ref(), &info.sender, & [&recipient])?;
cw20_base::contract::execute_transfer(deps, env, info, recipient, amount)?
}
// other variants....
};
// rest of code...
}
```

The key logic then looks like this:

```rust
pub(crate) fn verify_sender_and_addresses_on_whitelist(
deps: Deps,
sender: &Addr,
addresses: &[&str],
) -> Result<(), ContractError> {
let whitelist: Cw4Contract = WHITELIST.load(deps.storage)?;
if whitelist.is_member(&deps.querier, sender)?.is_none() {
return Err(ContractError::Unauthorized {});
}
for address in addresses {
let validated_address = deps.api.addr_validate(address)?;
if whitelist
.is_member(&deps.querier, &validated_address)?
.is_none()
{
return Err(ContractError::Unauthorized {});
}
}
Ok(())
}
```

Note that it just checks if the member is present in the group contract, it is unimportant what weight it has.
This means that even 0 weight (which will not allow it to vote in voting contracts) is sufficient to pass the whitelist.
It must be fully removed from the group contract to no longer pass the whitelisting check.

## New messages

### Redeem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::create_dir_all;

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

use dso_token::msg::{
use trusted_token::msg::{
AllRedeemsResponse, ExecuteMsg, InstantiateMsg, IsWhitelistedResponse, QueryMsg,
RedeemResponse, WhitelistResponse,
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit aca8af8

Please sign in to comment.