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

边栏推荐
猜你喜欢

Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)

Get and set settings in 26class

Numpy之matplotlib

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

Plt How to keep show() not closed

Chinese (Simplified) language pack

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

Yujun product methodology

idea中文插件chinese(simplified) language pack

Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
随机推荐
ROS的发布消息Publishers和订阅消息Subscribers
你了解如何比较两个对象吗
Crawl Douban to read top250 and import it into SqList database (or excel table)
Vscode 基础必备 常用插件
In and exceptions, count (*) query optimization
How to create and enforce indexes
VCD video disc
自己创建一个时间拦截器
Plt How to keep show() not closed
Deep understanding of MySQL lock and transaction isolation level
Using recursion to find all gray codes with n bits
Numpy's Matplotlib
Row lock analysis and deadlock
Leetcode 238 product of arrays other than itself
sql中的几种删除操作
Paging query and join Association query optimization
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
MySQL download and configuration MySQL remote control
利用递归实现求n位所有格雷码
Conditional compilation in precompiling instructions