Skip to content

Commit e1ad80f

Browse files
authored
feat: added storage deposit for safe_verify_deposit (#211)
* feat: added storage deposit for `safe_verify_deposit` * chore: bumped versions * fix: increase nonce if storage deposit is made * fix: clippy issue * fix: corrected recipient for storage deposit * fix: clippy issue
1 parent 9c236ec commit e1ad80f

File tree

8 files changed

+92
-53
lines changed

8 files changed

+92
-53
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridge-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bridge-cli"
3-
version = "0.3.16"
3+
version = "0.3.17"
44
edition = "2021"
55
repository = "https://github.com/Near-One/bridge-sdk-rs"
66

bridge-cli/src/omni_connector_command.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ pub enum OmniConnectorSubCommand {
9191
#[clap(about = "Deposit storage for a token on NEAR")]
9292
NearStorageDeposit {
9393
#[clap(short, long, help = "Token to deposit storage for")]
94-
token: String,
94+
token: AccountId,
9595
#[clap(short, long, help = "Amount to deposit")]
9696
amount: u128,
97+
#[clap(short, long, help = "Account to deposit storage for")]
98+
account_id: AccountId,
9799
#[command(flatten)]
98100
config_cli: CliConfig,
99101
},
@@ -529,10 +531,16 @@ pub async fn match_subcommand(cmd: OmniConnectorSubCommand, network: Network) {
529531
OmniConnectorSubCommand::NearStorageDeposit {
530532
token,
531533
amount,
534+
account_id,
532535
config_cli,
533536
} => {
534537
omni_connector(network, config_cli)
535-
.near_storage_deposit_for_token(token, amount, TransactionOptions::default())
538+
.near_storage_deposit_for_token(
539+
token,
540+
amount,
541+
account_id,
542+
TransactionOptions::default(),
543+
)
536544
.await
537545
.unwrap();
538546
}
@@ -868,10 +876,7 @@ pub async fn match_subcommand(cmd: OmniConnectorSubCommand, network: Network) {
868876
chain.into(),
869877
btc_tx_hash,
870878
vout,
871-
BtcDepositArgs::OmniDepositArgs {
872-
recipient_id,
873-
fee,
874-
},
879+
BtcDepositArgs::OmniDepositArgs { recipient_id, fee },
875880
TransactionOptions::default(),
876881
)
877882
.await
@@ -1002,26 +1007,42 @@ fn omni_connector(network: Network, cli_config: CliConfig) -> OmniConnector {
10021007
(
10031008
ChainKind::Zcash,
10041009
UTXOChainAccounts {
1005-
utxo_chain_connector: combined_config.zcash_connector,
1006-
utxo_chain_token: combined_config.zcash,
1010+
utxo_chain_connector: combined_config
1011+
.zcash_connector
1012+
.map(|account| account.parse().unwrap()),
1013+
utxo_chain_token: combined_config
1014+
.zcash
1015+
.map(|account| account.parse().unwrap()),
10071016
satoshi_relayer: None,
10081017
},
10091018
),
10101019
(
10111020
ChainKind::Btc,
10121021
UTXOChainAccounts {
1013-
utxo_chain_connector: combined_config.btc_connector,
1014-
utxo_chain_token: combined_config.btc,
1015-
satoshi_relayer: combined_config.satoshi_relayer,
1022+
utxo_chain_connector: combined_config
1023+
.btc_connector
1024+
.map(|account| account.parse().unwrap()),
1025+
utxo_chain_token: combined_config.btc.map(|account| account.parse().unwrap()),
1026+
satoshi_relayer: combined_config
1027+
.satoshi_relayer
1028+
.map(|account| account.parse().unwrap()),
10161029
},
10171030
),
10181031
]);
10191032

10201033
let near_bridge_client = NearBridgeClientBuilder::default()
10211034
.endpoint(combined_config.near_rpc.clone())
10221035
.private_key(combined_config.near_private_key)
1023-
.signer(combined_config.near_signer)
1024-
.omni_bridge_id(combined_config.near_token_locker_id)
1036+
.signer(
1037+
combined_config
1038+
.near_signer
1039+
.map(|account| account.parse().unwrap()),
1040+
)
1041+
.omni_bridge_id(
1042+
combined_config
1043+
.near_token_locker_id
1044+
.map(|account| account.parse().unwrap()),
1045+
)
10251046
.utxo_bridges(utxo_bridges)
10261047
.build()
10271048
.unwrap();

bridge-sdk/bridge-clients/near-bridge-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "near-bridge-client"
3-
version = "0.2.4"
3+
version = "0.2.5"
44
edition = "2021"
55

66
[dependencies]

bridge-sdk/bridge-clients/near-bridge-client/src/btc.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,38 @@ impl NearBridgeClient {
363363
Ok(tx_hash)
364364
}
365365

366-
/// Finalizes a BTC transfer by calling `verify_deposit` on the BTC connector contract.
366+
/// Finalizes a BTC transfer by calling `verify_deposit` or `verify_safe_deposit` on the BTC connector contract.
367367
#[tracing::instrument(skip_all, name = "NEAR FIN BTC TRANSFER")]
368368
pub async fn fin_btc_transfer(
369369
&self,
370370
chain: ChainKind,
371371
args: FinBtcTransferArgs,
372-
transaction_options: TransactionOptions,
372+
mut transaction_options: TransactionOptions,
373373
) -> Result<CryptoHash> {
374374
let endpoint = self.endpoint()?;
375375
let btc_connector = self.utxo_chain_connector(chain)?;
376376
let (method_name, deposit) = if args.deposit_msg.safe_deposit.is_some() {
377+
match self
378+
.get_required_storage_deposit(
379+
self.utxo_chain_token(chain)?,
380+
args.deposit_msg.recipient_id.clone(),
381+
)
382+
.await?
383+
{
384+
amount if amount > 0 => {
385+
self.storage_deposit_for_token(
386+
self.utxo_chain_token(chain)?,
387+
args.deposit_msg.recipient_id.clone(),
388+
amount,
389+
transaction_options.clone(),
390+
)
391+
.await?;
392+
393+
transaction_options.nonce = transaction_options.nonce.map(|nonce| nonce + 1);
394+
}
395+
_ => {}
396+
}
397+
377398
(
378399
"safe_verify_deposit".to_string(),
379400
BTC_SAFE_VERIFY_DEPOSIT_DEPOSIT,
@@ -922,11 +943,11 @@ impl NearBridgeClient {
922943
.as_ref()
923944
.ok_or(BridgeSdkError::ConfigError(
924945
"BTC Connector account id is not set".to_string(),
925-
))?
926-
.parse::<AccountId>()
946+
))
927947
.map_err(|_| {
928948
BridgeSdkError::ConfigError("Invalid btc connector account id".to_string())
929949
})
950+
.cloned()
930951
}
931952

932953
pub fn utxo_chain_token(&self, chain: ChainKind) -> Result<AccountId> {
@@ -939,9 +960,9 @@ impl NearBridgeClient {
939960
.as_ref()
940961
.ok_or(BridgeSdkError::ConfigError(
941962
"Bitcoin account id is not set".to_string(),
942-
))?
943-
.parse::<AccountId>()
963+
))
944964
.map_err(|_| BridgeSdkError::ConfigError("Invalid bitcoin account id".to_string()))
965+
.cloned()
945966
}
946967

947968
pub fn satoshi_relayer(&self, chain: ChainKind) -> Result<AccountId> {
@@ -954,10 +975,10 @@ impl NearBridgeClient {
954975
.as_ref()
955976
.ok_or(BridgeSdkError::ConfigError(
956977
"Satoshi Relayer account id is not set".to_string(),
957-
))?
958-
.parse::<AccountId>()
978+
))
959979
.map_err(|_| {
960980
BridgeSdkError::ConfigError("Invalid Satoshi Relayer account id".to_string())
961981
})
982+
.cloned()
962983
}
963984
}

