引言

智能合约安全是WEB3生态的生命线。一次漏洞可能导致数千万甚至数亿美元的损失。本文将系统性地探讨智能合约安全审计的完整方法论,从常见漏洞到审计工具,再到最佳实践。

常见漏洞类型

重入攻击(Reentrancy)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// ❌ 有重入漏洞的合约
contract VulnerableBank {
    mapping(address => uint256) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 amount) public {
        require(balances[msg.sender] >= amount, "Insufficient balance");

        // 滑洞:在更新状态前进行外部调用
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Transfer failed");

        balances[msg.sender] -= amount;
    }
}

// ✅ 修复后的合约(使用Checks-Effects-Interactions模式)
contract SecureBank {
    mapping(address => uint256) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 amount) public {
        require(balances[msg.sender] >= amount, "Insufficient balance");

        // 先更新状态
        balances[msg.sender] -= amount;

        // 再进行外部调用
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Transfer failed");
    }
}

// ✅ 使用ReentrancyGuard
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract GuardedBank is ReentrancyGuard {
    mapping(address => uint256) public balances;

    function withdraw(uint256 amount) external nonReentrant {
        require(balances[msg.sender] >= amount, "Insufficient balance");

        balances[msg.sender] -= amount;

        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Transfer failed");
    }
}

整数溢出/下溢

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// ❌ Solidity 0.8.0之前的溢出漏洞
contract OldVulnerable {
    uint256 public value;

    function unsafeAdd(uint256 a, uint256 b) public {
        // 可能溢出
        value = a + b;
    }

    function unsafeSubtract(uint256 a, uint256 b) public {
        // 可能下溢
        value = a - b;
    }
}

// ✅ Solidity 0.8.0+自动检查溢出
contract ModernSafe {
    uint256 public value;

    function safeAdd(uint256 a, uint256 b) public {
        // Solidity 0.8.0+自动检查溢出
        value = a + b;
    }

    function safeSubtract(uint256 a, uint256 b) public {
        // 自动检查下溢
        value = a - b;
    }

    // 使用SafeMath库(0.8.0之前)
    // using SafeMath for uint256;
}

// ✅ 使用OpenZeppelin的SafeMath(旧版Solidity)
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract SafeMathContract {
    using SafeMath for uint256;

    function add(uint256 a, uint256 b) public pure returns (uint256) {
        return a.add(b);  // 自动检查溢出
    }

    function sub(uint256 a, uint256 b) public pure returns (uint256) {
        return a.sub(b);  // 自动检查下溢
    }
}

访问控制漏洞

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ❌ 缺少访问控制
contract NoAccessControl {
    uint256 public importantValue;

    function setImportantValue(uint256 _value) public {
        // 任何人都可以调用
        importantValue = _value;
    }

    function destroy() public {
        // 任何人都可以销毁合约
        selfdestruct(payable(msg.sender));
    }
}

// ✅ 正确的访问控制
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

contract ProperAccessControl is Ownable, AccessControl {
    uint256 public importantValue;

    bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
    bytes32 public constant MANAGER_ROLE = keccak256("MANAGER_ROLE");

    constructor() Ownable(msg.sender) {
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _grantRole(ADMIN_ROLE, msg.sender);
        _setRoleAdmin(MANAGER_ROLE, ADMIN_ROLE);
    }

    // 只有所有者可以调用
    function setImportantValue(uint256 _value) external onlyOwner {
        importantValue = _value;
    }

    // 只有管理员可以调用
    function adminFunction() external onlyRole(ADMIN_ROLE) {
        // 管理员专属功能
    }

    // 管理员或经理可以调用
    function managerFunction() external onlyRole(ADMIN_ROLE) onlyRole(MANAGER_ROLE) {
        // 功能实现
    }

    // 基于时间的访问控制
    modifier onlyBefore(uint256 deadline) {
        require(block.timestamp < deadline, "Deadline passed");
        _;
    }

    function timedFunction() external onlyBefore(1735689600) {
        // 只能在指定时间前调用
    }

    // 多重签名
    mapping(bytes32 => bool) public signatures;
    uint256 public requiredSignatures = 2;

    function multiSigFunction(bytes32 data) external {
        bytes32 signature = keccak256(abi.encodePacked(data, msg.sender));
        signatures[signature] = true;

        uint256 count;
        bytes32 hash;
        for (uint256 i = 0; i < 255; i++) {
            hash = keccak256(abi.encodePacked(data, i));
            if (signatures[hash]) {
                count++;
            }
        }

        require(count >= requiredSignatures, "Not enough signatures");
    }
}

前端运行(Front-Running)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ❌ 容易被抢跑的合约
contract FrontRunnable {
    mapping(uint256 => uint256) public bids;
    uint256 public auctionEnd;

    function bid(uint256 amount) external payable {
        require(block.timestamp < auctionEnd, "Auction ended");

        // 滑洞:未隐藏出价,容易被抢跑
        bids[msg.sender] = msg.value;

        if (msg.value > bids[highestBidder]) {
            highestBidder = msg.sender;
        }
    }
}

// ✅ 使用commit-reveal方案
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

class CommitRevealAuction is ReentrancyGuard {
    struct Commitment {
        bytes32 hash;
        uint256 amount;
        bool revealed;
    }

    mapping(address => Commitment) public commitments;
    uint256 public commitDeadline;
    uint256 public revealDeadline;
    uint256 public highestBid;
    address public highestBidder;

    function commit(bytes32 hash) external payable {
        require(block.timestamp < commitDeadline, "Commit period ended");
        require(msg.value > 0, "Must commit with ETH");

        commitments[msg.sender] = Commitment({
            hash: hash,
            amount: msg.value,
            revealed: false
        });
    }

    function reveal(uint256 value, bytes32 salt) external nonReentrant {
        require(
            block.timestamp >= commitDeadline && block.timestamp < revealDeadline,
            "Not in reveal period"
        );

        bytes32 hash = keccak256(abi.encodePacked(value, salt));
        require(commitments[msg.sender].hash == hash, "Invalid reveal");

        commitments[msg.sender].revealed = true;

        if (value > highestBid) {
            // 退还之前的最高出价
            if (highestBidder != address(0)) {
                payable(highestBidder).transfer(highestBid);
            }

            highestBid = value;
            highestBidder = msg.sender;
        }
    }
}

// ✅ 使用暗池(暗拍卖)
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

contract SealedBidAuction {
    struct Bid {
        bytes32 blindedBid;
        uint256 deposit;
    }

    mapping(address => Bid) public bids;
    mapping(address => uint256) public refunds;

    uint256 public auctionEnd;
    address public highestBidder;
    uint256 public highestBid;
    bool public ended;

    function bid(bytes32 blindedBid) external payable {
        require(block.timestamp < auctionEnd, "Auction ended");
        require(msg.value >= highestBid / 10, "Deposit too low");

        bids[msg.sender] = Bid({
            blindedBid: blindedBid,
            deposit: msg.value
        });
    }

    function reveal(
        uint256[] calldata values,
        bytes32[] calldata secrets
    ) external {
        require(
            block.timestamp >= auctionEnd && !ended,
            "Cannot reveal"
        );

        for (uint256 i = 0; i < values.length; i++) {
            address bidder = msg.sender;
            Bid storage bid = bids[bidder];

            bytes32 hash = keccak256(abi.encodePacked(values[i], secrets[i]));

            if (hash != bid.blindedBid) {
                refunds[bidder] += bid.deposit;
                continue;
            }

            if (bid.deposit < values[i]) {
                refunds[bidder] += bid.deposit;
                continue;
            }

            if (values[i] > highestBid) {
                if (highestBidder != address(0)) {
                    refunds[highestBidder] += highestBid;
                }

                highestBidder = bidder;
                highestBid = values[i];
            }

            refunds[bidder] += bid.deposit - values[i];
        }
    }

    function withdrawRefund() external {
        uint256 refund = refunds[msg.sender];
        refunds[msg.sender] = 0;
        payable(msg.sender).transfer(refund);
    }
}

静态分析工具

Slither

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Slither安装
# pip install slither-analyzer

# 基础扫描
slither contract.sol

# 生成报告
slither contract.sol --json output.json
slither contract.sol --markdown output.md

# 自定义打印机
from slither import Slither
from slither.detectors import ReentrancyDetector
from slither.printers import CustomPrinter

slither = Slither('contract.sol')

# 检测重入漏洞
for detector in slither.detectors:
    if isinstance(detector, ReentrancyDetector):
        for finding in detector.detect():
            print(f"Reentrancy found: {finding}")

# 自定义检测器
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification

class MyCustomDetector(AbstractDetector):
    ARGUMENT = 'my-custom-detector'
    HELP = 'Custom detector description'
    IMPACT = DetectorClassification.HIGH
    CONFIDENCE = DetectorClassification.HIGH

    WIKI = 'https://github.com/my-detector/wiki'

    def detect(self):
        results = []

        for contract in self.contracts:
            for function in contract.functions:
                # 自定义检测逻辑
                if self.has_vulnerability(function):
                    results.append({
                        'contract': contract.name,
                        'function': function.name,
                        'line': function.source_mapping['start']['line']
                    })

        return results

Mythril

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Mythril安装
# pip install mythril

# 命令行使用
myth analyze contract.sol

# Python API
from mythril.platform import mythril_platform
from mythril.analysis import symbolic_executor

def analyze_contract(contract_path: str):
    platform = mythril_platform.get_platform()

    # 加载合约
    platform.set_execution_timeout(30)
    platform.load_bytecode(contract_path)

    # 执行符号执行
    executor = symbolic_executor.SymbolicExecutor()
    issues = executor.execute(platform.bytecode)

    # 分析结果
    for issue in issues:
        print(f"[{issue.severity}] {issue.title}")
        print(f"  Description: {issue.description}")
        print(f"  SWC ID: {issue.swc_id}")
        print()

# 自定义分析规则
from mythril.analysis.issue import Severity
from mythril.analysis.reporter import Issue

class MyCustomAnalyzer:
    def __init__(self):
        self.issues = []

    def check_access_control(self, bytecode):
        # 检查访问控制问题
        if not self.has_access_control(bytecode):
            self.issues.append(Issue(
                severity=Severity.HIGH,
                title="Missing Access Control",
                description="Critical functions lack access control",
                swc_id="SWC-105"
            ))

    def has_access_control(self, bytecode):
        # 实现检查逻辑
        return True

Echidna

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Echidna安装
# git clone https://github.com/crytic/echidna.git
# cd echidna
# cabal install

# Echidna配置文件
"""
echidna-test:
  # 测试用例
  testMode: assertion
  # 最大时间(秒)
  testLimit: 50000
  # 最大序列长度
  seqLen: 20
  # 合约覆盖率
  coverage: true
  # 指定部署账户
  deployer: "0x00a329c0648769a73afac7f9381e08fb43dbea70"
"""

# Solidity测试合约
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "echidna-test.sol";

contract VulnerableContract {
    uint256 public publicVar = 100;

    // 不变式:publicVar应该始终 <= 100
    function invariant_publicVar_not_greater_than_100() public view {
        assert(publicVar <= 100);
    }

    // 有漏洞的函数
    function setPublicVar(uint256 _value) public {
        publicVar = _value;  // Echidna会发现这里违反了不变式
    }

    // 正确的函数
    function safeSetPublicVar(uint256 _value) public {
        require(_value <= 100, "Value too large");
        publicVar = _value;
    }
}

// 运行Echidna
// echidna-test contract.sol --test-mode assertion

形式化验证

SMT求解器验证

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from z3 import *

def verify_swap_function():
    """使用Z3验证swap函数的正确性"""

    # 定义变量
    x = Real('x')  # 用户输入的token A数量
    y = Real('y')  # 用户输入的token B数量
    reserve_x = Real('reserve_x')  # 池中token A储备
    reserve_y = Real('reserve_y')  # 池中token B储备

    # 不变量
    k = Real('k')
    invariant = (reserve_x * reserve_y == k)

    # 前置条件
    preconditions = And(
        x > 0,
        y > 0,
        reserve_x > 0,
        reserve_y > 0,
        # 满足恒定乘积公式
        (reserve_x + x) * (reserve_y - y) == reserve_x * reserve_y,
        # y不能超过储备量
        y < reserve_y
    )

    # Swap后的状态
    new_reserve_x = reserve_x + x
    new_reserve_y = reserve_y - y

    # 后置条件
    postconditions = And(
        # 储备量应该增加/减少
        new_reserve_x == reserve_x + x,
        new_reserve_y == reserve_y - y,
        # 仍然满足恒定乘积
        new_reserve_x * new_reserve_y == reserve_x * reserve_y,
        # 储备量非负
        new_reserve_y >= 0
    )

    # 求解器验证
    s = Solver()
    s.add(invariant)
    s.add(preconditions)
    s.add(Not(postconditions))

    # 如果无解,说明后置条件总是满足
    if s.check() == unsat:
        print("✓ Swap函数是正确的")
        return True
    else:
        print("✗ 发现反例:")
        model = s.model()
        print(f"  x = {model[x]}")
        print(f"  y = {model[y]}")
        print(f"  reserve_x = {model[reserve_x]}")
        print(f"  reserve_y = {model[reserve_y]}")
        return False

