Skip to content

Commit

Permalink
Added only admin module
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbucur1 committed Jan 10, 2024
1 parent bff6b6a commit 73fc249
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
3 changes: 3 additions & 0 deletions contracts/on-chain-claim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
25 changes: 25 additions & 0 deletions contracts/on-chain-claim/interaction/devnet.snippets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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}"
}
13 changes: 10 additions & 3 deletions contracts/on-chain-claim/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
#![allow(unused_attributes)]

pub use address_info::AddressInfo;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

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)]
Expand Down Expand Up @@ -106,7 +112,6 @@ pub trait OnChainClaimContract: config::ConfigModule {
);
}

#[only_owner]
#[endpoint(updateState)]
fn update_state(
&self,
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions contracts/on-chain-claim/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions contracts/on-chain-claim/wasm/Cargo.lock

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

8 changes: 6 additions & 2 deletions contracts/on-chain-claim/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
)
}

Expand Down

0 comments on commit 73fc249

Please sign in to comment.