Skip to content

Commit

Permalink
Merge pull request #1517 from multiversx/composability-migration
Browse files Browse the repository at this point in the history
migration and new proxies for composability
  • Loading branch information
mihaicalinluca authored Mar 29, 2024
2 parents 30e0665 + 81aae14 commit 3ee9e95
Show file tree
Hide file tree
Showing 19 changed files with 1,152 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ multiversx_sc::imports!();
/// Test contract for investigating async calls.
#[multiversx_sc::contract]
pub trait BuiltinFuncFeatures {
#[proxy]
fn builtin_func_proxy(&self, to: ManagedAddress) -> builtin_func_proxy::Proxy<Self::Api>;

#[init]
fn init(&self) {}

#[endpoint]
fn call_set_user_name(&self, address: ManagedAddress, name: ManagedBuffer) {
self.builtin_func_proxy(address)
.set_user_name(&name)
self.tx()
.to(&address)
.typed(builtin_func_proxy::UserBuiltinProxy)
.set_user_name(name)
.async_call()
.call_and_exit()
}

#[endpoint]
fn call_delete_user_name(&self, address: ManagedAddress) {
self.builtin_func_proxy(address)
self.tx()
.to(&address)
.typed(builtin_func_proxy::UserBuiltinProxy)
.delete_user_name()
.async_call()
.call_and_exit();
.call_and_exit()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
multiversx_sc::imports!();
// Code generated by the multiversx-sc proxy generator. DO NOT EDIT.

#[multiversx_sc::derive::proxy]
pub trait UserBuiltin {
#[endpoint(SetUserName)]
fn set_user_name(&self, name: &ManagedBuffer);
////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

#[endpoint(DeleteUserName)]
fn delete_user_name(&self);
#![allow(dead_code)]
#![allow(clippy::all)]

use multiversx_sc::proxy_imports::*;

pub struct UserBuiltinProxy;

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

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

pub struct UserBuiltinProxyMethods<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> UserBuiltinProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn set_user_name<
Arg0: CodecInto<ManagedBuffer<Env::Api>>,
>(
self,
name: Arg0,
) -> TxProxyCall<Env, From, To, Gas, ()> {
self.wrapped_tx
.raw_call()
.function_name("SetUserName")
.argument(&name)
.original_result()
}

pub fn delete_user_name(
self,
) -> TxProxyCall<Env, From, To, Gas, ()> {
self.wrapped_tx
.raw_call()
.function_name("DeleteUserName")
.original_result()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
proxy-paths = ["../parent/src/child_proxy.rs"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Code generated by the multiversx-sc proxy generator. DO NOT EDIT.

////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

#![allow(dead_code)]
#![allow(clippy::all)]

use multiversx_sc::proxy_imports::*;

pub struct ChildProxy;

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

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

pub struct ChildProxyMethods<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, Gas> ChildProxyMethods<Env, From, (), Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
Gas: TxGas<Env>,
{
pub fn init(
self,
) -> TxProxyDeploy<Env, From, Gas, ()> {
self.wrapped_tx
.raw_deploy()
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> ChildProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
}

#[rustfmt::skip]
impl<Env, From, To, Gas> ChildProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn issue_wrapped_egld<
Arg0: CodecInto<ManagedBuffer<Env::Api>>,
Arg1: CodecInto<ManagedBuffer<Env::Api>>,
Arg2: CodecInto<BigUint<Env::Api>>,
>(
self,
token_display_name: Arg0,
token_ticker: Arg1,
initial_supply: Arg2,
) -> TxProxyCall<Env, From, To, Gas, ()> {
self.wrapped_tx
.raw_call()
.function_name("issueWrappedEgld")
.argument(&token_display_name)
.argument(&token_ticker)
.argument(&initial_supply)
.original_result()
}

pub fn wrapped_egld_token_identifier(
self,
) -> TxProxyCall<Env, From, To, Gas, TokenIdentifier<Env::Api>> {
self.wrapped_tx
.raw_call()
.function_name("getWrappedEgldTokenIdentifier")
.original_result()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

multiversx_sc::imports!();

pub mod child_proxy;

// Base cost for standalone + estimate cost of actual sc call
const ISSUE_EXPECTED_GAS_COST: u64 = 90_000_000 + 25_000_000;

#[multiversx_sc::contract]
pub trait Parent {
#[proxy]
fn child_proxy(&self, to: ManagedAddress) -> child::Proxy<Self::Api>;

#[init]
fn init(&self) {}

Expand All @@ -19,13 +18,14 @@ pub trait Parent {

#[endpoint(deployChildContract)]
fn deploy_child_contract(&self, code: ManagedBuffer) {
let (child_contract_address, _) = self.send_raw().deploy_contract(
self.blockchain().get_gas_left(),
&BigUint::zero(),
&code,
CodeMetadata::DEFAULT,
&ManagedArgBuffer::new(),
);
let gas_left = self.blockchain().get_gas_left();
let child_contract_address = self
.tx()
.raw_deploy()
.code(code)
.with_gas_limit(gas_left)
.returns(ReturnsNewAddress)
.sync_call();

self.child_contract_address().set(&child_contract_address);
}
Expand All @@ -40,12 +40,14 @@ pub trait Parent {
) {
let issue_cost = self.call_value().egld_value();
let child_contract_adress = self.child_contract_address().get();
let _: IgnoreValue = self
.child_proxy(child_contract_adress)

self.tx()
.to(&child_contract_adress)
.typed(child_proxy::ChildProxy)
.issue_wrapped_egld(token_display_name, token_ticker, initial_supply)
.with_egld_transfer(issue_cost.clone_value())
.egld(issue_cost)
.with_gas_limit(ISSUE_EXPECTED_GAS_COST)
.execute_on_dest_context();
.sync_call();
}

// storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ multiversx_sc::imports!();
/// Test contract for investigating the new async call framework.
#[multiversx_sc::module]
pub trait CallPromisesDirectModule {
#[proxy]
fn vault_proxy(&self) -> vault::Proxy<Self::Api>;

#[endpoint]
#[payable("*")]
fn promise_raw_single_token(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
multiversx_sc::imports!();

use crate::common::{self, CallbackData};
use crate::{
common::{self, CallbackData},
vault_proxy,
};

#[multiversx_sc::module]
pub trait CallPromisesModule: common::CommonModule {
#[proxy]
fn vault_proxy(&self) -> vault::Proxy<Self::Api>;

#[endpoint]
#[payable("*")]
fn forward_promise_accept_funds(&self, to: ManagedAddress) {
let payment = self.call_value().egld_or_single_esdt();
let gas_limit = self.blockchain().get_gas_left() / 2;
self.vault_proxy()
.contract(to)

self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer(payment)
.with_gas_limit(gas_limit)
.async_call_promise()
.register_promise()
.with_egld_or_single_esdt_transfer(payment)
.register_promise();
}

#[endpoint]
Expand All @@ -30,14 +31,15 @@ pub trait CallPromisesModule: common::CommonModule {
amount: BigUint,
) {
let gas_limit = self.blockchain().get_gas_left() - 20_000_000;
self.vault_proxy()
.contract(to)

self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.retrieve_funds(token, token_nonce, amount)
.with_gas_limit(gas_limit)
.async_call_promise()
.with_callback(self.callbacks().retrieve_funds_callback())
.with_extra_gas_for_callback(10_000_000)
.register_promise()
.register_promise();
}

#[promises_callback]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::common::{self, CallbackData};

use crate::{
common::{self, CallbackData},
vault_proxy,
};
#[multiversx_sc::module]
pub trait CallPromisesBackTransfersModule: common::CommonModule {
#[proxy]
fn vault_proxy(&self) -> vault::Proxy<Self::Api>;

#[endpoint]
fn forward_promise_retrieve_funds_back_transfers(
&self,
Expand All @@ -17,14 +16,15 @@ pub trait CallPromisesBackTransfersModule: common::CommonModule {
amount: BigUint,
) {
let gas_limit = self.blockchain().get_gas_left() - 20_000_000;
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.retrieve_funds(token, token_nonce, amount)
.with_gas_limit(gas_limit)
.async_call_promise()
.async_call()
.with_callback(self.callbacks().retrieve_funds_back_transfers_callback())
.with_extra_gas_for_callback(10_000_000)
.register_promise()
.register_promise();
}

#[promises_callback]
Expand Down
Loading

0 comments on commit 3ee9e95

Please sign in to comment.