Skip to content

Commit

Permalink
Factory bridge: revert withdraw type & add force_withdraw (#233)
Browse files Browse the repository at this point in the history
* Revert withdraw type

* update bridge token factory wasm

* Add force withdraw method

* Update contract build

* Fix access controle for `force_withdra`

* Update contract build

* Fix `force_withdraw`

* Update contract build

---------

Co-authored-by: Olga Kunyavskaya <olga.kunyavskaya@aurora.dev>
  • Loading branch information
karim-en and olga24912 authored Oct 13, 2023
1 parent 453e002 commit 52bfcf2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions bridge-token-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use near_sdk::{
};

pub use bridge_common::prover::{validate_eth_address, Proof};
use bridge_common::{parse_recipient, prover::*, result_types, Recipient};
use bridge_common::{parse_recipient, prover::*, Recipient};
pub use lock_event::EthLockedEvent;
pub use log_metadata_event::TokenMetadataEvent;

Expand Down Expand Up @@ -71,6 +71,15 @@ const TOKEN_TIMESTAMP_MAP_PREFIX: &[u8] = b"aTT";

pub type Mask = u128;

#[derive(Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize)]
pub enum ResultType {
Withdraw {
amount: Balance,
token: EthAddress,
recipient: EthAddress,
},
}

#[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum Role {
Expand All @@ -83,6 +92,7 @@ pub enum Role {
UnrestrictedUpdateMetadata,
UpgradableCodeStager,
UpgradableCodeDeployer,
ForceWithdrawer,
}

#[near_bindgen]
Expand Down Expand Up @@ -416,7 +426,7 @@ impl BridgeTokenFactory {
&mut self,
#[serializer(borsh)] amount: Balance,
#[serializer(borsh)] recipient: String,
) -> result_types::Withdraw {
) -> ResultType {
let token = env::predecessor_account_id();
let parts: Vec<&str> = token.as_str().split('.').collect();
assert_eq!(
Expand All @@ -430,7 +440,25 @@ impl BridgeTokenFactory {
);
let token_address = validate_eth_address(parts[0].to_string());
let recipient_address = validate_eth_address(recipient);
result_types::Withdraw::new(amount, token_address, recipient_address)
ResultType::Withdraw {
amount,
token: token_address,
recipient: recipient_address,
}
}

#[access_control_any(roles(Role::DAO, Role::ForceWithdrawer))]
#[result_serializer(borsh)]
pub fn force_withdraw(&self, token: String, amount: U128, recipient: String) -> ResultType {
assert!(
self.tokens.contains(&token),
"Such BridgeToken does not exist."
);
ResultType::Withdraw {
amount: amount.into(),
token: validate_eth_address(token),
recipient: validate_eth_address(recipient),
}
}

#[payable]
Expand Down Expand Up @@ -801,7 +829,11 @@ mod tests {
let address = validate_eth_address(token_locker());
assert_eq!(
contract.finish_withdraw(1_000, token_locker()),
result_types::Withdraw::new(1_000, address, address)
ResultType::Withdraw {
amount: 1_000,
token: address,
recipient: address
}
);
}

Expand Down
Binary file modified res/bridge_token_factory.wasm
Binary file not shown.

0 comments on commit 52bfcf2

Please # to comment.