Skip to content

Commit

Permalink
esdt-safe: create_transaction add min_bridge_amount
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinCarabas committed Dec 2, 2024
1 parent 6c13007 commit 2f1b424
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 90 deletions.
2 changes: 1 addition & 1 deletion bridge-proxy/src/bridge-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub trait BridgeProxyContract:
self.tx()
.to(esdt_safe_contract_address)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
.create_refund_transaction(
tx.from,
OptionalValue::Some(esdt_safe_proxy::RefundInfo {
address: tx.to,
Expand Down
11 changes: 2 additions & 9 deletions bridged-tokens-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,14 @@ pub trait BridgedTokensWrapper:
requested_token: TokenIdentifier,
safe_address: ManagedAddress<Self::Api>,
to: EthAddress<Self::Api>,
opt_min_bridge_amount: OptionalValue<BigUint<Self::Api>>,
) {
let converted_amount = self.unwrap_token_common(&requested_token);

let caller = self.blockchain().get_caller();
self.tx()
.to(safe_address)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
to,
OptionalValue::Some(esdt_safe_proxy::RefundInfo {
address: caller,
initial_batch_id: 0,
initial_nonce: 0,
}),
)
.create_transaction(to, opt_min_bridge_amount)
.single_esdt(&requested_token, 0, &converted_amount)
.sync_call();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ fn test_unwrap_token_create_transaction_should_fail_case_1() {
managed_token_id!(UNIVERSAL_TOKEN_IDENTIFIER),
ManagedAddress::new_from_bytes(b"0102030405060708090a0b0c0d0e0f10"),
address,
OptionalValue::None,
);
},
|r| r.assert_user_error("Contract is paused"),
Expand Down Expand Up @@ -434,6 +435,7 @@ fn test_unwrap_token_create_transaction_should_fail_case_2() {
managed_token_id!(UNIVERSAL_TOKEN_IDENTIFIER),
ManagedAddress::new_from_bytes(b"0102030405060708090a0b0c0d0e0f10"),
address,
OptionalValue::None,
);
},
|r| r.assert_user_error("Must pay more than 0 tokens!"),
Expand Down Expand Up @@ -472,6 +474,7 @@ fn test_unwrap_token_create_transaction_should_fail_case_3() {
managed_token_id!(UNIVERSAL_TOKEN_IDENTIFIER),
ManagedAddress::zero(),
address,
OptionalValue::None,
);
},
|r| r.assert_user_error("Esdt token unavailable"),
Expand Down Expand Up @@ -546,6 +549,7 @@ fn test_unwrap_token_create_transaction_should_fail_case_4() {
managed_token_id!(UNIVERSAL_TOKEN_IDENTIFIER),
ManagedAddress::zero(),
address,
OptionalValue::None,
);
},
|r| r.assert_user_error("Contract does not have enough funds"),
Expand Down
4 changes: 2 additions & 2 deletions common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub trait MockEsdtSafe {
fn upgrade(&self) {}

#[payable("*")]
#[endpoint(createTransaction)]
fn create_transaction(
#[endpoint(createRefundTransaction)]
fn create_refund_transaction(
&self,
_to: EthAddress<Self::Api>,
_opt_refund_info: OptionalValue<RefundInfo<Self::Api>>,
Expand Down
2 changes: 1 addition & 1 deletion common/mock-contracts/mock-esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ multiversx_sc_wasm_adapter::endpoints! {
(
init => init
upgrade => upgrade
createTransaction => create_transaction
createRefundTransaction => create_refund_transaction
)
}

Expand Down
3 changes: 3 additions & 0 deletions common/sc-proxies/src/bridged_tokens_wrapper_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,20 @@ where
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
Arg2: ProxyArg<eth_address::EthAddress<Env::Api>>,
Arg3: ProxyArg<OptionalValue<BigUint<Env::Api>>>,
>(
self,
requested_token: Arg0,
safe_address: Arg1,
to: Arg2,
opt_min_bridge_amount: Arg3,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("unwrapTokenCreateTransaction")
.argument(&requested_token)
.argument(&safe_address)
.argument(&to)
.argument(&opt_min_bridge_amount)
.original_result()
}

Expand Down
28 changes: 25 additions & 3 deletions common/sc-proxies/src/esdt_safe_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ where
///
/// fee_amount = price_per_gas_unit * eth_tx_gas_limit
pub fn create_transaction<
Arg0: ProxyArg<eth_address::EthAddress<Env::Api>>,
Arg1: ProxyArg<OptionalValue<BigUint<Env::Api>>>,
>(
self,
to: Arg0,
opt_min_bridge_amount: Arg1,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("createTransaction")
.argument(&to)
.argument(&opt_min_bridge_amount)
.original_result()
}

/// Create an Ethereum -> MultiversX refund transaction. Only fungible tokens are accepted.
///
/// Every transfer will have a part of the tokens subtracted as fees.
/// The fee amount depends on the global eth_tx_gas_limit
/// and the current GWEI price, respective to the bridged token
///
/// fee_amount = price_per_gas_unit * eth_tx_gas_limit
pub fn create_refund_transaction<
Arg0: ProxyArg<eth_address::EthAddress<Env::Api>>,
Arg1: ProxyArg<OptionalValue<RefundInfo<Env::Api>>>,
>(
Expand All @@ -147,13 +169,13 @@ where
opt_refund_info: Arg1,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("createTransaction")
.raw_call("createRefundTransaction")
.argument(&to)
.argument(&opt_refund_info)
.original_result()
}

pub fn create_transaction_sc_call<
pub fn create_refund_transaction_sc_call<
Arg0: ProxyArg<eth_address::EthAddress<Env::Api>>,
Arg1: ProxyArg<ManagedBuffer<Env::Api>>,
Arg2: ProxyArg<OptionalValue<RefundInfo<Env::Api>>>,
Expand All @@ -164,7 +186,7 @@ where
opt_refund_info: Arg2,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("createTransactionSCCall")
.raw_call("createRefundTransactionSCCall")
.argument(&to)
.argument(&data)
.argument(&opt_refund_info)
Expand Down
13 changes: 13 additions & 0 deletions common/sc-proxies/src/multisig_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ where
.original_result()
}

pub fn clear_actions_for_batch_id<
Arg0: ProxyArg<u64>,
>(
self,
eth_batch_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("clearActionsForBatchId")
.argument(&eth_batch_id)
.original_result()
}

/// Used by board members to sign actions.
pub fn sign<
Arg0: ProxyArg<usize>,
Expand Down
131 changes: 73 additions & 58 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub trait EsdtSafe:
fn create_transaction_common(
&self,
to: EthAddress<Self::Api>,
min_bridge_amount: BigUint<Self::Api>,
opt_refund_info: OptionalValue<RefundInfo<Self::Api>>,
) -> TransactionDetails<Self::Api> {
require!(self.not_paused(), "Cannot create transaction while paused");
Expand All @@ -266,6 +267,13 @@ pub trait EsdtSafe:
"Transaction fees cost more than the entire bridged amount"
);

if min_bridge_amount != BigUint::zero() {
require!(
required_fee < min_bridge_amount,
"Minimum bridged amount after fee is not achieved"
);
}

self.require_below_max_amount(&payment_token, &payment_amount);

// This addr is used for the refund, if the transaction fails
Expand Down Expand Up @@ -350,76 +358,83 @@ pub trait EsdtSafe:
#[payable("*")]
#[endpoint(createTransaction)]
fn create_transaction(
&self,
to: EthAddress<Self::Api>,
opt_min_bridge_amount: OptionalValue<BigUint<Self::Api>>,
) {
let transaction_details = match opt_min_bridge_amount {
OptionalValue::Some(min_bridge_amount) => {
self.create_transaction_common(to, min_bridge_amount, OptionalValue::None)
}
OptionalValue::None => {
self.create_transaction_common(to, BigUint::zero(), OptionalValue::None)
}
};
// let transaction_details = self.create_transaction_common(to, opt_refund_info);
self.create_transaction_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details
.refund_info
.address
.as_managed_buffer()
.clone(),
transaction_details.to_address,
);
}

/// Create an Ethereum -> MultiversX refund transaction. Only fungible tokens are accepted.
///
/// Every transfer will have a part of the tokens subtracted as fees.
/// The fee amount depends on the global eth_tx_gas_limit
/// and the current GWEI price, respective to the bridged token
///
/// fee_amount = price_per_gas_unit * eth_tx_gas_limit
#[payable("*")]
#[endpoint(createRefundTransaction)]
fn create_refund_transaction(
&self,
to: EthAddress<Self::Api>,
opt_refund_info: OptionalValue<RefundInfo<Self::Api>>,
) {
let transaction_details = self.create_transaction_common(to, opt_refund_info);

if !transaction_details.is_refund_tx {
self.create_transaction_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details
.refund_info
.address
.as_managed_buffer()
.clone(),
transaction_details.to_address,
);
} else {
self.create_refund_transaction_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details.refund_info.initial_batch_id,
transaction_details.refund_info.initial_nonce,
);
}
let transaction_details =
self.create_transaction_common(to, BigUint::zero(), opt_refund_info);

