Skip to content

Instantly share code, notes, and snippets.

@niksmac
Forked from fabdarice/Donation.sol
Created June 17, 2017 06:41
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 niksmac/fd195f5e96d9badfd5e6d79a3024e858 to your computer and use it in GitHub Desktop.
Save niksmac/fd195f5e96d9badfd5e6d79a3024e858 to your computer and use it in GitHub Desktop.
Relay contract that contains only one state variable 'user_amounts' - Link first to Donation contract, then update to DonationNew to add the cancelDonation function.
// First, deploying Relay, then deploying Donation and retrieve Donation contract address in 'donation_contract_address'
// Then, linking Relay to my first version of my contract Donation
Relay.deployed().then(function(contractInstance) {
contractInstance.changeContract(donation_contract_address);
})
// Then, I want to call sendDonation from the Donation contract
// !!!!! I DON'T KNOW WHAT IS THE CORRECT WAY TO CALL THIS !!!!!!
Relay.deployed().then(function(contractInstance) {
contractInstance.sendDonation(5) ;
})
// OR
Relay.deployed().then(function(contractInstance) {
contractInstance.currentVersion.delegateCall(sendDonation(5)) ;
})
// Now I want to update the Donation contract to add the cancelDonation function
// First I deploy the new contract DonationNew and retrieve it's address in 'donation_new_contract_address'
Relay.deployed().then(function(contractInstance) {
contractInstance.changeContract(donation_new_contract_address);
})
// are the state variables still available from the old contract to the new one?
// Then if I want to call the new function :
Relay.deployed().then(function(contractInstance) {
contractInstance.cancelDonation() ;
})
contract Donation {
mapping (address => uint) user_amounts;
/* DOES THIS METHODS MODIFY user_amounts of the Relay contract ??? */
function sendDonation(uint n) {
user_amounts[msg.sender] = user_amounts[msg.sender] + n
}
}
contract DonationNew {
mapping (address => uint) user_amounts;
function sendDonation(uint n) {
user_amounts[msg.sender] = user_amounts[msg.sender] + n
}
function cancelDonation() {
user_amounts[msg.sender] = 0
}
}
contract Relay {
address public currentVersion;
address public owner;
mapping (address => uint) user_amounts;
modifier onlyOwner() {
if (msg.sender != owner) {
throw;
}
_
}
function Relay(address initAddr) {
currentVersion = initAddr;
owner = msg.sender; // this owner may be another contract with multisig, not a single contract owner
}
function changeContract(address newVersion) public
onlyOwner()
{
currentVersion = newVersion;
}
function() {
if(!currentVersion.delegatecall(msg.data)) throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment