Skip to content

Commit

Permalink
test for upgradeable contracts (NethermindEth#203)
Browse files Browse the repository at this point in the history
* added erc20 unit test

* added test against custom errors

* formated

* made requested changes and moved test to contract module

* fixed zero address

* chore: Remove unused test files

* test for upgradeable contracts

* fixed build issue

* fix: pr#203

---------

Co-authored-by: julio4 <[email protected]>
  • Loading branch information
the-first-elder and julio4 committed Oct 24, 2024
1 parent dd4eb66 commit f617fee
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion listings/applications/constant_product_amm/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod contracts;
pub mod contracts;

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion listings/applications/erc20/src/token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
fn test_deploy_when_recipient_is_address_zero() {
let recipient: ContractAddress = Zero::zero();

let (_contract_address, _) = deploy_syscall(
let (contract_address, _) = deploy_syscall(
erc20::TEST_CLASS_HASH.try_into().unwrap(),
recipient.into(),
array![recipient.into(), token_name, decimals.into(), initial_supply, symbols].span(),
Expand Down
86 changes: 85 additions & 1 deletion listings/applications/upgradeable_contract/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
mod tests { // TODO
mod tests {
use starknet::class_hash::ClassHash;

use super::super::upgradeable_contract_v0::{
UpgradeableContract_V0, IUpgradeableContractDispatcher as IUpgradeableContractDispatcher_v0,
IUpgradeableContractDispatcherTrait as UpgradeableContractDispatcherTrait_v0,
UpgradeableContract_V0::{Event, Upgraded}
};

use super::super::upgradeable_contract_v1::{
UpgradeableContract_V1, IUpgradeableContractDispatcher as IUpgradeableContractDispatcher_v1,
IUpgradeableContractDispatcherTrait as UpgradeableContractDispatcherTrait_v1
};


use starknet::{
ContractAddress, SyscallResultTrait, syscalls::deploy_syscall, get_caller_address,
contract_address_const
};
use core::num::traits::Zero;

use starknet::testing::{set_contract_address, set_account_contract_address};

// deploy v0 contract
fn deploy_v0() -> (IUpgradeableContractDispatcher_v0, ContractAddress, ClassHash) {
let (contract_address, _) = deploy_syscall(
UpgradeableContract_V0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
(
IUpgradeableContractDispatcher_v0 { contract_address },
contract_address,
UpgradeableContract_V0::TEST_CLASS_HASH.try_into().unwrap()
)
}

// deploy v1 contract
fn deploy_v1() -> (IUpgradeableContractDispatcher_v1, ContractAddress, ClassHash) {
let (contract_address, _) = deploy_syscall(
UpgradeableContract_V1::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
(
IUpgradeableContractDispatcher_v1 { contract_address },
contract_address,
UpgradeableContract_V1::TEST_CLASS_HASH.try_into().unwrap()
)
}

#[test]
fn test_deploy_v0() {
deploy_v0();
}

#[test]
fn test_deploy_v1() {
deploy_v1();
}

#[test]
fn test_version_from_v0() {
let (dispatcher, _, _) = deploy_v0();
assert(dispatcher.version() == 0, 'incorrect version');
}

#[test]
#[should_panic(expected: ('Class hash cannot be zero', 'ENTRYPOINT_FAILED'))]
fn test_upgrade_when_classhash_is_zero() {
let (dispatcher_v0, _, _) = deploy_v0();
dispatcher_v0.upgrade(Zero::zero());
}

#[test]
fn test_succesful_upgrade_from_v0_to_v1() {
let (dispatcher_v0, contract_address, _) = deploy_v0();
let (_, _, class_hash) = deploy_v1();
dispatcher_v0.upgrade(class_hash);
// emit event
assert_eq!(
starknet::testing::pop_log(contract_address),
Option::Some(Event::Upgraded(Upgraded { implementation: class_hash }))
);

assert(dispatcher_v0.version() == 1, ' version did not upgrade');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub mod UpgradeableContract_V0 {


#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Upgraded: Upgraded
#[derive(Copy, Drop, Debug, PartialEq, starknet::Event)]
pub enum Event {
Upgraded: Upgraded,
}

#[derive(Drop, starknet::Event)]
struct Upgraded {
implementation: ClassHash
#[derive(Copy, Drop, Debug, PartialEq, starknet::Event)]
pub struct Upgraded {
pub implementation: ClassHash
}

#[abi(embed_v0)]
Expand Down

0 comments on commit f617fee

Please sign in to comment.