Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin func proxy migration #1537

Merged
merged 9 commits into from
Apr 9, 2024
100 changes: 50 additions & 50 deletions contracts/core/price-aggregator/scenarios/stress_submit_test.scen.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait ChangeOwnerModule {
let () = self
.send()
.change_owner_address(child_sc_address.clone(), &new_owner)
.execute_on_dest_context();
.sync_call();

self.get_owner_of_vault_contract(child_sc_address)
}
Expand Down
5 changes: 2 additions & 3 deletions contracts/modules/src/claim_developer_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ multiversx_sc::imports!();
pub trait ClaimDeveloperRewardsModule {
#[endpoint(claimDeveloperRewards)]
fn claim_developer_rewards(&self, child_sc_address: ManagedAddress) {
let () = self
.send()
self.send()
.claim_developer_rewards(child_sc_address)
.execute_on_dest_context();
.sync_call();
}
}
5 changes: 3 additions & 2 deletions framework/base/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod blockchain_api;
mod builtin_function_names;
mod call_value_api;
mod composite_api;
mod crypto_api;
Expand All @@ -16,7 +15,6 @@ pub mod uncallable;
mod vm_api;

pub use blockchain_api::*;
pub use builtin_function_names::*;
pub use call_value_api::*;
pub use composite_api::*;
pub use crypto_api::*;
Expand All @@ -30,3 +28,6 @@ pub use print_api::*;
pub use send_api::*;
pub use storage_api::*;
pub use vm_api::VMApi;

// Backwards compatibility.
pub use crate::types::system_proxy::builtin_func_names::*;
160 changes: 50 additions & 110 deletions framework/base/src/contract_base/wrappers/send_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ use core::marker::PhantomData;
use crate::codec::Empty;

use crate::{
api::{
BlockchainApi, BlockchainApiImpl, CallTypeApi, StorageReadApi,
CHANGE_OWNER_BUILTIN_FUNC_NAME, CLAIM_DEVELOPER_REWARDS_FUNC_NAME,
ESDT_LOCAL_BURN_FUNC_NAME, ESDT_LOCAL_MINT_FUNC_NAME, ESDT_NFT_ADD_QUANTITY_FUNC_NAME,
ESDT_NFT_ADD_URI_FUNC_NAME, ESDT_NFT_BURN_FUNC_NAME, ESDT_NFT_CREATE_FUNC_NAME,
ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME,
},
api::{BlockchainApi, CallTypeApi, StorageReadApi},
codec,
types::{
system_proxy, BigUint, ContractCall, ContractCallNoPayment, ESDTSystemSCAddress,
EgldOrEsdtTokenIdentifier, EsdtTokenPayment, GasLeft, ManagedAddress, ManagedArgBuffer,
ManagedBuffer, ManagedType, ManagedVec, ReturnsRawResult, ToSelf, TokenIdentifier, Tx,
TxScEnv,
system_proxy, BigUint, ContractCallNoPayment, ESDTSystemSCAddress,
EgldOrEsdtTokenIdentifier, EsdtTokenPayment, FunctionCall, GasLeft, ManagedAddress,
ManagedArgBuffer, ManagedBuffer, ManagedType, ManagedVec, OriginalResultMarker,
ReturnsRawResult, ReturnsResult, ToSelf, TokenIdentifier, Tx, TxScEnv,
},
};

Expand Down Expand Up @@ -359,25 +353,30 @@ where
}

/// Creates a call to the `ClaimDeveloperRewards` builtin function.
///
/// In itself, this does nothing. You need to then call turn the contract call into an async call.
#[allow(clippy::type_complexity)]
pub fn claim_developer_rewards(
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
&self,
child_sc_address: ManagedAddress<A>,
) -> ContractCallNoPayment<A, ()> {
ContractCallNoPayment::new(child_sc_address, CLAIM_DEVELOPER_REWARDS_FUNC_NAME)
) -> Tx<TxScEnv<A>, (), ManagedAddress<A>, (), (), FunctionCall<A>, OriginalResultMarker<()>>
{
Tx::new_tx_from_sc()
.to(child_sc_address)
.typed(system_proxy::UserBuiltinProxy)
.claim_developer_rewards()
}

/// Creates a call to the `ChangeOwnerAddress` builtin function.
///
/// In itself, this does nothing. You need to then call turn the contract call into an async call.
#[allow(clippy::type_complexity)]
pub fn change_owner_address(
&self,
child_sc_address: ManagedAddress<A>,
new_owner: &ManagedAddress<A>,
) -> ContractCallNoPayment<A, ()> {
self.contract_call(child_sc_address, CHANGE_OWNER_BUILTIN_FUNC_NAME)
.argument(&new_owner)
) -> Tx<TxScEnv<A>, (), ManagedAddress<A>, (), (), FunctionCall<A>, OriginalResultMarker<()>>
{
Tx::new_tx_from_sc()
.to(child_sc_address)
.typed(system_proxy::UserBuiltinProxy)
.change_owner_address(new_owner)
}

