Solidity接口可运行示例求助及函数未实现报错排查
Hey there! Let's tackle your two questions one by one, starting with a working Solidity interface example, then fixing that frustrating deployment error.
Interfaces in Solidity act as a blueprint for how contracts should interact with each other—they define function signatures, events, and modifiers without any implementation. Here's a complete, runnable example using a simplified ERC20 token interface:
First, define the interface:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; // Interface defining ERC20 core functions interface IERC20 { function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address recipient, uint256 amount) external returns (bool); event Transfer(address indexed from, address indexed to, uint256 value); }
Next, create a contract that implements this interface:
// Contract that fully implements the IERC20 interface contract MyToken is IERC20 { mapping(address => uint256) private _balances; uint256 private _totalSupply; // Initialize token with an initial supply assigned to the deployer constructor(uint256 initialSupply) { _totalSupply = initialSupply; _balances[msg.sender] = initialSupply; emit Transfer(address(0), msg.sender, initialSupply); } // Implement each interface function with the `override` keyword function totalSupply() external view override returns (uint256) { return _totalSupply; } function balanceOf(address account) external view override returns (uint256) { return _balances[account]; } function transfer(address recipient, uint256 amount) external override returns (bool) { require(_balances[msg.sender] >= amount, "Insufficient balance"); _balances[msg.sender] -= amount; _balances[recipient] += amount; emit Transfer(msg.sender, recipient, amount); return true; } }
And to show how interfaces enable cross-contract interaction, here's a contract that uses the IERC20 interface to query token balances:
// Contract that interacts with any IERC20-compliant token contract TokenBalanceChecker { function getTokenBalance(address tokenAddress, address user) external view returns (uint256) { // Use the interface to call balanceOf without needing the token's full code return IERC20(tokenAddress).balanceOf(user); } }
You can run this in Remix IDE: compile all contracts, deploy MyToken with an initial supply (e.g., 1000), then deploy TokenBalanceChecker and call getTokenBalance with the MyToken address and your wallet address to see the balance.
This error pops up when your contract claims to inherit an interface but fails to implement every single function defined in that interface. Solidity enforces full implementation to ensure the contract adheres to the interface's blueprint.
Example of the Broken Code
Here's a contract that triggers the error because it misses one interface function:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; interface IMyService { function processRequest() external returns (string memory); function getStatus() external view returns (uint256); } // ❌ Will throw the error - missing getStatus() implementation contract BrokenService is IMyService { function processRequest() external override returns (string memory) { return "Request processed"; } // Forgot to implement getStatus()! }
Fixed Version
To fix it, just implement all functions from the interface, making sure to match the function signature (parameters, return type, and modifiers like view/pure) exactly:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; interface IMyService { function processRequest() external returns (string memory); function getStatus() external view returns (uint256); } // ✅ Works perfectly - all interface functions are implemented contract FixedService is IMyService { uint256 private _status = 1; // 1 = ready, 0 = busy function processRequest() external override returns (string memory) { _status = 0; // Simulate processing... _status = 1; return "Request processed successfully"; } function getStatus() external view override returns (uint256) { return _status; } }
Quick Troubleshooting Tips
- Double-check every function in the interface—don't miss any, even if they seem trivial.
- Ensure your implementation matches the interface's function signature exactly (spelling, parameters, return types, and modifiers like
view/payablematter!). - Use the
overridekeyword (required in Solidity 0.8.0+) to explicitly mark functions that implement interface methods. - Note: You don't need to "implement" events from interfaces—just emit them in your contract where appropriate, like we did with the
Transferevent in the ERC20 example.
内容的提问来源于stack exchange,提问作者Marakhi Der

