主页 学院 指南 智能合约解析

智能合约解析: 非技术指南

Understand how code that runs by itself on a blockchain powers DeFi, NFTs and DAOs. No programming knowledge required: clear explanations, everyday analogies and practical takeaways.

12 分钟阅读 2026 年 9 月更新 区块链基础

什么是智能合约?

A smart contract is a program stored on a blockchain that runs by itself when specific conditions are met. Ethereum.org defines it as a program deployed at an address on the chain: it holds a balance, and it executes its code when it receives a transaction. Unlike a traditional contract, which relies on lawyers, courts or intermediaries to enforce its terms, a smart contract does exactly what its code says, every time, without anyone in the middle.

这一理念早于比特币。计算机科学家尼克·萨博在20世纪90年代中期创造了这一术语,并描述了数字协议如何能够自动执行协议条款(见来源)。2015年7月以太坊推出后,智能合约变得实用,此前维塔利克·布特林在2013年白皮书中提出了一种具有图灵完备编程语言的区块链。.

自动售货机类比

理解智能合约最简单的方式是Szabo本人使用过的类比,ethereum.org至今仍在使用:自动售货机。你投入一枚硬币(输入),机器检查金额(条件),然后它分发你的零食(输出)。它不需要收银员。它不在乎你是谁。它每次都遵循其程序。智能合约的工作方式相同,只不过它运行在区块链上,处理数字资产,并且一旦部署,没有人能拔掉机器的电源或更改其规则。.

At its core, a smart contract is if-then logic: if condition X is true, then execute action Y. For example: if Alice sends 1 ETH to this contract, then send 2,000 USDC from the contract's reserves to Alice's address. This predictable execution is what people mean by "trustless": you do not need to trust the other party, because the code determines the outcome.

自执行

No intermediaries. The code runs when its conditions are met, exactly as written.

不可变

Once deployed, the code cannot be edited. The rules stay as they were published, unless the contract was built with an upgrade mechanism.

无信任

You rely on the code, not on a counterparty. Anyone can read a verified contract's logic on a public block explorer.

智能合约如何工作

智能合约遵循明确的生命周期:开发者编写代码,将其部署到区块链上,然后用户(或其他合约)通过发送交易与其交互。以下是 ethereum.org 开发者文档中描述的每个步骤所发生的情况。.

1

已部署在区块链上

A developer writes the contract, usually in Solidity for Ethereum, and compiles it into bytecode. That bytecode is sent to the network in a special transaction. The network gives the contract its own address, just like a wallet address. From then on the contract lives on the blockchain and anyone can call it.

Deployment costs gas: the fee paid for processing the transaction and storing the contract code on every node. Larger contracts contain more bytecode and need more storage, so they cost more gas to deploy. See our gas fees guide for how the fee is calculated.

2

部署后不可更改

Once a contract is on the blockchain, its code cannot be edited. This is a feature: it guarantees that the rules cannot be changed after the fact. If you deposit into a lending contract with a published interest formula, nobody can rewrite that formula or seize your funds by editing the code.

不可变性也意味着漏洞是永久性的。已部署代码中的逻辑错误无法就地修补。这就是为什么测试、审计和形式化验证要在部署之前进行,也是为什么一些团队使用可升级代理模式,这种模式以牺牲少量不可变性为代价来换取修复漏洞的能力(参见常见问题解答)。.

3

由交易触发

智能合约不会自行运行。它们处于闲置状态,直到有人发送调用其某个函数的事务。该事务携带函数名称、参数和燃料费。例如,在 DeFi 交易所合约上调用 swap() 函数会触发代币交换逻辑。

Contracts can also call other contracts. This is the basis of DeFi composability: a single user transaction can trigger a chain of contract interactions across several protocols.

4

确定性执行

Given the same inputs and the same blockchain state, a smart contract always produces the same output. Every node on the network executes the contract independently and reaches the same result. This determinism is what lets thousands of computers agree on an outcome without trusting each other, per the ethereum.org EVM documentation.

这也意味着合约无法自行获取外部数据:它只知道区块链上的信息。要使用链下数据(如资产价格或天气读数),合约依赖 预言机 (如 Chainlink)将这些信息带上链。

智能合约用例

Smart contracts are behind almost every application in crypto. Here are the main categories where self-executing code has replaced a traditional intermediary.

DeFi(去中心化金融)

DeFi is the largest category of smart contract applications. Lending protocols such as Aave and Morpho use contracts to match lenders and borrowers without a bank. DEXs such as Uniswap enable token swaps without an order book or a centralized exchange. Staking contracts let users lock tokens and earn APY rewards automatically.

Every interaction (depositing collateral, borrowing, earning interest, liquidating undercollateralized positions) is handled by contract logic.

NFT(非同质化代币)

每个NFT系列都是一个智能合约。ERC-721和ERC-1155标准定义了在以太坊上如何创建、转移和拥有独特的数字资产。合约处理铸造、所有权记录和市场列表。.

Creator royalties show both the promise and the limits. A contract can publish a royalty rate (the ERC-2981 standard exists for this), but the standard only signals the rate; whether it is paid depends on the marketplace honouring it.

去中心化自治组织(DAOs)

A DAO is an organization whose rules are enforced by smart contracts. Members hold governance tokens that give them voting rights on proposals. Contracts count votes, enforce quorum, execute approved proposals and hold the treasury, without a CEO or a board.

Large DAOs such as MakerDAO (now Sky), Uniswap DAO and Arbitrum DAO manage substantial treasuries through on-chain votes. See our DAO guide for how governance works.

保险 & 供应链

Parametric insurance uses smart contracts to pay claims automatically from verifiable data. For example, a crop insurance contract can read a weather oracle, detect a drought and pay the farmer without a claims adjuster, paperwork or delay.

In supply chains, contracts can record goods moving from factory to consumer, verify provenance and release payment at each checkpoint. Several large companies have piloted such systems; results have been mixed, and adoption outside crypto remains limited.

在USDC上赚取7.50%年化收益率

智能合约为 Coinstancy Dollar Savings 背后的链上策略提供支持。利息每秒累计并自动复投。无锁定期,可随时提取。.

立即开始赚取

以太坊上的智能合约

以太坊是智能合约成为主流的平台,并且至今仍是最大的智能合约平台。2013 年的白皮书提出了一种内置图灵完备语言的区块链,因此它可以运行任意程序,而不仅仅像比特币那样转移价值。据 ethereum.org 的历史页面,该网络于 2015 年 7 月上线。如今,它承载着最大份额的 DeFi、NFT 和 DAO 应用,且其中越来越多的活动发生在其 Layer 2 网络上。.

Solidity: 以太坊的语言

大多数以太坊智能合约是用 Solidity 编写的,这是一种为以太坊虚拟机(EVM)设计的静态类型语言。其文档称它受到 C++、Python 和 JavaScript 的影响。它具有地址和余额的内置类型,以及对加密函数的内置访问。.

Vyper is the main alternative. It uses a Python-like syntax and aims for simplicity and auditability: per its documentation, it deliberately leaves out features such as inheritance, function and operator overloading and inline assembly, to reduce the attack surface. Some well-known DeFi protocols, including Curve, are written in Vyper.

以太坊虚拟机 (EVM)

The EVM is the runtime that executes contract bytecode. Every Ethereum node runs the same EVM, so contract execution is identical across the whole network. When you send a transaction to a contract, every validator processes the same bytecode with the same inputs and reaches the same result, per ethereum.org.

The EVM has become a de facto standard. Layer 2 networks such as Arbitrum, Optimism and Base, and independent chains such as BNB Chain and Avalanche, are "EVM-compatible": they run the same Solidity contracts with little or no modification.

Gas 成本和部署过程

Every operation on Ethereum costs gas, a unit of computational effort. A simple ETH transfer costs 21,000 gas, per ethereum.org, while deploying a large contract can cost millions of gas units. The fee equals gas used multiplied by the gas price in gwei. When the network is busy, gas prices rise and deployment and interaction become expensive; live prices are on the Etherscan gas tracker (see our gas fees guide).

A typical workflow is: write the contract in Solidity, compile it, test it on a local network (with tools such as Hardhat or Foundry), deploy to a public testnet such as Sepolia, commission a security audit, then deploy to mainnet. Professional teams usually control deployment and admin functions through a multisignature wallet such as Safe.

智能合约安全

Security is the most important concern in smart contract development. Because contracts are hard to change and hold real value, a single vulnerability can cost millions of dollars with no way to reverse it. The industry has built up a set of practices to reduce that risk.

安全审计

独立安全公司会逐行审查代码,查找重入、访问控制缺陷、算术错误和逻辑错误。所需时间和成本取决于代码的规模和复杂度;审计公司会在报告中公布其审计范围和发现的问题,协议方通常会将该报告公开。.

漏洞赏金

Protocols pay security researchers who report vulnerabilities responsibly. Platforms such as Immunefi host these programmes, with rewards scaled to severity; the largest critical-bug bounties run to millions of dollars. Each programme publishes its own reward table.

形式化验证

Formal verification uses mathematical proofs to show that a contract satisfies specific properties for all possible inputs. It is stronger than a manual review for the properties it covers, but it only proves what is written in the specification: if the specification misses a case, so does the proof.

Well-Known Audit Firms

知名机构包括 Trail of Bits(安全工具与审计)、OpenZeppelin(广泛使用的合约库的维护者)以及 Certora(形式化验证)。来自知名机构的审计是一个有用的信号,但请阅读报告:审计范围、发现的问题以及这些问题是否已修复,比机构标志更重要。.

Important: an audit is not a guarantee of safety. Audits are point-in-time reviews and can miss new attack patterns. Many exploited protocols had been audited. Look for several audits, an active bug bounty and a record of careful upgrades before you deposit.

著名的智能合约攻击

The history of smart contracts includes several large exploits that changed how the industry approaches security. Knowing them helps you judge the risk of any DeFi protocol. The figures below are the ones reported by the affected teams at the time.

2016 3.6M ETH drained

DAO 攻击

The DAO was the first large decentralized autonomous organization on Ethereum and raised a record amount of ETH in its 2016 token sale. An attacker exploited a reentrancy vulnerability: a flaw that let the attacker's contract call the withdrawal function again and again before the DAO updated its balance. About 3.6 million ETH was drained, per the ethereum.org history page, worth tens of millions of dollars at the time.

The community responded with a hard fork that returned the funds, which split the network into Ethereum (the forked chain) and Ethereum Classic (the original, unaltered history). The event made reentrancy the best-known smart contract vulnerability and led to the "checks-effects-interactions" pattern that the Solidity documentation now recommends.

2022 120,000 wETH minted

Wormhole 桥攻击

Wormhole, a cross-chain bridge linking Ethereum and Solana, was exploited in February 2022. The attacker found a flaw in the Solana-side contract that let them mint 120,000 wrapped ETH (wETH) on Solana without locking the corresponding ETH on Ethereum, according to the Wormhole team's incident report.

The root cause was a signature verification bypass: the contract did not properly validate the "guardian" signatures that authorize cross-chain transfers. Jump Crypto, which backs the Wormhole development team, replaced the missing ETH so that users were made whole. The incident showed the specific risk of bridge contracts, which must keep state consistent across several blockchains.

2023 About $197M, later returned

Euler Finance 漏洞利用

Euler Finance, an Ethereum lending protocol, lost about $197 million in a flash loan attack in March 2023, per the team's own disclosures. The attacker abused a flaw in the donateToReserves function to manipulate a debt position and extract funds from the lending pools.

Unusually, the attacker returned the funds after negotiations with the Euler team, making it one of the largest recoveries in DeFi. The incident showed that audited protocols (Euler had several audits) can still contain exploitable logic errors, and that fast incident response can sometimes recover stolen assets.

经验教训

  • 多次审计胜过单次审计。不同的审计人员能发现不同的漏洞。.
  • Bridges are high-value targets. Cross-chain contracts hold large sums and have a larger attack surface than single-chain protocols.
  • 闪电贷会放大一切。攻击者可以在单笔交易内借入大量资金,因此即使是很小的漏洞也可能被大规模利用。.
  • 不可变性是一把双刃剑。它保护用户免受规则变更的影响,但这也意味着漏洞无法就地修补。.

通过 Coinstancy 在 USDC 上赚取 7.50% 的年化收益率

利息每秒累计并自动复投。无锁定期,随时可提取。由链上智能合约提供支持。.

免费试用 Coinstancy

以太坊之外的智能合约

Ethereum made smart contracts mainstream, but many other blockchains now support them, often with different languages, execution environments and trade-offs. Here are the main alternatives.

