difftreelog
Add new storage field in contract V2
in: master
1 file changed
tests/src/eth/proxyContract.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {GAS_ARGS} from './util/helpers';1920import {itEth, expect, usingEthPlaygrounds, EthUniqueHelper} from './util/playgrounds';2122describe('EVM payable contracts', () => {23 let donor: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (_, privateKey) => {27 donor = privateKey('//Alice');28 });29 });3031 itEth('Update proxy contract', async({helper}) => {32 const deployer = await helper.eth.createAccountWithBalance(donor);33 const caller = await helper.eth.createAccountWithBalance(donor);34 const proxyContract = await deployProxyContract(helper, deployer);35 36 const realContractV1 = await deployRealContractV1(helper, deployer);37 const realContractV1proxy = new helper.web3!.eth.Contract(realContractV1.options.jsonInterface, proxyContract.options.address, {from: caller, ...GAS_ARGS});38 await proxyContract.methods.updateVersion(realContractV1.options.address).send();39 40 await realContractV1proxy.methods.flip().send();41 await realContractV1proxy.methods.flip().send();42 await realContractV1proxy.methods.flip().send();43 const value1 = await realContractV1proxy.methods.getValue().call();44 const flipCount1 = await realContractV1proxy.methods.getFlipCount().call();45 expect(flipCount1).to.be.equal('3');46 expect(value1).to.be.equal(true);4748 const realContractV2 = await deployRealContractV2(helper, deployer);49 const realContractV2proxy = new helper.web3!.eth.Contract(realContractV2.options.jsonInterface, proxyContract.options.address, {from: caller, ...GAS_ARGS});50 await proxyContract.methods.updateVersion(realContractV2.options.address).send();5152 await realContractV2proxy.methods.flip().send();53 await realContractV2proxy.methods.flip().send();54 await realContractV2proxy.methods.increaseFlipCount().send();55 const value2 = await realContractV2proxy.methods.getValue().call();56 const flipCount2 = await realContractV2proxy.methods.getFlipCount().call();57 expect(value2).to.be.equal(true);58 expect(flipCount2).to.be.equal('6');59 });6061 async function deployProxyContract(helper: EthUniqueHelper, deployer: string) {62 return await helper.ethContract.deployByCode(deployer, 'ProxyContract', `63 // SPDX-License-Identifier: UNLICENSED64 pragma solidity ^0.8.6;65 66 contract ProxyContract {67 event NewEvent(uint data);68 receive() external payable {}69 bytes32 private constant implementationSlot = bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1);70 constructor() {}71 function updateVersion(address newContractAddress) external {72 bytes32 slot = implementationSlot;73 assembly {74 sstore(slot, newContractAddress)75 }76 }77 fallback() external {78 bytes32 slot = implementationSlot;79 assembly {80 let ptr := mload(0x40)81 let contractAddress := sload(slot)82 83 calldatacopy(ptr, 0, calldatasize())84 85 let result := delegatecall(gas(), contractAddress, ptr, calldatasize(), 0, 0)86 let size := returndatasize()87 88 returndatacopy(ptr, 0, size)89 90 switch result91 case 0 { revert(ptr, size) }92 default { return(ptr, size) }93 }94 }95 }96 97 interface RealContract {98 function flip() external;99 function getValue() external view returns (bool);100 function getFlipCount() external view returns (uint);101 }`);102 }103104 async function deployRealContractV1(helper: EthUniqueHelper, deployer: string) {105 return await helper.ethContract.deployByCode(deployer, 'RealContractV1', `106 // SPDX-License-Identifier: UNLICENSED107 pragma solidity ^0.8.6;108 109 contract RealContractV1 {110 bool value = false;111 uint flipCount = 0;112 function flip() external {113 value = !value;114 flipCount++;115 }116 function getValue() external view returns (bool) {117 return value;118 }119 function getFlipCount() external view returns (uint) {120 return flipCount;121 }122 }`);123 }124125 async function deployRealContractV2(helper: EthUniqueHelper, deployer: string) {126 return await helper.ethContract.deployByCode(deployer, 'RealContractV2', `127 // SPDX-License-Identifier: UNLICENSED128 pragma solidity ^0.8.6;129 130 contract RealContractV2 {131 bool value = false;132 uint flipCount = 10;133 function flip() external {134 value = !value;135 flipCount--;136 }137 function increaseFlipCount() external {138 flipCount = flipCount + 5;139 }140 function getValue() external view returns (bool) {141 return value;142 }143 function getFlipCount() external view returns (uint) {144 return flipCount;145 }146 }`);147 }148});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {GAS_ARGS} from './util/helpers';1920import {itEth, expect, usingEthPlaygrounds, EthUniqueHelper} from './util/playgrounds';2122describe('EVM payable contracts', () => {23 let donor: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (_, privateKey) => {27 donor = privateKey('//Alice');28 });29 });3031 itEth('Update proxy contract', async({helper}) => {32 const deployer = await helper.eth.createAccountWithBalance(donor);33 const caller = await helper.eth.createAccountWithBalance(donor);34 const proxyContract = await deployProxyContract(helper, deployer);35 36 const realContractV1 = await deployRealContractV1(helper, deployer);37 const realContractV1proxy = new helper.web3!.eth.Contract(realContractV1.options.jsonInterface, proxyContract.options.address, {from: caller, ...GAS_ARGS});38 await proxyContract.methods.updateVersion(realContractV1.options.address).send();39 40 await realContractV1proxy.methods.flip().send();41 await realContractV1proxy.methods.flip().send();42 await realContractV1proxy.methods.flip().send();43 const value1 = await realContractV1proxy.methods.getValue().call();44 const flipCount1 = await realContractV1proxy.methods.getFlipCount().call();45 expect(flipCount1).to.be.equal('3');46 expect(value1).to.be.equal(true);4748 const realContractV2 = await deployRealContractV2(helper, deployer);49 const realContractV2proxy = new helper.web3!.eth.Contract(realContractV2.options.jsonInterface, proxyContract.options.address, {from: caller, ...GAS_ARGS});50 await proxyContract.methods.updateVersion(realContractV2.options.address).send();5152 await realContractV2proxy.methods.flip().send();53 await realContractV2proxy.methods.flip().send();54 await realContractV2proxy.methods.setStep(5).send();55 await realContractV2proxy.methods.increaseFlipCount().send();56 const value2 = await realContractV2proxy.methods.getValue().call();57 const flipCount2 = await realContractV2proxy.methods.getFlipCount().call();58 expect(value2).to.be.equal(true);59 expect(flipCount2).to.be.equal('6');60 });6162 async function deployProxyContract(helper: EthUniqueHelper, deployer: string) {63 return await helper.ethContract.deployByCode(deployer, 'ProxyContract', `64 // SPDX-License-Identifier: UNLICENSED65 pragma solidity ^0.8.6;66 67 contract ProxyContract {68 event NewEvent(uint data);69 receive() external payable {}70 bytes32 private constant implementationSlot = bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1);71 constructor() {}72 function updateVersion(address newContractAddress) external {73 bytes32 slot = implementationSlot;74 assembly {75 sstore(slot, newContractAddress)76 }77 }78 fallback() external {79 bytes32 slot = implementationSlot;80 assembly {81 let ptr := mload(0x40)82 let contractAddress := sload(slot)83 84 calldatacopy(ptr, 0, calldatasize())85 86 let result := delegatecall(gas(), contractAddress, ptr, calldatasize(), 0, 0)87 let size := returndatasize()88 89 returndatacopy(ptr, 0, size)90 91 switch result92 case 0 { revert(ptr, size) }93 default { return(ptr, size) }94 }95 }96 }97 98 interface RealContract {99 function flip() external;100 function getValue() external view returns (bool);101 function getFlipCount() external view returns (uint);102 }`);103 }104105 async function deployRealContractV1(helper: EthUniqueHelper, deployer: string) {106 return await helper.ethContract.deployByCode(deployer, 'RealContractV1', `107 // SPDX-License-Identifier: UNLICENSED108 pragma solidity ^0.8.6;109 110 contract RealContractV1 {111 bool value = false;112 uint flipCount = 0;113 function flip() external {114 value = !value;115 flipCount++;116 }117 function getValue() external view returns (bool) {118 return value;119 }120 function getFlipCount() external view returns (uint) {121 return flipCount;122 }123 }`);124 }125126 async function deployRealContractV2(helper: EthUniqueHelper, deployer: string) {127 return await helper.ethContract.deployByCode(deployer, 'RealContractV2', `128 // SPDX-License-Identifier: UNLICENSED129 pragma solidity ^0.8.6;130 131 contract RealContractV2 {132 bool value = false;133 uint flipCount = 10;134 uint step = 1;135 function flip() external {136 value = !value;137 flipCount--;138 }139 function setStep(uint value) external {140 step = value;141 }142 function increaseFlipCount() external {143 flipCount = flipCount + step;144 }145 function getValue() external view returns (bool) {146 return value;147 }148 function getFlipCount() external view returns (uint) {149 return flipCount;150 }151 }`);152 }153});