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

边栏推荐
- ros::spinOnce()和ros::spin()的使用和区别
- Deep learning: numpy
- 微信小程序 自定义 弹框组件
- Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
- Boyun, standing at the forefront of China's container industry
- Eigen库计算两个向量夹角
- ISO documents
- Do you know how to compare two objects
- 你了解如何比较两个对象吗
- 知識點總結
猜你喜欢

Let torch cuda. is_ Experience of available() changing from false to true

深入理解MySQL锁与事务隔离级别

Solve the problem that each letter occupies a space in pycharm

Numpy's Matplotlib

Decompilation of zero time technology smart contract security series articles

properties文件乱码

idea中文插件chinese(simplified) language pack

Crawl Douban to read top250 and import it into SqList database (or excel table)

RSA encryption and decryption details

LeetCode 128最长连续序列
随机推荐
将字符串B插入字符串A,有多少种插入办法可以使新串是一个回文串
Conditional compilation in precompiling instructions
Comparing the size relationship between two objects turns out to be so fancy
新手炒股开户选哪个证券公司比较好?怎样炒股比较安全??
刻录光盘的程序步骤
Yujun product methodology
GDB installation
Leetcode 238 product of arrays other than itself
Binary search-2
RSA encryption and decryption details
Bayesian network explanation
50行代码爬取Top500图书导入TXT文档
Map and list < map > transfer to corresponding objects
交叉编译环境出现.so链接文件找不到问题
商品秒杀系统
Regular match same character
MySQL的MVCC机制详解
Properties file garbled
Handwritten numeral recognition based on tensorflow
微信小程序 uniapp 左滑 删除 带删除icon