Skip to content

Commit

Permalink
Merge pull request #256 from multiversx/bridge-proxy-refund-refactor
Browse files Browse the repository at this point in the history
bridge-proxy: refund refactor
  • Loading branch information
CostinCarabas authored Dec 2, 2024
2 parents 2f1b424 + 0da9aa3 commit 87441db
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
9 changes: 6 additions & 3 deletions bridge-proxy/src/bridge-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ pub trait BridgeProxyContract:
#[promises_callback]
fn execution_callback(&self, #[call_result] result: ManagedAsyncCallResult<()>, tx_id: usize) {
if result.is_err() {
self.refund_transaction(tx_id);
let tx = self.get_pending_transaction_by_id(tx_id);
self.refund_transactions(tx_id).set(&tx);
}
self.cleanup_transaction(tx_id);
}

#[endpoint(refundTransaction)]
fn refund_transaction(&self, tx_id: usize) {
let tx = self.get_pending_transaction_by_id(tx_id);
let tx = self.refund_transactions(tx_id).get();
let esdt_safe_contract_address = self.get_esdt_safe_address();

let unwrapped_token = self.unwrap_token(&tx.token_id, tx_id);
Expand Down Expand Up @@ -185,7 +187,8 @@ pub trait BridgeProxyContract:
}

fn finish_execute_gracefully(&self, tx_id: usize) {
self.refund_transaction(tx_id);
let tx = self.get_pending_transaction_by_id(tx_id);
self.refund_transactions(tx_id).set(&tx);
self.cleanup_transaction(tx_id);
}

Expand Down
4 changes: 4 additions & 0 deletions bridge-proxy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub trait ConfigModule {
#[storage_mapper("pendingTransactions")]
fn pending_transactions(&self) -> MapMapper<usize, EthTransaction<Self::Api>>;

#[view(refundTransactions)]
#[storage_mapper("refundTransactions")]
fn refund_transactions(&self, tx_id: usize) -> SingleValueMapper<EthTransaction<Self::Api>>;

#[storage_mapper("payments")]
fn payments(&self, tx_id: usize) -> SingleValueMapper<EsdtTokenPayment<Self::Api>>;

Expand Down
18 changes: 11 additions & 7 deletions bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ fn bridge_proxy_too_small_gas_sc_call_test() {
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transaction_by_id(1u32)
.returns(ExpectValue(eth_tx))
.returns(ExpectValue(eth_tx.clone()))
.run();

test.world
Expand All @@ -771,10 +771,12 @@ fn bridge_proxy_too_small_gas_sc_call_test() {
.execute(1u32)
.run();

// Refund: Funds are transfered to EsdtSafe
test.world
.check_account(ESDT_SAFE_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, amount.clone());
.check_account(BRIDGE_PROXY_ADDRESS)
.check_storage("str:refundTransactions|u32:1", "0x30313032303330343035303630373038303931300000000000000000050063726f7766756e64696e675f5f5f5f5f5f5f5f5f5f5f0000000d4252494447452d3132333435360000000201f4000000000000000101000000150000000466756e6400000000000f42400100000000")
.check_storage("str:batchId|u32:1", "1")
.check_storage("str:highestTxId", "1")
.check_storage("str:payments|u32:1", "nested:str:BRIDGE-123456|u64:0|biguint:500");
}

#[test]
Expand Down Expand Up @@ -840,8 +842,10 @@ fn bridge_proxy_empty_endpoint_with_args_test() {
.execute(1u32)
.run();

// Refund: Funds are transfered to EsdtSafe
test.world
.check_account(ESDT_SAFE_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, amount.clone());
.check_account(BRIDGE_PROXY_ADDRESS)
.check_storage("str:refundTransactions|u32:1", "0x30313032303330343035303630373038303931300000000000000000050063726f7766756e64696e675f5f5f5f5f5f5f5f5f5f5f0000000d4252494447452d3132333435360000000201f4000000000000000101000000110000000000000000009896800100000000")
.check_storage("str:batchId|u32:1", "1")
.check_storage("str:highestTxId", "1")
.check_storage("str:payments|u32:1", "nested:str:BRIDGE-123456|u64:0|biguint:500");
}
6 changes: 4 additions & 2 deletions bridge-proxy/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

// Init: 1
// Upgrade: 1
// Endpoints: 8
// Endpoints: 10
// Async Callback (empty): 1
// Promise callbacks: 1
// Total number of exported functions: 12
// Total number of exported functions: 14

#![no_std]

Expand All @@ -23,8 +23,10 @@ multiversx_sc_wasm_adapter::endpoints! {
upgrade => upgrade
deposit => deposit
execute => execute
refundTransaction => refund_transaction
getPendingTransactionById => get_pending_transaction_by_id
getPendingTransactions => get_pending_transactions
refundTransactions => refund_transactions
highestTxId => highest_tx_id
pause => pause_endpoint
unpause => unpause_endpoint
Expand Down

0 comments on commit 87441db

Please sign in to comment.