Skip to content

Commit

Permalink
fix(forge): getCode cheatcode (gakonst#649)
Browse files Browse the repository at this point in the history
* fix getCode

* fmt
  • Loading branch information
brockelmore authored Jan 31, 2022
1 parent 16b20da commit be2cfad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
24 changes: 12 additions & 12 deletions evm-adapters/src/sputnik/cheatcodes/cheatcode_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};
use std::collections::BTreeMap;

use serde::Deserialize;
use std::{fs::File, io::Read, path::Path};

use sputnik::{
Expand All @@ -28,12 +27,11 @@ use ethers::{
abi::{RawLog, Token},
contract::EthLogDecode,
core::{abi::AbiDecode, k256::ecdsa::SigningKey, utils},
prelude::artifacts::deserialize_bytes,
signers::{LocalWallet, Signer},
solc::ProjectPathsConfig,
solc::{artifacts::CompactContractBytecode, ProjectPathsConfig},
types::{Address, H160, H256, U256},
};
use ethers_core::types::Bytes;

use std::{convert::Infallible, str::FromStr};

use crate::sputnik::cheatcodes::{
Expand Down Expand Up @@ -656,12 +654,6 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> CheatcodeStackExecutor<'a, 'b, B, P>
HEVMCalls::GetCode(inner) => {
self.add_debug(CheatOp::GETCODE);

#[derive(Deserialize)]
struct ContractFile {
#[serde(deserialize_with = "deserialize_bytes")]
bin: Bytes,
}

let path = if inner.0.ends_with(".json") {
Path::new(&inner.0).to_path_buf()
} else {
Expand All @@ -686,9 +678,17 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> CheatcodeStackExecutor<'a, 'b, B, P>
Err(e) => return evm_error(&e.to_string()),
}

match serde_json::from_str::<ContractFile>(&data) {
match serde_json::from_str::<CompactContractBytecode>(&data) {
Ok(contract_file) => {
res = ethers::abi::encode(&[Token::Bytes(contract_file.bin.to_vec())]);
if let Some(bin) =
contract_file.bytecode.and_then(|bcode| bcode.object.into_bytes())
{
res = ethers::abi::encode(&[Token::Bytes(bin.to_vec())]);
} else {
return evm_error(
"No bytecode for contract. is it abstract or unlinked?",
)
}
}
Err(e) => return evm_error(&e.to_string()),
}
Expand Down
12 changes: 10 additions & 2 deletions evm-adapters/testdata/Contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
"stateMutability": "pure"
}
],
"bin": "0x608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80637ddeef2414602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60006007905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea2646970667358221220521a806ba8927fda1a9b7bf0458b0a0abf456e4611953e01489bee91783418b064736f6c634300080a0033",
"bin-runtime": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80637ddeef2414602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60006007905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea2646970667358221220521a806ba8927fda1a9b7bf0458b0a0abf456e4611953e01489bee91783418b064736f6c634300080a0033"
"bytecode": {
"object": "0x608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80637ddeef2414602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60006007905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea2646970667358221220521a806ba8927fda1a9b7bf0458b0a0abf456e4611953e01489bee91783418b064736f6c634300080a0033",
"sourceMap": "24597:616:0:-:0;;;24625:60;;;-1:-1:-1;;;;;;24625:60:0;24642:42;24625:60;;;24597:616;;;;;;;;;;;;;;;;",
"linkReferences": {}
},
"deployed_bytecode": {
"object": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80637ddeef2414602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60006007905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea2646970667358221220521a806ba8927fda1a9b7bf0458b0a0abf456e4611953e01489bee91783418b064736f6c634300080a0033",
"sourceMap": "24597:616:0:-:0;;;24625:60;;;-1:-1:-1;;;;;;24625:60:0;24642:42;24625:60;;;24597:616;;;;;;;;;;;;;;;;",
"linkReferences": {}
}
}

0 comments on commit be2cfad

Please sign in to comment.