Solidity新手编写闪电贷合约遇Remix语法错误,求排查
Solidity闪电贷合约编译错误修复
错误信息
contracts/flash.sol:8:1: ParserError: Expected pragma, import directive or contract/interface/library/struct/enum definition. address private wallet = 0x7e31a8ba5cF188fd39f9aaCF667E9dFE2311A882; ^-----^
问题代码
pragma solidity ^0.6.0; import "https://github.com/aave/aave-solidity/contracts/AAVE.sol"; import "https://github.com/Uniswap/uniswap-v2-core/contracts/interfaces/IUniswapV2Pair.sol"; import "https://github.com/sushiswap/sushiswap-v2-core/contracts/interfaces/ISushiV2Pair.sol"; // Set the wallet address address private wallet = 0x0000000000000000000000000; // Set the contract addresses address private aave = 0x7deB5e830be29F91E298ba5FF1356BB7fC8B8C9D; // AAVE contract address address private uniswap = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f; // Uniswap contract address address private sushiswap = 0x6b3595068778dd592e39a122f4f5a5cf09c90fe2; // SushiSwap contract address // Set the token addresses address private eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // ETH token address address private ape = 0x27Dce1e12396F3a2B49E4FdD7a4C9d938E5e5F97; // APE token address // Set the contract ABIs AAVE aaveContract; IUniswapV2Pair uniswapContract; ISushiV2Pair sushiswapContract; constructor() public { aaveContract = AAVE(aave); uniswapContract = IUniswapV2Pair(uniswap); sushiswapContract = ISushiV2Pair(sushiswap); } // Borrow 100 ETH from AAVE function borrowFromAAVE() public { aaveContract.borrow(eth, 100 ether, wallet); } // Swap 20 ETH to APE on SushiSwap function swapETHtoAPEonSushiSwap(uint amount) public { sushiswapContract.swap(amount ether, 10**18, ape, wallet, address(this)); } // Swap 80 ETH to APE on Uniswap function swapETHtoAPEonUniswap(uint amount) public { uniswapContract.swapETHForExactTokens(amount ether, 10**18, ape, wallet, address(this)); } // Swap all APE to ETH on SushiSwap function swapAPEtoETHonSushiSwap(uint amount) public { sushiswapContract.swap(amount, 10**18, eth, wallet, address(this)); } // Pay back the loan to AAVE function payBackLoanToAAVE() public { // First, check if the wallet has sufficient balance to pay back the loan require(wallet.balance >= aaveContract.borrowBalance(eth, wallet), "Insufficient balance to pay back the loan."); // Pay back the loan aaveContract.repayBorrow(eth, wallet); } // Keep the profit in the wallet function keepProfitInWallet(uint amount) public { // First, check if the contract has sufficient balance to transfer the profit to the wallet require(address(this).balance >= amount, "Insufficient balance in the contract."); // Transfer the profit to the wallet wallet.transfer(amount); }
问题根源
Solidity语法要求所有状态变量、函数、构造函数等必须定义在contract、interface或library代码块内部,不能直接写在全局作用域(pragma和import语句之后直接声明变量/函数)。你的代码中,wallet、aave等变量以及构造函数、各种函数都直接暴露在全局,违反了Solidity的结构规范,导致解析器报错。
修复方案
将所有变量、构造函数和函数包裹在一个contract代码块内,修改后的代码如下:
pragma solidity ^0.6.0; import "https://github.com/aave/aave-solidity/contracts/AAVE.sol"; import "https://github.com/Uniswap/uniswap-v2-core/contracts/interfaces/IUniswapV2Pair.sol"; import "https://github.com/sushiswap/sushiswap-v2-core/contracts/interfaces/ISushiV2Pair.sol"; // 核心修改:添加contract代码块包裹所有内容 contract FlashLoanArbitrage { // Set the wallet address address private wallet = 0x0000000000000000000000000; // Set the contract addresses address private aave = 0x7deB5e830be29F91E298ba5FF1356BB7fC8B8C9D; // AAVE contract address address private uniswap = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f; // Uniswap contract address address private sushiswap = 0x6b3595068778dd592e39a122f4f5a5cf09c90fe2; // SushiSwap contract address // Set the token addresses address private eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // ETH token address address private ape = 0x27Dce1e12396F3a2B49E4FdD7a4C9d938E5e5F97; // APE token address // Set the contract ABIs AAVE aaveContract; IUniswapV2Pair uniswapContract; ISushiV2Pair sushiswapContract; constructor() public { aaveContract = AAVE(aave); uniswapContract = IUniswapV2Pair(uniswap); sushiswapContract = ISushiV2Pair(sushiswap); } // Borrow 100 ETH from AAVE function borrowFromAAVE() public { aaveContract.borrow(eth, 100 ether, wallet); } // Swap 20 ETH to APE on SushiSwap function swapETHtoAPEonSushiSwap(uint amount) public { sushiswapContract.swap(amount ether, 10**18, ape, wallet, address(this)); } // Swap 80 ETH to APE on Uniswap function swapETHtoAPEonUniswap(uint amount) public { uniswapContract.swapETHForExactTokens(amount ether, 10**18, ape, wallet, address(this)); } // Swap all APE to ETH on SushiSwap function swapAPEtoETHonSushiSwap(uint amount) public { sushiswapContract.swap(amount, 10**18, eth, wallet, address(this)); } // Pay back the loan to AAVE function payBackLoanToAAVE() public { // First, check if the wallet has sufficient balance to pay back the loan require(wallet.balance >= aaveContract.borrowBalance(eth, wallet), "Insufficient balance to pay back the loan."); // Pay back the loan aaveContract.repayBorrow(eth, wallet); } // Keep the profit in the wallet function keepProfitInWallet(uint amount) public { // First, check if the contract has sufficient balance to transfer the profit to the wallet require(address(this).balance >= amount, "Insufficient balance in the contract."); // Transfer the profit to the wallet wallet.transfer(amount); } }
额外提示
- 注意
swapETHForExactTokens等函数的参数是否符合对应接口的定义,Uniswap V2的swapETHForExactTokens参数顺序和你的代码可能存在差异,后续测试时需要核对接口文档。 - 闪电贷的核心逻辑需要确保在同一个交易内完成借贷、套利、还款流程,当前代码的函数是分散的,需要整合为单一的闪电贷执行函数,否则无法完成闭环。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

