Skip to content

Commit

Permalink
chore: Make consistent forge script artifacts (foundry-rs#2490)
Browse files Browse the repository at this point in the history
* chore: replace skip with Default

* chore: replace tx with transaction

* feat: add script and artifact to testdata

* fix: rename 'type'; change defaults from null

* fix: compiler warning

* chore: forge fmt
  • Loading branch information
odyslam authored Jul 28, 2022
1 parent 9bca0e5 commit 2f4dc6c
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 17 deletions.
46 changes: 29 additions & 17 deletions cli/src/cmd/forge/script/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,35 +239,47 @@ impl Drop for ScriptSequence {
#[serde(rename_all = "camelCase")]
pub struct TransactionWithMetadata {
pub hash: Option<TxHash>,
#[serde(rename = "type")]
#[serde(rename = "transactionType")]
pub opcode: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "default_string")]
pub contract_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "default_address")]
pub contract_address: Option<Address>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "default_string")]
pub function: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "default_vec_of_strings")]
pub arguments: Option<Vec<String>>,
pub tx: TypedTransaction,
pub transaction: TypedTransaction,
}

fn default_string() -> Option<String> {
Some("".to_owned())
}

fn default_address() -> Option<Address> {
Some(Address::from_low_u64_be(0))
}

fn default_vec_of_strings() -> Option<Vec<String>> {
Some(vec![])
}

