1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use eosio::*;
use eosio_cdt::*;

#[derive(Read, Write, NumBytes, Copy, Clone)]
pub struct Account {
    pub balance: Asset,
}

impl Table for Account {
    type Row = Self;

    const NAME: TableName = TableName::new(n!("accounts"));

    fn primary_key(row: &Self::Row) -> u64 {
        row.balance.symbol.code().as_u64()
    }
}

#[derive(Read, Write, NumBytes, Copy, Clone)]
pub struct CurrencyStats {
    pub supply: Asset,
    pub max_supply: Asset,
    pub issuer: AccountName,
}

impl Table for CurrencyStats {
    type Row = Self;

    const NAME: TableName = TableName::new(n!("stat"));

    fn primary_key(row: &Self::Row) -> u64 {
        row.supply.symbol.code().as_u64()
    }
}

#[eosio::action]
fn create(issuer: AccountName, max_supply: Asset) {
    let code = current_receiver();
    require_auth(code);

    let symbol = max_supply.symbol;
    assert!(symbol.is_valid(), "invalid symbol name");
    assert!(max_supply.is_valid(), "invalid supply");
    assert!(max_supply.amount > 0, "max-supply must be positive");

    let symbol_code = symbol.code();
    let stats_table = CurrencyStats::table(code, symbol_code);
    assert!(
        stats_table.find(symbol_code).is_none(),
        "token with symbol already exists",
    );

    let stats = CurrencyStats {
        supply: Asset { amount: 0, symbol },
        max_supply,
        issuer,
    };

    stats_table.emplace(code, stats).expect("write");
}

#[eosio::action]
fn issue(to: AccountName, quantity: Asset, memo: String) {
    let symbol = quantity.symbol;
    assert!(symbol.is_valid(), "invalid symbol name");
    assert!(memo.len() <= 256, "memo has more than 256 bytes");

    let code = current_receiver();
    let symbol_code = symbol.code();
    let stats_table = CurrencyStats::table(code, symbol_code);
    let cursor = stats_table
        .find(symbol_code)
        .expect("token with symbol does not exist, create token before issue");

    let mut st = cursor.get().expect("read");
    assert!(
        to == st.issuer,
        "tokens can only be issued to issuer account",
    );
    require_auth(st.issuer);
    assert!(quantity.is_valid(), "invalid quantity");
    assert!(quantity.amount > 0, "must issue positive quantity");
    assert!(
        quantity.symbol == st.supply.symbol,
        "symbol precision mismatch",
    );
    assert!(
        quantity.amount <= st.max_supply.amount - st.supply.amount,
        "quantity exceeds available supply",
    );

    st.supply += quantity;
    cursor.modify(Payer::Same, st).expect("write");

    add_balance(st.issuer, quantity, st.issuer);

    if to != st.issuer {
        let action = Transfer {
            from: st.issuer,
            to,
            quantity,
            memo,
        };
        send_inline_action(&action.to_action(current_receiver(), vec![
            PermissionLevel {
                actor: st.issuer,
                permission: n!("active").into(),
            },
        ]))
        .expect("failed to send inline action");
    }
}

#[eosio::action]
fn retire(quantity: Asset, memo: String) {
    let symbol = quantity.symbol;
    assert!(symbol.is_valid(), "invalid symbol name");
    assert!(memo.len() <= 256, "memo has more than 256 bytes");

    let code = current_receiver();
    let symbol_code = symbol.code();
    let stats_table = CurrencyStats::table(code, symbol_code);
    let cursor = stats_table
        .find(symbol_code)
        .expect("token with symbol does not exist");

    let mut st = cursor.get().expect("error reading stats table");
    require_auth(st.issuer);
    assert!(quantity.is_valid(), "invalid quantity");
    assert!(quantity.amount > 0, "must retire positive quantity");
    assert!(symbol == st.supply.symbol, "symbol precision mismatch");

    st.supply -= quantity;
    cursor.modify(Payer::Same, st).expect("write");
    sub_balance(st.issuer, quantity);
}

#[eosio::action]
fn transfer(from: AccountName, to: AccountName, quantity: Asset, memo: String) {
    assert!(from != to, "cannot transfer to self");
    require_auth(from);
    assert!(is_account(to), "to account does not exist");

    let code = current_receiver();
    let symbol_code = quantity.symbol.code();
    let stats_table = CurrencyStats::table(code, symbol_code);
    let cursor = stats_table
        .find(symbol_code)
        .expect("token with symbol does not exist");
    let st = cursor.get().expect("read");

    require_recipient(from);
    require_recipient(to);

    assert!(quantity.is_valid(), "invalid quantity");
    assert!(quantity.amount > 0, "must transfer positive quantity");
    assert!(
        quantity.symbol == st.supply.symbol,
        "symbol precision mismatch",
    );
    assert!(memo.len() <= 256, "memo has more than 256 bytes");

    let payer = if has_auth(to) { to } else { from };

    sub_balance(from, quantity);
    add_balance(to, quantity, payer);
}

fn sub_balance(owner: AccountName, value: Asset) {
    let code = current_receiver();
    let table = Account::table(code, owner);
    let cursor = table
        .find(value.symbol.code())
        .expect("no balance object found");
    let mut from = cursor.get().expect("read");
    assert!(from.balance.amount >= value.amount, "overdrawn balance");

    from.balance -= value;
    cursor.modify(Payer::Same, from).expect("write");
}

fn add_balance(owner: AccountName, value: Asset, ram_payer: AccountName) {
    let code = current_receiver();
    let accounts_table = Account::table(code, owner);
    let cursor = accounts_table.find(value.symbol.code());
    match cursor {
        Some(cursor) => {
            let mut account = cursor.get().expect("read");
            account.balance += value;
            cursor
                .modify(Payer::New(ram_payer), &account)
                .expect("write");
        }
        None => {
            let account = Account { balance: value };
            accounts_table.emplace(ram_payer, &account).expect("write");
        }
    }
}

#[eosio::action]
fn open(owner: AccountName, symbol: Symbol, ram_payer: AccountName) {
    require_auth(ram_payer);
    assert!(is_account(owner), "owner account does not exist");

    let code = current_receiver();
    let symbol_code = symbol.code();

    let stats_table = CurrencyStats::table(code, symbol_code);
    let st = stats_table
        .find(symbol_code)
        .expect("symbol does not exist")
        .get()
        .expect("read");
    assert!(st.supply.symbol == symbol, "symbol precision mismatch");

    let accts_table = Account::table(code, owner);
    if accts_table.find(symbol_code).is_none() {
        let account = Account {
            balance: Asset { amount: 0, symbol },
        };
        accts_table.emplace(ram_payer, account).expect("write");
    }
}

#[eosio::action]
fn close(owner: AccountName, symbol: Symbol) {
    require_auth(owner);
    let code = current_receiver();
    let accts_table = Account::table(code, owner);
    let accts_cursor = accts_table.find(symbol.code()).expect(
        "Balance row already deleted or never existed. Action won't have any \
         effect.",
    );
    let account = accts_cursor.get().expect("read");
    assert!(
        account.balance.amount == 0,
        "Cannot close because the balance is not zero.",
    );
    accts_cursor.erase().expect("read");
}

eosio::abi!(create, issue, transfer, open, close, retire);