Skip to content

Commit

Permalink
feat: add transfer_ownership (#1193)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR: 0.1 day

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

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.
<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #1192 

## What is the new behavior?
- Add transfer of ownership of the contract.
- Add tests.

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1193)
<!-- Reviewable:end -->
  • Loading branch information
greged93 committed Jun 10, 2024
1 parent 5660664 commit 164f2eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/kakarot/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/end_to_end/test_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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)

0 comments on commit 164f2eb

Please sign in to comment.