Skip to content

Instantly share code, notes, and snippets.

@myself659
Forked from LCamel/__INIT__.md
Created November 13, 2024 08:21
Show Gist options
  • Save myself659/ef9ba5b1934b00fd140ea89307370876 to your computer and use it in GitHub Desktop.
Save myself659/ef9ba5b1934b00fd140ea89307370876 to your computer and use it in GitHub Desktop.
init demo
// We may #define our own mnemonic in the assembler.
// For example, we often want to push a 0 to the top of the stack.
// Instead of PUSH1 00, we can replace it with RETURNDATASIZE (0x3D)
// to squeeze out some gas (only before CALL-ing any other contract).
// By defining "_MYZERO_ 3D", we can make our intention clear.
var asm = (c) => {
var bin = {};
`SUB 03 CODESIZE 38 CODECOPY 39 _MYZERO_ 3D
MSTORE8 53 PUSH1 60 DUP1 80 RETURN F3`
.replace(/(\w+)\s*(\w+)\s*/g, (_,k,v) => bin[k] = v); // make bin
return c.replace(/\/\/.*?\n/g, "") // remove comments
.replace(/\s*(\w+)\s*/g, (_,s) => bin[s]||s); // replace with bin
}
// append any body after this 0x0B-byte deployer
var deployer = asm(`
PUSH1 0B // deployer size (fixed)
CODESIZE // code size == deployer size + body size
SUB // RETURN arg 1: body size
DUP1 // CODECOPY arg 2: body size
PUSH1 0B // CODECOPY arg 1: code src offset
_MYZERO_ // CODECOPY arg 0: memory dest offset 0
CODECOPY // mem[0 ... body size) = body
_MYZERO_ // RETURN arg 0: memory offset 0
RETURN // return mem[0 ... body size)
`);
console.log("deployer: " + deployer);
% evm --code 604260005360016000F3 run
0x42
// We may #define our own mnemonic in the assembler.
// For example, we often want to push a 0 to the top of the stack.
// Instead of PUSH1 00, we can replace it with RETURNDATASIZE (0x3D)
// to squeeze out some gas (only before CALL-ing any other contract).
// By defining "_MYZERO_ 3D", we can make our intention clear.
var asm = (c) => {
var bin = {};
`SUB 03 CODESIZE 38 CODECOPY 39 _MYZERO_ 3D
MSTORE8 53 PUSH1 60 DUP1 80 RETURN F3`
.replace(/(\w+)\s*(\w+)\s*/g, (_,k,v) => bin[k] = v); // make bin
return c.replace(/\/\/.*?\n/g, "") // remove comments
.replace(/\s*(\w+)\s*/g, (_,s) => bin[s]||s); // replace with bin
}
var init42 = asm(`
PUSH1 42 // MSTORE8 arg 1: value
PUSH1 00 // MSTORE8 arg 0: mem dest offset
MSTORE8 // mem[0] = 0x42
PUSH1 01 // RETURN arg 1: body size
PUSH1 00 // RETURN arg 0: mem offset
RETURN // return mem[offset ... offset + body size)
`);
console.log("init42: " + init42);
var initCafeBabe = asm(`
PUSH1 04 // CODECOPY arg 2: body size
PUSH1 0C // CODECOPY arg 1: code src offset (skip deployer size)
PUSH1 00 // CODECOPY arg 0: mem dest offset
CODECOPY // mem[0..4) = 0xCAFEBABE
PUSH1 04 // RETURN arg 1: body size
PUSH1 00 // RETURN arg 0: mem offset
RETURN // return mem[offset ... offset + body size)
CAFEBABE // 4-byte body being appended here
`)
console.log("initCafeBabe: " + initCafeBabe);
// append any body after this 0x0B-byte deployer
var deployer = asm(`
PUSH1 0B // deployer size (fixed)
CODESIZE // code size == deployer size + body size
SUB // RETURN arg 1: body size
DUP1 // CODECOPY arg 2: body size
PUSH1 0B // CODECOPY arg 1: code src offset
_MYZERO_ // CODECOPY arg 0: memory dest offset 0
CODECOPY // mem[0 ... body size) = body
_MYZERO_ // RETURN arg 0: memory offset 0
RETURN // return mem[0 ... body size)
`);
console.log("deployer: " + deployer);
var ethers = await import("ethers");
var provider = new ethers.providers.AlchemyProvider(
process.env.NETWORK, process.env.ALCHEMY_API_KEY);
var signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
var deploy = async (initCode) => {
console.log("==== deploy: initCode: " + initCode);
var tx = await signer.sendTransaction({data: "0x" + initCode});
console.log("tx.hash: " + tx.hash);
var receipt = await tx.wait();
console.log("receipt: " + JSON.stringify(receipt));
var body = await provider.getCode(receipt.contractAddress);
console.log("body: " + body);
return [tx, receipt, body];
}
await deploy(init42);
await deploy(initCafeBabe);
await deploy(deployer + "CAFEBABE");
var init42 = asm(`
PUSH1 42 // MSTORE8 arg 1: value
PUSH1 00 // MSTORE8 arg 0: mem dest offset
MSTORE8 // mem[0] = 0x42
PUSH1 01 // RETURN arg 1: body size
PUSH1 00 // RETURN arg 0: mem offset
RETURN // return mem[offset ... offset + body size)
`);
console.log("init42: " + init42);
var initCafeBabe = asm(`
PUSH1 04 // CODECOPY arg 2: body size
PUSH1 0C // CODECOPY arg 1: code src offset (skip deployer size)
PUSH1 00 // CODECOPY arg 0: mem dest offset
CODECOPY // mem[0..4) = 0xCAFEBABE
PUSH1 04 // RETURN arg 1: body size
PUSH1 00 // RETURN arg 0: mem offset
RETURN // return mem[offset ... offset + body size)
CAFEBABE // 4-byte body being appended here
`)
console.log("initCafeBabe: " + initCafeBabe);
% node init.mjs
init42: 604260005360016000F3
initCafeBabe: 6004600C60003960046000F3CAFEBABE
deployer: 600B380380600B3D393DF3
==== deploy: initCode: 604260005360016000F3
tx.hash: 0xb55ef0e9973dd3439577010082c198afc89173fdd05a04cb51adc89a8a590635
receipt: {"to":null,"from":"0x3b78Dda6F6d5e88A5dae7FC9fAEE2c629e2e7ebf","contractAddress":"0x4c9634D1Aa4Ef00fb9E2d241649e48F96d91f2a5","transactionIndex":10,"gasUsed":{"type":"BigNumber","hex":"0xd06a"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0xd81c240ba450c09adc0d9635cd0b026892e6984a63ee42ebd0d949f00acde165","transactionHash":"0xb55ef0e9973dd3439577010082c198afc89173fdd05a04cb51adc89a8a590635","logs":[],"blockNumber":7365886,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x82bc3b"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x59682f07"},"status":1,"type":2,"byzantium":true}
body: 0x42
==== deploy: initCode: 6004600C60003960046000F3CAFEBABE
tx.hash: 0x0832df27ffa5aa9651f3c52181b819849d5ea5ea41e7b44b804fe17b0adb5c3a
receipt: {"to":null,"from":"0x3b78Dda6F6d5e88A5dae7FC9fAEE2c629e2e7ebf","contractAddress":"0x52307B14A4065B3cAEa50000851BFa8F10482633","transactionIndex":3,"gasUsed":{"type":"BigNumber","hex":"0xd328"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x75903e6a3084100c80be255ef012d99125f70dbd95c68038b4693b85743ca2a4","transactionHash":"0x0832df27ffa5aa9651f3c52181b819849d5ea5ea41e7b44b804fe17b0adb5c3a","logs":[],"blockNumber":7365887,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x02294a"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x59682f07"},"status":1,"type":2,"byzantium":true}
body: 0xcafebabe
==== deploy: initCode: 600B380380600B3D393DF3CAFEBABE
tx.hash: 0xc663cbd3a69a281a68c7fc2e0c586699aabf5037546108fa147bc718f6dc5e04
receipt: {"to":null,"from":"0x3b78Dda6F6d5e88A5dae7FC9fAEE2c629e2e7ebf","contractAddress":"0x84D7D0a0A2eF7a6533780d314b6507dF398F36d0","transactionIndex":3,"gasUsed":{"type":"BigNumber","hex":"0xd333"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x34f44716ff467f5fe886ce7612ac202e3858255ed7839def245a5f7fdd19c598","transactionHash":"0xc663cbd3a69a281a68c7fc2e0c586699aabf5037546108fa147bc718f6dc5e04","logs":[],"blockNumber":7365888,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x031edd"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x59682f07"},"status":1,"type":2,"byzantium":true}
body: 0xcafebabe
var ethers = await import("ethers");
var provider = new ethers.providers.AlchemyProvider(
process.env.NETWORK, process.env.ALCHEMY_API_KEY);
var signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
var tx = await signer.sendTransaction({data: "0x" + init42});
console.log("tx.hash: " + tx.hash);
var receipt = await tx.wait();
console.log("receipt: " + JSON.stringify(receipt));
var body = await provider.getCode(receipt.contractAddress);
console.log("body: " + body);
init42: 604260005360016000F3
tx.hash: 0xb5b18bb0ed8b8ab85cf2168591266c66e78f4b6e5c1cd80439531ddf08ab1f44
receipt: {"to":null,"from":"0x3b78Dda6F6d5e88A5dae7FC9fAEE2c629e2e7ebf","contractAddress":"0x4dA201456B2D0a4BB25e208BF8Bc1A2697C17741","transactionIndex":45,"gasUsed":{"type":"BigNumber","hex":"0xd06a"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x5f2b9be391b281b15e74001d52ab28a9b40f7b121fb97f38deeb54002e0eb7e5","transactionHash":"0xb5b18bb0ed8b8ab85cf2168591266c66e78f4b6e5c1cd80439531ddf08ab1f44","logs":[],"blockNumber":7340280,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0xac26b5"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x59682f09"},"status":1,"type":2,"byzantium":true}
body: 0x42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment