Last active
July 11, 2019 20:04
-
-
Save fabdarice/d513d620d9355312d085c7a68e6c6118 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.
This file contains 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
// 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() ; | |
}) |
This file contains 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
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 | |
} | |
} |
This file contains 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
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 | |
} | |
} |
This file contains 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
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; | |
} | |
} |
And also, are the state variables preserved?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
** Which is the right way to do it **