1#![no_std]
2
3use gmeta::{InOut, Metadata};
4use gstd::collections::{btree_map::Entry, BTreeMap};
5use gstd::prelude::*;
6use gstd::ActorId;
7use gstd::MessageId;
8
9impl Metadata for Contract {
10 type Init = ();
11 type Handle = InOut<Action, EventResult>;
12 type Others = ();
13 type Reply = ();
14 type Signal = ();
15 type State = InOut<Query, Reply>;
16}
17
18type EventResult = Result<Event, Error>;
19
20#[derive(Clone, Default, Encode, Decode, TypeInfo)]
21pub struct Contract(pub BTreeMap<String, String>);
22
23impl Contract {
24 pub fn add_url(&mut self, code: String, url: String) {
25 match self.0.entry(code) {
26 Entry::Vacant(v) => {
27 v.insert(url);
28 }
29 Entry::Occupied(_) => {
30 panic!("failed to add url: code exists")
31 }
32 }
33 }
34 pub fn get_url(&self, code: String) -> Option<String> {
35 self.0.get(&code).cloned()
36 }
37}
38
39#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
40pub enum Action {
41 ValueAvailable,
42 WithdrawAll,
43 Withdraw(u128),
44 Deposit,
45 SendValue { to: ActorId, value: u128 },
46 SendValueTwice { to: ActorId, value: u128 },
47 AddUrl { code: String, url: String },
48}
49
50#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
51pub enum Error {
52 CodeExists,
53 CodeNotFound,
54 ValueNotAvailable,
55 WithdrawFailed,
56}
57
58#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
59pub enum Event {
60 ValueAvailable(u128),
61 Withdrew(u128),
62 Deposited(u128),
63 Added { code: String, url: String },
64 Log(String),
65 SentValue { to: ActorId, value: u128 },
66 SentValueTwice { to: ActorId, value: u128 },
67}
68
69#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
70pub enum Query {
71 All,
72 Code(String),
73 Whoami,
74 BlockNumber,
75 BlockTimestamp,
76 ProgramId,
77 MessageId,
78 }
80
81#[derive(Encode, Decode, TypeInfo)]
82pub enum Reply {
83 All(Contract),
84 Url(Option<String>),
85 Whoami(ActorId),
86 BlockNumber(u32),
87 BlockTimestamp(u64),
88 ProgramId(ActorId),
89 MessageId(MessageId),
90 }