Skip to content

Commit

Permalink
feat: generalizing adder example
Browse files Browse the repository at this point in the history
  • Loading branch information
claudioantonio committed Oct 31, 2024
1 parent 74d474d commit 3f89f15
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ pragma solidity ^0.8.27;

import {Script} from "forge-std/Script.sol";
import {Vm} from "forge-std/Vm.sol";
import {IAdder} from "src/IAdder.sol";
import {Deployments, LibDeployments} from "script/LibDeployments.sol";

contract TestScript is Script {
contract AssertionScript is Script {
using LibDeployments for Vm;

function run() external {
Deployments memory deployments = vm.loadDeployments();
if (deployments.adder.number() >= 1) return;
IAdder adder = IAdder(deployments.contracts[0]);
if (adder.number() >= 1) return;
vm.writeFile("./exploited", "");
}
}
7 changes: 3 additions & 4 deletions tests/bounties/src/adder/script/LibDeployments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
pragma solidity ^0.8.27;

import {Vm} from "forge-std/Vm.sol";
import {IAdder} from "src/IAdder.sol";

struct Deployments {
IAdder adder;
address[] contracts;
}

library LibDeployments {
Expand All @@ -16,8 +15,8 @@ library LibDeployments {
return path;
}

function storeDeployments(Vm vm, Deployments memory deployment) internal {
string memory json = vm.serializeAddress("deployments", "adder", address(deployment.adder));
function storeDeployments(Vm vm, Deployments memory deployments) internal {
string memory json = vm.serializeAddress("deployments", "contracts", deployments.contracts);
vm.writeJson(json, getDeploymentsPath(vm));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {Vm} from "forge-std/Vm.sol";
import {SafeAdder} from "src/safe/Adder.sol";
import {Deployments, LibDeployments} from "script/LibDeployments.sol";

contract DeployScript is Script {
contract SetupScript is Script {
using LibDeployments for Vm;

function run() external {
Deployments memory deployments;
deployments.contracts = new address[](1);

vm.startBroadcast();
deployments.adder = new SafeAdder();
deployments.contracts[0] = new SafeAdder();
vm.stopBroadcast();

vm.storeDeployments(deployments);
Expand Down
2 changes: 1 addition & 1 deletion tests/bounties/src/adder/script/unsafe/Exploit.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract ExploitScript is Script {

function run() external {
Deployments memory deployments = vm.loadDeployments();
IAdder adder = deployments.adder;
IAdder adder = Iadder(deployments.contracts[0]);
uint256 number = adder.number();
uint256 increment;
unchecked {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {Vm} from "forge-std/Vm.sol";
import {UnsafeAdder} from "src/unsafe/Adder.sol";
import {Deployments, LibDeployments} from "script/LibDeployments.sol";

contract DeployScript is Script {
contract SetupScript is Script {
using LibDeployments for Vm;

function run() external {
Deployments memory deployments;
deployments.contracts = new address[](1);

vm.startBroadcast();
deployments.adder = new UnsafeAdder();
deployments.contracts[0] = new UnsafeAdder();
vm.stopBroadcast();

vm.storeDeployments(deployments);
Expand Down

0 comments on commit 3f89f15

Please sign in to comment.