Skip to content

Commit 8821079

Browse files
committed
update: add generic V assign concrete types
1 parent aa915f0 commit 8821079

File tree

7 files changed

+57
-52
lines changed

7 files changed

+57
-52
lines changed

protocol-units/bridge/chains/ethereum/src/client.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use bridge_shared::bridge_contracts::{
1515
BridgeContractInitiator, BridgeContractInitiatorError, BridgeContractInitiatorResult,
1616
};
1717
use bridge_shared::types::{
18-
Amount, BridgeTransferDetails, BridgeTransferId, HashLock, HashLockPreImage, InitiatorAddress,
19-
RecipientAddress, TimeLock,
18+
Amount, BridgeTransferDetails, BridgeTransferId, EthValue, HashLock, HashLockPreImage,
19+
InitiatorAddress, RecipientAddress, TimeLock,
2020
};
2121
use serde_with::serde_as;
2222
use std::fmt::{self, Debug};
@@ -135,7 +135,11 @@ impl EthClient {
135135
Ok(())
136136
}
137137

138-
pub async fn deposit_weth_and_approve(&mut self, caller: Address, amount: U256) -> Result<(), anyhow::Error> {
138+
pub async fn deposit_weth_and_approve(
139+
&mut self,
140+
caller: Address,
141+
amount: U256,
142+
) -> Result<(), anyhow::Error> {
139143
let provider_signer = self.rpc_provider.default_signer_address();
140144
let deposit_weth_signer = self.get_signer_address();
141145
println!("provider_signer: {:?}", provider_signer);
@@ -145,12 +149,17 @@ impl EthClient {
145149
send_transaction(call, &utils::send_tx_rules(), RETRIES, GAS_LIMIT)
146150
.await
147151
.expect("Failed to deposit eth to weth contract");
148-
149-
150-
let approve_call: alloy::contract::CallBuilder<_, &_, _> = contract.approve(self.initiator_contract_address()?, amount).from(deposit_weth_signer);
151-
let WETH9::balanceOfReturn { _0: balance } = contract.balanceOf(deposit_weth_signer).call().await.expect("Failed to get balance");
152+
153+
let approve_call: alloy::contract::CallBuilder<_, &_, _> = contract
154+
.approve(self.initiator_contract_address()?, amount)
155+
.from(deposit_weth_signer);
156+
let WETH9::balanceOfReturn { _0: balance } = contract
157+
.balanceOf(deposit_weth_signer)
158+
.call()
159+
.await
160+
.expect("Failed to get balance");
152161
println!("balance: {}", balance);
153-
162+
154163
send_transaction(approve_call, &utils::send_tx_rules(), RETRIES, GAS_LIMIT)
155164
.await
156165
.expect("Failed to approve");
@@ -212,7 +221,7 @@ impl EthClient {
212221
)),
213222
}
214223
}
215-
224+
216225
pub fn weth_contract(&self) -> BridgeContractWETH9Result<&WETH9Contract> {
217226
match &self.weth_contract {
218227
Some(contract) => Ok(contract),
@@ -222,7 +231,6 @@ impl EthClient {
222231
}
223232
}
224233

225-
226234
pub fn counterparty_contract_address(&self) -> BridgeContractCounterpartyResult<Address> {
227235
match &self.counterparty_contract {
228236
Some(contract) => Ok(contract.address().to_owned()),
@@ -255,6 +263,7 @@ impl EthClient {
255263
impl BridgeContractInitiator for EthClient {
256264
type Address = EthAddress;
257265
type Hash = EthHash;
266+
type Value = EthValue;
258267

259268
// `_initiator_address`, or in the contract, `originator` is set
260269
// via the `msg.sender`, which is stored in the `rpc_provider`.
@@ -366,6 +375,7 @@ impl BridgeContractInitiator for EthClient {
366375
impl BridgeContractCounterparty for EthClient {
367376
type Address = EthAddress;
368377
type Hash = EthHash;
378+
type Value = EthValue;
369379

370380
async fn lock_bridge_transfer_assets(
371381
&mut self,

protocol-units/bridge/chains/ethereum/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use bridge_shared::{
2323
use futures::channel::mpsc::UnboundedReceiver;
2424
use serde::{Deserialize, Serialize};
2525

26-
use crate::AtomicBridgeInitiator::AtomicBridgeInitiatorInstance;
2726
use crate::AtomicBridgeCounterparty::AtomicBridgeCounterpartyInstance;
27+
use crate::AtomicBridgeInitiator::AtomicBridgeInitiatorInstance;
2828
use crate::WETH9::WETH9Instance;
2929
pub const INITIATED_SELECT: FixedBytes<32> =
3030
AtomicBridgeInitiator::BridgeTransferInitiated::SIGNATURE_HASH;

protocol-units/bridge/chains/movement/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use tokio::{
3333

3434
use url::Url;
3535

36+
mod types;
3637
pub mod utils;
3738

3839
const DUMMY_ADDRESS: AccountAddress = AccountAddress::new([0; 32]);
@@ -208,6 +209,7 @@ impl MovementClient {
208209
impl BridgeContractCounterparty for MovementClient {
209210
type Address = MovementAddress;
210211
type Hash = [u8; 32];
212+
type Value = MovementValue;
211213

212214
async fn lock_bridge_transfer_assets(
213215
&mut self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub type MovementValue = u64;

protocol-units/bridge/shared/src/bridge_contracts.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use thiserror::Error;
22

33
use crate::types::{
4-
Amount, BridgeAddressType, BridgeHashType, BridgeTransferDetails, BridgeTransferId, HashLock,
5-
HashLockPreImage, InitiatorAddress, RecipientAddress, TimeLock
4+
Amount, BridgeAddressType, BridgeHashType, BridgeTransferDetails, BridgeTransferId,
5+
BridgeValueType, HashLock, HashLockPreImage, InitiatorAddress, RecipientAddress, TimeLock,
66
};
77

88
#[derive(Error, Debug, Clone, PartialEq, Eq)]
@@ -70,14 +70,15 @@ pub type BridgeContractWETH9Result<T> = Result<T, BridgeContractWETH9Error>;
7070
pub trait BridgeContractInitiator: Clone + Unpin + Send + Sync {
7171
type Address: BridgeAddressType;
7272
type Hash: BridgeHashType;
73+
type Value: BridgeValueType;
7374

7475
async fn initiate_bridge_transfer(
7576
&mut self,
7677
initiator_address: InitiatorAddress<Self::Address>,
7778
recipient_address: RecipientAddress<Vec<u8>>,
7879
hash_lock: HashLock<Self::Hash>,
7980
time_lock: TimeLock,
80-
amount: Amount,
81+
amount: Amount<Self::Value>,
8182
) -> BridgeContractInitiatorResult<()>;
8283

8384
async fn complete_bridge_transfer(
@@ -94,13 +95,16 @@ pub trait BridgeContractInitiator: Clone + Unpin + Send + Sync {
9495
async fn get_bridge_transfer_details(
9596
&mut self,
9697
bridge_transfer_id: BridgeTransferId<Self::Hash>,
97-
) -> BridgeContractInitiatorResult<Option<BridgeTransferDetails<Self::Address, Self::Hash>>>;
98+
) -> BridgeContractInitiatorResult<
99+
Option<BridgeTransferDetails<Self::Address, Self::Hash, Self::Value>>,
100+
>;
98101
}
99102

100103
#[async_trait::async_trait]
101104
pub trait BridgeContractCounterparty: Clone + Unpin + Send + Sync {
102105
type Address: BridgeAddressType;
103106
type Hash: BridgeHashType;
107+
type Value: BridgeValueType;
104108

105109
async fn lock_bridge_transfer_assets(
106110
&mut self,
@@ -109,7 +113,7 @@ pub trait BridgeContractCounterparty: Clone + Unpin + Send + Sync {
109113
time_lock: TimeLock,
110114
initiator: InitiatorAddress<Vec<u8>>,
111115
recipient: RecipientAddress<Self::Address>,
112-
amount: Amount,
116+
amount: Amount<Self::Value>,
113117
) -> BridgeContractCounterpartyResult<()>;
114118

115119
async fn complete_bridge_transfer(
@@ -134,8 +138,5 @@ pub trait BridgeContractWETH9: Clone + Unpin + Send + Sync {
134138
type Address: BridgeAddressType;
135139
type Hash: BridgeHashType;
136140

137-
async fn deposit_weth(
138-
&mut self,
139-
amount: Amount,
140-
) -> BridgeContractWETH9Result<()>;
141+
async fn deposit_weth(&mut self, amount: Amount) -> BridgeContractWETH9Result<()>;
141142
}

protocol-units/bridge/shared/src/initiator_contract.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
pub type SCIResult<A, H> = Result<SmartContractInitiatorEvent<A, H>, SmartContractInitiatorError>;
1515

1616
#[derive(Debug, Clone, PartialEq, Eq)]
17-
pub enum SmartContractInitiatorEvent<A, H> {
18-
InitiatedBridgeTransfer(BridgeTransferDetails<A, H>),
17+
pub enum SmartContractInitiatorEvent<A, H, V> {
18+
InitiatedBridgeTransfer(BridgeTransferDetails<A, H, V>),
1919
CompletedBridgeTransfer(BridgeTransferId<H>),
2020
}
2121

protocol-units/bridge/shared/src/types.rs

+21-30
Original file line numberDiff line numberDiff line change
@@ -141,76 +141,66 @@ impl From<Uint<256, 4>> for TimeLock {
141141
}
142142

143143
#[derive(Deref, DerefMut, Debug, Clone, Copy, PartialEq, Eq)]
144-
pub struct Amount(pub BridgedToken);
145-
146-
impl Amount {
147-
pub fn weth(&self) -> u64 {
148-
match self.0 {
149-
BridgedToken::Weth(value) => value,
150-
BridgedToken::WethAndEth((weth_value, eth_value)) => weth_value,
151-
_ => 0,
152-
}
153-
}
154-
pub fn eth(&self) -> u64 {
155-
match self.0 {
156-
BridgedToken::Eth(value) => value,
157-
BridgedToken::WethAndEth((weth_value, eth_value)) => eth_value,
158-
_ => 0,
159-
}
160-
}
144+
pub struct Amount<V>(pub V);
145+
146+
pub enum EthValue {
147+
Weth(u64),
148+
Eth(u64),
149+
WethAndEth((u64, u64)),
161150
}
162151

163-
impl From<Uint<256, 4>> for Amount {
152+
impl From<Uint<256, 4>> for Amount<EthValue> {
164153
fn from(value: Uint<256, 4>) -> Self {
165154
// Extract the lower 64 bits.
166155
let lower_64_bits: u64 = value.as_limbs()[0];
167-
Amount(BridgedToken::Eth(lower_64_bits))
156+
//TODO: unit test this for Weth and Eth
157+
Amount(EthValue::Eth(lower_64_bits))
168158
}
169159
}
170160

171161
#[derive(Debug, PartialEq, Eq, Clone)]
172-
pub struct BridgeTransferDetails<A, H> {
162+
pub struct BridgeTransferDetails<A, H, V> {
173163
pub bridge_transfer_id: BridgeTransferId<H>,
174164
pub initiator_address: InitiatorAddress<A>,
175165
pub recipient_address: RecipientAddress<Vec<u8>>,
176166
pub hash_lock: HashLock<H>,
177167
pub time_lock: TimeLock,
178-
pub amount: Amount,
168+
pub amount: Amount<V>,
179169
}
180170

181-
impl<A, H> Default for BridgeTransferDetails<A, H> {
171+
impl<A, H, V> Default for BridgeTransferDetails<A, H, V> {
182172
fn default() -> Self {
183173
todo!()
184174
}
185175
}
186176

187177
#[derive(Debug, PartialEq, Eq, Clone)]
188-
pub struct LockDetails<A, H> {
178+
pub struct LockDetails<A, H, V> {
189179
pub bridge_transfer_id: BridgeTransferId<H>,
190180
pub initiator_address: InitiatorAddress<Vec<u8>>,
191181
pub recipient_address: RecipientAddress<A>,
192182
pub hash_lock: HashLock<H>,
193183
pub time_lock: TimeLock,
194-
pub amount: Amount,
184+
pub amount: Amount<V>,
195185
}
196186

197187
#[derive(Debug, PartialEq, Eq, Clone)]
198-
pub struct CounterpartyCompletedDetails<A, H> {
188+
pub struct CounterpartyCompletedDetails<A, H, V> {
199189
pub bridge_transfer_id: BridgeTransferId<H>,
200190
pub initiator_address: InitiatorAddress<Vec<u8>>,
201191
pub recipient_address: RecipientAddress<A>,
202192
pub hash_lock: HashLock<H>,
203193
pub secret: HashLockPreImage,
204-
pub amount: Amount,
194+
pub amount: Amount<V>,
205195
}
206196

207-
impl<A, H> CounterpartyCompletedDetails<A, H>
197+
impl<A, H, V> CounterpartyCompletedDetails<A, H, V>
208198
where
209199
InitiatorAddress<Vec<u8>>: From<InitiatorAddress<A>>,
210200
RecipientAddress<A>: From<RecipientAddress<Vec<u8>>>,
211201
{
212202
pub fn from_bridge_transfer_details(
213-
bridge_transfer_details: BridgeTransferDetails<A, H>,
203+
bridge_transfer_details: BridgeTransferDetails<A, H, V>,
214204
secret: HashLockPreImage,
215205
) -> Self {
216206
CounterpartyCompletedDetails {
@@ -224,8 +214,8 @@ where
224214
}
225215
}
226216

227-
impl<A, H> CounterpartyCompletedDetails<A, H> {
228-
pub fn from_lock_details(lock_details: LockDetails<A, H>, secret: HashLockPreImage) -> Self {
217+
impl<A, H, V> CounterpartyCompletedDetails<A, H, V> {
218+
pub fn from_lock_details(lock_details: LockDetails<A, H, V>, secret: HashLockPreImage) -> Self {
229219
CounterpartyCompletedDetails {
230220
bridge_transfer_id: lock_details.bridge_transfer_id,
231221
initiator_address: lock_details.initiator_address,
@@ -243,6 +233,7 @@ pub trait BridgeAddressType:
243233
Debug + PartialEq + Eq + Hash + Unpin + Send + Sync + Clone + From<Vec<u8>>
244234
{
245235
}
236+
pub trait BridgeValueType: Debug + PartialEq + Eq + Clone + Send + Sync + Unpin {}
246237

247238
pub trait Convert<O> {
248239
fn convert(other: &Self) -> O;

0 commit comments

Comments
 (0)