Skip to content

Commit fe51993

Browse files
authored
Merge pull request #11 from machisuji/main
updated marketplace to scrypto v0.3
2 parents bb4362e + 7d4173e commit fe51993

File tree

7 files changed

+667
-0
lines changed

7 files changed

+667
-0
lines changed

1-exchanges/marketplace/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "marketplace"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
19+
[lib]
20+
crate-type = ["cdylib", "lib"]

1-exchanges/marketplace/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Marketplace
2+
3+
A simple marketplace where people can create buy and sell orders (no AMMs).
4+
A user gets an order ticket for each created (buy or sell) order which they can use
5+
to withdraw the bought tokens (and change) or the payment for sold tokens.
6+
7+
You can run the demo on Windows by opening PowerShell and running:
8+
9+
```
10+
powershell .\tests\demo.ps1
11+
```
12+
13+
On Linux it is the following using bash:
14+
15+
```
16+
bash ./tests/demo.sh
17+
```
18+
19+
## Source
20+
21+
This is based on the example I submitted to the [community-scrypto repository](https://github.com/radixdlt/community-scrypto-examples/tree/main/defi/marketplace).

1-exchanges/marketplace/src/data.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use sbor::*;
2+
use scrypto::prelude::*;
3+
4+
#[derive(Debug, TypeId, Encode, Decode, Describe)]
5+
pub struct Order {
6+
/// Order number (starting at 1)
7+
pub number: i64,
8+
/// True if this is a buy order, false if it is a sell order
9+
pub buy: bool,
10+
/// Kind of token that is being bought or sold
11+
pub token: ResourceDef,
12+
/// Price (in market's currency) the buyer is willing to bid or seller is asking
13+
pub price: Decimal,
14+
/// Vault holding the purchased (or to be sold) tokens
15+
pub purse: Vault,
16+
/// Vault from which the payment for any purchases or sales will be withdrawn
17+
/// (must be in market's currency)
18+
pub payment: Vault
19+
}
20+
21+
#[allow(dead_code)]
22+
impl Order {
23+
pub fn token_symbol(&self) -> String {
24+
self.token.metadata()["symbol"].clone()
25+
}
26+
27+
pub fn currency(&self) -> String {
28+
self.payment.resource_def().metadata()["symbol"].clone()
29+
}
30+
31+
pub fn is_filled(&self) -> bool {
32+
if self.buy {
33+
self.payment.amount() == 0.into() || self.payment.amount() < self.price
34+
} else {
35+
self.purse.is_empty()
36+
}
37+
}
38+
39+
pub fn is_market_order(&self) -> bool {
40+
self.price == 0.into()
41+
}
42+
43+
pub fn is_buy_order(&self) -> bool {
44+
self.buy
45+
}
46+
47+
pub fn is_sell_order(&self) -> bool {
48+
!self.is_buy_order()
49+
}
50+
}
51+
52+
#[derive(NonFungibleData)]
53+
pub struct OrderTicket {
54+
pub order_number: i64,
55+
pub order_token_address: String,
56+
pub order_currency: String
57+
}
58+
59+
#[derive(Debug, TypeId, Encode, Decode, Describe)]
60+
pub struct MarketPrices {
61+
asset_prices: HashMap<String, Decimal>
62+
}
63+
64+
#[allow(dead_code)]
65+
impl MarketPrices {
66+
pub fn new() -> MarketPrices {
67+
MarketPrices { asset_prices: HashMap::new() }
68+
}
69+
70+
pub fn assets(&self) -> Vec<String> {
71+
self.asset_prices.keys().cloned().collect()
72+
}
73+
74+
pub fn get(&self, asset_symbol: String) -> Option<Decimal> {
75+
self.asset_prices.get(&asset_symbol).cloned()
76+
}
77+
78+
pub fn for_address(&self, address: Address) -> Option<Decimal> {
79+
let resource_def = ResourceDef::from(address);
80+
let asset_symbol = resource_def.metadata()["symbol"].clone();
81+
82+
self.get(asset_symbol)
83+
}
84+
85+
pub fn update(&mut self, asset_symbol: String, price: Decimal) {
86+
self.asset_prices.insert(asset_symbol, price);
87+
}
88+
}

0 commit comments

Comments
 (0)