区块链 语言 虚拟机 / 运行时 Key Characteristic
以太坊 Solidity / Vyper EVM Largest ecosystem, longest track record
Solana Rust (Anchor framework) Sealevel (SVM) Parallel execution, low fees
Cosmos 生态系统 Rust (CosmWasm) CosmWasm 虚拟机 跨链通信(IBC),应用链
Arbitrum Solidity (EVM) Nitro(乐观汇总) Ethereum security, much lower fees
Base Solidity (EVM) OP Stack(乐观汇总) Built by Coinbase on the OP Stack

Solana:程序,而非合约

Solana takes a different approach. Smart contracts on Solana are called "programs" and are usually written in Rust, often with the Anchor framework. Its runtime, Sealevel, can execute transactions in parallel as long as they do not touch the same accounts, whereas the EVM processes transactions one after another.

This design gives high throughput and very low fees, at the cost of a steeper learning curve for developers and a security model that differs from the EVM. Details are in the Solana developer documentation.

Cosmos:CosmWasm 与跨链智能合约

The Cosmos ecosystem uses CosmWasm, a WebAssembly-based contract platform where contracts are written in Rust. CosmWasm relies on Rust's memory safety and on an actor-style execution model that avoids several common attack patterns, including reentrancy.

The distinctive feature of Cosmos is IBC (Inter-Blockchain Communication), a protocol that lets contracts on different Cosmos chains exchange messages and assets without a third-party bridge. The result is a network of interoperable, application-specific blockchains.

第2层汇总:Arbitrum 和 Base

Arbitrum 和 Base 是以太坊 Layer 2 网络,它们运行与主网相同的 EVM 合约,在主链之外执行交易,并将压缩后的交易数据回传至以太坊。开发者可以在大幅降低 gas 成本的同时获得以太坊的安全保障,并实现更快的确认速度。.

对开发者而言,迁移很简单:大多数 Solidity 合约几乎无需修改即可部署到 Arbitrum 或 Base。对用户而言,体验接近主网,费用以美分而非美元计。请参阅我们的 gas 费用指南 了解 L2 费用是如何构成的。

智能合约的未来

Smart contracts have matured a great deal since The DAO, and the technology keeps changing. Here are three developments shaping the next generation.

账户抽象

Account abstraction (ERC-4337) turns a wallet into a smart contract. Instead of depending on a single private key, the wallet can enforce programmable rules: social recovery, spending limits, scheduled payments, session keys for games, and transactions whose gas is paid by an application.

这解决了加密货币中两个最大的可用性问题:助记词和 gas。Safe 等智能合约钱包以及多个消费级钱包已经提供了这些功能。.

AI 辅助开发

大型语言模型正越来越多地用于起草、解释和审查智能合约代码。它们可以生成 Solidity 样板代码、标记常见漏洞模式,并将通俗语言规范转化为初稿。.

AI-generated code still needs human review and a proper audit before it handles real funds. The realistic near-term effect is faster development and cheaper first-pass review, not contracts that write and verify themselves.

跨链互操作性

智能合约越来越多地跨越多个链。LayerZero、Chainlink CCIP 和 Cosmos IBC 等协议提供了消息传递层,使不同区块链上的合约能够相互通信。.

目标是让以太坊上的合约能够在一次用户交互中触发 Solana 上的操作、在 Arbitrum 上结算并回报结果。这将减少流动性碎片化,使用户无论从哪条链开始,都能获得最优价格和应用。跨链桥和消息层仍然是漏洞利用的主要来源,因此这一领域需要谨慎对待。.

常见问题

