1234567891011121314151617import {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});