1
// Copyright 2024 Moonbeam Foundation.
2
// This file is part of Moonbeam.
3

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

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

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

            
17
use std::fs::File;
18
use std::io::prelude::*;
19

            
20
// Length of encoded constructor parameters
21
const PARAMS_LEN: usize = 256;
22

            
23
2
fn main() {
24
2
	let hex_str = include_str!("resources/foreign_erc20_initcode.hex");
25
2
	let prefix_0x = hex_str.chars().nth(1) == Some('x');
26
2
	let bytecode = if prefix_0x {
27
2
		hex::decode(&hex_str[2..])
28
	} else {
29
		hex::decode(hex_str)
30
	}
31
2
	.expect("fail to decode hexadecimal string in file foreign_erc20_initcode.hex");
32

            
33
	// The encoded parameters at the end of the initializer bytecode should be removed,
34
	// (the runtime will append the constructor parameters dynamically).
35
2
	let bytecode_end = if bytecode.len() > PARAMS_LEN {
36
2
		bytecode.len() - PARAMS_LEN
37
	} else {
38
		0
39
	};
40

            
41
2
	let mut file = File::create("resources/foreign_erc20_initcode.bin")
42
2
		.expect("Fail to create file resources/foreign_erc20_initcode.bin");
43
2
	file.write_all(&bytecode[..bytecode_end])
44
2
		.expect("fail to write bytecode in /foreign_erc20_initcode.bin");
45
2
}