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

esdt-safe: Fix withdraw total fees on ethereum #221

Merged
merged 6 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions bridge-proxy/src/esdt_safe_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,35 @@ where
.original_result()
}

pub fn withdraw_total_fees_on_ethereum<
pub fn withdraw_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawRefundFeesForEthereum")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

pub fn withdraw_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawTotalFeesOnEthereum")
.raw_call("withdrawTransactionFees")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

Expand Down Expand Up @@ -247,6 +266,32 @@ where
.original_result()
}

pub fn get_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getRefundFeesForEthereum")
.argument(&token_id)
.original_result()
}

pub fn get_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getTransactionFees")
.argument(&token_id)
.original_result()
}

pub fn bridged_tokens_wrapper_address(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedAddress<Env::Api>> {
Expand Down
49 changes: 47 additions & 2 deletions bridged-tokens-wrapper/src/esdt_safe_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,35 @@ where
.original_result()
}

pub fn withdraw_total_fees_on_ethereum<
pub fn withdraw_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawRefundFeesForEthereum")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

pub fn withdraw_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawTotalFeesOnEthereum")
.raw_call("withdrawTransactionFees")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

Expand Down Expand Up @@ -247,6 +266,32 @@ where
.original_result()
}

pub fn get_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getRefundFeesForEthereum")
.argument(&token_id)
.original_result()
}

pub fn get_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getTransactionFees")
.argument(&token_id)
.original_result()
}

pub fn bridged_tokens_wrapper_address(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedAddress<Env::Api>> {
Expand Down
61 changes: 52 additions & 9 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub trait EsdtSafe:
}

let actual_bridged_amount = refund_tx.amount - &required_fee;
self.total_fees_on_ethereum(&refund_tx.token_identifier)
self.refund_fees_for_ethereum(&refund_tx.token_identifier)
.update(|fees| *fees += required_fee);
let tx_nonce = self.get_and_save_next_tx_id();

Expand Down Expand Up @@ -375,13 +375,39 @@ pub trait EsdtSafe:
}

#[only_owner]
#[endpoint(withdrawTotalFeesOnEthereum)]
fn withdraw_total_fees_on_ethereum(&self, token_id: TokenIdentifier) {
let amount_out = self.total_fees_on_ethereum(&token_id).get();
#[endpoint(withdrawRefundFeesForEthereum)]
fn withdraw_refund_fees_for_ethereum(
&self,
token_id: TokenIdentifier,
multisig_owner: ManagedAddress,
) {
let refund_fees_for_ethereum_mapper = self.refund_fees_for_ethereum(&token_id);
require!(
!refund_fees_for_ethereum_mapper.is_empty(),
"There are no fees to withdraw"
);
let amount_out = refund_fees_for_ethereum_mapper.get();
self.tx()
.to(ToCaller)
.to(multisig_owner)
.single_esdt(&token_id, 0, &amount_out)
.transfer();
refund_fees_for_ethereum_mapper.set(BigUint::zero());
}

#[only_owner]
#[endpoint(withdrawTransactionFees)]
fn withdraw_transaction_fees(&self, token_id: TokenIdentifier, multisig_owner: ManagedAddress) {
let accumulated_transaction_fees_mapper = self.accumulated_transaction_fees(&token_id);
require!(
!accumulated_transaction_fees_mapper.is_empty(),
"There are no fees to withdraw"
);
let amount_out = accumulated_transaction_fees_mapper.get();
self.tx()
.to(multisig_owner)
.single_esdt(&token_id, 0, &amount_out)
.transfer();
accumulated_transaction_fees_mapper.set(BigUint::zero());
}

#[view(computeTotalAmmountsFromIndex)]
Expand Down Expand Up @@ -437,8 +463,6 @@ pub trait EsdtSafe:
refund_amounts
}

// views

#[view(getTotalRefundAmounts)]
fn get_total_refund_amounts(&self) -> MultiValueEncoded<MultiValue2<TokenIdentifier, BigUint>> {
let mut refund_amounts = MultiValueEncoded::new();
Expand All @@ -452,6 +476,25 @@ pub trait EsdtSafe:
refund_amounts
}

#[view(getRefundFeesForEthereum)]
fn get_refund_fees_for_ethereum(&self, token_id: TokenIdentifier) -> BigUint {
let refund_fees_for_ethereum_mapper = self.refund_fees_for_ethereum(&token_id);
if refund_fees_for_ethereum_mapper.is_empty() {
BigUint::zero()
} else {
refund_fees_for_ethereum_mapper.get()
}
}

#[view(getTransactionFees)]
fn get_transaction_fees(&self, token_id: TokenIdentifier) -> BigUint {
let accumulated_transaction_fees_mapper = self.accumulated_transaction_fees(&token_id);
if accumulated_transaction_fees_mapper.is_empty() {
BigUint::zero()
} else {
accumulated_transaction_fees_mapper.get()
}
}
// private

fn rebalance_for_refund(&self, token_id: &TokenIdentifier, amount: &BigUint) {
Expand Down Expand Up @@ -521,8 +564,8 @@ pub trait EsdtSafe:
#[storage_mapper("totalRefundAmount")]
fn total_refund_amount(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[storage_mapper("totalFeesOnEthereum")]
fn total_fees_on_ethereum(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;
#[storage_mapper("refundFeesForEthereum")]
fn refund_fees_for_ethereum(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[storage_mapper("refundAmount")]
fn refund_amount(
Expand Down
9 changes: 6 additions & 3 deletions esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 46
// Endpoints: 49
// Async Callback (empty): 1
// Total number of exported functions: 49
// Total number of exported functions: 52

#![no_std]

Expand All @@ -25,10 +25,13 @@ multiversx_sc_wasm_adapter::endpoints! {
createTransaction => create_transaction
claimRefund => claim_refund
setBridgedTokensWrapperAddress => set_bridged_tokens_wrapper_contract_address
withdrawTotalFeesOnEthereum => withdraw_total_fees_on_ethereum
withdrawRefundFeesForEthereum => withdraw_refund_fees_for_ethereum
withdrawTransactionFees => withdraw_transaction_fees
computeTotalAmmountsFromIndex => compute_total_amounts_from_index
getRefundAmounts => get_refund_amounts
getTotalRefundAmounts => get_total_refund_amounts
getRefundFeesForEthereum => get_refund_fees_for_ethereum
getTransactionFees => get_transaction_fees
getBridgedTokensWrapperAddress => bridged_tokens_wrapper_address
setFeeEstimatorContractAddress => set_fee_estimator_contract_address
setEthTxGasLimit => set_eth_tx_gas_limit
Expand Down
49 changes: 47 additions & 2 deletions multi-transfer-esdt/src/esdt_safe_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,35 @@ where
.original_result()
}

pub fn withdraw_total_fees_on_ethereum<
pub fn withdraw_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawRefundFeesForEthereum")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

pub fn withdraw_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
token_id: Arg0,
multisig_owner: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("withdrawTotalFeesOnEthereum")
.raw_call("withdrawTransactionFees")
.argument(&token_id)
.argument(&multisig_owner)
.original_result()
}

Expand Down Expand Up @@ -247,6 +266,32 @@ where
.original_result()
}

pub fn get_refund_fees_for_ethereum<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getRefundFeesForEthereum")
.argument(&token_id)
.original_result()
}

pub fn get_transaction_fees<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getTransactionFees")
.argument(&token_id)
.original_result()
}

pub fn bridged_tokens_wrapper_address(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedAddress<Env::Api>> {
Expand Down
Loading
Loading