# 简单的demo解读智能合约的编译流程

智能合约的编译compile:编译步骤的目标是把源代码转成 ABI 和 Bytecode,并且能够处理编译时抛出的错误,确保不会在包含错误的源代码上进行编译。

# contracts中新建car.sol合约源码

# scripts中新建compile.js编译脚本

# 编译结果解析

{ contracts: { ':Car': { assembly: [Object], bytecode: '608060405234801561001057600080fd5b5060405161038c38038061038c8 3398101604052805101805161003a906000906020840190610041565b50506100dc565b828054600 181600116156101000203166002900490600052602060002090601f016020900481019282601f106 1008257805160ff19168380011785556100af565b828001600101855582156100af579182015b828 111156100af578251825591602001919060010190610094565b506100bb9291506100bf565b50905 65b6100d991905b808211156100bb57600081556001016100c5565b90565b6102a1806100eb60003 96000f30060806040526004361061004b5763ffffffff7c010000000000000000000000000000000 00000000000000000000000006000350416632d9fff018114610050578063c1fd4137146100da575 b600080fd5b34801561005c57600080fd5b50610065610135565b604080516020808252835181830 1528351919283929083019185019080838360005b8381101561009f5781810151838201526020016 10087565b50505050905090810190601f1680156100cc5780820380516001836020036101000a031 916815260200191505b509250505060405180910390f35b3480156100e657600080fd5b506040805 160206004803580820135601f8101849004840285018401909552848452610133943694929360249 3928401919081908401838280828437509497506101c39650505050505050565b005b60008054604 08051602060026001851615610100026000190190941693909304601f81018490048402820184019 0925281815292918301828280156101bb5780601f106101905761010080835404028352916020019 16101bb565b820191906000526020600020905b81548152906001019060200180831161019e57829 003601f168201915b505050505081565b80516101d69060009060208401906101da565b5050565b8 28054600181600116156101000203166002900490600052602060002090601f01602090048101928 2601f1061021b57805160ff1916838001178555610248565b8280016001018555821561024857918 2015b8281111561024857825182559160200191906001019061022d565b506102549291506102585 65b5090565b61027291905b80821115610254576000815560010161025e565b905600a165627a7a7 2305820fc9db709577938943344a7dede7c9b2eb5d14d95c7a50ea783b551b0bbc30aa80029', functionHashes: [Object], gasEstimates: [Object], interface: '[{"constant":true,"inputs":[],"name":"brand","outputs":[{"na me":"","type":"string"}],"payable":false,"stateMutability":"view","type":"functi on"},{"constant":false,"inputs":[{"name":"newBrand","type":"string"}],"name":"se tBrand","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"func tion"},{"inputs":[{"name":"initialBrand","type":"string"}],"payable":false,"stat eMutability":"nonpayable","type":"constructor"}]', metadata: '{"compiler":{"version":"0.4.24+commit.e67f0147"},"language":" Solidity","output":{"abi":[{"constant":true,"inputs":[],"name":"brand","outputs" :[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":" function"},{"constant":false,"inputs":[{"name":"newBrand","type":"string"}],"nam e":"setBrand","outputs":[],"payable":false,"stateMutability":"nonpayable","type" :"function"},{"inputs":[{"name":"initialBrand","type":"string"}],"payable":false ,"stateMutability":"nonpayable","type":"constructor"}],"devdoc":{"methods":{}}," userdoc":{"methods":{}}},"settings":{"compilationTarget":{"":"Car"},"evmVersion" :"byzantium","libraries":{},"optimizer":{"enabled":true,"runs":200},"remappings" :[]},"sources":{"":{"keccak256":"0x29e015bc2eccdfc18f15561c6112dc6043c624dccc8b4 320a44bb73d962d4667","urls":["bzzr://1e7dcce4915a8bd73be4e6309e98e0c68c4fb5197ce 8d4f2a784f2d6d4789fe9"]}},"version":1}', opcodes: 'PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 MLOAD PUSH2 0x38C CODESIZE S UB DUP1 PUSH2 0x38C DUP4 CODECOPY DUP2 ADD PUSH1 0x40 MSTORE DUP1 MLOAD ADD DUP1 MLOAD PUSH2 0x3A SWAP1 PUSH1 0x0 SWAP1 PUSH1 0x20 DUP5 ADD SWAP1 PUSH2 0x41 JUM P JUMPDEST POP POP PUSH2 0xDC JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTO RE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x82 JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP 4 DUP1 ADD OR DUP6 SSTORE PUSH2 0xAF JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD D UP6 SSTORE DUP3 ISZERO PUSH2 0xAF JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZ ERO PUSH2 0xAF JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUS H1 0x1 ADD SWAP1 PUSH2 0x94 JUMP JUMPDEST POP PUSH2 0xBB SWAP3 SWAP2 POP PUSH2 0 xBF JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0xD9 SWAP2 SWAP1 JUMPDEST DUP1 D UP3 GT ISZERO PUSH2 0xBB JUMPI PUSH1 0x0 DUP2 SSTORE PUSH1 0x1 ADD PUSH2 0xC5 JU MP JUMPDEST SWAP1 JUMP JUMPDEST PUSH2 0x2A1 DUP1 PUSH2 0xEB PUSH1 0x0 CODECOPY P USH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH 2 0x4B JUMPI PUSH4 0xFFFFFFFF PUSH29 0x10000000000000000000000000000000000000000 0000000000000000 PUSH1 0x0 CALLDATALOAD DIV AND PUSH4 0x2D9FFF01 DUP2 EQ PUSH2 0 x50 JUMPI DUP1 PUSH4 0xC1FD4137 EQ PUSH2 0xDA JUMPI JUMPDEST PUSH1 0x0 DUP1 REVE RT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x5C JUMPI PUSH1 0x0 DUP1 REVERT JUMPDES T POP PUSH2 0x65 PUSH2 0x135 JUMP JUMPDEST PUSH1 0x40 DUP1 MLOAD PUSH1 0x20 DUP1 DUP3 MSTORE DUP4 MLOAD DUP2 DUP4 ADD MSTORE DUP4 MLOAD SWAP2 SWAP3 DUP4 SWAP3 S WAP1 DUP4 ADD SWAP2 DUP6 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 L T ISZERO PUSH2 0x9F JUMPI DUP2 DUP2 ADD MLOAD DUP4 DUP3 ADD MSTORE PUSH1 0x20 AD D PUSH2 0x87 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0xCC JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PU SH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JU MPDEST POP SWAP3 POP POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDE ST CALLVALUE DUP1 ISZERO PUSH2 0xE6 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUS H1 0x40 DUP1 MLOAD PUSH1 0x20 PUSH1 0x4 DUP1 CALLDATALOAD DUP1 DUP3 ADD CALLDATA LOAD PUSH1 0x1F DUP2 ADD DUP5 SWAP1 DIV DUP5 MUL DUP6 ADD DUP5 ADD SWAP1 SWAP6 M STORE DUP5 DUP5 MSTORE PUSH2 0x133 SWAP5 CALLDATASIZE SWAP5 SWAP3 SWAP4 PUSH1 0x 24 SWAP4 SWAP3 DUP5 ADD SWAP2 SWAP1 DUP2 SWAP1 DUP5 ADD DUP4 DUP3 DUP1 DUP3 DUP5 CALLDATACOPY POP SWAP5 SWAP8 POP PUSH2 0x1C3 SWAP7 POP POP POP POP POP POP POP JUMP JUMPDEST STOP JUMPDEST PUSH1 0x0 DUP1 SLOAD PUSH1 0x40 DUP1 MLOAD PUSH1 0x2 0 PUSH1 0x2 PUSH1 0x1 DUP6 AND ISZERO PUSH2 0x100 MUL PUSH1 0x0 NOT ADD SWAP1 SW AP5 AND SWAP4 SWAP1 SWAP4 DIV PUSH1 0x1F DUP2 ADD DUP5 SWAP1 DIV DUP5 MUL DUP3 A DD DUP5 ADD SWAP1 SWAP3 MSTORE DUP2 DUP2 MSTORE SWAP3 SWAP2 DUP4 ADD DUP3 DUP3 D UP1 ISZERO PUSH2 0x1BB JUMPI DUP1 PUSH1 0x1F LT PUSH2 0x190 JUMPI PUSH2 0x100 DU P1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0x1BB JUMP JU MPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP 1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0x19E JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP POP POP POP DUP2 JUMP JUMPDEST DUP1 MLOAD PUSH2 0x1D6 SWAP1 PUSH1 0x0 SW AP1 PUSH1 0x20 DUP5 ADD SWAP1 PUSH2 0x1DA JUMP JUMPDEST POP POP JUMP JUMPDEST DU P3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x21B JUMP I DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0x248 JUMP JU MPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0x248 JUMPI SWA P2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x248 JUMPI DUP3 MLOAD DUP3 SSTOR E SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x22D JUMP JUMPDEST POP PUSH2 0x254 SWAP3 SWAP2 POP PUSH2 0x258 JUMP JUMPDEST POP SWAP1 JUMP JUMPDE ST PUSH2 0x272 SWAP2 SWAP1 JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x254 JUMPI PUSH1 0x0 DUP2 SSTORE PUSH1 0x1 ADD PUSH2 0x25E JUMP JUMPDEST SWAP1 JUMP STOP LOG1 PUS H6 0x627A7A723058 KECCAK256 0xfc SWAP14 0xb7 MULMOD JUMPI PUSH26 0x38943344A7DED E7C9B2EB5D14D95C7A50EA783B551B0BBC30AA8 STOP 0x29 ', runtimeBytecode: '60806040526004361061004b5763ffffffff7c0100000000000000 0000000000000000000000000000000000000000006000350416632d9fff018114610050578063c1 fd4137146100da575b600080fd5b34801561005c57600080fd5b50610065610135565b6040805160 208082528351818301528351919283929083019185019080838360005b8381101561009f57818101 5183820152602001610087565b50505050905090810190601f1680156100cc578082038051600183 6020036101000a031916815260200191505b509250505060405180910390f35b3480156100e65760 0080fd5b506040805160206004803580820135601f81018490048402850184019095528484526101 339436949293602493928401919081908401838280828437509497506101c3965050505050505056 5b005b6000805460408051602060026001851615610100026000190190941693909304601f810184 900484028201840190925281815292918301828280156101bb5780601f1061019057610100808354 0402835291602001916101bb565b820191906000526020600020905b815481529060010190602001 80831161019e57829003601f168201915b505050505081565b80516101d690600090602084019061 01da565b5050565b828054600181600116156101000203166002900490600052602060002090601f 016020900481019282601f1061021b57805160ff1916838001178555610248565b82800160010185 558215610248579182015b8281111561024857825182559160200191906001019061022d565b5061 0254929150610258565b5090565b61027291905b80821115610254576000815560010161025e565b 905600a165627a7a72305820fc9db709577938943344a7dede7c9b2eb5d14d95c7a50ea783b551b0 bbc30aa80029', srcmap: '28:209:0:-;;;76:76;8:9:-1;5:2;;;30:1;27;20:12;5:2;76:76:0;;;;;; ;;;;;;;;;;;126:18;;;;:5;;:18;;;;;:::i;:::-;;76:76;28:209;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;28:209: 0;;;-1:-1:-1;28:209:0;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;' , srcmapRuntime: '28:209:0:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;48:19;;8:9:-1;5:2; ;;30:1;27;20:12;5:2;48:19:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8:100:-1;33:3;30:1 ;27:10;8:100;;;90:11;;;84:18;71:11;;;64:39;52:2;45:10;8:100;;;12:14;48:19:0;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;160:74;;8:9:-1;5:2;;;30:1;27;20:12;5: 2;-1:-1;160:74:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;160 :74:0;;-1:-1:-1;160:74:0;;-1:-1:-1;;;;;;;160:74:0;;;48:19;;;;;;;;;;;;;;;-1:-1:-1 ;;48:19:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;160:74::-;212:14;;;;:5;;:14;;;;;:::i;:::-;;160:74 ;:::o;28:209::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;28:209:0;;;-1:-1:-1;28:209:0;:::i;:::-;;;:::o;:::- ;;;;;;;;;;;;;;;;;;;;:::o' } }, sourceList: [ '' ], sources: { '': { AST: [Object] } } }

contracts实际编译出来的是所有的合约,而我们合约中只有Car的合约。

每个合约下面都有 assembly、bytecode、interface、metadata、opcodes 等字段。我们重点了解下:

  1. bytecode:即署合约到以太坊测试网络时需要使用;
  2. interface:即ABI:使用 web3 初始化智能合约交互实例的时候需要使用(备注:interface是被JSON.stringify处理过的字符串) interface中包含的元素数组:要么是合约构造函数,要么是可以调用的合约接口,并且每个元素里面有注明函数的类型、接收的参数类型、返回值的类型。

# 保存编译结果:编译脚本处理

编译脚本

# star or 打赏

Imtoken地址: 0x4a6Ac825993737a4f7F1ed12fcAc1b27e247c55A

打赏码 赞助支持