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

bridge-proxy: refund refactor #256

Merged
merged 2 commits into from
Dec 2, 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
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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, anyone can call this, ok for me 👍

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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this view can be used to "select" only the transaction you want to refund in case we do not put a "general" sc calls executors for refunds

#[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