self.create_refund_transaction_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details.refund_info.initial_batch_id,
transaction_details.refund_info.initial_nonce,
);
}

#[payable("*")]
#[endpoint(createTransactionSCCall)]
fn create_transaction_sc_call(
#[endpoint(createRefundTransactionSCCall)]
fn create_refund_transaction_sc_call(
&self,
to: EthAddress<Self::Api>,
data: ManagedBuffer<Self::Api>,
opt_refund_info: OptionalValue<RefundInfo<Self::Api>>,
) {
let transaction_details = self.create_transaction_common(to, opt_refund_info);

if !transaction_details.is_refund_tx {
self.create_transaction_sc_call_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details
.refund_info
.address
.as_managed_buffer()
.clone(),
transaction_details.to_address,
data,
);
} else {
self.create_refund_transaction_sc_call_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details.refund_info.initial_batch_id,
transaction_details.refund_info.initial_nonce,
data,
);
}
let transaction_details =
self.create_transaction_common(to, BigUint::zero(), opt_refund_info);

self.create_refund_transaction_sc_call_event(
transaction_details.batch_id,
transaction_details.tx_nonce,
transaction_details.payment_token,
transaction_details.actual_bridged_amount,
transaction_details.required_fee,
transaction_details.refund_info.initial_batch_id,
transaction_details.refund_info.initial_nonce,
data,
);
}

/// Claim funds for failed MultiversX -> Ethereum transactions.
Expand Down
Loading

0 comments on commit 2f1b424

Please sign in to comment.