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
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use crate::*;
use astar_primitives::{
    evm::EVM_REVERT_CODE,
    genesis::{get_from_seed, GenesisAccount},
};
use sp_core::crypto::Ss58Codec;

/// Provides the JSON representation of predefined genesis config for given `id`.
pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<Vec<u8>> {
    let genesis = match id.try_into() {
        Ok("development") => default_config(),
        _ => return None,
    };
    Some(
        serde_json::to_string(&genesis)
            .expect("serialization to json is expected to work. qed.")
            .into_bytes(),
    )
}

/// Get the default genesis config for the local runtime.
pub fn default_config() -> serde_json::Value {
    let alice = GenesisAccount::<sr25519::Public>::from_seed("Alice");
    let bob = GenesisAccount::<sr25519::Public>::from_seed("Bob");
    let charlie = GenesisAccount::<sr25519::Public>::from_seed("Charlie");
    let dave = GenesisAccount::<sr25519::Public>::from_seed("Dave");
    let eve = GenesisAccount::<sr25519::Public>::from_seed("Eve");

    let balances: Vec<(AccountId, Balance)> = vec![
        (alice.account_id(), 1_000_000_000 * AST),
        (bob.account_id(), 1_000_000_000 * AST),
        (
            TreasuryPalletId::get().into_account_truncating(),
            1_000_000_000 * AST,
        ),
        (
            CommunityTreasuryPalletId::get().into_account_truncating(),
            1_000_000_000 * AST,
        ),
        (
            // Private key: 0x01ab6e801c06e59ca97a14fc0a1978b27fa366fc87450e0b65459dd3515b7391
            // H160 public address: 0xaaafB3972B05630fCceE866eC69CdADd9baC2771
            AccountId::from_ss58check("5FQedkNQcF2fJPwkB6Z1ZcMgGti4vcJQNs6x85YPv3VhjBBT").unwrap(),
            1_000_000_000 * AST,
        ),
    ];

    let accounts = vec![&alice, &bob, &charlie, &dave, &eve]
        .iter()
        .map(|x| x.account_id())
        .collect::<Vec<_>>();

    let config = RuntimeGenesisConfig {
        system: Default::default(),
        sudo: SudoConfig {
            key: Some(alice.account_id()),
        },
        balances: BalancesConfig { balances },
        vesting: VestingConfig { vesting: vec![] },
        aura: AuraConfig {
            authorities: vec![get_from_seed::<AuraId>("Alice")],
        },
        grandpa: GrandpaConfig {
            authorities: vec![(get_from_seed::<GrandpaId>("Alice"), 1)],
            ..Default::default()
        },
        evm: EVMConfig {
            // We need _some_ code inserted at the precompile address so that
            // the evm will actually call the address.
            accounts: Precompiles::used_addresses_h160()
                .map(|addr| {
                    (
                        addr,
                        fp_evm::GenesisAccount {
                            nonce: Default::default(),
                            balance: Default::default(),
                            storage: Default::default(),
                            code: EVM_REVERT_CODE.into(),
                        },
                    )
                })
                .collect(),
            ..Default::default()
        },
        ethereum: Default::default(),
        assets: Default::default(),
        transaction_payment: Default::default(),
        dapp_staking: DappStakingConfig {
            reward_portion: vec![
                Permill::from_percent(40),
                Permill::from_percent(30),
                Permill::from_percent(20),
                Permill::from_percent(10),
            ],
            slot_distribution: vec![
                Permill::from_percent(10),
                Permill::from_percent(20),
                Permill::from_percent(30),
                Permill::from_percent(40),
            ],
            tier_thresholds: vec![
                TierThreshold::DynamicPercentage {
                    percentage: Perbill::from_parts(35_700_000), // 3.57%
                    minimum_required_percentage: Perbill::from_parts(23_800_000), // 2.38%
                },
                TierThreshold::DynamicPercentage {
                    percentage: Perbill::from_parts(8_900_000), // 0.89%
                    minimum_required_percentage: Perbill::from_parts(6_000_000), // 0.6%
                },
                TierThreshold::DynamicPercentage {
                    percentage: Perbill::from_parts(23_800_000), // 2.38%
                    minimum_required_percentage: Perbill::from_parts(17_900_000), // 1.79%
                },
                TierThreshold::FixedPercentage {
                    required_percentage: Perbill::from_parts(600_000), // 0.06%
                },
            ],
            slots_per_tier: vec![10, 20, 30, 40],
            safeguard: Some(false),
            ..Default::default()
        },
        inflation: InflationConfig {
            params: InflationParameters {
                max_inflation_rate: Perquintill::from_percent(7),
                treasury_part: Perquintill::from_percent(5),
                collators_part: Perquintill::from_percent(3),
                dapps_part: Perquintill::from_percent(20),
                base_stakers_part: Perquintill::from_percent(25),
                adjustable_stakers_part: Perquintill::from_percent(35),
                bonus_part: Perquintill::from_percent(12),
                ideal_staking_rate: Perquintill::from_percent(50),
            },
            ..Default::default()
        },
        council_membership: CouncilMembershipConfig {
            members: accounts
                .clone()
                .try_into()
                .expect("Should support at least 5 members."),
            phantom: Default::default(),
        },
        technical_committee_membership: TechnicalCommitteeMembershipConfig {
            members: accounts[..3]
                .to_vec()
                .try_into()
                .expect("Should support at least 3 members."),
            phantom: Default::default(),
        },
        community_council_membership: CommunityCouncilMembershipConfig {
            members: accounts
                .try_into()
                .expect("Should support at least 5 members."),
            phantom: Default::default(),
        },
        council: Default::default(),
        technical_committee: Default::default(),
        community_council: Default::default(),
        democracy: Default::default(),
        treasury: Default::default(),
        community_treasury: Default::default(),
    };

    serde_json::to_value(&config).expect("Could not build genesis config.")
}