Skip to content

Commit

Permalink
Merge pull request #1515 from multiversx/proxy-pause-sc
Browse files Browse the repository at this point in the history
proxy pause sc migration to new proxy and unified syntax
  • Loading branch information
mihaicalinluca authored Mar 29, 2024
2 parents d02b4a4 + e3f758a commit 30e0665
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
55 changes: 55 additions & 0 deletions contracts/examples/proxy-pause/src/pause_sc_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use multiversx_sc::proxy_imports::*;

pub struct PausableProxy;

impl<Env, From, To, Gas> TxProxyTrait<Env, From, To, Gas> for PausableProxy
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
type TxProxyMethods = PausableProxyMethods<Env, From, To, Gas>;

fn proxy_methods(self, tx: Tx<Env, From, To, (), Gas, (), ()>) -> Self::TxProxyMethods {
PausableProxyMethods { wrapped_tx: tx }
}
}

pub struct PausableProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
wrapped_tx: Tx<Env, From, To, (), Gas, (), ()>,
}

#[rustfmt::skip]
impl<Env, From, To, Gas> PausableProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn pause(
self,
) -> TxProxyCall<Env, From, To, Gas, ()> {
self.wrapped_tx
.raw_call()
.function_name("pause")
.original_result()
}

pub fn unpause(
self,
) -> TxProxyCall<Env, From, To, Gas, ()> {
self.wrapped_tx
.raw_call()
.function_name("unpause")
.original_result()
}
}
26 changes: 8 additions & 18 deletions contracts/examples/proxy-pause/src/proxy_pause.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
#![no_std]

use multiversx_sc::imports::*;

mod pause_proxy {
#[multiversx_sc::proxy]
pub trait Pausable {
#[endpoint]
fn pause(&self);

#[endpoint]
fn unpause(&self);
}
}
pub mod pause_sc_proxy;

#[multiversx_sc::contract]
pub trait PauseProxy {
Expand Down Expand Up @@ -46,23 +36,26 @@ pub trait PauseProxy {

fn for_each_contract<F>(&self, f: F)
where
F: Fn(pause_proxy::ProxyTo<Self::Api>),
F: Fn(pause_sc_proxy::PausableProxyMethods<TxScEnv<Self::Api>, (), &ManagedAddress, ()>),
{
for contract_address in self.contracts().iter() {
f(self.pausable_contract().contract(contract_address));
f(self
.tx()
.to(&contract_address)
.typed(pause_sc_proxy::PausableProxy));
}
}

#[endpoint]
fn pause(&self) {
self.require_owner();
self.for_each_contract(|mut contract| contract.pause().execute_on_dest_context());
self.for_each_contract(|contract| contract.pause().sync_call());
}

#[endpoint]
fn unpause(&self) {
self.require_owner();
self.for_each_contract(|mut contract| contract.unpause().execute_on_dest_context());
self.for_each_contract(|contract| contract.unpause().sync_call());
}

fn require_owner(&self) {
Expand All @@ -79,7 +72,4 @@ pub trait PauseProxy {
#[view]
#[storage_mapper("contracts")]
fn contracts(&self) -> SetMapper<ManagedAddress>;

#[proxy]
fn pausable_contract(&self) -> pause_proxy::Proxy<Self::Api>;
}

0 comments on commit 30e0665

Please sign in to comment.