Skip to content

Commit 4a869b9

Browse files
volivagithub-actions[bot]athei
authored
feat(revive): add contract instantiated event (#8789)
# Description This PR adds the `Instantiated` event for pallet-revive for the top frame. Addresses issue #8677 This might need refreshing the weights of bot `instantiate` and `instantiate_with_code`, as it emits a new event. ## Integration No additional work is needed to integrate this feature. The pallet will emit on `Instantiated` event every time `instantiate` or `instantiate_with_code` successfully performs an instantiation. # Checklist * [x] My PR includes a detailed description as outlined in the "Description" and its two subsections above. * [x] My PR follows the [labeling requirements]( https://github.com/paritytech/polkadot-sdk/blob/master/docs/contributor/CONTRIBUTING.md#Process ) of this project (at minimum one label for `T` required) * External contributors: ask maintainers to put the right label on your PR. * [x] I have made corresponding changes to the documentation (if applicable) * [x] I have added tests that prove my fix is effective or that my feature works (if applicable) --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Alexander Theißen <[email protected]>
1 parent e774263 commit 4a869b9

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

prdoc/pr_8789.prdoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: '[pallet-revive] Add contract instantiated event'
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: ' `instantiate` and `instantiate_with_code` emit a `Instantiated` event.'
9+
10+
crates:
11+
- name: pallet-revive
12+
bump: major

substrate/frame/revive/src/exec.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ where
815815
skip_transfer: bool,
816816
bump_nonce: BumpNonce,
817817
) -> Result<(H160, ExecReturnValue), ExecError> {
818+
let deployer = T::AddressMapper::to_address(&origin);
818819
let (mut stack, executable) = Stack::<'_, T, E>::new(
819820
FrameArgs::Instantiate {
820821
sender: origin.clone(),
@@ -830,9 +831,15 @@ where
830831
)?
831832
.expect(FRAME_ALWAYS_EXISTS_ON_INSTANTIATE);
832833
let address = T::AddressMapper::to_address(&stack.top_frame().account_id);
833-
stack
834+
let result = stack
834835
.run(executable, input_data, bump_nonce)
835-
.map(|_| (address, stack.first_frame.last_frame_output))
836+
.map(|_| (address, stack.first_frame.last_frame_output));
837+
if let Ok((contract, ref output)) = result {
838+
if !output.did_revert() {
839+
Contracts::<T>::deposit_event(Event::Instantiated { deployer, contract });
840+
}
841+
}
842+
result
836843
}
837844

838845
#[cfg(any(feature = "runtime-benchmarks", test))]
@@ -930,7 +937,7 @@ where
930937
if let Some(info) = <ContractInfoOf<T>>::get(&address) {
931938
CachedContract::Cached(info)
932939
} else {
933-
return Ok(None)
940+
return Ok(None);
934941
},
935942
(None, Some(precompile)) if precompile.has_contract_info() => {
936943
if let Some(info) = <ContractInfoOf<T>>::get(&address) {

substrate/frame/revive/src/exec/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,13 @@ fn instantiation_work_with_success_output() {
11451145
ContractInfo::<Test>::load_code_hash(&instantiated_contract_id).unwrap(),
11461146
dummy_ch
11471147
);
1148+
assert_eq!(
1149+
&events(),
1150+
&[Event::Instantiated {
1151+
deployer: ALICE_ADDR,
1152+
contract: instantiated_contract_address
1153+
}]
1154+
);
11481155
});
11491156
}
11501157

substrate/frame/revive/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ pub mod pallet {
361361
/// Number of topics is capped by [`limits::NUM_EVENT_TOPICS`].
362362
topics: Vec<H256>,
363363
},
364+
365+
/// Contract deployed by deployer at the specified address.
366+
Instantiated { deployer: H160, contract: H160 },
364367
}
365368

366369
#[pallet::error]

substrate/frame/revive/src/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@ fn instantiate_and_call_and_deposit_event() {
519519
}),
520520
topics: vec![],
521521
},
522+
EventRecord {
523+
phase: Phase::Initialization,
524+
event: RuntimeEvent::Contracts(crate::Event::Instantiated {
525+
deployer: ALICE_ADDR,
526+
contract: addr
527+
}),
528+
topics: vec![],
529+
},
522530
]
523531
);
524532
});
@@ -2220,6 +2228,14 @@ fn instantiate_with_zero_balance_works() {
22202228
}),
22212229
topics: vec![],
22222230
},
2231+
EventRecord {
2232+
phase: Phase::Initialization,
2233+
event: RuntimeEvent::Contracts(crate::Event::Instantiated {
2234+
deployer: ALICE_ADDR,
2235+
contract: addr,
2236+
}),
2237+
topics: vec![],
2238+
},
22232239
]
22242240
);
22252241
});
@@ -2286,6 +2302,14 @@ fn instantiate_with_below_existential_deposit_works() {
22862302
}),
22872303
topics: vec![],
22882304
},
2305+
EventRecord {
2306+
phase: Phase::Initialization,
2307+
event: RuntimeEvent::Contracts(crate::Event::Instantiated {
2308+
deployer: ALICE_ADDR,
2309+
contract: addr,
2310+
}),
2311+
topics: vec![],
2312+
},
22892313
]
22902314
);
22912315
});

0 commit comments

Comments
 (0)