owned this note
owned this note
Published
Linked with GitHub
# Create Contract + run it
## Create contract bytecode
Contract code in `tmp.sol`:
```
contract Multiply7 {
event Print(uint);
function multiply(uint input) public returns (uint) {
emit Print(input * 7);
return input * 7;
}
}
```
Compile: `solc --ir --overwrite --bin --asm-json --opcodes --pretty-json -o out tmp.sol`
## GETH run in dev mode
Run from: `~/development/go-ethereum/build/bin`
```
./geth --authrpc.addr localhost --authrpc.port 8551 --authrpc.vhosts localhost --authrpc.jwtsecret /tmp/jwtsecret --http --http.port 8545 --ws --ws.port 3334 --dev --ipcpath "/home/matesoos/.ethereum/geth.ipc" --http.corsdomain https://remix.ethereum.org
```
## GETH connect to console (nodejs-like)
Get console: `./geth attach`
## Create contract and send transaction to it
Beware, you CANNOT get the return value from a transaction. And the thing below is a full-blown "real" transaction in the dev chain. So it won't give the return value. But it persists storage.
In console:
```
acc = eth.accounts[0]
eth.getBalance(acc)
web3.fromWei(eth.getBalance(acc))
/* transfer money */
trans = eth.sendTransaction({
from: acc,
to: '0xce8dba5e4157c2b284d8853afeeea259344c1653',
value: web3.toWei(0.1, 'ether')
});
eth.getTransaction(trans)
web3.fromWei(100000000000000000)
/* The "multiply" contract from https://ethereum.org/en/developers/docs/apis/json-rpc/
data = "0x608060405234801561001057600080fd5b50610218806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a600480360381019061004591906100f4565b610060565b6040516100579190610130565b60405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783610090919061017a565b60405161009d9190610130565b60405180910390a16007826100b2919061017a565b9050919050565b600080fd5b6000819050919050565b6100d1816100be565b81146100dc57600080fd5b50565b6000813590506100ee816100c8565b92915050565b60006020828403121561010a576101096100b9565b5b6000610118848285016100df565b91505092915050565b61012a816100be565b82525050565b60006020820190506101456000830184610121565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610185826100be565b9150610190836100be565b925082820261019e816100be565b915082820484148315176101b5576101b461014b565b5b509291505056fea2646970667358221220958bd2025162f9f370e334e1ab2d5e610b3ad0364f0b6756c3c36fa3f2c5505464736f6c637828302e382e31372d646576656c6f702e323032322e382e32352b636f6d6d69742e32326130633436650059"
/* create contract */
trans = eth.sendTransaction({
from: acc,
gas: 0x3c31e,
data: data
});
receipt = eth.getTransactionReceipt(trans)
/* get 4 bytes of function selector */
web3.sha3("multiply(uint256)").substring(0, 10)
/* let's mutiply 6 and combine it with the selector:*/
a = "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"
trans2 = eth.sendTransaction({
from: acc,
to: receipt.contractAddress,
gas: 0x3c31e,
data: a
});
receipt = eth.getTransactionReceipt(trans2)
```
## Run eth_call to avoid all the creating & also be able to get return value
Call and gets the result immediately, but does not change state (e.g. storage). We need to fill `stateDiff` with e.g. the contract, pretend state, etc. And allows to deploy the contract as well.
```
eth.call({from:acc, to:receipt.contractAddress, data:"0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"})
```
Even better: `traceCall` instead of `call` -- gives the trace too.
Maybe use the EVM tool: https://github.com/ethereum/go-ethereum/tree/master/cmd/evm
## Tracing without JSON-RPC
Set `my.json` to:
```
from = "0xf1d8ca1d88fa23dbaa9914a5da44aad8021eb650"
to = "0x8A8eAFb1cf62BfBeb1741769DAE1a9dd47996192"
data = "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"
alloc = {
"0x8A8eAFb1cf62BfBeb1741769DAE1a9dd47996192": {
"balance": "0x5ffd4878be161d74",
"code": "0x5b357f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000087f00000000000000000000000000000000000000000000000000000000000000017f00000000000000000000000000000000000000000000000000000000000000097f00000000000000000000000000000000000000000000000000000000000000097f00000000000000000000000000000000000000000000000000000000000000097f00000000000000000000000000000000000000000000000000000000000000080a357f00000000000000000000000000000000000000000000000000000000000000087f0000000000000000000000000000000000000000000000000000000000000006f37f00000000000000000000000000000000000000000000000000000000000000037f0000000000000000000000000000000000000000000000000000000000000004355b7f00000000000000000000000000000000000000000000000000000000000000037f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000097f00000000000000000000000000000000000000000000000000000000000000037f00000000000000000000000000000000000000000000000000000000000000085b357f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000077f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000017f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000057f00000000000000000000000000000000000000000000000000000000000000087f0000000000000000000000000000000000000000000000000000000000000001357f0000000000000000000000000000000000000000000000000000000000000006077f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000077f00000000000000000000000000000000000000000000000000000000000000027f000000000000000000000000000000000000000000000000000000000000000a7f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000017f0000000000000000000000000000000000000000000000000000000000000002357f00000000000000000000000000000000000000000000000000000000000000017f000000000000000000000000000000000000000000000000000000000000000a7f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000057f0000000000000000000000000000000000000000000000000000000000000005357f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000037f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000077f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000075b7f00000000000000000000000000000000000000000000000000000000000000087f00000000000000000000000000000000000000000000000000000000000000017f00000000000000000000000000000000000000000000000000000000000000077f00000000000000000000000000000000000000000000000000000000000000097f00000000000000000000000000000000000000000000000000000000000000017f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000087f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000037f0000000000000000000000000000000000000000000000000000000000000008357f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000087f000000000000000000000000000000000000000000000000000000000000000a7f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000057f000000000000000000000000000000000000000000000000000000000000000a7f00000000000000000000000000000000000000000000000000000000000000085b",
"nonce": "0xac"
},
"0xf1d8ca1d88fa23dbaa9914a5da44aad8021eb650" : {
"balance": "0xffffffff",
"nonce": "0x0"
}
}
debug.traceCall({from: from, to: to, data: data}, "latest", {stateOverrides : alloc})
```
Then run:
```
geth --verbosity 0 --dev --exec 'loadScript("my.js")' console
```
And that's it! Gives back the trace!
## Tracing can be useful to find bugs
tracing visualization goevmlab: https://github.com/holiman/goevmlab
tracing: https://www.youtube.com/watch?v=b8RdmGsilfU
It can do opcode tracing (default), or it can do other kinds of tracing, too, like call tracing.
## Something different -- access list (not related)
Cool access list thing: https://eips.ethereum.org/EIPS/eip-2930