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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#![no_std]
use ft_logic_io::instruction::*;
use ft_logic_io::*;
use ft_main_io::LogicAction;
use gstd::{exec, msg, prelude::*, prog::ProgramGenerator, ActorId};

mod messages;
use hashbrown::HashMap;
use messages::*;
use primitive_types::{H256, H512};

const GAS_STORAGE_CREATION: u64 = 3_000_000_000;
const DELAY: u32 = 600_000;

#[derive(Default)]
struct FTLogic {
    admin: ActorId,
    ftoken_id: ActorId,
    transaction_status: HashMap<H256, TransactionStatus>,
    instructions: HashMap<H256, (Instruction, Instruction)>,
    storage_code_hash: H256,
    id_to_storage: HashMap<String, ActorId>,
}

static mut FT_LOGIC: Option<FTLogic> = None;

impl FTLogic {
    /// The message received from the main contract.
    ///
    /// Arguments:
    /// * `transaction_hash`: the hash associated with that transaction;
    /// * `account`: the account that sent the message to the main contract;
    /// * `action`: the message payload.
    async fn message(&mut self, transaction_hash: H256, account: &ActorId, payload: &[u8]) {
        self.assert_main_contract();
        let action = LogicAction::decode(&mut &payload[..]).expect("Can't decode `Action`");

        let transaction_status = self
            .transaction_status
            .get(&transaction_hash)
            .unwrap_or(&TransactionStatus::InProgress);

        match transaction_status {
            // The transaction has already been made but there wasn't enough gas for a message reply.
            TransactionStatus::Success => reply_ok(),
            TransactionStatus::Failure => reply_err(),
            // The transaction took place for the first time
            // Or there was not enough gas to change the `TransactionStatus`.
            TransactionStatus::InProgress => {
                send_delayed_clear(transaction_hash);
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::InProgress);
                match action {
                    LogicAction::Mint { recipient, amount } => {
                        self.mint(transaction_hash, &recipient, amount).await;
                    }
                    LogicAction::Burn { sender, amount } => {
                        self.burn(transaction_hash, account, &sender, amount).await;
                    }
                    LogicAction::Transfer {
                        sender,
                        recipient,
                        amount,
                    } => {
                        self.transfer(transaction_hash, account, &sender, &recipient, amount)
                            .await;
                    }
                    LogicAction::Approve {
                        approved_account,
                        amount,
                    } => {
                        self.approve(transaction_hash, account, &approved_account, amount)
                            .await;
                    }
                    LogicAction::Permit {
                        owner_account,
                        approved_account,
                        amount,
                        permit_id,
                        sign,
                    } => {
                        let payload = PermitUnsigned {
                            owner_account,
                            approved_account,
                            amount,
                            permit_id,
                        };
                        self.permit(
                            transaction_hash,
                            &owner_account,
                            &approved_account,
                            amount,
                            &sign,
                            &payload,
                        )
                        .await;
                    }
                }
            }
        }
    }

    async fn mint(&mut self, transaction_hash: H256, recipient: &ActorId, amount: u128) {
        let recipient_storage = self.get_storage_address(recipient);

        let result =
            increase_balance(transaction_hash, &recipient_storage, recipient, amount).await;

        match result {
            Ok(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok()
            }
            Err(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Failure);
                reply_err();
            }
        }
    }

    async fn burn(
        &mut self,
        transaction_hash: H256,
        account: &ActorId,
        sender: &ActorId,
        amount: u128,
    ) {
        let sender_storage = self.get_storage_address(sender);

        let result =
            decrease_balance(transaction_hash, &sender_storage, account, sender, amount).await;

        match result {
            Ok(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok()
            }
            Err(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Failure);
                reply_err();
            }
        }
    }

    async fn transfer(
        &mut self,
        transaction_hash: H256,
        msg_source: &ActorId,
        sender: &ActorId,
        recipient: &ActorId,
        amount: u128,
    ) {
        let sender_storage = self.get_storage_address(sender);
        let recipient_storage = self.get_storage_address(recipient);

        if recipient_storage == sender_storage {
            self.transfer_single_storage(
                transaction_hash,
                &sender_storage,
                msg_source,
                sender,
                recipient,
                amount,
            )
            .await;
            return;
        }
        let (decrease_instruction, increase_instruction) = self
            .instructions
            .entry(transaction_hash)
            .or_insert_with(|| {
                let decrease_instruction = create_decrease_instruction(
                    transaction_hash,
                    msg_source,
                    &sender_storage,
                    sender,
                    amount,
                );
                let increase_instruction = create_increase_instruction(
                    transaction_hash,
                    &recipient_storage,
                    recipient,
                    amount,
                );
                (decrease_instruction, increase_instruction)
            });

        if decrease_instruction.start().await.is_err() {
            self.transaction_status
                .insert(transaction_hash, TransactionStatus::Failure);
            reply_err();
            return;
        }

        match increase_instruction.start().await {
            Err(_) => {
                if decrease_instruction.abort().await.is_ok() {
                    self.transaction_status
                        .insert(transaction_hash, TransactionStatus::Failure);
                    reply_err();
                }
            }
            Ok(_) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok();
            }
        }
    }

    async fn transfer_single_storage(
        &mut self,
        transaction_hash: H256,
        storage_id: &ActorId,
        msg_source: &ActorId,
        sender: &ActorId,
        recipient: &ActorId,
        amount: u128,
    ) {
        let result = transfer(
            transaction_hash,
            storage_id,
            msg_source,
            sender,
            recipient,
            amount,
        )
        .await;

        match result {
            Ok(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok()
            }
            Err(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Failure);
                reply_err();
            }
        }
    }

    async fn approve(
        &mut self,
        transaction_hash: H256,
        account: &ActorId,
        approved_account: &ActorId,
        amount: u128,
    ) {
        self.transaction_status
            .insert(transaction_hash, TransactionStatus::InProgress);
        let account_storage = self.get_storage_address(account);

        let result = approve(
            transaction_hash,
            &account_storage,
            account,
            approved_account,
            amount,
        )
        .await;

        match result {
            Ok(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok()
            }
            Err(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Failure);
                reply_err();
            }
        }
    }

    fn check_signature(message: &PermitUnsigned, owner: &ActorId, sign: &H512) -> bool {
        let message_u8 = message.encode();
        light_sr25519::verify(sign.as_bytes(), message_u8, owner).is_ok()
    }

    async fn permit(
        &mut self,
        transaction_hash: H256,
        owner: &ActorId,
        spender: &ActorId,
        amount: u128,
        owner_sign: &H512,
        message: &PermitUnsigned,
    ) {
        if !FTLogic::check_signature(message, owner, owner_sign) {
            reply_err();
            return;
        }

        self.transaction_status
            .insert(transaction_hash, TransactionStatus::InProgress);

        if !self
            .check_and_increment_permit_id(transaction_hash, owner, &message.permit_id)
            .await
        {
            self.transaction_status
                .insert(transaction_hash, TransactionStatus::Failure);
            reply_err();
            return;
        }

        let account_storage = self.get_storage_address(owner);
        let result = approve(transaction_hash, &account_storage, owner, spender, amount).await;
        match result {
            Ok(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Success);
                reply_ok()
            }
            Err(()) => {
                self.transaction_status
                    .insert(transaction_hash, TransactionStatus::Failure);
                reply_err();
            }
        }
    }

    fn update_storage_hash(&mut self, storage_code_hash: H256) {
        self.assert_admin();
        self.storage_code_hash = storage_code_hash;
    }

    fn get_storage_address(&mut self, address: &ActorId) -> ActorId {
        let encoded = hex::encode(address.as_ref());
        let id: String = encoded.chars().next().expect("Can't be None").to_string();
        if let Some(address) = self.id_to_storage.get(&id) {
            *address
        } else {
            let (_message_id, address) = ProgramGenerator::create_program_with_gas(
                self.storage_code_hash.into(),
                "",
                GAS_STORAGE_CREATION,
                0,
            )
            .expect("Error in creating Storage program");
            self.id_to_storage.insert(id, address);
            address
        }
    }

    async fn get_permit_id(&self, account: &ActorId) {
        let encoded = hex::encode(account.as_ref());
        let id: String = encoded.chars().next().expect("Can't be None").to_string();
        if let Some(address) = self.id_to_storage.get(&id) {
            let permit_id = get_permit_id(address, account).await;
            msg::reply(FTLogicEvent::PermitId(permit_id), 0)
                .expect("Error in a reply `FTLogicEvent::PermitId`");
        } else {
            msg::reply(FTLogicEvent::PermitId(0), 0)
                .expect("Error in a reply `FTLogicEvent::PermitId`");
        }
    }

    async fn check_and_increment_permit_id(
        &self,
        transaction_hash: H256,
        account: &ActorId,
        expected_id: &u128,
    ) -> bool {
        let encoded = hex::encode(account.as_ref());
        let id: String = encoded.chars().next().expect("Can't be None").to_string();
        if let Some(address) = self.id_to_storage.get(&id) {
            return check_and_increment_permit_id(address, transaction_hash, account, *expected_id)
                .await;
        }
        false
    }

    async fn get_balance(&self, account: &ActorId) {
        let encoded = hex::encode(account.as_ref());
        let id: String = encoded.chars().next().expect("Can't be None").to_string();
        if let Some(address) = self.id_to_storage.get(&id) {
            let balance = get_balance(address, account).await;
            msg::reply(FTLogicEvent::Balance(balance), 0)
                .expect("Error in a reply `FTLogicEvent::Balance`");
        } else {
            msg::reply(FTLogicEvent::Balance(0), 0)
                .expect("Error in a reply `FTLogicEvent::Balance`");
        }
    }

    fn clear(&mut self, transaction_hash: H256) {
        self.transaction_status.remove(&transaction_hash);
    }

    fn assert_main_contract(&self) {
        assert_eq!(
            self.ftoken_id,
            msg::source(),
            "Only main fungible token contract can send that message"
        );
    }

    fn assert_admin(&self) {
        assert_eq!(
            self.admin,
            msg::source(),
            "Only admin can send that message"
        );
    }
}

