git.delta.rocks / unique-network / refs/commits / da2e878d93b2

difftreelog

source

tests/src/eth/proxyContract.test.ts5.7 KiBsourcehistory
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';1819import {itEth, expect, usingEthPlaygrounds, EthUniqueHelper} from './util';2021describe('EVM payable contracts', () => {22  let donor: IKeyringPair;2324  before(async function() {25    await usingEthPlaygrounds(async (_, privateKey) => {26      donor = await privateKey({url: import.meta.url});27    });28  });2930  itEth('Update proxy contract', async({helper}) => {31    const deployer = await helper.eth.createAccountWithBalance(donor);32    const caller = await helper.eth.createAccountWithBalance(donor);33    const proxyContract = await deployProxyContract(helper, deployer);3435    const realContractV1 = await deployRealContractV1(helper, deployer);36    const realContractV1proxy = new helper.web3!.eth.Contract(realContractV1.options.jsonInterface, proxyContract.options.address, {from: caller, gas: helper.eth.DEFAULT_GAS});37    await proxyContract.methods.updateVersion(realContractV1.options.address).send();3839    await realContractV1proxy.methods.flip().send();40    await realContractV1proxy.methods.flip().send();41    await realContractV1proxy.methods.flip().send();42    const value1 = await realContractV1proxy.methods.getValue().call();43    const flipCount1 = await realContractV1proxy.methods.getFlipCount().call();44    expect(flipCount1).to.be.equal('3');45    expect(value1).to.be.equal(true);4647    const realContractV2 = await deployRealContractV2(helper, deployer);48    const realContractV2proxy = new helper.web3!.eth.Contract(realContractV2.options.jsonInterface, proxyContract.options.address, {from: caller, gas: helper.eth.DEFAULT_GAS});49    await proxyContract.methods.updateVersion(realContractV2.options.address).send();5051    await realContractV2proxy.methods.flip().send();52    await realContractV2proxy.methods.flip().send();53    await realContractV2proxy.methods.setStep(5).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        uint step = 1;134        function flip() external {135          value = !value;136          flipCount--;137        }138        function setStep(uint value) external {139          step = value;140        }141        function increaseFlipCount() external {142          flipCount = flipCount + step;143        }144        function getValue() external view returns (bool) {145          return value;146        }147        function getFlipCount() external view returns (uint) {148          return flipCount;149        }150      }`);151  }152});