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

Refund fixes #181

Merged
merged 10 commits into from
Jul 31, 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions bridge-proxy/src/bridge-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use multiversx_sc::imports::*;

pub mod bridge_proxy_contract_proxy;
pub mod config;
pub mod esdt_safe_proxy;
pub mod bridged_tokens_wrapper;

use transaction::EthTransaction;

const DEFAULT_GAS_LIMIT_FOR_REFUND_CALLBACK: u64 = 20000000; // 20 million

#[multiversx_sc::contract]
pub trait BridgeProxyContract:
config::ConfigModule + multiversx_sc_modules::pause::PauseModule
Expand All @@ -28,28 +30,32 @@ pub trait BridgeProxyContract:
fn deposit(&self, eth_tx: EthTransaction<Self::Api>) {
self.require_not_paused();
let caller = self.blockchain().get_caller();
let payment = self.call_value().single_esdt();
require!(
caller == self.multi_transfer_address().get(),
"Only MultiTransfer can do deposits"
);
self.pending_transactions().push(&eth_tx);
let tx_id = self.pending_transactions().push(&eth_tx);
self.payments(tx_id).set(&payment);
}

#[endpoint(execute)]
fn execute(&self, tx_id: usize) {
self.require_not_paused();
let tx = self.get_pending_transaction_by_id(tx_id);
let payment = self.payments(tx_id).get();

require!(tx.call_data.is_some(), "There is no data for a SC call!");

let call_data = unsafe { tx.call_data.clone().unwrap_unchecked() };
self.send()
.contract_call::<IgnoreValue>(tx.to.clone(), call_data.endpoint.clone())
.with_raw_arguments(call_data.args.clone().into())
.with_esdt_transfer((tx.token_id.clone(), 0, tx.amount.clone()))
.with_esdt_transfer(payment)
.with_gas_limit(call_data.gas_limit)
.async_call_promise()
.with_callback(self.callbacks().execution_callback(tx_id))
.with_extra_gas_for_callback(DEFAULT_GAS_LIMIT_FOR_REFUND_CALLBACK)
.register_promise();
}

Expand All @@ -67,13 +73,14 @@ pub trait BridgeProxyContract:

fn refund_transaction(&self, tx_id: usize) {
let tx = self.get_pending_transaction_by_id(tx_id);
let esdt_safe_addr = self.esdt_safe_address().get();
let payment = self.payments(tx_id).get();
let esdt_safe_addr = self.bridged_tokens_wrapper_address().get();

self.tx()
.to(esdt_safe_addr)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(tx.from)
.single_esdt(&tx.token_id, 0, &tx.amount)
.typed(bridged_tokens_wrapper::BridgedTokensWrapperProxy)
.unwrap_token_create_transaction(&tx.token_id, tx.from)
.single_esdt(&payment.token_identifier, payment.token_nonce, &payment.amount)
.sync_call();
}

Expand Down
295 changes: 295 additions & 0 deletions bridge-proxy/src/bridged_tokens_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
// 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 BridgedTokensWrapperProxy;

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

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

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

#[rustfmt::skip]
impl<Env, From, To, Gas> BridgedTokensWrapperProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn upgrade(
self,
) -> TxTypedUpgrade<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_upgrade()
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> BridgedTokensWrapperProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn add_wrapped_token<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<u32>,
>(
self,
universal_bridged_token_ids: Arg0,
num_decimals: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("addWrappedToken")
.argument(&universal_bridged_token_ids)
.argument(&num_decimals)
.original_result()
}

pub fn update_wrapped_token<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<u32>,
>(
self,
universal_bridged_token_ids: Arg0,
num_decimals: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("updateWrappedToken")
.argument(&universal_bridged_token_ids)
.argument(&num_decimals)
.original_result()
}

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

pub fn whitelist_token<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<u32>,
Arg2: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
chain_specific_token_id: Arg0,
chain_specific_token_decimals: Arg1,
universal_bridged_token_ids: Arg2,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("whitelistToken")
.argument(&chain_specific_token_id)
.argument(&chain_specific_token_decimals)
.argument(&universal_bridged_token_ids)
.original_result()
}

pub fn update_whitelisted_token<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<u32>,
>(
self,
chain_specific_token_id: Arg0,
chain_specific_token_decimals: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("updateWhitelistedToken")
.argument(&chain_specific_token_id)
.argument(&chain_specific_token_decimals)
.original_result()
}

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

pub fn deposit_liquidity(
self,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("depositLiquidity")
.original_result()
}

/// Will wrap what it can, and send back the rest unchanged
pub fn wrap_tokens(
self,
) -> TxTypedCall<Env, From, To, (), Gas, ManagedVec<Env::Api, EsdtTokenPayment<Env::Api>>> {
self.wrapped_tx
.raw_call("wrapTokens")
.original_result()
}

pub fn unwrap_token<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
requested_token: Arg0,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("unwrapToken")
.argument(&requested_token)
.original_result()
}

pub fn unwrap_token_create_transaction<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
Arg1: ProxyArg<eth_address::EthAddress<Env::Api>>,
>(
self,
requested_token: Arg0,
to: Arg1,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("unwrapTokenCreateTransaction")
.argument(&requested_token)
.argument(&to)
.original_result()
}

pub fn universal_bridged_token_ids(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, TokenIdentifier<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getUniversalBridgedTokenIds")
.original_result()
}

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

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

pub fn chain_specific_token_ids<
Arg0: ProxyArg<TokenIdentifier<Env::Api>>,
>(
self,
universal_token_id: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, TokenIdentifier<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getchainSpecificTokenIds")
.argument(&universal_token_id)
.original_result()
}

pub fn pause_endpoint(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("pause")
.original_result()
}

pub fn unpause_endpoint(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("unpause")
.original_result()
}

pub fn paused_status(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, bool> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("isPaused")
.original_result()
}
}
Loading
Loading