From 164f2ebd2da750c81835e4f0b48253458e5e36cc Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Sat, 8 Jun 2024 16:24:47 +0200 Subject: [PATCH] feat: add transfer_ownership (#1193) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Time spent on this PR: 0.1 day ## Pull request type Please check the type of change your PR introduces: - [ ] Bugfix - [x] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? Ownership of the Kakarot contract can't be transfered. Resolves #1192 ## What is the new behavior? - Add transfer of ownership of the contract. - Add tests. - - - This change is [Reviewable](https://reviewable.io/reviews/kkrt-labs/kakarot/1193) --- src/kakarot/kakarot.cairo | 18 +++++++++++++++++- tests/end_to_end/test_kakarot.py | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/kakarot/kakarot.cairo b/src/kakarot/kakarot.cairo index 6fe35c9fb..d294a80b9 100644 --- a/src/kakarot/kakarot.cairo +++ b/src/kakarot/kakarot.cairo @@ -10,7 +10,7 @@ from starkware.cairo.common.math_cmp import is_not_zero from starkware.cairo.common.uint256 import Uint256 from starkware.starknet.common.syscalls import get_caller_address, replace_class, get_tx_info from starkware.cairo.common.registers import get_fp_and_pc -from openzeppelin.access.ownable.library import Ownable +from openzeppelin.access.ownable.library import Ownable, Ownable_owner // Local dependencies from backend.starknet import Starknet @@ -55,6 +55,22 @@ func upgrade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( return (); } +// @notive Returns the owner of the contract +@external +func get_owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable_owner.read(); +} + +// @notive Transfer the ownership of the contract +// @param new_owner The new owner +@external +func transfer_ownership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_owner: felt +) { + Ownable.transfer_ownership(new_owner); + return (); +} + // @notice Set the native token used by kakarot // @dev Set the native token which will emulate the role of ETH on Ethereum // @param native_token_address The address of the native token diff --git a/tests/end_to_end/test_kakarot.py b/tests/end_to_end/test_kakarot.py index 552199fa4..a9d616762 100644 --- a/tests/end_to_end/test_kakarot.py +++ b/tests/end_to_end/test_kakarot.py @@ -15,6 +15,7 @@ hex_string_to_bytes_array, ) from tests.utils.reporting import traceit +from tests.utils.syscall_handler import SyscallHandler params_execute = [pytest.param(case.pop("params"), **case) for case in test_cases] @@ -281,3 +282,27 @@ async def test_should_upgrade_class_hash( assert prev_class_hash != new_class_hash assert new_class_hash == class_hashes["replace_class"] await invoke("kakarot", "upgrade", prev_class_hash) + + class TestTransferOwnership: + @SyscallHandler.patch("Ownable_owner", 0xDEAD) + async def test_should_raise_when_caller_is_not_owner( + self, kakarot, invoke, other + ): + prev_owner = await kakarot.functions["get_owner"].call() + try: + await invoke("kakarot", "transfer_ownership", account=other) + except Exception as e: + print(e) + new_owner = await kakarot.functions["get_owner"].call() + assert prev_owner == new_owner + + @SyscallHandler.patch("Ownable_owner", SyscallHandler.caller_address) + async def test_should_transfer_ownership(self, kakarot, invoke, other): + prev_owner = (await kakarot.functions["get_owner"].call()).owner + await invoke("kakarot", "transfer_ownership", other.address) + new_owner = (await kakarot.functions["get_owner"].call()).owner + + assert prev_owner != new_owner + assert new_owner == other.address + + await invoke("kakarot", "transfer_ownership", prev_owner, account=other)