Last active
August 30, 2022 10:35
-
-
Save shobhitic/04789d8270fabc6ba869a7053b64c323 to your computer and use it in GitHub Desktop.
Call methods of a smart contract from another - https://www.youtube.com/watch?v=1GeejH5RqeQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.16; | |
import "./ContractC.sol"; | |
import "./InterfaceA.sol"; | |
contract ContractA { | |
// call with interface | |
// call with contract | |
// with data | |
// send value | |
function sendMoney(uint256 id, address _contract) payable external returns (address) { | |
InterfaceA ia = InterfaceA(_contract); | |
return ia.sendEth{value: msg.value}(id); | |
} | |
function sendMoneyToC(uint256 id, address _contract) payable external returns (address) { | |
ContractC cc = ContractC(_contract); | |
return cc.sendEth{value: msg.value}(id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.16; | |
import "./InterfaceA.sol"; | |
contract ContractB is InterfaceA { | |
// call with interface | |
// call with contract | |
// with data | |
// send value | |
mapping (uint256 => uint256) donations; | |
mapping (address => uint256) donors; | |
function sendEth(uint256 id) payable external returns (address) { | |
donations[id] += msg.value; | |
donors[msg.sender] += msg.value; | |
return msg.sender; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.16; | |
contract ContractC { | |
function sendEth(uint256 _ignore) payable external returns (address) { | |
return msg.sender; | |
} | |
function withdraw() external { | |
payable(address(this)).transfer(address(this).balance); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.16; | |
interface InterfaceA { | |
function sendEth(uint256 id) payable external returns (address); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment