difftreelog
Merge branch 'feature/app-staking' of https://github.com/UniqueNetwork/unique-chain into feature/app-staking
in: master
11 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5735,6 +5735,7 @@
name = "pallet-evm-contract-helpers"
version = "0.2.0"
dependencies = [
+ "ethereum",
"evm-coder",
"fp-evm-mapping",
"frame-support",
pallets/evm-contract-helpers/Cargo.tomldiffbeforeafterboth--- a/pallets/evm-contract-helpers/Cargo.toml
+++ b/pallets/evm-contract-helpers/Cargo.toml
@@ -9,6 +9,7 @@
"derive",
] }
log = { default-features = false, version = "0.4.14" }
+ethereum = { version = "0.12.0", default-features = false }
# Substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -17,7 +17,9 @@
//! Implementation of magic contract
use core::marker::PhantomData;
-use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};
+use evm_coder::{
+ abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*, ToLog,
+};
use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};
use pallet_evm::{
ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,
@@ -33,6 +35,35 @@
use up_sponsorship::SponsorshipHandler;
use sp_std::vec::Vec;
+/// Pallet events.
+#[derive(ToLog)]
+pub enum ContractHelpersEvents {
+ /// Contract sponsor was set.
+ ContractSponsorSet {
+ /// Contract address of the affected collection.
+ #[indexed]
+ contract_address: address,
+ /// New sponsor address.
+ sponsor: address,
+ },
+
+ /// New sponsor was confirm.
+ ContractSponsorshipConfirmed {
+ /// Contract address of the affected collection.
+ #[indexed]
+ contract_address: address,
+ /// New sponsor address.
+ sponsor: address,
+ },
+
+ /// Collection sponsor was removed.
+ ContractSponsorRemoved {
+ /// Contract address of the affected collection.
+ #[indexed]
+ contract_address: address,
+ },
+}
+
/// See [`ContractHelpersCall`]
pub struct ContractHelpers<T: Config>(SubstrateRecorder<T>);
impl<T: Config> WithRecorder<T> for ContractHelpers<T> {
@@ -46,7 +77,7 @@
}
/// @title Magic contract, which allows users to reconfigure other contracts
-#[solidity_interface(name = ContractHelpers)]
+#[solidity_interface(name = ContractHelpers, events(ContractHelpersEvents))]
impl<T: Config> ContractHelpers<T>
where
T::AccountId: AsRef<[u8; 32]>,
@@ -91,8 +122,12 @@
self.recorder().consume_sload()?;
self.recorder().consume_sstore()?;
- Pallet::<T>::self_sponsored_enable(&T::CrossAccountId::from_eth(caller), contract_address)
- .map_err(dispatch_to_evm::<T>)?;
+ Pallet::<T>::force_set_sponsor(
+ &T::CrossAccountId::from_eth(caller),
+ contract_address,
+ &T::CrossAccountId::from_eth(contract_address),
+ )
+ .map_err(dispatch_to_evm::<T>)?;
Ok(())
}
pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth161617#![doc = include_str!("../README.md")]17#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]18#![cfg_attr(not(feature = "std"), no_std)]19#![deny(missing_docs)]19#![warn(missing_docs)]202021use codec::{Decode, Encode, MaxEncodedLen};21use codec::{Decode, Encode, MaxEncodedLen};22pub use pallet::*;22pub use pallet::*;27#[frame_support::pallet]27#[frame_support::pallet]28pub mod pallet {28pub mod pallet {29 pub use super::*;29 pub use super::*;30 use crate::eth::ContractHelpersEvents;30 use frame_support::pallet_prelude::*;31 use frame_support::pallet_prelude::*;31 use pallet_evm_coder_substrate::DispatchResult;32 use pallet_evm_coder_substrate::DispatchResult;32 use sp_core::H160;33 use sp_core::H160;33 use pallet_evm::account::CrossAccountId;34 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};34 use up_data_structs::SponsorshipState;35 use up_data_structs::SponsorshipState;36 use evm_coder::ToLog;353736 #[pallet::config]38 #[pallet::config]37 pub trait Config:39 pub trait Config:38 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config40 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config39 {41 {42 /// Overarching event type.43 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;4440 /// Address, under which magic contract will be available45 /// Address, under which magic contract will be available41 type ContractAddress: Get<H160>;46 type ContractAddress: Get<H160>;150 QueryKind = ValueQuery,156 QueryKind = ValueQuery,151 >;157 >;158159 #[pallet::event]160 #[pallet::generate_deposit(pub fn deposit_event)]161 pub enum Event<T: Config> {162 /// Contract sponsor was set.163 ContractSponsorSet(164 /// Contract address of the affected collection.165 H160,166 /// New sponsor address.167 T::AccountId,168 ),169170 /// New sponsor was confirm.171 ContractSponsorshipConfirmed(172 /// Contract address of the affected collection.173 H160,174 /// New sponsor address.175 T::AccountId,176 ),177178 /// Collection sponsor was removed.179 ContractSponsorRemoved(180 /// Contract address of the affected collection.181 H160,182 ),183 }152184153 impl<T: Config> Pallet<T> {185 impl<T: Config> Pallet<T> {154 /// Get contract owner.186 /// Get contract owner.170 SponsorshipState::<T::CrossAccountId>::Unconfirmed(sponsor.clone()),202 SponsorshipState::<T::CrossAccountId>::Unconfirmed(sponsor.clone()),171 );203 );204205 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(206 contract,207 sponsor.as_sub().clone(),208 ));209 <PalletEvm<T>>::deposit_log(210 ContractHelpersEvents::ContractSponsorSet {211 contract_address: contract,212 sponsor: *sponsor.as_eth(),213 }214 .to_log(contract),215 );172 Ok(())216 Ok(())173 }217 }174218175 /// Set `contract` as self sponsored.219 /// Set sponsor as already confirmed.176 ///220 ///177 /// `sender` must be owner of contract.221 /// `sender` must be owner of contract.178 pub fn self_sponsored_enable(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {222 pub fn force_set_sponsor(223 sender: &T::CrossAccountId,224 contract_address: H160,225 sponsor: &T::CrossAccountId,226 ) -> DispatchResult {179 Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;227 Pallet::<T>::ensure_owner(contract_address, *sender.as_eth())?;180 Sponsoring::<T>::insert(228 Sponsoring::<T>::insert(181 contract,229 contract_address,182 SponsorshipState::<T::CrossAccountId>::Confirmed(T::CrossAccountId::from_eth(230 SponsorshipState::<T::CrossAccountId>::Confirmed(T::CrossAccountId::from_eth(183 contract,231 contract_address,184 )),232 )),185 );233 );234235 let eth_sponsor = *sponsor.as_eth();236 let sub_sponsor = sponsor.as_sub().clone();237238 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(239 contract_address,240 sub_sponsor.clone(),241 ));242 <PalletEvm<T>>::deposit_log(243 ContractHelpersEvents::ContractSponsorSet {244 contract_address,245 sponsor: eth_sponsor,246 }247 .to_log(contract_address),248 );249250 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(251 contract_address,252 sub_sponsor,253 ));254 <PalletEvm<T>>::deposit_log(255 ContractHelpersEvents::ContractSponsorshipConfirmed {256 contract_address,257 sponsor: eth_sponsor,258 }259 .to_log(contract_address),260 );261186 Ok(())262 Ok(())187 }263 }188264189 /// Remove sponsor for `contract`.265 /// Remove sponsor for `contract`.190 ///266 ///191 /// `sender` must be owner of contract.267 /// `sender` must be owner of contract.192 pub fn remove_sponsor(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {268 pub fn remove_sponsor(sender: &T::CrossAccountId, contract_address: H160) -> DispatchResult {193 Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;269 Pallet::<T>::ensure_owner(contract_address, *sender.as_eth())?;194 Sponsoring::<T>::remove(contract);270 Sponsoring::<T>::remove(contract_address);271272 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorRemoved(contract_address));273 <PalletEvm<T>>::deposit_log(274 ContractHelpersEvents::ContractSponsorRemoved { contract_address }.to_log(contract_address),275 );276195 Ok(())277 Ok(())196 }278 }197279198 /// Confirm sponsorship.280 /// Confirm sponsorship.199 ///281 ///200 /// `sender` must be same that set via [`set_sponsor`].282 /// `sender` must be same that set via [`set_sponsor`].201 pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {283 pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract_address: H160) -> DispatchResult {202 match Sponsoring::<T>::get(contract) {284 match Sponsoring::<T>::get(contract_address) {203 SponsorshipState::Unconfirmed(sponsor) => {285 SponsorshipState::Unconfirmed(sponsor) => {204 ensure!(sponsor == *sender, Error::<T>::NoPermission);286 ensure!(sponsor == *sender, Error::<T>::NoPermission);287 let eth_sponsor = *sponsor.as_eth();288 let sub_sponsor = sponsor.as_sub().clone();205 Sponsoring::<T>::insert(289 Sponsoring::<T>::insert(206 contract,290 contract_address,207 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor),291 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor),208 );292 );293294 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(295 contract_address,296 sub_sponsor,297 ));298 <PalletEvm<T>>::deposit_log(299 ContractHelpersEvents::ContractSponsorshipConfirmed {300 contract_address,301 sponsor: eth_sponsor,302 }303 .to_log(contract_address),304 );305209 Ok(())306 Ok(())210 }307 }pallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterbothbinary blob — no preview
pallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth--- 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
runtime/common/config/ethereum.rsdiffbeforeafterboth--- a/runtime/common/config/ethereum.rs
+++ b/runtime/common/config/ethereum.rs
@@ -112,6 +112,7 @@
}
impl pallet_evm_contract_helpers::Config for Runtime {
+ type Event = Event;
type ContractAddress = HelpersContractAddress;
type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit;
}
runtime/common/construct_runtime/mod.rsdiffbeforeafterboth--- a/runtime/common/construct_runtime/mod.rs
+++ b/runtime/common/construct_runtime/mod.rs
@@ -85,7 +85,7 @@
Ethereum: pallet_ethereum::{Pallet, Config, Call, Storage, Event, Origin} = 101,
EvmCoderSubstrate: pallet_evm_coder_substrate::{Pallet, Storage} = 150,
- EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage} = 151,
+ EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage, Event<T>} = 151,
EvmTransactionPayment: pallet_evm_transaction_payment::{Pallet} = 152,
EvmMigration: pallet_evm_migration::{Pallet, Call, Storage} = 153,
}
tests/src/eth/api/ContractHelpers.soldiffbeforeafterboth--- 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
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth--- 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);
tests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth--- 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",