#[gstd::async_main]
async fn main() {
    let action: FTLogicAction = msg::load().expect("Error in loading `StorageAction`");
    let logic: &mut FTLogic = unsafe { FT_LOGIC.get_or_insert(Default::default()) };
    match action {
        FTLogicAction::Message {
            transaction_hash,
            account,
            payload,
        } => logic.message(transaction_hash, &account, &payload).await,
        FTLogicAction::UpdateStorageCodeHash(storage_code_hash) => {
            logic.update_storage_hash(storage_code_hash)
        }
        FTLogicAction::Clear(transaction_hash) => logic.clear(transaction_hash),
        FTLogicAction::GetBalance(account) => logic.get_balance(&account).await,
        FTLogicAction::GetPermitId(account) => logic.get_permit_id(&account).await,
        _ => {}
    }
}

#[no_mangle]
unsafe extern "C" fn init() {
    let init_config: InitFTLogic = msg::load().expect("Unable to decode `InitFTLogic`");
    let ft_logic = FTLogic {
        admin: init_config.admin,
        storage_code_hash: init_config.storage_code_hash,
        ftoken_id: msg::source(),
        ..Default::default()
    };
    FT_LOGIC = Some(ft_logic);
}

fn reply_err() {
    msg::reply(FTLogicEvent::Err, 0).expect("Error in sending a reply `FTLogicEvent::Err`");
}

fn reply_ok() {
    msg::reply(FTLogicEvent::Ok, 0).expect("Error in sending a reply `FTLogicEvent::Ok`");
}

fn send_delayed_clear(transaction_hash: H256) {
    msg::send_delayed(
        exec::program_id(),
        FTLogicAction::Clear(transaction_hash),
        0,
        DELAY,
    )
    .expect("Error in sending a delayled message `FTStorageAction::Clear`");
}

#[no_mangle]
extern "C" fn state() {
    let logic = unsafe { FT_LOGIC.as_ref().expect("FTLogic is not initialized") };
    let logic_state = FTLogicState {
        admin: logic.admin,
        ftoken_id: logic.ftoken_id,
        transaction_status: logic
            .transaction_status
            .iter()
            .map(|(key, value)| (*key, value.clone()))
            .collect(),
        instructions: logic
            .instructions
            .iter()
            .map(|(key, value)| (*key, value.clone()))
            .collect(),
        storage_code_hash: logic.storage_code_hash,
        id_to_storage: logic
            .id_to_storage
            .iter()
            .map(|(key, value)| (key.clone(), *value))
            .collect(),
    };
    msg::reply(logic_state, 0).expect("Failed to share state");
}