# 验证AMM池
def verify_amm_invariant():
    """验证AMM恒定乘积不变式"""

    # 初始状态
    x0 = Real('x0')
    y0 = Real('y0')
    k = x0 * y0

    # 交易后状态
    dx = Real('dx')
    dy = Real('dy')
    x1 = x0 + dx
    y1 = y0 + dy

    # 验证恒定乘积
    s = Solver()

    # 约束条件
    s.add(x0 > 0, y0 > 0)
    s.add(k == x0 * y0)
    s.add(x0 * y0 == x1 * y1)

    # 检查是否可满足
    if s.check() == sat:
        model = s.model()
        print(f"有效交易: dx = {model[dx]}, dy = {model[dy]}")
        return True
    else:
        print("违反恒定乘积")
        return False

Certora规范

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Certora规范语言
// 使用SMT求解器验证智能合约

methods {
    function swap(uint256 amount0In, uint256 amount1In, address to, bytes calldata data) external;
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1);
}

// 确保swap遵循恒定乘积公式
RULE invariant CONSTANT_PRODUCT
    calldataarg uint256 amount0In;
    calldataarg uint256 amount1In;
    address to;
    bytes data;
{
    env e;
    require e.msg.value == 0;

    uint256 reserve0Before;
    uint256 reserve1Before;
    reserve0Before, reserve1Before = getReserves();

    swap(e, amount0In, amount1In, to, data);

    uint256 reserve0After;
    uint256 reserve1After;
    reserve0After, reserve1After = getReserves();

    // 恒定乘积公式:reserve0 * reserve1 应该保持不变
    assert(reserve0Before * reserve1Before == reserve0After * reserve1After,
        "Invariant violated: constant product formula");
}

// 确保swap不会导致储备量变为0
RULE invariant NO_ZERO_RESERVES
    calldataarg uint256 amount0In;
    calldataarg uint256 amount1In;
    address to;
    bytes data;
{
    env e;

    swap(e, amount0In, amount1In, to, data);

    uint256 reserve0;
    uint256 reserve1;
    reserve0, reserve1 = getReserves();

    assert(reserve0 > 0 && reserve1 > 0, "Reserves cannot be zero");
}

// 验证转账函数
FUNCTION transfer(address to, uint256 amount)
    creates
        evm(uint256 balance) = balanceOf(to),
        evm(uint256 balance) = balanceOf(msg.sender)
    updates
        balanceOf(to) = toBalance => toBalance >= balance,
        balanceOf(msg.sender) = fromBalance => fromBalance <= balance;

// 确保总供应量不变
RULE invariant TOTAL_SUPPLY
{
    env e;
    uint256 totalBefore = totalSupply();

    // 执行任意操作
    havoc(e);

    uint256 totalAfter = totalSupply();

    assert(totalBefore == totalAfter, "Total supply changed");
}

审计流程

完整审计清单

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 智能合约审计清单

## 1. 代码质量检查
- [ ] 遵循Solidity最佳实践
- [ ] 使用最新编译器版本
- [ ] 启用优化器
- [ ] 遵循Checks-Effects-Interactions模式
- [ ] 避免使用tx.origin进行身份验证
- [ ] 使用SafeMath(旧版本)
- [ ] 正确处理浮点数(使用定点数)

## 2. 访问控制审查
- [ ] 关键函数有适当的访问控制
- [ ] onlyOwner修饰符正确使用
- [ ] 角色权限合理配置
- [ ] 多重签名机制
- [ ] 时间锁机制

## 3. 状态管理
- [ ] 外部调用在状态更新之后
- [ ] 重入保护
- [ ] 正确的事件记录
- [ ] 临界区管理

## 4. 数值处理
- [ ] 整数溢出保护
- [ ] 除法检查(除数不为0)
- [ ] 舍入误差处理
- [ ] 浮点数使用正确

## 5. 逻辑漏洞
- [ ] 业务逻辑完整性
- [ ] 边界条件处理
- [ ] 异常情况处理
- [ ] 竞态条件检查

## 6. DeFi特定检查
- [ ] 滑点保护
- [ ] MEV防护
- [ ] 抢跑保护
- [ ] 清算机制
- [ ] 价格操纵防护
- [ ] oracle使用正确

## 7. Gas优化
- [ ] 循环优化
- [ ] 存储优化
- [ ] 批量操作
- [ ] 事件记录优化

## 8. 升级机制
- [ ] 代理模式正确实现
- [ ] 存储布局兼容性
- [ ] 升级流程安全
- [ ] 紧急暂停机制

## 9. 测试覆盖
- [ ] 单元测试覆盖率 > 90%
- [ ] 集成测试完整
- [ ] 模糊测试
- [ ] 形式化验证

