Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions contracts/badge-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ pub struct BadgeMinted {
pub minted_at: u64,
}

#[contractevent]
pub struct ContractUpgraded {
#[topic]
pub admin: Address,
pub new_wasm_hash: soroban_sdk::BytesN<32>,
}

// The actual contract struct and implementation are only compiled when building
// the badge-nft wasm itself (default feature). Dependents disable this feature
// to avoid duplicate symbol errors at link time.
#[cfg(feature = "contract")]
mod contract_impl {
use soroban_sdk::{contract, contractimpl, Address, Env, Vec};
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, Vec};

use crate::types::{Badge, DataKey};
use crate::BadgeMinted;
use crate::{BadgeMinted, ContractUpgraded};

#[contract]
pub struct BadgeNFT;
Expand Down Expand Up @@ -127,6 +134,27 @@ mod contract_impl {
}
false
}

/// Upgrades the contract WASM. Only callable by the Protocol Admin.
pub fn upgrade_contract(env: Env, admin: Address, new_wasm_hash: BytesN<32>) {
admin.require_auth();

let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
assert!(admin == stored_admin, "Unauthorized");

env.deployer()
.update_current_contract_wasm(new_wasm_hash.clone());

ContractUpgraded {
admin,
new_wasm_hash,
}
.publish(&env);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions contracts/course-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ pub struct ModuleCompleted {
pub new_progress: u32,
}

#[contractevent]
pub struct ContractUpgraded {
#[topic]
pub admin: Address,
pub new_wasm_hash: BytesN<32>,
}

#[contractimpl]
impl CourseRegistry {
/// Sets the official Protocol Admin. Must be called once upon deployment.
Expand Down Expand Up @@ -321,6 +328,27 @@ impl CourseRegistry {
}
}
}

/// Upgrades the contract WASM. Only callable by the Protocol Admin.
pub fn upgrade_contract(env: Env, admin: Address, new_wasm_hash: BytesN<32>) {
admin.require_auth();

let stored_admin: Address = env
.storage()
.instance()
.get(&DataKey::Admin)
.expect("Not initialized");
assert!(admin == stored_admin, "Unauthorized");

env.deployer()
.update_current_contract_wasm(new_wasm_hash.clone());

ContractUpgraded {
admin,
new_wasm_hash,
}
.publish(&env);
}
}

mod test;
Loading
Loading