跳转至主要内容
Change page

编译智能合约

上次修改时间: @mrip03(opens in a new tab), 2024年3月19日

你需要编译你的合约,以便你的 web 应用程序和以太坊虚拟机能够理解它。

前提条件

在阅读关于编译的文档之前,请阅读我们的智能合约以太坊虚拟机可能会有帮助。

以太坊虚拟机

要使以太坊虚拟机能够运行你的合约,你的合约必须被编译为字节码。 编译过程把如下代码:

1pragma solidity 0.4.24;
2
3contract Greeter {
4
5 function greet() public constant returns (string) {
6 return "Hello";
7 }
8
9}
显示全部
复制

转化为

1PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH2 0x41 JUMPI PUSH1 0x0 CALLDATALOAD PUSH29 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0xCFAE3217 EQ PUSH2 0x46 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x52 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x5B PUSH2 0xD6 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 MSTORE DUP4 DUP2 DUP2 MLOAD DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x9B JUMPI DUP1 DUP3 ADD MLOAD DUP2 DUP5 ADD MSTORE PUSH1 0x20 DUP2 ADD SWAP1 POP PUSH2 0x80 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0xC8 JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP SWAP3 POP POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x60 PUSH1 0x40 DUP1 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 PUSH1 0x5 DUP2 MSTORE PUSH1 0x20 ADD PUSH32 0x48656C6C6F000000000000000000000000000000000000000000000000000000 DUP2 MSTORE POP SWAP1 POP SWAP1 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 SLT 0xec 0xe 0xf5 0xf8 SLT 0xc7 0x2d STATICCALL ADDRESS SHR 0xdb COINBASE 0xb1 BALANCE 0xe8 0xf8 DUP14 0xda 0xad DUP13 LOG1 0x4c 0xb4 0x26 0xc2 DELEGATECALL PUSH7 0x8994D3E002900

Web 应用程序

编译器还将生成你需要的应用程序二进制接口 (ABI) ,以便你的应用程序能够理解合约并调用合约的功能。

ABI 是一份 JSON 文件,描述了部署的合约及这个智能合约的函数。 这在 web2 和 web3 之间的鸿沟上架起交流的桥梁

Javascript 客户端库将读取应用程序二进制接口 (ABI),以便你在 Web 应用程序接口中调用你的智能合约。

以下是 ERC-20 代币合约的应用程序二进制接口。 ERC-20 是你可以在以太坊交易的代币。

1[
2 {
3 "constant": true,
4 "inputs": [],
5 "name": "name",
6 "outputs": [
7 {
8 "name": "",
9 "type": "string"
10 }
11 ],
12 "payable": false,
13 "stateMutability": "view",
14 "type": "function"
15 },
16 {
17 "constant": false,
18 "inputs": [
19 {
20 "name": "_spender",
21 "type": "address"
22 },
23 {
24 "name": "_value",
25 "type": "uint256"
26 }
27 ],
28 "name": "approve",
29 "outputs": [
30 {
31 "name": "",
32 "type": "bool"
33 }
34 ],
35 "payable": false,
36 "stateMutability": "nonpayable",
37 "type": "function"
38 },
39 {
40 "constant": true,
41 "inputs": [],
42 "name": "totalSupply",
43 "outputs": [
44 {
45 "name": "",
46 "type": "uint256"
47 }
48 ],
49 "payable": false,
50 "stateMutability": "view",
51 "type": "function"
52 },
53 {
54 "constant": false,
55 "inputs": [
56 {
57 "name": "_from",
58 "type": "address"
59 },
60 {
61 "name": "_to",
62 "type": "address"
63 },
64 {
65 "name": "_value",
66 "type": "uint256"
67 }
68 ],
69 "name": "transferFrom",
70 "outputs": [
71 {
72 "name": "",
73 "type": "bool"
74 }
75 ],
76 "payable": false,
77 "stateMutability": "nonpayable",
78 "type": "function"
79 },
80 {
81 "constant": true,
82 "inputs": [],
83 "name": "decimals",
84 "outputs": [
85 {
86 "name": "",
87 "type": "uint8"
88 }
89 ],
90 "payable": false,
91 "stateMutability": "view",
92 "type": "function"
93 },
94 {
95 "constant": true,
96 "inputs": [
97 {
98 "name": "_owner",
99 "type": "address"
100 }
101 ],
102 "name": "balanceOf",
103 "outputs": [
104 {
105 "name": "balance",
106 "type": "uint256"
107 }
108 ],
109 "payable": false,
110 "stateMutability": "view",
111 "type": "function"
112 },
113 {
114 "constant": true,
115 "inputs": [],
116 "name": "symbol",
117 "outputs": [
118 {
119 "name": "",
120 "type": "string"
121 }
122 ],
123 "payable": false,
124 "stateMutability": "view",
125 "type": "function"
126 },
127 {
128 "constant": false,
129 "inputs": [
130 {
131 "name": "_to",
132 "type": "address"
133 },
134 {
135 "name": "_value",
136 "type": "uint256"
137 }
138 ],
139 "name": "transfer",
140 "outputs": [
141 {
142 "name": "",
143 "type": "bool"
144 }
145 ],
146 "payable": false,
147 "stateMutability": "nonpayable",
148 "type": "function"
149 },
150 {
151 "constant": true,
152 "inputs": [
153 {
154 "name": "_owner",
155 "type": "address"
156 },
157 {
158 "name": "_spender",
159 "type": "address"
160 }
161 ],
162 "name": "allowance",
163 "outputs": [
164 {
165 "name": "",
166 "type": "uint256"
167 }
168 ],
169 "payable": false,
170 "stateMutability": "view",
171 "type": "function"
172 },
173 {
174 "payable": true,
175 "stateMutability": "payable",
176 "type": "fallback"
177 },
178 {
179 "anonymous": false,
180 "inputs": [
181 {
182 "indexed": true,
183 "name": "owner",
184 "type": "address"
185 },
186 {
187 "indexed": true,
188 "name": "spender",
189 "type": "address"
190 },
191 {
192 "indexed": false,
193 "name": "value",
194 "type": "uint256"
195 }
196 ],
197 "name": "Approval",
198 "type": "event"
199 },
200 {
201 "anonymous": false,
202 "inputs": [
203 {
204 "indexed": true,
205 "name": "from",
206 "type": "address"
207 },
208 {
209 "indexed": true,
210 "name": "to",
211 "type": "address"
212 },
213 {
214 "indexed": false,
215 "name": "value",
216 "type": "uint256"
217 }
218 ],
219 "name": "Transfer",
220 "type": "event"
221 }
222]
显示全部
复制

延伸阅读

本文对你有帮助吗?

网站最后更新: 2024年7月10日

学习

  • 学习中心
  • 什么是以太坊?
  • 什么是以太币 (ETH)?
  • 以太坊钱包
  • 什么是 Web3?
  • 智能合约
  • Gas fees
  • 运行节点
  • 以太坊安全和预防欺诈措施
  • 测试中心
  • 以太坊词汇表
(opens in a new tab)(opens in a new tab)(opens in a new tab)
  • 关于我们
  • 以太坊品牌资产
  • 行为守则
  • 工作机会
  • 隐私政策
  • 使用条款
  • 缓存政策
  • 媒体联系方式(opens in a new tab)