智能合约是以太坊的核心功能,可以自动执行预定义的逻辑。本文详细介绍如何编写、编译、部署智能合约,以及如何在多节点网络中验证合约同步,帮助开发者掌握智能合约开发的完整流程。
一、什么是智能合约
1. 基本概念
智能合约是运行在区块链上的程序,可以自动执行预定义的逻辑,无需第三方干预。智能合约一旦部署到区块链,代码就不可更改,所有执行结果都永久记录在区块链上。
2. 智能合约的特点
自动执行
- 满足条件自动执行
- 无需人工干预
- 减少信任成本
- 提高效率
不可篡改
- 部署后代码不可更改
- 执行结果永久记录
- 提高可信度
- 保护用户利益
透明公开
- 代码公开可查
- 执行过程透明
- 任何人都可验证
- 建立信任
3. 智能合约的应用
DeFi应用
- 去中心化交易所
- 借贷平台
- 流动性挖矿
- 稳定币系统
NFT和游戏
- 数字艺术品
- 游戏资产
- 收藏品
- 虚拟世界
企业应用
- 供应链管理
- 身份验证
- 投票系统
- 数据存储
二、如何编写智能合约
1. 简单存储合约
基础合约
1 | |
合约说明
pragma solidity ^0.8.0:指定Solidity版本contract SimpleStorage:定义合约uint256 public storedData:公共状态变量function set:修改状态的函数function get:只读函数(view)event DataChanged:事件,用于日志
2. 合约结构
1 | |
函数:solidity
1 | |
}
1 | |
}
1 | |
}事件:solidity
1 | |
}
1 | |
npm安装
npm install -g solc
或使用solc-select
npm install -g solc-select
solc-select install 0.8.0
solc-select use 0.8.0
编译合约:
1 | |
输出文件:
SimpleStorage.bin:字节码SimpleStorage.abi:ABI接口
2. 使用Truffle
初始化项目: truffle init 编译合约: truffle compile 输出位置:
build/contracts/SimpleStorage.json
3. 使用Hardhat
初始化项目: npx hardhat 编译合约: npx hardhat compile 输出位置:
artifacts/contracts/SimpleStorage.sol/SimpleStorage.json
四、如何部署智能合约
1. 使用Geth控制台部署
准备字节码和ABI:
// 加载字节码
var bytecode = “0x608060405234801561001057600080fd5b50…”
// 加载ABI
var abi = [{“constant”:true,”inputs”:[],”name”:”get”,”outputs”:[{“name”:””,”type”:”uint256”}],”type”:”function”},…]
1 | |
})
1 | |
}
1 | |
3. 使用Truffle部署
1 | |
};
执行部署:
1 | |
4. 使用Hardhat部署
// scripts/deploy.js
async function main() {
const [deployer] = await ethers.getSigners();
console.log(“部署账户:”, deployer.address);
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const contract = await SimpleStorage.deploy();
await contract.deployed();
console.log("合约地址:", contract.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
npx hardhat run scripts/deploy.js –network localhost
1 | |
在Geth控制台:
var contract = eth.contract(abi).at(contractAddress)
// 调用view函数(不消耗Gas)
contract.get()
// 调用状态改变函数
contract.set(42, {from: eth.accounts[0], gas: 100000})
while (eth.getTransactionReceipt(tx) == null) {
console.log(“等待确认…”)
1 | |
使用ethers.js:
const contract = new ethers.Contract(contractAddress, abi, signer);
const value = await contract.get();
console.log(‘Value:’, value.toString());
const tx = await contract.set(42);
await tx.wait();
2. 监听事件
使用Web3.js:
contract.events.DataChanged({
fromBlock: 0
}, function(error, event) {
console.log(‘事件:’, event);
})
.on(‘data’, function(event) {
console.log(‘旧值:’, event.returnValues.oldValue);
console.log(‘新值:’, event.returnValues.newValue);
});
contract.on(“DataChanged”, (oldValue, newValue, event) => {
console.log(‘旧值:’, oldValue.toString());
console.log(‘新值:’, newValue.toString());
});
1 | |
部署合约:
// 在节点1部署
var contract = eth.contract(abi).new({
1 | |
var contractAddress = eth.getTransactionReceipt(contract.transactionHash).contractAddress
2. 在节点2验证
验证同步:
// 在节点2,使用相同ABI
// 调用合约方法
contract.get() // 应该返回相同值
// 验证合约代码
eth.getCode(contractAddress) // 应该返回合约字节码
1 | |
节点1调用:
// 在节点1
contract.set(100, {from: eth.accounts[0], gas: 100000})
1 | |
本文标题: 智能合约编写部署
发布时间: 2024年02月03日 00:00
最后更新: 2026年09月16日 05:40
原始链接: https://haoxiang.eu.org/7690c0ba/
版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0许可协议,转载请注明出处!

