-
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
5 changed files
with
110 additions
and
8 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
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,48 @@ | ||
use starknet::{ClassHash, SyscallResult, SyscallResultTrait}; | ||
use zeroable::Zeroable; | ||
use result::ResultTrait; | ||
use serde::Serde; | ||
use clone::Clone; | ||
use traits::PartialEq; | ||
|
||
#[starknet::interface] | ||
trait IUpgradeable<T> { | ||
fn upgrade(ref self: T, new_class_hash: ClassHash); | ||
} | ||
|
||
#[derive(Clone, Drop, Serde, PartialEq, starknet::Event)] | ||
struct Upgraded { | ||
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(); | ||
} | ||
} | ||
|
||
#[starknet::contract] | ||
mod placeholder { | ||
use starknet::{ClassHash}; | ||
use dojo::upgradable::{IUpgradeable, UpgradeableTrait}; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[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,42 @@ | ||
use option::OptionTrait; | ||
use starknet::ClassHash; | ||
use traits::TryInto; | ||
|
||
use dojo::upgradable::{placeholder, 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 placeholder_address = deploy_contract(placeholder::TEST_CLASS_HASH, array![].span()); | ||
let upgradable_dispatcher = IUpgradeableDispatcher { contract_address: placeholder_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: placeholder_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