-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
World deployments with deterministic addresses
- Loading branch information
Showing
15 changed files
with
488 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use starknet::{ClassHash, SyscallResult, SyscallResultTrait}; | ||
|
||
use dojo::world::IWorldDispatcher; | ||
|
||
#[starknet::interface] | ||
trait IBase<T> { | ||
fn world(self: @T) -> IWorldDispatcher; | ||
} | ||
|
||
#[starknet::contract] | ||
mod base { | ||
use starknet::{ClassHash, get_caller_address}; | ||
|
||
use dojo::upgradable::{IUpgradeable, UpgradeableTrait}; | ||
use dojo::world::IWorldDispatcher; | ||
|
||
#[storage] | ||
struct Storage { | ||
world_dispatcher: IWorldDispatcher, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) { | ||
self.world_dispatcher.write(IWorldDispatcher { contract_address: get_caller_address() }); | ||
} | ||
|
||
#[external(v0)] | ||
fn world(self: @ContractState) -> IWorldDispatcher { | ||
self.world_dispatcher.read() | ||
} | ||
|
||
#[external(v0)] | ||
impl Upgradeable of IUpgradeable<ContractState> { | ||
/// Upgrade contract implementation to new_class_hash | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `new_class_hash` - The new implementation class hahs. | ||
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { | ||
UpgradeableTrait::upgrade(new_class_hash); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use option::OptionTrait; | ||
use starknet::ClassHash; | ||
use traits::TryInto; | ||
|
||
use dojo::base::base; | ||
use dojo::upgradable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait}; | ||
use dojo::test_utils::deploy_contract; | ||
|
||
#[starknet::contract] | ||
mod contract_upgrade { | ||
#[storage] | ||
struct Storage {} | ||
|
||
#[starknet::interface] | ||
trait IQuantumLeap<TState> { | ||
fn plz_more_tps(self: @TState) -> felt252; | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) {} | ||
|
||
#[external(v0)] | ||
impl QuantumLeap of IQuantumLeap<ContractState> { | ||
fn plz_more_tps(self: @ContractState) -> felt252 { | ||
'daddy' | ||
} | ||
} | ||
} | ||
|
||
use contract_upgrade::{IQuantumLeapDispatcher, IQuantumLeapDispatcherTrait}; | ||
|
||
#[test] | ||
#[available_gas(6000000)] | ||
fn test_upgrade() { | ||
let base_address = deploy_contract(base::TEST_CLASS_HASH, array![].span()); | ||
let upgradable_dispatcher = IUpgradeableDispatcher { contract_address: base_address }; | ||
|
||
let new_class_hash: ClassHash = contract_upgrade::TEST_CLASS_HASH.try_into().unwrap(); | ||
upgradable_dispatcher.upgrade(new_class_hash); | ||
|
||
let quantum_dispatcher = IQuantumLeapDispatcher { contract_address: base_address }; | ||
assert(quantum_dispatcher.plz_more_tps() == 'daddy', 'quantum leap failed'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use starknet::{ClassHash, SyscallResult, SyscallResultTrait}; | ||
use zeroable::Zeroable; | ||
use result::ResultTrait; | ||
|
||
#[starknet::interface] | ||
trait IUpgradeable<T> { | ||
fn upgrade(ref self: T, new_class_hash: ClassHash); | ||
} | ||
|
||
trait UpgradeableTrait { | ||
fn upgrade(new_class_hash: ClassHash); | ||
} | ||
|
||
impl UpgradeableTraitImpl of UpgradeableTrait { | ||
fn upgrade(new_class_hash: ClassHash) { | ||
assert(new_class_hash.is_non_zero(), 'class_hash cannot be zero'); | ||
starknet::replace_class_syscall(new_class_hash).unwrap_syscall(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.