mirror of
https://github.com/autistic-symposium/web3-starter-sol.git
synced 2025-07-21 14:18:51 -04:00
Clean up
This commit is contained in:
parent
c2d0866191
commit
db37d209ab
58 changed files with 4663 additions and 1043 deletions
42
advanced_knowledge/calldata/README.md
Normal file
42
advanced_knowledge/calldata/README.md
Normal file
|
@ -0,0 +1,42 @@
|
|||
## calldata
|
||||
|
||||
<br>
|
||||
|
||||
* calldata is the encoded parameter(s) sent on functions by smart contracts to the evm (for example, through `abi.econde()`,`abi.econcdeWithSelector() for a particular interfaced function, or `abi.encodePacked()` for efficient dynamic variables).
|
||||
|
||||
* **function signature**: `selector()` generates the 4-bytes representing the method in the interface: this is how the evm knows which function is being interacted. function signatures are defined as the first four bytes of the Keccak hash of the canonical representation of the function signature.
|
||||
|
||||
* each piece of calldata is 32 bytes long (64 chars), where 20 hex == 32-bytes
|
||||
|
||||
* types:
|
||||
- static variables: the encoded representation of `uint`, `address`, `bool`, `bytes`, `tuples`
|
||||
- dynamics variables: non-fixed-size types: `bytes`, `string`, dynamic and fixed arrays `<T>[]`
|
||||
|
||||
* opcodes are `1 byte` in length, leading to `256` different possible opcodes. the EVM only uses `140` opcodes.
|
||||
|
||||
<br>
|
||||
|
||||
----
|
||||
|
||||
### decompilers
|
||||
|
||||
<br>
|
||||
|
||||
* **[dedaub](https://library.dedaub.com/)**
|
||||
* **[ethervm.io](https://ethervm.io/decompile)**
|
||||
* **[panoramix](https://github.com/eveem-org/panoramix)**
|
||||
* **[froundry's cast with --debug](https://book.getfoundry.sh/cast/index.html)**
|
||||
|
||||
|
||||
<br>
|
||||
|
||||
|
||||
---
|
||||
|
||||
### resources
|
||||
|
||||
<br>
|
||||
|
||||
* **[mev memoirs in the arena part 1, by noxx](https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter?s=r)**
|
||||
* **[mev memoirs in the arena part 2, by noxx](https://noxx.substack.com/p/mev-memoirs-into-the-arena-chapter-3e9)**
|
||||
* **[ethereum tx enconding and legacy encoding](https://hoangtrinhj.com/articles/ethereum-transaction-encoding)**
|
92
advanced_knowledge/calldata/precompute_contract_address.sol
Normal file
92
advanced_knowledge/calldata/precompute_contract_address.sol
Normal file
|
@ -0,0 +1,92 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.17;
|
||||
|
||||
// Contract address can be precomputed, before the contract is deployed, using create2
|
||||
|
||||
contract Factory {
|
||||
// Returns the address of the newly deployed contract
|
||||
function deploy(
|
||||
address _owner,
|
||||
uint _foo,
|
||||
bytes32 _salt
|
||||
) public payable returns (address) {
|
||||
// This syntax is a newer way to invoke create2 without assembly, you just need to pass salt
|
||||
// https://docs.soliditylang.org/en/latest/control-structures.html#salted-contract-creations-create2
|
||||
return address(new TestContract{salt: _salt}(_owner, _foo));
|
||||
}
|
||||
}
|
||||
|
||||
// This is the older way of doing it using assembly
|
||||
contract FactoryAssembly {
|
||||
event Deployed(address addr, uint salt);
|
||||
|
||||
// 1. Get bytecode of contract to be deployed
|
||||
// NOTE: _owner and _foo are arguments of the TestContract's constructor
|
||||
function getBytecode(address _owner, uint _foo) public pure returns (bytes memory) {
|
||||
bytes memory bytecode = type(TestContract).creationCode;
|
||||
|
||||
return abi.encodePacked(bytecode, abi.encode(_owner, _foo));
|
||||
}
|
||||
|
||||
// 2. Compute the address of the contract to be deployed
|
||||
// NOTE: _salt is a random number used to create an address
|
||||
function getAddress(
|
||||
bytes memory bytecode,
|
||||
uint _salt
|
||||
) public view returns (address) {
|
||||
bytes32 hash = keccak256(
|
||||
abi.encodePacked(bytes1(0xff), address(this), _salt, keccak256(bytecode))
|
||||
);
|
||||
|
||||
// NOTE: cast last 20 bytes of hash to address
|
||||
return address(uint160(uint(hash)));
|
||||
}
|
||||
|
||||
// 3. Deploy the contract
|
||||
// NOTE:
|
||||
// Check the event log Deployed which contains the address of the deployed TestContract.
|
||||
// The address in the log should equal the address computed from above.
|
||||
function deploy(bytes memory bytecode, uint _salt) public payable {
|
||||
address addr;
|
||||
|
||||
/*
|
||||
NOTE: How to call create2
|
||||
|
||||
create2(v, p, n, s)
|
||||
create new contract with code at memory p to p + n
|
||||
and send v wei
|
||||
and return the new address
|
||||
where new address = first 20 bytes of keccak256(0xff + address(this) + s + keccak256(mem[p…(p+n)))
|
||||
s = big-endian 256-bit value
|
||||
*/
|
||||
assembly {
|
||||
addr := create2(
|
||||
callvalue(), // wei sent with current call
|
||||
// Actual code starts after skipping the first 32 bytes
|
||||
add(bytecode, 0x20),
|
||||
mload(bytecode), // Load the size of code contained in the first 32 bytes
|
||||
_salt // Salt from function arguments
|
||||
)
|
||||
|
||||
if iszero(extcodesize(addr)) {
|
||||
revert(0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
emit Deployed(addr, _salt);
|
||||
}
|
||||
}
|
||||
|
||||
contract TestContract {
|
||||
address public owner;
|
||||
uint public foo;
|
||||
|
||||
constructor(address _owner, uint _foo) payable {
|
||||
owner = _owner;
|
||||
foo = _foo;
|
||||
}
|
||||
|
||||
function getBalance() public view returns (uint) {
|
||||
return address(this).balance;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue