Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active August 30, 2022 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shobhitic/04789d8270fabc6ba869a7053b64c323 to your computer and use it in GitHub Desktop.
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
// 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);
}
}
// 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;
}
}
// 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);
}
}
// 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