Skip to content

Instantly share code, notes, and snippets.

@ergatea
Created October 21, 2022 00:21
Show Gist options
  • Save ergatea/8bb42011fa3ef866158849ec4d9a235f to your computer and use it in GitHub Desktop.
Save ergatea/8bb42011fa3ef866158849ec4d9a235f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.17+commit.8df45f5f.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
//import "hardhat/console.sol";
contract BatcherV2 {
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1167.md
address private immutable original;
bytes32 byteCode;
uint n;
address private immutable deployer;
constructor(uint _n) {
original = address(this);
deployer = msg.sender;
createProxies(_n);
}
function createProxies(uint _n) internal {
bytes memory miniProxy = bytes.concat(bytes20(0x3D602d80600A3D3981F3363d3d373d3D3D363d73), bytes20(address(this)), bytes15(0x5af43d82803e903d91602b57fd5bf3));
byteCode = keccak256(abi.encodePacked(miniProxy));
address proxy;
uint oldN = n;
for(uint i=0; i<_n; i++) {
bytes32 salt = keccak256(abi.encodePacked(msg.sender, i+oldN));
assembly {
proxy := create2(0, add(miniProxy, 32), mload(miniProxy), salt)
}
}
// update n
n = oldN + _n;
}
function callback(address target, bytes memory data) external {
require(msg.sender == original, "Only original can call this function.");
(bool success, ) = target.call(data);
require(success, "Transaction failed.");
}
function proxyFor(address sender, uint i) public view returns (address proxy) {
bytes32 salt = keccak256(abi.encodePacked(sender, i));
proxy = address(uint160(uint(keccak256(abi.encodePacked(
hex'ff',
address(this),
salt,
byteCode
)))));
}
// increase proxy count
function increase(uint _n) external {
require(msg.sender == deployer, "Only deployer can call this function.");
createProxies(_n);
}
function execute(uint _start, uint _count, address target, bytes memory data) external {
require(msg.sender == deployer, "Only deployer can call this function.");
for(uint i=_start; i<_start+_count; i++) {
address proxy = proxyFor(msg.sender, i);
BatcherV2(proxy).callback(target, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment