I'm trying to write a super simple contract that's gas efficient utilizing inline assembly in solidity. All this function needs to do is create a child smart contract with one function, self destruct. The function also should check msg.sender to make sure it's the contract owner. This GasToken project does this in a form where they've tokenized the effective stored gas. They took it to another level where they also used a vanity address to make this even more gas efficient. I've been trying to generate a vanity address so that I can use their code exactly, but it's taking an extremely long time so I resorted to modifying the code so that I can do it with a normal address and not a vanity address with 10 leading zeros. So for this question's purpose, let's not do a vanity address.
contract pseudocode:
if (msg.sender != 0x1234....5678) { throw; }
suicide(msg.sender)
How can I write this in inline assembly in solidity? Here is the source for gasToken.io where they used the vanity address. I'd like to NOT use the vanity address to simplify my code for the time being since this kind of vanity address is hard to generate without mining hardware. I've been referencing solidity opcodes but have been unable to successfully get this up and running.
// Creates a child contract that can only be destroyed by this contract.
function makeChild() internal returns (address addr) {
assembly {
// EVM assembler of runtime portion of child contract:
// ;; Pseudocode: if (msg.sender != 0x0000000000b3f879cb30fe243b4dfee438691c04) { throw; }
// ;; suicide(msg.sender)
// PUSH15 0xb3f879cb30fe243b4dfee438691c04 ;; hardcoded address of this contract
// CALLER
// XOR
// PC
// JUMPI
// CALLER
// SELFDESTRUCT
// Or in binary: 6eb3f879cb30fe243b4dfee438691c043318585733ff
// Since the binary is so short (22 bytes), we can get away
// with a very simple initcode:
// PUSH22 0x6eb3f879cb30fe243b4dfee438691c043318585733ff
// PUSH1 0
// MSTORE ;; at this point, memory locations mem[10] through
// ;; mem[31] contain the runtime portion of the child
// ;; contract. all that's left to do is to RETURN this
// ;; chunk of memory.
// PUSH1 22 ;; length
// PUSH1 10 ;; offset
// RETURN
// Or in binary: 756eb3f879cb30fe243b4dfee438691c043318585733ff6000526016600af3
// Almost done! All we have to do is put this short (31 bytes) blob into
// memory and call CREATE with the appropriate offsets.
let solidity_free_mem_ptr := mload(0x40)
mstore(solidity_free_mem_ptr, 0x00756eb3f879cb30fe243b4dfee438691c043318585733ff6000526016600af3)
addr := create(0, add(solidity_free_mem_ptr, 1), 31)
}
}