/// Allows synchronously calling a local function by name. Execution is resumed afterwards.
Expand All @@ -398,19 +397,6 @@ where
.sync_call()
}

fn call_local_esdt_built_in_function_minimal(
&self,
function_name: &str,
arg_buffer: ManagedArgBuffer<A>,
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.raw_call(function_name)
.arguments_raw(arg_buffer)
.sync_call()
}

/// Allows synchronous minting of ESDT/SFT (depending on nonce). Execution is resumed afterwards.
///
/// Note that the SC must have the ESDTLocalMint or ESDTNftAddQuantity roles set,
Expand All @@ -420,21 +406,12 @@ where
///
/// This function cannot be used for NFTs.
pub fn esdt_local_mint(&self, token: &TokenIdentifier<A>, nonce: u64, amount: &BigUint<A>) {
let mut arg_buffer = ManagedArgBuffer::new();
let func_name: &str;

arg_buffer.push_arg(token);

if nonce == 0 {
func_name = ESDT_LOCAL_MINT_FUNC_NAME;
} else {
func_name = ESDT_NFT_ADD_QUANTITY_FUNC_NAME;
arg_buffer.push_arg(nonce);
}

arg_buffer.push_arg(amount);

self.call_local_esdt_built_in_function_minimal(func_name, arg_buffer);
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_local_mint(token, nonce, amount)
.sync_call()
}

/// Allows synchronous minting of ESDT/SFT (depending on nonce). Execution is resumed afterwards.
Expand Down Expand Up @@ -463,20 +440,12 @@ where
/// Note that the SC must have the ESDTLocalBurn or ESDTNftBurn roles set,
/// or this will fail with "action is not allowed".
pub fn esdt_local_burn(&self, token: &TokenIdentifier<A>, nonce: u64, amount: &BigUint<A>) {
let mut arg_buffer = ManagedArgBuffer::new();
let func_name: &str;

arg_buffer.push_arg(token);
if nonce == 0 {
func_name = ESDT_LOCAL_BURN_FUNC_NAME;
} else {
func_name = ESDT_NFT_BURN_FUNC_NAME;
arg_buffer.push_arg(nonce);
}

arg_buffer.push_arg(amount);

self.call_local_esdt_built_in_function_minimal(func_name, arg_buffer);
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_local_burn(token, nonce, amount)
.sync_call()
}

/// Allows synchronous burning of ESDT/SFT/NFT (depending on nonce). Execution is resumed afterwards.
Expand Down Expand Up @@ -544,36 +513,13 @@ where
attributes: &T,
uris: &ManagedVec<A, ManagedBuffer<A>>,
) -> u64 {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(token);
arg_buffer.push_arg(amount);
arg_buffer.push_arg(name);
arg_buffer.push_arg(royalties);
arg_buffer.push_arg(hash);
arg_buffer.push_arg(attributes);

if uris.is_empty() {
// at least one URI is required, so we push an empty one
arg_buffer.push_arg(codec::Empty);
} else {
// The API function has the last argument as variadic,
// so we top-encode each and send as separate argument
for uri in uris {
arg_buffer.push_arg(uri);
}
}

let output = self.call_local_esdt_built_in_function(
A::blockchain_api_impl().get_gas_left(),
ManagedBuffer::from(ESDT_NFT_CREATE_FUNC_NAME),
arg_buffer,
);

if let Some(first_result_bytes) = output.try_get(0) {
first_result_bytes.parse_as_u64().unwrap_or_default()
} else {
0
}
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_nft_create(token, amount, name, royalties, hash, attributes, uris)
.returns(ReturnsResult)
.sync_call()
}

/// Creates a new NFT token of a certain type (determined by `token_identifier`).
Expand Down Expand Up @@ -778,15 +724,12 @@ where
return;
}

let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(token_id);
arg_buffer.push_arg(nft_nonce);

for uri in new_uris {
arg_buffer.push_arg(uri);
}

self.call_local_esdt_built_in_function_minimal(ESDT_NFT_ADD_URI_FUNC_NAME, arg_buffer);
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.nft_add_multiple_uri(token_id, nft_nonce, new_uris)
.sync_call()
}

/// Changes attributes of an NFT, via a synchronous builtin function call.
Expand All @@ -796,14 +739,11 @@ where
nft_nonce: u64,
new_attributes: &T,
) {
let mut arg_buffer = ManagedArgBuffer::new();
arg_buffer.push_arg(token_id);
arg_buffer.push_arg(nft_nonce);
arg_buffer.push_arg(new_attributes);

self.call_local_esdt_built_in_function_minimal(
ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME,
arg_buffer,
);
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.nft_update_attributes(token_id, nft_nonce, new_attributes)
.sync_call()
}
}
1 change: 1 addition & 0 deletions framework/base/src/types/interaction/system_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod builtin_func_names;
mod builtin_func_proxy;
mod esdt_system_sc_proxy;
mod legacy_system_sc_proxy;
Expand Down
Loading
Loading