Skip to content

Instantly share code, notes, and snippets.

@josephnicholas
Created November 30, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephnicholas/4926cb37874b09d704a0b5aed3aaeb01 to your computer and use it in GitHub Desktop.
Save josephnicholas/4926cb37874b09d704a0b5aed3aaeb01 to your computer and use it in GitHub Desktop.
Pet Shop Tutorial with exercise on callers
pragma solidity ^0.5.0;
contract Adoption {
address[16] public adopters;
// Adopting a pet
function adopt(uint petId) public returns(uint) {
require(petId >= 0 && petId <= 15);
require(adopters[petId] != msg.sender, "Pet cannot be adopted by the same owners.");
require(adopters[petId] == address(0), "Pet has owner");
adopters[petId] = msg.sender;
return petId;
}
// Retrieving adopters
function getAdopters() public view returns(address[16] memory) {
return adopters;
}
}
contract AdoptionCaller {
address[16] public adopters;
function baseAdopt(address base, uint petId) public {
Adoption oAdopt = Adoption(base);
oAdopt.adopt(petId);
}
function callBaseAdopt(address base, uint petId) public returns(bool) {
(bool status, bytes memory returnData) = base.call(abi.encodeWithSignature("adopt(uint256)", petId));
return status;
}
function callBaseDelegateAdopt(address base, uint petId) public returns(bool) {
(bool status, bytes memory returnData) = base.delegatecall(abi.encodeWithSignature("adopt(uint256)", petId));
return status;
}
// Retrieving adopters
function localAdopters() public view returns(address[16] memory) {
return adopters;
}
}
// Some other external pet caller
contract ExternalPetCaller {
function externalDelegateAdopt(AdoptionCaller adoptCaller, address base, uint petId) public returns(bool) {
(bool result) = adoptCaller.callBaseDelegateAdopt(base, petId);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment