[英]Figuring out a solidity Interface issue
I got an example of a solidity Interface.我有一个solidity接口的例子。
1/ Any clue if this method is accurate as it's implementing the Interface within the inherited from Contract and not within the extension Contract. 1/ 任何线索,如果此方法是准确的,因为它是在继承自 Contract 而不是在扩展 Contract 内实现接口。
2/ I tried implementing it, contractA functions run correctly, however contractB getCount function is not running correctly. 2/ 我尝试实现它,contractA 函数运行正确,但是 contractB getCount function 运行不正确。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface InterfaceA {
function count() external view returns (uint256);
function increment() external;
}
contract contractA {
uint256 number = 0;
function count() external view returns (uint256) {
return number;
}
function increment() external {
number++ ;
}
}
// SPDX-License-Identifier: MIT
import './contractA.sol' ;
pragma solidity ^0.8.0;
contract contractB {
address addressA;
function setPointer(address _addressA) external {
addressA = _addressA;
}
function getCount() external view returns (uint256) {
InterfaceA b = InterfaceA(addressA);
b.count();
}
function addToIncrement() external {
InterfaceA b = InterfaceA(addressA);
b.increment();
}
}
I do not think that you can get the address of contractA, inside contractB like this:我不认为你可以像这样在contractB内部获得contractA的地址:
address addressA;
you should add a getter function inside contractA to return the address of contractA你应该在contractA里面添加一个getter function来返回contractA的地址
contractA {
function getAddress() public returns(address){
return address(this)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.