1234567891011121314151617import {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Struct} from '@polkadot/types';2021import {IEvent} from '../util/playgrounds/types';22import {ApiPromise} from '@polkadot/api';2324const encodeEvent = (api: ApiPromise, pallet: string, palletEvents: string, event: string, fields: any) => {25 const palletIndex = api.runtimeMetadata.asV14.pallets.find(p => p.name.toString() == pallet)!.index.toNumber();26 const eventMeta = api.events[palletEvents][event].meta;27 const eventIndex = eventMeta.index.toNumber();28 const data = [29 palletIndex, eventIndex,30 ];31 const metaEvent = api.registry.findMetaEvent(new Uint8Array(data));32 data.push(...new Struct(api.registry, {data: metaEvent}, {data: fields}).toU8a());3334 const typeName = api.registry.lookup.names.find(n => n.endsWith('RuntimeEvent'))!;35 return api.registry.createType(typeName, new Uint8Array(data)).toHex();36};3738describe('EVM Migrations', () => {39 let superuser: IKeyringPair;40 let charlie: IKeyringPair;4142 before(async function() {43 await usingEthPlaygrounds(async (_helper, privateKey) => {44 superuser = await privateKey('//Alice');45 charlie = await privateKey('//Charlie');46 });47 });4849 50 itEth('Deploy contract saved state', async ({helper}) => {51 525354555657585960616263646566676869707172 const ADDRESS = '0x4956bf52ef9ed8789f21bc600e915e0d961079f6';73 const CODE = '0x608060405234801561001057600080fd5b506004361061004c5760003560e01c80631ab06ee514610051578063371303c01461006d5780637bfdec3b146100775780639507d39a14610095575b600080fd5b61006b60048036038101906100669190610160565b6100c5565b005b6100756100e1565b005b61007f6100f8565b60405161008c91906101af565b60405180910390f35b6100af60048036038101906100aa9190610133565b610101565b6040516100bc91906101af565b60405180910390f35b8060016000848152602001908152602001600020819055505050565b60016000546100f091906101ca565b600081905550565b60008054905090565b600060016000838152602001908152602001600020549050919050565b60008135905061012d8161025e565b92915050565b60006020828403121561014957610148610259565b5b60006101578482850161011e565b91505092915050565b6000806040838503121561017757610176610259565b5b60006101858582860161011e565b92505060206101968582860161011e565b9150509250929050565b6101a981610220565b82525050565b60006020820190506101c460008301846101a0565b92915050565b60006101d582610220565b91506101e083610220565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156102155761021461022a565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b61026781610220565b811461027257600080fd5b5056fea26469706673582212206a02d2fb5c244105ab884961479c1aee3b4c1011e4b5530ab483eb22344a865664736f6c63430008060033';74 const DATA = [75 76 ['0x0000000000000000000000000000000000000000000000000000000000000000', '0x000000000000000000000000000000000000000000000000000000000000000a'],77 78 ['0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f', '0x0000000000000000000000000000000000000000000000000000000000000001'],79 ['0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f', '0x0000000000000000000000000000000000000000000000000000000000000002'],80 ['0x7dfe757ecd65cbd7922a9c0161e935dd7fdbcc0e999689c7d31633896b1fc60b', '0x0000000000000000000000000000000000000000000000000000000000000003'],81 ['0xedc95719e9a3b28dd8e80877cb5880a9be7de1a13fc8b05e7999683b6b567643', '0x0000000000000000000000000000000000000000000000000000000000000004'],82 ];8384 const caller = await helper.eth.createAccountWithBalance(superuser);8586 const txBegin = helper.constructApiCall('api.tx.evmMigration.begin', [ADDRESS]);87 const txSetData = helper.constructApiCall('api.tx.evmMigration.setData', [ADDRESS, DATA]);88 const txFinish = helper.constructApiCall('api.tx.evmMigration.finish', [ADDRESS, CODE]);89 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txBegin])).to.be.fulfilled;90 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txSetData])).to.be.fulfilled;91 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txFinish])).to.be.fulfilled;9293 const web3 = helper.getWeb3();94 const contract = new web3.eth.Contract([95 {96 inputs: [],97 name: 'counterValue',98 outputs: [{99 internalType: 'uint256',100 name: '',101 type: 'uint256',102 }],103 stateMutability: 'view',104 type: 'function',105 },106 {107 inputs: [{108 internalType: 'uint256',109 name: 'key',110 type: 'uint256',111 }],112 name: 'get',113 outputs: [{114 internalType: 'uint256',115 name: '',116 type: 'uint256',117 }],118 stateMutability: 'view',119 type: 'function',120 },121 ], ADDRESS, {from: caller, gas: helper.eth.DEFAULT_GAS});122123 expect(await contract.methods.counterValue().call()).to.be.equal('10');124 for(let i = 1; i <= 4; i++) {125 expect(await contract.methods.get(i).call()).to.be.equal(i.toString());126 }127 });128 itEth('Fake collection creation on substrate side', async ({helper}) => {129 const txInsertEvents = helper.constructApiCall('api.tx.evmMigration.insertEvents', [[130 encodeEvent(helper.getApi(), 'Common', 'common', 'CollectionCreated', [131 132 9999,133 134 1,135 136 charlie.address,137 ]),138 ]]);139 await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txInsertEvents]);140 const event = helper.chainLog[helper.chainLog.length - 1].events as IEvent[];141 const eventStrings = event.map(e => `${e.section}.${e.method}`);142143 expect(eventStrings).to.contain('common.CollectionCreated');144 });145 itEth('Fake token creation on substrate side', async ({helper}) => {146 const txInsertEvents = helper.constructApiCall('api.tx.evmMigration.insertEvents', [[147 encodeEvent(helper.getApi(), 'Common', 'common', 'ItemCreated', [148 149 9999,150 151 9999,152 153 {Substrate: charlie.address},154 155 1,156 ]),157 ]]);158 await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txInsertEvents]);159 const event = helper.chainLog[helper.chainLog.length - 1].events as IEvent[];160 const eventStrings = event.map(e => `${e.section}.${e.method}`);161162 expect(eventStrings).to.contain('common.ItemCreated');163 });164 itEth('Fake token creation on ethereum side', async ({helper}) => {165 const collection = await helper.nft.mintCollection(superuser);166 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);167 const caller = await helper.eth.createAccountWithBalance(superuser);168 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller);169170 const events: any = [];171 contract.events.allEvents((_: any, event: any) => {172 events.push(event);173 });174175 {176 const txInsertEthLogs = helper.constructApiCall('api.tx.evmMigration.insertEthLogs', [[177 {178 179 address: collectionAddress,180181 topics: [182 183 helper.getWeb3().eth.abi.encodeEventSignature('Transfer(address,address,uint256)'),184 185 helper.getWeb3().eth.abi.encodeParameter('address', '0x' + '00'.repeat(20)),186 helper.getWeb3().eth.abi.encodeParameter('address', caller),187 helper.getWeb3().eth.abi.encodeParameter('uint256', 9999),188 ],189190 191 192 193 data: [],194 },195 ]]);196 await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [txInsertEthLogs]);197 }198199 if(events.length == 0) await helper.wait.newBlocks(1);200 const event = events[0];201202 expect(event.address).to.be.equal(collectionAddress);203 expect(event.returnValues.from).to.be.equal('0x' + '00'.repeat(20));204 expect(event.returnValues.to).to.be.equal(caller);205 expect(event.returnValues.tokenId).to.be.equal('9999');206 });207});