impl TransactionWithMetadata {
pub fn new(
tx: TypedTransaction,
transaction: TypedTransaction,
result: &ScriptResult,
local_contracts: &BTreeMap<Address, (String, &Abi)>,
decoder: &CallTraceDecoder,
) -> eyre::Result<Self> {
let mut metadata = Self { tx, ..Default::default() };
let mut metadata = Self { transaction, ..Default::default() };

if let Some(NameOrAddress::Address(to)) = metadata.tx.to().cloned() {
if let Some(NameOrAddress::Address(to)) = metadata.transaction.to().cloned() {
if to == DEFAULT_CREATE2_DEPLOYER {
metadata.set_create(true, Address::from_slice(&result.returned), local_contracts)
} else {
metadata.set_call(to, local_contracts, decoder)?;
}
} else if metadata.tx.to().is_none() {
} else if metadata.transaction.to().is_none() {
metadata.set_create(
false,
result.address.expect("There should be a contract address."),
Expand Down Expand Up @@ -301,7 +313,7 @@ impl TransactionWithMetadata {
) -> eyre::Result<()> {
self.opcode = "CALL".to_string();

if let Some(data) = self.tx.data() {
if let Some(data) = self.transaction.data() {
if data.0.len() >= 4 {
if let Some((contract_name, abi)) = local_contracts.get(&target) {
// This CALL is made to a local contract.
Expand Down Expand Up @@ -339,23 +351,23 @@ impl TransactionWithMetadata {
}

pub fn set_tx(&mut self, tx: TypedTransaction) {
self.tx = tx;
self.transaction = tx;
}

pub fn change_type(&mut self, is_legacy: bool) {
self.tx = if is_legacy {
TypedTransaction::Legacy(self.tx.clone().into())
self.transaction = if is_legacy {
TypedTransaction::Legacy(self.transaction.clone().into())
} else {
TypedTransaction::Eip1559(self.tx.clone().into())
TypedTransaction::Eip1559(self.transaction.clone().into())
};
}

pub fn typed_tx(&self) -> &TypedTransaction {
&self.tx
&self.transaction
}

pub fn typed_tx_mut(&mut self) -> &mut TypedTransaction {
&mut self.tx
&mut self.transaction
}

pub fn is_create2(&self) -> bool {
Expand Down
63 changes: 63 additions & 0 deletions testdata/script/broadcast/deploy.sol/31337/run-latest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"transactions": [
{
"hash": null,
"type": "CREATE",
"contractName": null,
"contractAddress": "0x731a10897d267e19b34503ad902d0a29173ba4b1",
"function": null,
"arguments": null,
"transaction": {
"type": "0x02",
"from": "0x00a329c0648769a73afac7f9381e08fb43dbea72",
"gas": "0x54653",
"value": "0x0",
"data": "0x608060405234801561001057600080fd5b506103d9806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063d5dcf1271461003b578063f8194e4814610050575b600080fd5b61004e6100493660046100e9565b600155565b005b61006361005e366004610118565b610079565b60405161007091906101f9565b60405180910390f35b6060600061008783826102b5565b5060008260405160200161009b9190610375565b60405160208183030381529060405290507fefdeaaf566f7751d16a12c7fa8909eb74120f42cba334d07dd5246c48f1fba81816040516100db91906101f9565b60405180910390a192915050565b6000602082840312156100fb57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561012a57600080fd5b813567ffffffffffffffff8082111561014257600080fd5b818401915084601f83011261015657600080fd5b81358181111561016857610168610102565b604051601f8201601f19908116603f0116810190838211818310171561019057610190610102565b816040528281528760208487010111156101a957600080fd5b826020860160208301376000928101602001929092525095945050505050565b60005b838110156101e45781810151838201526020016101cc565b838111156101f3576000848401525b50505050565b60208152600082518060208401526102188160408501602087016101c9565b601f01601f19169190910160400192915050565b600181811c9082168061024057607f821691505b60208210810361026057634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156102b057600081815260208120601f850160051c8101602086101561028d5750805b601f850160051c820191505b818110156102ac57828155600101610299565b5050505b505050565b815167ffffffffffffffff8111156102cf576102cf610102565b6102e3816102dd845461022c565b84610266565b602080601f83116001811461031857600084156103005750858301515b600019600386901b1c1916600185901b1785556102ac565b600085815260208120601f198616915b8281101561034757888601518255948401946001909101908401610328565b50858210156103655787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6502432b63637960d51b8152600082516103968160068501602087016101c9565b919091016006019291505056fea2646970667358221220a380cb042b6ca762a5a0f97e497c4cffa21c45dc21e2dab4107e5415921a704a64736f6c634300080f0033",
"nonce": "0x0",
"accessList": []
}
},
{
"hash": null,
"type": "CALL",
"contractName": null,
"contractAddress": "0x731a10897d267e19b34503ad902d0a29173ba4b1",
"function": null,
"arguments": null,
"transaction": {
"type": "0x02",
"from": "0x00a329c0648769a73afac7f9381e08fb43dbea72",
"to": "0x731a10897d267e19b34503ad902d0a29173ba4b1",
"gas": "0xef15",
"value": "0x0",
"data": "0xf8194e48000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000046a6f686e00000000000000000000000000000000000000000000000000000000",
"nonce": "0x1",
"accessList": []
}
},
{
"hash": null,
"type": "CALL",
"contractName": null,
"contractAddress": "0x731a10897d267e19b34503ad902d0a29173ba4b1",
"function": null,
"arguments": null,
"transaction": {
"type": "0x02",
"from": "0x00a329c0648769a73afac7f9381e08fb43dbea72",
"to": "0x731a10897d267e19b34503ad902d0a29173ba4b1",
"gas": "0xdcde",
"value": "0x0",
"data": "0xd5dcf127000000000000000000000000000000000000000000000000000000000000007b",
"nonce": "0x2",
"accessList": []
}
}
],
"receipts": [],
"libraries": [],
"pending": [],
"path": "broadcast/deploy.sol/31337/run-latest.json",
"returns": {},
"timestamp": 1658913881
}
38 changes: 38 additions & 0 deletions testdata/script/deploy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

import {DSTest} from "../lib/ds-test/src/test.sol";
import {Cheats} from "../cheats/Cheats.sol";

contract Greeter {
string name;
uint256 age;

event Greet(string greet);

function greeting(string memory _name) public returns (string memory) {
name = _name;
string memory greet = string(abi.encodePacked("Hello ", _name));
emit Greet(greet);
return greet;
}

function setAge(uint256 _age) external {
age = _age;
}
}

contract Deploy is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);

Greeter greeter;
string greeting;

function run() external {
cheats.startBroadcast();
greeter = new Greeter();
greeting = greeter.greeting("john");
greeter.setAge(123);
cheats.stopBroadcast();
}
}

0 comments on commit 2f4dc6c

Please sign in to comment.