## 10. 文档
- [ ] NatSpec注释完整
- [ ] 架构文档
- [ ] 用户文档
- [ ] API文档

分阶段审计

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
interface AuditPhases {
  phase1: {
    name: "初步扫描",
    duration: "2-3天",
    tools: ["Slither", "Mythril", "MythX"],
    output: "漏洞清单"
  },
  phase2: {
    name: "人工代码审查",
    duration: "1-2周",
    methods: ["行内审查", "架构分析", "威胁建模"],
    output: "审计报告初稿"
  },
  phase3: {
    name: "测试和验证",
    duration: "1周",
    methods: ["单元测试", "集成测试", "形式化验证"],
    output: "测试报告"
  },
  phase4: {
    name: "修复验证",
    duration: "3-5天",
    process: "修复后重新测试",
    output: "最终审计报告"
  }
}

DeFi安全最佳实践

Oracle使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ✅ 使用Chainlink Price Feed
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract OracleExample {
    AggregatorV3Interface internal priceFeed;

    constructor() {
        // ETH/USD Price Feed
        priceFeed = AggregatorV3Interface(
            0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
        );
    }

    function getLatestPrice() public view returns (int256) {
        (
            uint80 roundId,
            int256 price,
            uint256 startedAt,
            uint256 timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();

        // 检查价格是否新鲜
        require(
            timeStamp + 3 hours > block.timestamp,
            "Price too stale"
        );

        // 检查数据是否为空
        require(price > 0, "Invalid price");

        return price;
    }

    function getPrice(uint256 amount) external view returns (uint256) {
        int256 price = getLatestPrice();

        // price有8位小数
        return (amount * uint256(price)) / 1e8;
    }
}

// ✅ TWAP(时间加权平均价格)
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";

contract TWAPExample {
    struct Observation {
        uint256 timestamp;
        uint256 price0Cumulative;
        uint256 price1Cumulative;
    }

    function getTWAP(
        address pool,
        uint32 secondsAgo
    ) external view returns (uint256 price) {
        // 获取当前观察值
        (
            uint256 price0Cumulative,
            uint256 price1Cumulative,
            uint32 blockTimestamp
        ) = IUniswapV3Pool(pool).observe(secondsAgo);

        // 计算TWAP
        uint256 timeElapsed = blockTimestamp - (blockTimestamp - secondsAgo);
        require(timeElapsed > 0, "Not enough data");

        price = (price0Cumulative - price1Cumulative) / timeElapsed;

        return price;
    }
}

滑点保护

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ✅ 滑点保护实现
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";

contract SlippageProtection {
    IUniswapV2Router02 public router =
        IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);

    function swapWithSlippage(
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut
    ) external returns (uint256 amountOut) {
        // 授权
        IERC20(tokenIn).approve(address(router), amountIn);

        // 定义路径
        address[] memory path = new address[](2);
        path[0] = tokenIn;
        path[1] = tokenOut;

        // 执行交换
        uint256[] memory amounts = router.swapExactTokensForTokens(
            amountIn,
            minAmountOut,  // 最小输出量(滑点保护)
            path,
            block.timestamp
        );

        amountOut = amounts[1];

        require(
            amountOut >= minAmountOut,
            "Slippage exceeded"
        );
    }

    function calculateMinAmountOut(
        uint256 amountIn,
        uint256 slippageBps
    ) external pure returns (uint256) {
        // slippageBps: 基点,100 = 1%
        uint256 slippage = (amountIn * slippageBps) / 10000;
        return amountIn - slippage;
    }
}

// ✅ 动态滑点
contract DynamicSlippage {
    uint256 public baseSlippage = 30;  // 0.3%
    uint256 public maxSlippage = 300;  // 3%

    function getDynamicSlippage(uint256 volatility) public view returns (uint256) {
        // 根据波动率调整滑点
        uint256 slippage = baseSlippage + (volatility * 10);

        // 不超过最大滑点
        if (slippage > maxSlippage) {
            slippage = maxSlippage;
        }

        return slippage;
    }

    function swapWithDynamicSlippage(
        uint256 amountIn,
        uint256 volatility
    ) external returns (uint256) {
        uint256 slippageBps = getDynamicSlippage(volatility);
        uint256 minAmountOut = calculateMinAmountOut(amountIn, slippageBps);

        // 执行交换...
    }
}

总结

智能合约安全审计是WEB3开发不可或缺的环节。系统性的审计流程、专业的分析工具和严格的安全实践是保护资产安全的关键。

记住:

  • 安全无小事
  • 审计不等于100%安全
  • 防御深度原则
  • 持续监控和更新

参考资料