From 837c29c459b6244903097fbe8c7fcc3810f6ecf7 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Mon, 05 Sep 2022 14:57:13 +0000 Subject: [PATCH] fix: Rename event field name. test: Add eth test for contract sponsor events. --- --- a/pallets/evm-contract-helpers/src/eth.rs +++ b/pallets/evm-contract-helpers/src/eth.rs @@ -42,9 +42,8 @@ ContractSponsorSet { /// Contract address of the affected collection. #[indexed] - contract: address, + contract_address: address, /// New sponsor address. - #[indexed] sponsor: address, }, @@ -52,9 +51,8 @@ ContractSponsorshipConfirmed { /// Contract address of the affected collection. #[indexed] - contract: address, + contract_address: address, /// New sponsor address. - #[indexed] sponsor: address, }, @@ -62,7 +60,7 @@ ContractSponsorRemoved { /// Contract address of the affected collection. #[indexed] - contract: address, + contract_address: address, }, } --- a/pallets/evm-contract-helpers/src/lib.rs +++ b/pallets/evm-contract-helpers/src/lib.rs @@ -208,7 +208,7 @@ )); >::deposit_log( ContractHelpersEvents::ContractSponsorSet { - contract, + contract_address: contract, sponsor: *sponsor.as_eth(), } .to_log(contract), @@ -221,14 +221,14 @@ /// `sender` must be owner of contract. pub fn force_set_sponsor( sender: &T::CrossAccountId, - contract: H160, + contract_address: H160, sponsor: &T::CrossAccountId, ) -> DispatchResult { - Pallet::::ensure_owner(contract, *sender.as_eth())?; + Pallet::::ensure_owner(contract_address, *sender.as_eth())?; Sponsoring::::insert( - contract, + contract_address, SponsorshipState::::Confirmed(T::CrossAccountId::from_eth( - contract, + contract_address, )), ); @@ -236,27 +236,27 @@ let sub_sponsor = sponsor.as_sub().clone(); >::deposit_event(Event::::ContractSponsorSet( - contract, + contract_address, sub_sponsor.clone(), )); >::deposit_log( ContractHelpersEvents::ContractSponsorSet { - contract, + contract_address, sponsor: eth_sponsor, } - .to_log(contract), + .to_log(contract_address), ); >::deposit_event(Event::::ContractSponsorshipConfirmed( - contract, + contract_address, sub_sponsor, )); >::deposit_log( ContractHelpersEvents::ContractSponsorshipConfirmed { - contract, + contract_address, sponsor: eth_sponsor, } - .to_log(contract), + .to_log(contract_address), ); Ok(()) @@ -265,13 +265,13 @@ /// Remove sponsor for `contract`. /// /// `sender` must be owner of contract. - pub fn remove_sponsor(sender: &T::CrossAccountId, contract: H160) -> DispatchResult { - Pallet::::ensure_owner(contract, *sender.as_eth())?; - Sponsoring::::remove(contract); + pub fn remove_sponsor(sender: &T::CrossAccountId, contract_address: H160) -> DispatchResult { + Pallet::::ensure_owner(contract_address, *sender.as_eth())?; + Sponsoring::::remove(contract_address); - >::deposit_event(Event::::ContractSponsorRemoved(contract)); + >::deposit_event(Event::::ContractSponsorRemoved(contract_address)); >::deposit_log( - ContractHelpersEvents::ContractSponsorRemoved { contract }.to_log(contract), + ContractHelpersEvents::ContractSponsorRemoved { contract_address }.to_log(contract_address), ); Ok(()) @@ -280,27 +280,27 @@ /// Confirm sponsorship. /// /// `sender` must be same that set via [`set_sponsor`]. - pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract: H160) -> DispatchResult { - match Sponsoring::::get(contract) { + pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract_address: H160) -> DispatchResult { + match Sponsoring::::get(contract_address) { SponsorshipState::Unconfirmed(sponsor) => { ensure!(sponsor == *sender, Error::::NoPermission); let eth_sponsor = *sponsor.as_eth(); let sub_sponsor = sponsor.as_sub().clone(); Sponsoring::::insert( - contract, + contract_address, SponsorshipState::::Confirmed(sponsor), ); >::deposit_event(Event::::ContractSponsorshipConfirmed( - contract, + contract_address, sub_sponsor, )); >::deposit_log( ContractHelpersEvents::ContractSponsorshipConfirmed { - contract, + contract_address, sponsor: eth_sponsor, } - .to_log(contract), + .to_log(contract_address), ); Ok(()) --- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol +++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol @@ -21,9 +21,19 @@ } } +/// @dev inlined interface +contract ContractHelpersEvents { + event ContractSponsorSet(address indexed contractAddress, address sponsor); + event ContractSponsorshipConfirmed( + address indexed contractAddress, + address sponsor + ); + event ContractSponsorRemoved(address indexed contractAddress); +} + /// @title Magic contract, which allows users to reconfigure other contracts /// @dev the ERC-165 identifier for this interface is 0xd77fab70 -contract ContractHelpers is Dummy, ERC165 { +contract ContractHelpers is Dummy, ERC165, ContractHelpersEvents { /// Get user, which deployed specified contract /// @dev May return zero address in case if contract is deployed /// using uniquenetwork evm-migration pallet, or using other terms not --- a/tests/src/eth/api/ContractHelpers.sol +++ b/tests/src/eth/api/ContractHelpers.sol @@ -12,9 +12,19 @@ function supportsInterface(bytes4 interfaceID) external view returns (bool); } +/// @dev inlined interface +interface ContractHelpersEvents { + event ContractSponsorSet(address indexed contractAddress, address sponsor); + event ContractSponsorshipConfirmed( + address indexed contractAddress, + address sponsor + ); + event ContractSponsorRemoved(address indexed contractAddress); +} + /// @title Magic contract, which allows users to reconfigure other contracts /// @dev the ERC-165 identifier for this interface is 0xd77fab70 -interface ContractHelpers is Dummy, ERC165 { +interface ContractHelpers is Dummy, ERC165, ContractHelpersEvents { /// Get user, which deployed specified contract /// @dev May return zero address in case if contract is deployed /// using uniquenetwork evm-migration pallet, or using other terms not --- a/tests/src/eth/contractSponsoring.test.ts +++ b/tests/src/eth/contractSponsoring.test.ts @@ -24,6 +24,7 @@ SponsoringMode, createEthAccount, ethBalanceViaSub, + normalizeEvents, } from './util/helpers'; describe('Sponsoring EVM contracts', () => { @@ -36,6 +37,33 @@ expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true; }); + itWeb3.only('Set self sponsored events', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const flipper = await deployFlipper(web3, owner); + const helpers = contractHelpers(web3, owner); + + const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send(); + const events = normalizeEvents(result.events); + expect(events).to.be.deep.equal([ + { + address: flipper.options.address, + event: 'ContractSponsorSet', + args: { + contractAddress: flipper.options.address, + sponsor: flipper.options.address, + }, + }, + { + address: flipper.options.address, + event: 'ContractSponsorshipConfirmed', + args: { + contractAddress: flipper.options.address, + sponsor: flipper.options.address, + }, + }, + ]); + }); + itWeb3('Self sponsored can not be set by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => { const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); @@ -75,6 +103,26 @@ expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true; }); + itWeb3('Set sponsor event', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const flipper = await deployFlipper(web3, owner); + const helpers = contractHelpers(web3, owner); + + const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send(); + const events = normalizeEvents(result.events); + expect(events).to.be.deep.equal([ + { + address: flipper.options.address, + event: 'ContractSponsorSet', + args: { + contractAddress: flipper.options.address, + sponsor: sponsor, + }, + }, + ]); + }); + itWeb3('Sponsor can not be set by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => { const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper); @@ -97,6 +145,26 @@ expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true; }); + itWeb3('Confirm sponsorship event', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const flipper = await deployFlipper(web3, owner); + const helpers = contractHelpers(web3, owner); + await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected; + const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor}); + const events = normalizeEvents(result.events); + expect(events).to.be.deep.equal([ + { + address: flipper.options.address, + event: 'ContractSponsorshipConfirmed', + args: { + contractAddress: flipper.options.address, + sponsor: sponsor, + }, + }, + ]); + }); + itWeb3('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({api, web3, privateKeyWrapper}) => { const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper); @@ -160,6 +228,28 @@ expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false; }); + itWeb3('Remove sponsor event', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const flipper = await deployFlipper(web3, owner); + const helpers = contractHelpers(web3, owner); + + await helpers.methods.setSponsor(flipper.options.address, sponsor).send(); + await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor}); + + const result = await helpers.methods.removeSponsor(flipper.options.address).send(); + const events = normalizeEvents(result.events); + expect(events).to.be.deep.equal([ + { + address: flipper.options.address, + event: 'ContractSponsorRemoved', + args: { + contractAddress: flipper.options.address, + }, + }, + ]); + }); + itWeb3('Sponsor can not be removed by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => { const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); --- a/tests/src/eth/util/contractHelpersAbi.json +++ b/tests/src/eth/util/contractHelpersAbi.json @@ -1,5 +1,56 @@ [ { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "contractAddress", + "type": "address" + } + ], + "name": "ContractSponsorRemoved", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "contractAddress", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "sponsor", + "type": "address" + } + ], + "name": "ContractSponsorSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "contractAddress", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "sponsor", + "type": "address" + } + ], + "name": "ContractSponsorshipConfirmed", + "type": "event" + }, + { "inputs": [ { "internalType": "address", -- gitstuff