|
| 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