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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{account::Account, compile, executor::FakeExecutor};
use diem_types::transaction::WriteSetPayload;

pub fn add_currency_to_system(
    executor: &mut FakeExecutor,
    currency_code_to_register: &str,
    dr_account: &Account,
    current_dr_sequence_number: u64,
) -> u64 {
    let mut dr_sequence_number = current_dr_sequence_number;

    {
        let compiled_script = {
            let script = "
            import 0x1.DiemTransactionPublishingOption;
            main(config: signer) {
                DiemTransactionPublishingOption.set_open_module(&config, false);
                return;
            }
            ";
            compile::compile_script("file_name", script, vec![])
        };

        let txn = dr_account
            .transaction()
            .script(compiled_script)
            .sequence_number(dr_sequence_number)
            .sign();

        executor.execute_and_apply(txn);
    };

    executor.new_block();

    dr_sequence_number += 1;

    let (compiled_module, module) = {
        let module = format!(
            r#"
            module 0x1.{currency_code} {{
                import 0x1.Diem;
                import 0x1.FixedPoint32;
                struct {currency_code} has store {{ x: bool }}
                public init(dr_account: &signer, tc_account: &signer) {{
                    Diem.register_SCS_currency<Self.{currency_code}>(
                        move(dr_account),
                        move(tc_account),
                        FixedPoint32.create_from_rational(1, 1),
                        100,
                        1000,
                        h"{currency_code_hex}"
                    );
                    return;
                }}
            }}
            "#,
            currency_code = currency_code_to_register,
            currency_code_hex = hex::encode(currency_code_to_register),
        );

        compile::compile_module("this_is_a_filename", &module)
    };

    let txn = dr_account
        .transaction()
        .module(module)
        .sequence_number(dr_sequence_number)
        .sign();

    executor.execute_and_apply(txn);

    dr_sequence_number += 1;

    {
        let compiled_script = {
            let script = format!(
                r#"
            import 0x1.{currency_code};
            main(dr_account: signer, tc_account: signer) {{
                {currency_code}.init(&dr_account, &tc_account);
                return;
            }}
            "#,
                currency_code = currency_code_to_register
            );
            compile::compile_script("file_name", &script, vec![compiled_module])
        };

        let write_set_payload = WriteSetPayload::Script {
            execute_as: *Account::new_blessed_tc().address(),
            script: compiled_script,
        };
        let txn = dr_account
            .transaction()
            .write_set(write_set_payload)
            .sequence_number(dr_sequence_number)
            .sign();
        executor.execute_and_apply(txn);
    };

    executor.new_block();

    dr_sequence_number + 1
}