Skip to content

Commit

Permalink
Marketplace operators (#1108)
Browse files Browse the repository at this point in the history
* Add operator to create listing

* Begin on freeze message

* Added more tests and fixes

* Tests for freeze

* Freeze + cancel scaffold

* Happy path cancel tests

* Rename to cancel frozen credits

* Relase credits scaffold

* Happy path tests for release credits

* Add tests for release credits

* Added tests for cancel frozen credits

* Clean up and finish

* Add retire info to buy and release messages

* Fix failing e2e test
  • Loading branch information
gjermundgaraba authored Dec 20, 2023
1 parent 7810f84 commit f9be465
Show file tree
Hide file tree
Showing 13 changed files with 2,096 additions and 32 deletions.
4 changes: 2 additions & 2 deletions chain/tests/e2e/marketplace/buy_credits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *E2ETestSuite) TestBuyCreditsWithoutFeeSplit() {
// Buy some credits
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, executeContractCmd, append([]string{
marketplaceAddress,
fmt.Sprintf(`{"buy_credits": {"owner": "%s", "denom": "PTEST/00001", "number_of_credits_to_buy": 2}}`, creditOwnerAddress.String()),
fmt.Sprintf(`{"buy_credits": {"owner": "%s", "denom": "PTEST/00001", "number_of_credits_to_buy": 2, "retire": false}}`, creditOwnerAddress.String()),
fmt.Sprintf("--amount=%s%s", "3000000", sdk.DefaultBondDenom),
fmt.Sprintf("--%s=%s", flags.FlagFrom, buyerKey.Name),
}, s.CommonFlags...))
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *E2ETestSuite) TestBuyCreditsWithFeeSplit() {
// Buy some credits
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, executeContractCmd, append([]string{
marketplaceAddress,
fmt.Sprintf(`{"buy_credits": {"owner": "%s", "denom": "PTEST/00001", "number_of_credits_to_buy": 2}}`, creditOwnerAddress.String()),
fmt.Sprintf(`{"buy_credits": {"owner": "%s", "denom": "PTEST/00001", "number_of_credits_to_buy": 2, "retire": false}}`, creditOwnerAddress.String()),
fmt.Sprintf("--amount=%s%s", "3000000", sdk.DefaultBondDenom),
fmt.Sprintf("--%s=%s", flags.FlagFrom, buyerKey.Name),
fmt.Sprintf("--%s=%s", flags.FlagGas, "300000"),
Expand Down
10 changes: 10 additions & 0 deletions cosmwasm/Cargo.lock

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

1 change: 1 addition & 0 deletions cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/EmpowerPlastic/empowerchain"
cosmwasm-schema = "1.1.9"
cosmwasm-std = "1.1.9"
cw-storage-plus = "1.0.1"
cw-storage-macro = "1.0.1"
serde = { version = "1.0.151", features = ["derive"] }
thiserror = "1.0.38"
cosmos-sdk-proto = { version = "0.16", default-features = false }
Expand Down
31 changes: 30 additions & 1 deletion cosmwasm/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# CosmWasm workspace

This workspace contains contracts and libraries meant to be used on EmpowerChain.
This workspace contains contracts and libraries meant to be used on EmpowerChain.

## Pre-requisites
You need to install and set up the normal CosmWasm toolchain (including cosmwasm-check).

## Build

To build:
```bash
$ cargo build
```

To build wasm:
```bash
$ cargo wasm
```

## Test

To run the tests:
```bash
$ cargo test
```

## Generate schema

To generate schema:
```bash
$ cargo schema
```
1 change: 1 addition & 0 deletions cosmwasm/contracts/plastic-credit-marketplace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw-storage-macro = { workspace = true }
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
cosmos-sdk-proto = { workspace = true, default-features = false }
Expand Down
30 changes: 29 additions & 1 deletion cosmwasm/contracts/plastic-credit-marketplace/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
# Plastic Credit Marketplace

The plastic credit marketplace is cosmwasm smart contract built on top of the EmpowerChain `plasticcredit` module.
It allows users to list and buy plastic credits.
It allows users to list and buy plastic credits.

## Listings

The plastic credit marketplace functions by storing a list of `Listing` objects in the state.
Anyone who has a plastic credit can create a listing by using the `CreateListing` message.

It allows anyone to buy a listing by using the `BuyCredits` message.
They can then buy as many credits as they want (up to the amount listed, and given the correct amount of funds are sent with the message)

## Operators

TODO

Each listing has an optional `operator` field, which allows the operator set to freeze any number of credits in the listing
for the purpose of finishing a transaction off-chain. For instance, for off-chain payments the operator is given the ability to
freeze the credits as a way to escrow the credits. When the payment has been settled off-chain the operator (or the seller)
can then release the credits to the buyer.

## Fee split

The plastic credit marketplace uses the `fee-splitter` package to allow for the marketplace to set up a fee structure.
The fee is a percentage of the listing price, and can be split between any number of parties.

A typical setup might be that the developer, or the deployer of the contract, gets a small fee, as well as the chain it runs on.
For instance, it could be set up like this:
- A total 5% fee for each listing
- 75% of the fee goes to the developer
- 25% of the fee goes to the chain
14 changes: 13 additions & 1 deletion cosmwasm/contracts/plastic-credit-marketplace/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum ContractError {
#[error("listing needs to have a price per credit")]
ZeroPrice {},

#[error("not enough credits available on listing to buy")]
#[error("not enough credits available on listing")]
NotEnoughCredits {},

#[error("not enough funds sent to buy credits")]
Expand All @@ -31,6 +31,18 @@ pub enum ContractError {
#[error("listing already exists")]
ListingAlreadyExists {},

#[error("timeout not in future")]
TimeoutNotInFuture {},

#[error("timeout over maximum, which is set to {max_timeout} seconds")]
TimeoutTooLong {max_timeout: u64},

#[error("freeze timed out")]
TimedOut {},

#[error("freeze not found")]
FreezeNotFound {},

#[error("fee split error {0}")]
FeeSplitError(#[from] FeeSplitterError),
}
Loading

0 comments on commit f9be465

Please sign in to comment.