什么是智能合约的简明解释?
A smart contract is a program stored on a blockchain that runs by itself when its conditions are met. Ethereum.org compares it to a vending machine: you put in money (input), the machine checks the conditions (enough payment, item in stock), and it delivers the product (output) without a cashier. Once the contract is deployed, nobody can quietly change its rules.
部署后智能合约可以更改吗?
By default, no. The code of a deployed contract cannot be edited. Developers can, however, use upgradeable proxy patterns (such as the Transparent Proxy or UUPS patterns documented by OpenZeppelin) in which users interact with a fixed proxy address while the logic contract behind it can be swapped. This adds flexibility but also a trust assumption: whoever holds the upgrade key can change how the contract behaves. Check whether a contract is upgradeable, and who controls the upgrade, before you deposit funds.
智能合约在法律上具有约束力吗?
这取决于司法管辖区。美国几个州,包括亚利桑那州、田纳西州和怀俄明州,已通过法律承认通过区块链代码记录或执行的合同。英国法律委员会在2021年得出结论,英格兰和威尔士现有的合同法可以容纳智能法律合同。然而,在大多数情况下,智能合约并不能取代书面协议:最好将其视为一种可以与书面协议并存的自动执行工具。.
部署智能合约的费用是多少?
Deployment costs gas, like any other transaction. The fee equals the gas used multiplied by the current gas price, per ethereum.org. Deploying a contract uses far more gas than a simple transfer, because the network has to store the contract code, and larger contracts cost more. Layer 2 networks such as Arbitrum and Base charge much less than Ethereum mainnet, and non-EVM chains such as Solana use their own fee models. Check the live gas price on a tracker before deploying.
使用智能合约是否需要了解编程?
不。大多数用户通过网页界面与智能合约交互,而无需查看代码。当你在去中心化交易所(DEX)上兑换代币、在 Aave 上借贷或铸造 NFT 时,你正是通过该界面调用智能合约。了解基础知识仍然有帮助:它能让你判断风险、阅读审计摘要,并决定哪些协议值得投入你的资金。.
如果智能合约出现漏洞会怎样?
Because deployed code cannot simply be patched, a bug can be exploited and funds can be lost for good. That is why audits, formal verification and bug bounty programmes happen before launch, and why many protocols add timelocks, multisignature controls and emergency pause functions. In one extreme case, the 2016 DAO attack, the Ethereum community chose to hard fork the chain to reverse the theft; ethereum.org records the event in its history. That remains a rare and controversial response.

继续学习

探索更多关于 DeFi 协议和加密基础的指南。.

智能合约驱动您的收益

通过 Coinstancy 美元储蓄,在 USDC 上赚取 7.50% 的年化收益率。利息每秒累计并自动复投。无锁定期,可随时提取。储蓄存款首先进入由 Coinstancy 团队管理的多签钱包,然后被分配到相关的链上策略,该策略基于您刚刚了解到的相同技术构建。.

在Coinstancy开始赚取收益

来源与延伸阅读

本页面中的数字和声明均基于以下文档。具有时效性的数字(利率、收益率、费用、市场数据)会变动:在据此行动之前,请在来源处核实实时数值。.

  1. Ethereum.org, Introduction to smart contractsethereum.org

    Definition of a smart contract, the vending machine analogy, deployment, immutability and the need for oracles.

  2. 以太坊白皮书(Vitalik Buterin,2013 年)ethereum.org

    The original proposal for a blockchain with a Turing-complete scripting language for smart contracts.

  3. Nick Szabo, Smart Contracts: Building Blocks for Digital Markets (1996)fon.hum.uva.nl

    The essay that coined the term and used the vending machine as the "primitive ancestor" of smart contracts.

  4. Solidity documentationdocs.soliditylang.org

    Solidity 是一种用于 EVM 的静态类型语言,受 C++、Python 和 JavaScript 影响。.

  5. Vyper documentationdocs.vyperlang.org

    Vyper design goals and the language features it deliberately leaves out for auditability.

  6. Ethereum.org, Ethereum Virtual Machine (EVM)ethereum.org

    EVM 如何在每个节点上确定性地执行合约字节码。.

  7. Ethereum.org,以太坊历史ethereum.org

    Timeline of the network, including the 2015 launch and the 2016 DAO attack and fork.

  8. OpenZeppelin documentationdocs.openzeppelin.com

    Standard contract libraries and the Transparent and UUPS upgradeable proxy patterns.

  9. ERC-721: Non-Fungible Token Standardeips.ethereum.org

    The token standard behind most NFTs on Ethereum.

  10. UK Law Commission, Smart legal contractslawcom.gov.uk

    2021年就现行法律如何适用于智能法律合约向政府提供的建议。.

最后审核:2026年9月。外部链接在新标签页中打开;Coinstancy 对其内容不承担责任。.

准备好使用智能合约赚取收益了吗?

通过 Coinstancy Dollar Savings,在 USDC 上赚取 7.50% 的年化收益率。利息每秒累计并自动复投。无锁定期,随时可提取。.