当前位置:网站首页>Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
2022-06-26 18:26:00 【ling1998】
When the contract inherits the sub contract and contains a constructor, an error occurs
error message
When writing a business contract , An error occurs when the inherited contract contains a constructor , Remove the constructor from the inherited contract , Some strange , So write a simple example to reproduce , The contract code is as follows :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Base contract interface
interface IcontractA {
function getMsg() external view returns(string memory);
}
// Base contract implementation
contract contractA is IcontractA {
string message;
// Constructors
constructor(string memory _message) {
message = _message;
}
function _getMsg() internal view returns (string memory) {
return message;
}
function getMsg() external override view returns(string memory) {
return _getMsg();
}
}
// Sub contract - Inheritance base contract
contract contractB is contractA {
address admin;
// Constructors
constructor(string memory _message) {
admin = msg.sender;
}
// Call the base contract function
function callA() external view returns(string memory) {
return _getMsg();
}
}error message
TypeError: Contract "contractB" should be marked as abstract. --> Test/CallContract.sol:28:1: | 28 | contract contractB is contractA { | ^ (Relevant source part starts here and spans across multiple lines). Note: Missing implementation: --> Test/CallContract.sol:14:5: | 14 | constructor(string memory _message) { | ^ (Relevant source part starts here and spans across multiple lines).
Remix Browser execution results :

reason
When the contract is inherited , Derivative contracts ( Sub contract ) You need to provide all the parameters required by the base class constructor , See contract — Solidity develop file
Solution
Add the parameters required by the base contract in the derived contract constructor
32 The adjustment is as follows :
constructor(string memory _message) contractA(_message) {
The revised contract code is as follows :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Base contract interface
interface IcontractA {
function getMsg() external view returns(string memory);
}
// Base contract implementation
contract contractA is IcontractA {
string message;
// Constructors
constructor(string memory _message) {
message = _message;
}
function _getMsg() internal view returns (string memory) {
return message;
}
function getMsg() external override view returns(string memory) {
return _getMsg();
}
}
// Sub contract - Inheritance base contract
contract contractB is contractA {
address admin;
// Constructors
constructor(string memory _message) contractA(_message) {
admin = msg.sender;
}
// Call the base contract function
function callA() external view returns(string memory) {
return _getMsg();
}
}If the base contract constructor has no arguments , You also need to provide the base contract constructor in the derived contract , It's just no reference , Fine tune the above code , The base contract constructor has no parameters
32 The adjustment is as follows :
constructor(string memory _message) contractA() {
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Base contract interface
interface IcontractA {
function getMsg() external view returns(string memory);
}
// Base contract implementation
contract contractA is IcontractA {
string message;
// Constructors
constructor() {
message = "_message";
}
function _getMsg() internal view returns (string memory) {
return message;
}
function getMsg() external override view returns(string memory) {
return _getMsg();
}
}
// Sub contract - Inheritance base contract
contract contractB is contractA {
address admin;
// Constructors
constructor() contractA() {
admin = msg.sender;
}
// Call the base contract function
function callA() external view returns(string memory) {
return _getMsg();
}
}contract B Create another contract in A, Call contract A in view Function time , Out of commission view
When a base contract is created in a sub contract , Call your own query function through the newly created base contract ( Use view Modifier ) when , Functions in a sub contract cannot use view Modifier , The contract code is as follows :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Base contract interface
interface IcontractA {
function getMsg() external view returns(string memory);
}
// Base contract implementation
contract contractA is IcontractA {
string message;
// Constructors
constructor(string memory _message) {
message = _message;
}
function _getMsg() internal view returns (string memory) {
return message;
}
function getMsg() external override view returns (string memory) {
return _getMsg();
}
}
// Sub contract - Inheritance base contract
contract contractB is contractA {
address admin;
// Constructors
constructor(string memory _message) contractA(_message) {
admin = msg.sender;
}
// Call the base contract function
function callA() external view returns (string memory) {
return _getMsg();
}
// Create base contract , Call function
function callANewContract() external view returns (string memory) {
contractA contractAddr = new contractA("Hello, I am tracy");
return contractA(contractAddr).getMsg();
}
}The error message is as follows :
TypeError: Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. --> Test/CallContract.sol:43:34: | 43 | contractA contractAddr = new contractA("Hello, I am tracy"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Remove 42 In a row view Modifier , Deployment contract B, Found calling function callANewContract() It's actually a charge gas Function of fee ( The yellow background is marked as "charge" gas Fee function ), As shown in the figure below :

边栏推荐
- Publish message publishers and subscribe message subscribers of ROS
- Temporarily turn off MySQL cache
- IDEA收藏代码、快速打开favorites收藏窗口
- A little experience of next (ITER (dataloader))
- System table SQLite of SQLite database_ master
- Bayesian network explanation
- Vscode 基础必备 常用插件
- 交叉编译环境出现.so链接文件找不到问题
- Example of using QPushButton style (and method of adding drop-down menu to button SetMenu)
- 爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
猜你喜欢

MySQL download and configuration MySQL remote control

Leetcode 128 longest continuous sequence

Image binarization

Case study of row lock and isolation level

tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板

Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template

How pycharm modifies multiline annotation shortcuts

微信小程序 自定义 弹框组件

Yujun product methodology

The cross compilation environment appears So link file not found problem
随机推荐
How about opening an account at Guojin securities? Is it safe to open an account?
必须要掌握的面试重点——索引和事务(附讲B-树与B+树)
LeetCode 128最长连续序列
ROS query topic specific content common instructions
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
The eigen library calculates the angle between two vectors
Determine whether a sequence is a stack pop-up sequence
Handwritten promise all
Summary of alter operation in SQL
成功解决之idea引用Lombok的@Slf4j后无法正常使用log
ISO文件
VCD-影音光碟
临时关闭MySQL缓存
How pycharm modifies multiline annotation shortcuts
元宇宙链游开发案例版 NFT元宇宙链游系统开发技术分析
PC end records 515 ground sweeping robot /scan data
微信小程序 uniapp 左滑 删除 带删除icon
成功解决之微服务@Value获取配置文件乱码问题
Li Kou daily question - day 28 -566 Reshape matrix
JVM entry Door (1)