Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evm: Generate DST20 deploy TX input #2426

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/ain-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/ain-contracts/dst20/input.json

This file was deleted.

50 changes: 37 additions & 13 deletions lib/ain-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ fn get_bytecode(input: &str) -> Result<Vec<u8>> {
hex::decode(&bytecode_raw[2..]).map_err(|e| format_err!(e.to_string()))
}

pub fn get_dst20_deploy_input(init_bytecode: Vec<u8>, name: &str, symbol: &str) -> Result<Vec<u8>> {
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<H160> {
let number_str = format!("{:x}", token_id);
let padded_number_str = format!("{number_str:0>38}");
Expand All @@ -58,8 +82,8 @@ pub fn intrinsics_address_from_id(id: u64) -> Result<H160> {
#[derive(Clone)]
pub struct Contract {
pub codehash: H256,
pub bytecode: Vec<u8>,
pub input: Vec<u8>,
pub runtime_bytecode: Vec<u8>,
pub init_bytecode: Vec<u8>,
}

#[derive(Clone)]
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
};

Expand All @@ -131,8 +155,8 @@ lazy_static::lazy_static! {

Contract {
codehash: Blake2Hasher::hash(&bytecode),
bytecode,
input: Vec::new(),
runtime_bytecode: bytecode,
init_bytecode: Vec::new(),
}
};
}
Expand Down
24 changes: 10 additions & 14 deletions lib/ain-evm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -92,16 +92,16 @@ pub fn transfer_domain_contract() -> Result<DeployContractInfo> {

Ok(DeployContractInfo {
address: fixed_address,
bytecode: Bytes::from(contract.bytecode),
bytecode: Bytes::from(contract.runtime_bytecode),
storage: Vec::new(),
})
}

pub fn dst20_contract(
backend: &EVMBackend,
address: H160,
name: String,
symbol: String,
name: &str,
symbol: &str,
) -> Result<DeployContractInfo> {
match backend.get_account(&address) {
None => {}
Expand All @@ -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,
})
}
Expand Down
Loading