diff --git a/lib/Cargo.lock b/lib/Cargo.lock index b29ffffde6..88daa544de 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -84,6 +84,7 @@ name = "ain-contracts" version = "0.1.0" dependencies = [ "anyhow", + "ethabi", "ethereum", "ethereum-types", "ethers", diff --git a/lib/ain-contracts/Cargo.toml b/lib/ain-contracts/Cargo.toml index bbfc25c5d4..47e9064d1a 100644 --- a/lib/ain-contracts/Cargo.toml +++ b/lib/ain-contracts/Cargo.toml @@ -14,6 +14,7 @@ hex.workspace = true sha3.workspace = true sp-core.workspace = true lazy_static.workspace = true +ethabi.workspace = true [build-dependencies] ethers.workspace = true diff --git a/lib/ain-contracts/dst20/input.json b/lib/ain-contracts/dst20/input.json deleted file mode 100644 index af623eba20..0000000000 --- a/lib/ain-contracts/dst20/input.json +++ /dev/null @@ -1 +0,0 @@ -{"object":"0x608060405234801562000010575f80fd5b506040516200175a3803806200175a8339818101604052810190620000369190620001eb565b81818160039081620000499190620004a5565b5080600490816200005b9190620004a5565b505050505062000589565b5f604051905090565b5f80fd5b5f80fd5b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b620000c7826200007f565b810181811067ffffffffffffffff82111715620000e957620000e86200008f565b5b80604052505050565b5f620000fd62000066565b90506200010b8282620000bc565b919050565b5f67ffffffffffffffff8211156200012d576200012c6200008f565b5b62000138826200007f565b9050602081019050919050565b5f5b838110156200016457808201518184015260208101905062000147565b5f8484015250505050565b5f620001856200017f8462000110565b620000f2565b905082815260208101848484011115620001a457620001a36200007b565b5b620001b184828562000145565b509392505050565b5f82601f830112620001d057620001cf62000077565b5b8151620001e28482602086016200016f565b91505092915050565b5f80604083850312156200020457620002036200006f565b5b5f83015167ffffffffffffffff81111562000224576200022362000073565b5b6200023285828601620001b9565b925050602083015167ffffffffffffffff81111562000256576200025562000073565b5b6200026485828601620001b9565b9150509250929050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680620002bd57607f821691505b602082108103620002d357620002d262000278565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620003377fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620002fa565b620003438683620002fa565b95508019841693508086168417925050509392505050565b5f819050919050565b5f819050919050565b5f6200038d6200038762000381846200035b565b62000364565b6200035b565b9050919050565b5f819050919050565b620003a8836200036d565b620003c0620003b78262000394565b84845462000306565b825550505050565b5f90565b620003d6620003c8565b620003e38184846200039d565b505050565b5b818110156200040a57620003fe5f82620003cc565b600181019050620003e9565b5050565b601f82111562000459576200042381620002d9565b6200042e84620002eb565b810160208510156200043e578190505b620004566200044d85620002eb565b830182620003e8565b50505b505050565b5f82821c905092915050565b5f6200047b5f19846008026200045e565b1980831691505092915050565b5f6200049583836200046a565b9150826002028217905092915050565b620004b0826200026e565b67ffffffffffffffff811115620004cc57620004cb6200008f565b5b620004d88254620002a5565b620004e58282856200040e565b5f60209050601f8311600181146200051b575f841562000506578287015190505b62000512858262000488565b86555062000581565b601f1984166200052b86620002d9565b5f5b8281101562000554578489015182556001820191506020850194506020810190506200052d565b8683101562000574578489015162000570601f8916826200046a565b8355505b6001600288020188555050505b505050505050565b6111c380620005975f395ff3fe608060405234801561000f575f80fd5b50600436106100a7575f3560e01c8063395093511161006f578063395093511461016557806370a082311461019557806395d89b41146101c5578063a457c2d7146101e3578063a9059cbb14610213578063dd62ed3e14610243576100a7565b806306fdde03146100ab578063095ea7b3146100c957806318160ddd146100f957806323b872dd14610117578063313ce56714610147575b5f80fd5b6100b3610273565b6040516100c09190610add565b60405180910390f35b6100e360048036038101906100de9190610b8e565b610303565b6040516100f09190610be6565b60405180910390f35b610101610325565b60405161010e9190610c0e565b60405180910390f35b610131600480360381019061012c9190610c27565b61032e565b60405161013e9190610be6565b60405180910390f35b61014f61035c565b60405161015c9190610c92565b60405180910390f35b61017f600480360381019061017a9190610b8e565b610364565b60405161018c9190610be6565b60405180910390f35b6101af60048036038101906101aa9190610cab565b61039a565b6040516101bc9190610c0e565b60405180910390f35b6101cd6103df565b6040516101da9190610add565b60405180910390f35b6101fd60048036038101906101f89190610b8e565b61046f565b60405161020a9190610be6565b60405180910390f35b61022d60048036038101906102289190610b8e565b6104e4565b60405161023a9190610be6565b60405180910390f35b61025d60048036038101906102589190610cd6565b610506565b60405161026a9190610c0e565b60405180910390f35b60606003805461028290610d41565b80601f01602080910402602001604051908101604052809291908181526020018280546102ae90610d41565b80156102f95780601f106102d0576101008083540402835291602001916102f9565b820191905f5260205f20905b8154815290600101906020018083116102dc57829003601f168201915b5050505050905090565b5f8061030d610588565b905061031a81858561058f565b600191505092915050565b5f600254905090565b5f80610338610588565b9050610345858285610752565b6103508585856107dd565b60019150509392505050565b5f6012905090565b5f8061036e610588565b905061038f8185856103808589610506565b61038a9190610d9e565b61058f565b600191505092915050565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b6060600480546103ee90610d41565b80601f016020809104026020016040519081016040528092919081815260200182805461041a90610d41565b80156104655780601f1061043c57610100808354040283529160200191610465565b820191905f5260205f20905b81548152906001019060200180831161044857829003601f168201915b5050505050905090565b5f80610479610588565b90505f6104868286610506565b9050838110156104cb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104c290610e41565b60405180910390fd5b6104d8828686840361058f565b60019250505092915050565b5f806104ee610588565b90506104fb8185856107dd565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036105fd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105f490610ecf565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160361066b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161066290610f5d565b60405180910390fd5b8060015f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040516107459190610c0e565b60405180910390a3505050565b5f61075d8484610506565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146107d757818110156107c9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107c090610fc5565b60405180910390fd5b6107d6848484840361058f565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361084b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161084290611053565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036108b9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108b0906110e1565b60405180910390fd5b6108c4838383610a49565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905081811015610947576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161093e9061116f565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550815f808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610a309190610c0e565b60405180910390a3610a43848484610a4e565b50505050565b505050565b505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a8a578082015181840152602081019050610a6f565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610aaf82610a53565b610ab98185610a5d565b9350610ac9818560208601610a6d565b610ad281610a95565b840191505092915050565b5f6020820190508181035f830152610af58184610aa5565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610b2a82610b01565b9050919050565b610b3a81610b20565b8114610b44575f80fd5b50565b5f81359050610b5581610b31565b92915050565b5f819050919050565b610b6d81610b5b565b8114610b77575f80fd5b50565b5f81359050610b8881610b64565b92915050565b5f8060408385031215610ba457610ba3610afd565b5b5f610bb185828601610b47565b9250506020610bc285828601610b7a565b9150509250929050565b5f8115159050919050565b610be081610bcc565b82525050565b5f602082019050610bf95f830184610bd7565b92915050565b610c0881610b5b565b82525050565b5f602082019050610c215f830184610bff565b92915050565b5f805f60608486031215610c3e57610c3d610afd565b5b5f610c4b86828701610b47565b9350506020610c5c86828701610b47565b9250506040610c6d86828701610b7a565b9150509250925092565b5f60ff82169050919050565b610c8c81610c77565b82525050565b5f602082019050610ca55f830184610c83565b92915050565b5f60208284031215610cc057610cbf610afd565b5b5f610ccd84828501610b47565b91505092915050565b5f8060408385031215610cec57610ceb610afd565b5b5f610cf985828601610b47565b9250506020610d0a85828601610b47565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610d5857607f821691505b602082108103610d6b57610d6a610d14565b5b50919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610da882610b5b565b9150610db383610b5b565b9250828201905080821115610dcb57610dca610d71565b5b92915050565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f775f8201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b5f610e2b602583610a5d565b9150610e3682610dd1565b604082019050919050565b5f6020820190508181035f830152610e5881610e1f565b9050919050565b7f45524332303a20617070726f76652066726f6d20746865207a65726f206164645f8201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b5f610eb9602483610a5d565b9150610ec482610e5f565b604082019050919050565b5f6020820190508181035f830152610ee681610ead565b9050919050565b7f45524332303a20617070726f766520746f20746865207a65726f2061646472655f8201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b5f610f47602283610a5d565b9150610f5282610eed565b604082019050919050565b5f6020820190508181035f830152610f7481610f3b565b9050919050565b7f45524332303a20696e73756666696369656e7420616c6c6f77616e63650000005f82015250565b5f610faf601d83610a5d565b9150610fba82610f7b565b602082019050919050565b5f6020820190508181035f830152610fdc81610fa3565b9050919050565b7f45524332303a207472616e736665722066726f6d20746865207a65726f2061645f8201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b5f61103d602583610a5d565b915061104882610fe3565b604082019050919050565b5f6020820190508181035f83015261106a81611031565b9050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f20616464725f8201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b5f6110cb602383610a5d565b91506110d682611071565b604082019050919050565b5f6020820190508181035f8301526110f8816110bf565b9050919050565b7f45524332303a207472616e7366657220616d6f756e74206578636565647320625f8201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b5f611159602683610a5d565b9150611164826110ff565b604082019050919050565b5f6020820190508181035f8301526111868161114d565b905091905056fea26469706673582212203aa41342448711bc720579d3c6d118ea09749c4045d7f6250a490a827d38de1e64736f6c63430008140033000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000005445354323000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054453543230000000000000000000000000000000000000000000000000000000"} diff --git a/lib/ain-contracts/src/lib.rs b/lib/ain-contracts/src/lib.rs index 22897ae61b..45242deaf7 100644 --- a/lib/ain-contracts/src/lib.rs +++ b/lib/ain-contracts/src/lib.rs @@ -39,6 +39,30 @@ fn get_bytecode(input: &str) -> Result> { hex::decode(&bytecode_raw[2..]).map_err(|e| format_err!(e.to_string())) } +pub fn get_dst20_deploy_input(init_bytecode: Vec, name: &str, symbol: &str) -> Result> { + let name = ethabi::Token::String(name.to_string()); + let symbol = ethabi::Token::String(symbol.to_string()); + + let constructor = ethabi::Constructor { + inputs: vec![ + ethabi::Param { + name: String::from("name"), + kind: ethabi::ParamType::String, + internal_type: None, + }, + ethabi::Param { + name: String::from("symbol"), + kind: ethabi::ParamType::String, + internal_type: None, + }, + ], + }; + + constructor + .encode_input(init_bytecode, &[name, symbol]) + .map_err(|e| format_err!(e)) +} + pub fn dst20_address_from_token_id(token_id: u64) -> Result { let number_str = format!("{:x}", token_id); let padded_number_str = format!("{number_str:0>38}"); @@ -58,8 +82,8 @@ pub fn intrinsics_address_from_id(id: u64) -> Result { #[derive(Clone)] pub struct Contract { pub codehash: H256, - pub bytecode: Vec, - pub input: Vec, + pub runtime_bytecode: Vec, + pub init_bytecode: Vec, } #[derive(Clone)] @@ -75,8 +99,8 @@ lazy_static::lazy_static! { FixedContract { contract: Contract { codehash: Blake2Hasher::hash(&bytecode), - bytecode, - input: Vec::new(), + runtime_bytecode: bytecode, + init_bytecode: Vec::new(), }, fixed_address: H160([ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, @@ -98,8 +122,8 @@ lazy_static::lazy_static! { FixedContract { contract: Contract { codehash: Blake2Hasher::hash(&bytecode), - bytecode, - input, + runtime_bytecode: bytecode, + init_bytecode: input, }, fixed_address: H160([ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, @@ -112,14 +136,14 @@ lazy_static::lazy_static! { let bytecode = solc_artifact_bytecode_str!( "dst20", "deployed_bytecode.json" ); - let input = get_bytecode(include_str!( - "../dst20/input.json" - )).unwrap(); + let input = solc_artifact_bytecode_str!( + "dst20", "bytecode.json" + ); Contract { codehash: Blake2Hasher::hash(&bytecode), - bytecode, - input, + runtime_bytecode: bytecode, + init_bytecode: input, } }; @@ -131,8 +155,8 @@ lazy_static::lazy_static! { Contract { codehash: Blake2Hasher::hash(&bytecode), - bytecode, - input: Vec::new(), + runtime_bytecode: bytecode, + init_bytecode: Vec::new(), } }; } diff --git a/lib/ain-evm/src/contract.rs b/lib/ain-evm/src/contract.rs index 1f9307b0df..8e42327c49 100644 --- a/lib/ain-evm/src/contract.rs +++ b/lib/ain-evm/src/contract.rs @@ -71,7 +71,7 @@ pub fn intrinsics_contract( Ok(DeployContractInfo { address: fixed_address, - bytecode: Bytes::from(contract.bytecode), + bytecode: Bytes::from(contract.runtime_bytecode), storage: vec![ (H256::from_low_u64_be(0), u256_to_h256(U256::one())), (H256::from_low_u64_be(1), u256_to_h256(evm_block_number)), @@ -92,7 +92,7 @@ pub fn transfer_domain_contract() -> Result { Ok(DeployContractInfo { address: fixed_address, - bytecode: Bytes::from(contract.bytecode), + bytecode: Bytes::from(contract.runtime_bytecode), storage: Vec::new(), }) } @@ -100,8 +100,8 @@ pub fn transfer_domain_contract() -> Result { pub fn dst20_contract( backend: &EVMBackend, address: H160, - name: String, - symbol: String, + name: &str, + symbol: &str, ) -> Result { match backend.get_account(&address) { None => {} @@ -113,21 +113,17 @@ pub fn dst20_contract( } } - let Contract { bytecode, .. } = get_dst20_contract(); + let Contract { + runtime_bytecode, .. + } = get_dst20_contract(); let storage = vec![ - ( - H256::from_low_u64_be(3), - get_abi_encoded_string(name.as_str()), - ), - ( - H256::from_low_u64_be(4), - get_abi_encoded_string(symbol.as_str()), - ), + (H256::from_low_u64_be(3), get_abi_encoded_string(name)), + (H256::from_low_u64_be(4), get_abi_encoded_string(symbol)), ]; Ok(DeployContractInfo { address, - bytecode: Bytes::from(bytecode), + bytecode: Bytes::from(runtime_bytecode), storage, }) } diff --git a/lib/ain-evm/src/evm.rs b/lib/ain-evm/src/evm.rs index 207d9ff49b..6341915caf 100644 --- a/lib/ain-evm/src/evm.rs +++ b/lib/ain-evm/src/evm.rs @@ -1,7 +1,8 @@ use std::{path::PathBuf, sync::Arc}; use ain_contracts::{ - get_dst20_contract, get_reserved_contract, get_transferdomain_contract, Contract, FixedContract, + get_dst20_contract, get_dst20_deploy_input, get_reserved_contract, get_transferdomain_contract, + Contract, FixedContract, }; use anyhow::format_err; use ethereum::{ @@ -426,12 +427,13 @@ impl EVMServices { address, bytecode, storage, - } = dst20_contract(executor.backend, address, name, symbol)?; + } = dst20_contract(executor.backend, address, &name, &symbol)?; if let Err(e) = executor.deploy_contract(address, bytecode.clone(), storage) { debug!("[construct_block] EvmOut failed with {e}"); } - let (tx, receipt) = dst20_deploy_contract_tx(token_id, &base_fee)?; + let (tx, receipt) = + dst20_deploy_contract_tx(token_id, &base_fee, &name, &symbol)?; (tx, Vec::new(), (receipt, Some(address))) } @@ -621,7 +623,9 @@ impl EVMServices { } pub fn reserve_dst20_namespace(&self, executor: &mut AinExecutor) -> Result<()> { - let Contract { bytecode, .. } = get_reserved_contract(); + let Contract { + runtime_bytecode, .. + } = get_reserved_contract(); let addresses = (1..=1024) .map(|token_id| ain_contracts::dst20_address_from_token_id(token_id).unwrap()) .collect::>(); @@ -631,14 +635,16 @@ impl EVMServices { "[reserve_dst20_namespace] Deploying address to {:#?}", address ); - executor.deploy_contract(address, bytecode.clone().into(), Vec::new())?; + executor.deploy_contract(address, runtime_bytecode.clone().into(), Vec::new())?; } Ok(()) } pub fn reserve_intrinsics_namespace(&self, executor: &mut AinExecutor) -> Result<()> { - let Contract { bytecode, .. } = get_reserved_contract(); + let Contract { + runtime_bytecode, .. + } = get_reserved_contract(); let addresses = (1..=127) .map(|token_id| ain_contracts::intrinsics_address_from_id(token_id).unwrap()) .collect::>(); @@ -648,21 +654,27 @@ impl EVMServices { "[reserve_intrinsics_namespace] Deploying address to {:#?}", address ); - executor.deploy_contract(address, bytecode.clone().into(), Vec::new())?; + executor.deploy_contract(address, runtime_bytecode.clone().into(), Vec::new())?; } Ok(()) } } -fn dst20_deploy_contract_tx(token_id: u64, base_fee: &U256) -> Result<(Box, ReceiptV3)> { +fn dst20_deploy_contract_tx( + token_id: u64, + base_fee: &U256, + name: &str, + symbol: &str, +) -> Result<(Box, ReceiptV3)> { let tx = TransactionV2::Legacy(LegacyTransaction { nonce: U256::from(token_id), gas_price: *base_fee, gas_limit: U256::from(u64::MAX), action: TransactionAction::Create, value: U256::zero(), - input: get_dst20_contract().input, + input: get_dst20_deploy_input(get_dst20_contract().init_bytecode, name, symbol) + .map_err(|e| format_err!(e))?, signature: TransactionSignature::new(27, LOWER_H256, LOWER_H256) .ok_or(format_err!("Invalid transaction signature format"))?, }) @@ -680,7 +692,7 @@ fn transfer_domain_deploy_contract_tx(base_fee: &U256) -> Result<(SignedTx, Rece gas_limit: U256::from(u64::MAX), action: TransactionAction::Create, value: U256::zero(), - input: get_transferdomain_contract().contract.input, + input: get_transferdomain_contract().contract.init_bytecode, signature: TransactionSignature::new(27, LOWER_H256, LOWER_H256) .ok_or(format_err!("Invalid transaction signature format"))?, })