bridge-sdk/bridge-clients/near-bridge-client/src/near_bridge_client.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ pub struct FastFinTransferArgs {
8282
#[derive(Default, Clone)]
8383
pub struct UTXOChainAccounts {
8484
#[doc = r"UTXO Chain Connector account id on Near"]
85-
pub utxo_chain_connector: Option<String>,
85+
pub utxo_chain_connector: Option<AccountId>,
8686
#[doc = r"UTXO Chain Token account id on Near"]
87-
pub utxo_chain_token: Option<String>,
87+
pub utxo_chain_token: Option<AccountId>,
8888
#[doc = r"Satoshi Relayer Account Id which sign transaction in UTXO Chain Bridge"]
89-
pub satoshi_relayer: Option<String>,
89+
pub satoshi_relayer: Option<AccountId>,
9090
}
9191

9292
/// Bridging NEAR-originated NEP-141 tokens
@@ -97,9 +97,9 @@ pub struct NearBridgeClient {
9797
#[doc = r"NEAR private key"]
9898
private_key: Option<String>,
9999
#[doc = r"NEAR account id of the transaction signer"]
100-
signer: Option<String>,
100+
signer: Option<AccountId>,
101101
#[doc = r"`OmniBridge` account id on Near"]
102-
omni_bridge_id: Option<String>,
102+
omni_bridge_id: Option<AccountId>,
103103
#[doc = r"Accounts Id for UTXO chains Bridges"]
104104
utxo_bridges: HashMap<ChainKind, UTXOChainAccounts>,
105105
}
@@ -333,24 +333,22 @@ impl NearBridgeClient {
333333
#[tracing::instrument(skip_all, name = "STORAGE DEPOSIT")]
334334
pub async fn storage_deposit_for_token(
335335
&self,
336-
token_id: String,
336+
token_id: AccountId,
337+
account_id: AccountId,
337338
amount: u128,
338339
transaction_options: TransactionOptions,
339340
) -> Result<CryptoHash> {
340341
let endpoint = self.endpoint()?;
341-
let omni_bridge_id = self.omni_bridge_id()?;
342342

343343
let tx_hash = near_rpc_client::change_and_wait(
344344
endpoint,
345345
ChangeRequest {
346346
signer: self.signer()?,
347347
nonce: transaction_options.nonce,
348-
receiver_id: token_id.parse().map_err(|err| {
349-
BridgeSdkError::ConfigError(format!("Failed to parse token_id: {err}"))
350-
})?,
348+
receiver_id: token_id,
351349
method_name: "storage_deposit".to_string(),
352350
args: serde_json::json!({
353-
"account_id": omni_bridge_id
351+
"account_id": account_id
354352
})
355353
.to_string()
356354
.into_bytes(),
@@ -613,7 +611,9 @@ impl NearBridgeClient {
613611
let endpoint = self.endpoint()?;
614612
let omni_bridge_id = self.omni_bridge_id()?;
615613

616-
let required_balance = self.get_required_balance_for_init_transfer().await?
614+
let required_balance = self
615+
.get_required_balance_for_init_transfer()
616+
.await?
617617
.saturating_add(native_fee);
618618

619619
let nonce = if self
@@ -940,9 +940,9 @@ impl NearBridgeClient {
940940
.as_ref()
941941
.ok_or(BridgeSdkError::ConfigError(
942942
"Near signer account id is not set".to_string(),
943-
))?
944-
.parse::<AccountId>()
943+
))
945944
.map_err(|_| BridgeSdkError::ConfigError("Invalid near signer account id".to_string()))
945+
.cloned()
946946
}
947947

948948
pub fn signer(&self) -> Result<near_crypto::InMemorySigner> {
@@ -972,8 +972,8 @@ impl NearBridgeClient {
972972
.as_ref()
973973
.ok_or(BridgeSdkError::ConfigError(
974974
"OmniBridge account id is not set".to_string(),
975-
))?
976-
.parse::<AccountId>()
975+
))
977976
.map_err(|_| BridgeSdkError::ConfigError("Invalid omni bridge account id".to_string()))
977+
.cloned()
978978
}
979979
}

bridge-sdk/connectors/omni-connector/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "omni-connector"
3-
version = "0.2.14"
3+
version = "0.2.15"
44
edition = "2021"
55

66
[dependencies]

bridge-sdk/connectors/omni-connector/src/omni_connector.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ impl OmniConnector {
346346

347347
pub async fn near_storage_deposit_for_token(
348348
&self,
349-
token_id: String,
349+
token_id: AccountId,
350350
amount: u128,
351+
account_id: AccountId,
351352
transaction_options: TransactionOptions,
352353
) -> Result<CryptoHash> {
353354
let near_bridge_client = self.near_bridge_client()?;
354355
near_bridge_client
355-
.storage_deposit_for_token(token_id, amount, transaction_options)
356+
.storage_deposit_for_token(token_id, account_id, amount, transaction_options)
356357
.await
357358
}
358359

@@ -484,10 +485,9 @@ impl OmniConnector {
484485
let near_bridge_client = self.near_bridge_client()?;
485486
let deposit_msg = match deposit_args {
486487
BtcDepositArgs::DepositMsg { msg } => msg,
487-
BtcDepositArgs::OmniDepositArgs {
488-
recipient_id,
489-
fee,
490-
} => near_bridge_client.get_deposit_msg_for_omni_bridge(recipient_id, fee)?,
488+
BtcDepositArgs::OmniDepositArgs { recipient_id, fee } => {
489+
near_bridge_client.get_deposit_msg_for_omni_bridge(recipient_id, fee)?
490+
}
491491
};
492492

493493
let args = FinBtcTransferArgs {
@@ -1718,10 +1718,7 @@ impl OmniConnector {
17181718
ChainKind::Btc,
17191719
btc_tx_hash,
17201720
vout,
1721-
BtcDepositArgs::OmniDepositArgs {
1722-
recipient_id,
1723-
fee,
1724-
},
1721+
BtcDepositArgs::OmniDepositArgs { recipient_id, fee },
17251722
transaction_options,
17261723
)
17271724
.await

0 commit comments

Comments
 (0)