'How to pass struct as argument from one contract to another contract
I can pass array, uint, bool mappings from one contract to another contract. but I can not pass struct as argument,
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;
interface SampleInterface {
struct Point {
uint256 x;
uint256 y;
}
function setPoint(Point memory) external;
function getPoint() external view returns(Point memory);
}
contract Sample is SampleInterface {
Point point;
function setPoint(Point memory _point) external {
point = _point;
}
function getPoint() external view returns(Point memory) {
return point;
}
}
This is my source contract. I have deployed the above contract and put the contract address into the following contract to call setPoint and getPoint,
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;
interface SampleInterface {
struct Point {
uint256 x;
uint256 y;
}
function setPoint(Point memory point) external;
function getPoint() external view returns(Point memory);
}
contract Operator {
address sample;
constructor(address _sample) {
sample = _sample;
}
function setPoint(Point memory point) external {
SampleInterface(sample).setPoint(point);
}
function getPoint() external view returns(Point memory) {
return SampleInterface(sample).getPoint();
}
}
But I am getting the following error,
Compiling your contracts...
===========================
> Compiling ./contracts/Operator.sol
DeclarationError: Identifier not found or not unique.
--> project:/contracts/Operator.sol:18:20:
|
18 | function setPoint(Point memory point) external {
| ^^^^^
Compilation failed. See above.
Truffle v5.5.9 (core: 5.5.9)
Node v12.14.0
Help me to fix this issue. How can I call setPoint and getPoint from Operator contract.
Solution 1:[1]
The Point structure doesn't exist in the scope of your contract, only in the interface that you've declared. To fix this issue, you need to use SampleInterface.Point instead of Point.
function setPoint(SampleInterface.Point memory point) external {
SampleInterface(sample).setPoint(point);
}
function getPoint() external view returns(SampleInterface.Point memory) {
return SampleInterface(sample).getPoint();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | its5Q |
