diff --git a/contracts/on-chain-claim/Cargo.toml b/contracts/on-chain-claim/Cargo.toml index 6cc99a8d..f066fae3 100644 --- a/contracts/on-chain-claim/Cargo.toml +++ b/contracts/on-chain-claim/Cargo.toml @@ -11,5 +11,8 @@ path = "src/contract.rs" [dependencies.multiversx-sc] version = "0.45.2" +[dependencies.multiversx-sc-modules] +version = "0.45.2" + [dev-dependencies.multiversx-sc-scenario] version = "0.45.2" diff --git a/contracts/on-chain-claim/interaction/devnet.snippets.sh b/contracts/on-chain-claim/interaction/devnet.snippets.sh index 28baf31a..72ae2fba 100644 --- a/contracts/on-chain-claim/interaction/devnet.snippets.sh +++ b/contracts/on-chain-claim/interaction/devnet.snippets.sh @@ -5,6 +5,9 @@ CHAINID=D DEPLOY_GAS="25000000" SFT_IDENTIFIER=0x585354525245504149522d653162363733 #XSTRREPAIR-e1b673 + +CONTRACT_ADDRESS="erd1qqqqqqqqqqqqqpgqkm3wla3wk0yqk7lk725wee8yh0e2zeru76ls3gr0nj" + deploy() { mxpy --verbose contract deploy \ --bytecode="output/on-chain-claim.wasm" \ @@ -26,3 +29,25 @@ deploy() { echo "" echo "Smart contract address: ${ADDRESS}" } + +upgrade() { + mxpy --verbose contract upgrade ${CONTRACT_ADDRESS} \ + --bytecode="output/on-chain-claim.wasm" \ + --arguments ${SFT_IDENTIFIER} \ + --pem=${WALLET} \ + --gas-limit=${DEPLOY_GAS} \ + --proxy=${PROXY} \ + --chain=${CHAINID} \ + --recall-nonce \ + --send \ + --outfile="upgrade-devnet.interaction.json" || return + + TRANSACTION=$(mxpy data parse --file="upgrade-devnet.interaction.json" --expression="data['emittedTransactionHash']") + ADDRESS=$(mxpy data parse --file="upgrade-devnet.interaction.json" --expression="data['contractAddress']") + + mxpy data store --key=address-devnet --value=${ADDRESS} + mxpy data store --key=deployTransaction-devnet --value=${TRANSACTION} + + echo "" + echo "Smart contract address: ${ADDRESS}" +} diff --git a/contracts/on-chain-claim/src/contract.rs b/contracts/on-chain-claim/src/contract.rs index cabb0575..4f2a5c2f 100644 --- a/contracts/on-chain-claim/src/contract.rs +++ b/contracts/on-chain-claim/src/contract.rs @@ -2,7 +2,6 @@ #![allow(unused_attributes)] pub use address_info::AddressInfo; - multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -10,13 +9,20 @@ pub mod address_info; pub mod config; use crate::config::{MAX_REPAIR_GAP, SFT_AMOUNT}; +use multiversx_sc_modules::only_admin; #[multiversx_sc::contract] -pub trait OnChainClaimContract: config::ConfigModule { +pub trait OnChainClaimContract: config::ConfigModule + only_admin::OnlyAdminModule { #[init] fn init(&self, repair_streak_token_id: TokenIdentifier) { + require!( + repair_streak_token_id.is_valid_esdt_identifier(), + "Invalid token ID" + ); self.repair_streak_token_identifier() .set(repair_streak_token_id); + let caller = self.blockchain().get_caller(); + self.add_admin(caller); } #[endpoint(claim)] @@ -106,7 +112,6 @@ pub trait OnChainClaimContract: config::ConfigModule { ); } - #[only_owner] #[endpoint(updateState)] fn update_state( &self, @@ -116,6 +121,8 @@ pub trait OnChainClaimContract: config::ConfigModule { total_epochs_claimed: u64, best_streak: u64, ) { + self.require_caller_is_admin(); + let address_info = AddressInfo::new( current_streak, last_epoch_claimed, diff --git a/contracts/on-chain-claim/tests/tests.rs b/contracts/on-chain-claim/tests/tests.rs index 1670328e..6a6a8efe 100644 --- a/contracts/on-chain-claim/tests/tests.rs +++ b/contracts/on-chain-claim/tests/tests.rs @@ -1,5 +1,6 @@ use config::ConfigModule; use multiversx_sc::types::{ManagedAddress, TokenIdentifier}; +use multiversx_sc_modules::only_admin::OnlyAdminModule; use multiversx_sc_scenario::{scenario_model::*, *}; use on_chain_claim::*; @@ -145,6 +146,16 @@ fn check_update_state() { assert!(address_info_mapper.is_empty()); }) + .whitebox_call( + &on_chain_claim_whitebox, + ScCallStep::new().from(SC_ADDR), + |sc| { + let address = AddressValue::from(OWNER_ADDR).to_address(); + let managed_address = ManagedAddress::from(address); + + sc.add_admin(managed_address); + }, + ) .whitebox_call( &on_chain_claim_whitebox, ScCallStep::new().from(OWNER_ADDR), @@ -482,6 +493,16 @@ fn best_streak() { .set(TokenIdentifier::from(TOKEN_IDENTIFIER)); }, ) + .whitebox_call( + &on_chain_claim_whitebox, + ScCallStep::new().from(SC_ADDR), + |sc| { + let address = AddressValue::from(OWNER_ADDR).to_address(); + let managed_address = ManagedAddress::from(address); + + sc.add_admin(managed_address); + }, + ) .whitebox_query(&on_chain_claim_whitebox, |sc| { let repair_streak_token_identifier = sc.repair_streak_token_identifier().get(); let identifier = TokenIdentifier::from(TOKEN_IDENTIFIER); @@ -603,6 +624,16 @@ fn on_chain_claim_whitebox() { .set(TokenIdentifier::from(TOKEN_IDENTIFIER)); }, ) + .whitebox_call( + &on_chain_claim_whitebox, + ScCallStep::new().from(SC_ADDR), + |sc| { + let address = AddressValue::from(OWNER_ADDR).to_address(); + let managed_address = ManagedAddress::from(address); + + sc.add_admin(managed_address); + }, + ) .whitebox_query(&on_chain_claim_whitebox, |sc| { let repair_streak_token_identifier = sc.repair_streak_token_identifier().get(); let identifier = TokenIdentifier::from(TOKEN_IDENTIFIER); diff --git a/contracts/on-chain-claim/wasm/Cargo.lock b/contracts/on-chain-claim/wasm/Cargo.lock index b8fb9937..1d91944e 100644 --- a/contracts/on-chain-claim/wasm/Cargo.lock +++ b/contracts/on-chain-claim/wasm/Cargo.lock @@ -121,6 +121,15 @@ dependencies = [ "syn", ] +[[package]] +name = "multiversx-sc-modules" +version = "0.45.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5833f8bc88104357d38a8952d2a16c3e66080e2e512c0e7001c0c003006c475" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "multiversx-sc-wasm-adapter" version = "0.45.2" @@ -153,6 +162,7 @@ name = "on-chain-claim" version = "0.0.0" dependencies = [ "multiversx-sc", + "multiversx-sc-modules", ] [[package]] diff --git a/contracts/on-chain-claim/wasm/src/lib.rs b/contracts/on-chain-claim/wasm/src/lib.rs index a0dd1d1a..91ca48a9 100644 --- a/contracts/on-chain-claim/wasm/src/lib.rs +++ b/contracts/on-chain-claim/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 6 +// Endpoints: 10 // Async Callback (empty): 1 -// Total number of exported functions: 8 +// Total number of exported functions: 12 #![no_std] @@ -28,6 +28,10 @@ multiversx_sc_wasm_adapter::endpoints! { canBeRepaired => can_be_repaired getAddressInfo => address_info getRepairStreakTokenIdentifier => repair_streak_token_identifier + isAdmin => is_admin + addAdmin => add_admin + removeAdmin => remove_admin + getAdmins => admins ) }