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

边栏推荐
- CD-CompactDisk
- Properties file garbled
- properties文件乱码
- Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
- Using recursion to find all gray codes with n bits
- Solve the problem that each letter occupies a space in pycharm
- CD-CompactDisk
- 交叉编译环境出现.so链接文件找不到问题
- sqlite数据库的系统表sqlite_master
- Handwritten numeral recognition based on tensorflow
猜你喜欢

爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中

Binary search-1

Decompilation of zero time technology smart contract security series articles

Deep understanding of MySQL lock and transaction isolation level

Redis单点登陆系统+投票系统

VCD video disc

限流设计及实现

一些基本错误

行锁分析和死锁

Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
随机推荐
几种常见的UML关系图汇总
深度学习之Numpy篇
Yujun product methodology
Properties file garbled
PC end records 515 ground sweeping robot /scan data
Leetcode 238 product of arrays other than itself
LeetCode 238 除自身以外数组的乘积
DVD-数字通用光盘
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
成功解决之微服务@Value获取配置文件乱码问题
JS common regular expressions
Bayesian network explanation
转:实事求是
How pycharm modifies multiline annotation shortcuts
Interview key points that must be mastered index and affairs (with B-tree and b+ tree)
Paging query and join Association query optimization
Map and list < map > transfer to corresponding objects
ros::spinOnce()和ros::spin()的使用和区别
wm_concat()和group_concat()函数
必须要掌握的面试重点——索引和事务(附讲B-树与B+树)