1
// Copyright 2025 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;
18
use std::io::prelude::*;
19
use std::path::Path;
20

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

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

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

            
42
2
	let file_path = "resources/foreign_erc20_initcode.bin";
43
2

            
44
2
	if Path::new(file_path).exists() {
45
2
		let existing_content = fs::read(file_path).expect("Unable to read file");
46
2
		let existing_hex_code = hex::encode(existing_content);
47
2
		if existing_hex_code == hex::encode(&bytecode[..bytecode_end]) {
48
2
			return;
49
		}
50
	}
51

            
52
	let mut file = fs::File::create(file_path)
53
		.expect("Fail to create file resources/foreign_erc20_initcode.bin");
54
	file.write_all(&bytecode[..bytecode_end])
55
		.expect("fail to write bytecode in /foreign_erc20_initcode.bin");
56
2
}