当前位置:网站首页>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 :

边栏推荐
- Temporarily turn off MySQL cache
- Leetcode interview question 29 clockwise print matrix
- 同花顺开户怎么样安全吗?怎么炒股开户
- Plt How to keep show() not closed
- Handwritten numeral recognition based on tensorflow
- sql中的几种删除操作
- Solve the problem that each letter occupies a space in pycharm
- Leetcode 128 longest continuous sequence
- Insert string B into string A. how many insertion methods can make the new string a palindrome string
- JVM entry Door (1)
猜你喜欢
随机推荐
输入n个整数,输出出现次数大于等于数组长度一半的数
LeetCode 面试题29 顺时针打印矩阵
Static registration and dynamic registration of JNI
Image binarization
为什么我不推荐去SAP培训机构参加培训?
判断某个序列是否为栈的弹出序列
Deep learning: numpy
idea中文插件chinese(simplified) language pack
JVM入個門(1)
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
Temporarily turn off MySQL cache
ros::spinOnce()和ros::spin()的使用和区别
自己创建一个时间拦截器
Boyun, standing at the forefront of China's container industry
How pycharm modifies multiline annotation shortcuts
wm_concat()和group_concat()函数
比较两个对象的大小关系原来可以如此花里胡哨
Numpy之matplotlib
ROS query topic specific content common instructions
必须要掌握的面试重点——索引和事务(附讲B-树与B+树)









