Skip to content

Commit

Permalink
The deposit structure supports bridge scene,add l2_hash field
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-robin committed Dec 4, 2023
1 parent c3d1dda commit e0203c9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ Unreleased
- Add Rust and Golang unit tests, add wasm tests
- Add example code in folder `examples`
- Add Makefile to build the bindings and lint the code.
- Add `l2_hash` field in `Deposit`. Replace the original `eth_hash` semantics.

### Changed
- Changed the `eth_hash` field to `Option` in `Deposit`.
- Renamed the `eth_hash` field to `l2_hash` in `Fullexit`.
5 changes: 3 additions & 2 deletions binding_tests/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestSignOrderMatching(t *testing.T) {
func TestDeposit(t *testing.T) {
fromAddress := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
toAddress := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
ethHash := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
l2Hash := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
builder := sdk.DepositBuilder {
fromAddress,
toAddress,
Expand All @@ -198,7 +198,8 @@ func TestDeposit(t *testing.T) {
sdk.TokenId(4),
*big.NewInt(100),
100,
ethHash,
l2Hash,
nil,
}
tx := sdk.NewDeposit(builder)
assert.NotNil(t, tx)
Expand Down
5 changes: 3 additions & 2 deletions binding_tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTypeDeposit(t *testing.T) {
l1_source_token := sdk.TokenId(2)
amount := *big.NewInt(123)
serial_id := uint64(123)
eth_hash := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
l2_hash := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
builder := sdk.DepositBuilder{
from_address,
to_address,
Expand All @@ -26,7 +26,8 @@ func TestTypeDeposit(t *testing.T) {
l1_source_token,
amount,
serial_id,
eth_hash,
l2_hash,
nil,
}
deposit := sdk.NewDeposit(builder);
bytes := deposit.GetBytes()
Expand Down
5 changes: 3 additions & 2 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ dictionary DepositBuilder {
TokenId l1_source_token;
BigUint amount;
u64 serial_id;
H256 eth_hash;
H256 l2_hash;
H256 ?eth_hash;
};


Expand Down Expand Up @@ -154,7 +155,7 @@ dictionary FullExitBuilder {
sequence<ContractPrice> contract_prices;
sequence<SpotPriceInfo> margin_prices;
u64 serial_id;
H256 eth_hash;
H256 l2_hash;
};


Expand Down
8 changes: 5 additions & 3 deletions types/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ pub struct DepositBuilder {
pub l1_source_token: TokenId,
pub amount: BigUint,
pub serial_id: u64,
pub eth_hash: H256,
pub l2_hash: H256,
pub eth_hash: Option<H256>,
}

impl DepositBuilder {
Expand All @@ -105,6 +106,7 @@ impl DepositBuilder {
l1_source_token: self.l1_source_token,
amount: self.amount,
serial_id: self.serial_id,
l2_hash: self.l2_hash,
eth_hash: self.eth_hash,
}
}
Expand Down Expand Up @@ -192,7 +194,7 @@ pub struct FullExitBuilder {
pub contract_prices: Vec<ContractPrice>,
pub margin_prices: Vec<SpotPriceInfo>,
pub serial_id: u64,
pub eth_hash: H256,
pub l2_hash: H256,
}

impl FullExitBuilder {
Expand All @@ -210,7 +212,7 @@ impl FullExitBuilder {
margin_prices: self.margin_prices,
},
serial_id: self.serial_id,
eth_hash: self.eth_hash,
l2_hash: self.l2_hash,
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions types/src/tx_type/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ pub struct Deposit {
pub amount: BigUint,
/// serial id for unique tx_hash
pub serial_id: u64,
pub eth_hash: H256,
/// Transaction hash of linea/zksync/starket etc
pub l2_hash: H256,
/// Transaction hash of ethereum, exist when deposit going from Ethereum bridge to linea/zksync/starket etc
pub eth_hash: Option<H256>,
}

impl Deposit {
Expand All @@ -50,7 +53,7 @@ impl GetBytes for Deposit {
let bytes_len = self.bytes_len();
let mut out = Vec::with_capacity(bytes_len);
out.extend_from_slice(&self.serial_id.to_be_bytes());
out.extend_from_slice(self.eth_hash.as_bytes());
out.extend_from_slice(self.l2_hash.as_bytes());
assert_eq!(out.len(), bytes_len);
out
}
Expand All @@ -72,7 +75,7 @@ mod test {
fn test_deposit_get_bytes() {
let address =
ZkLinkAddress::from_str("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9").unwrap();
let eth_hash =
let l2_hash =
H256::from_str("0xe35f3a39d542f6d276c2f203e8fd64fcb8bf5db062b71ccacf45d5ecd9d456f3")
.unwrap();
let builder = DepositBuilder {
Expand All @@ -84,7 +87,8 @@ mod test {
l1_source_token: TokenId(18),
amount: BigUint::from(100u32),
serial_id: 32001,
eth_hash,
eth_hash: None,
l2_hash,
};
let deposit = builder.build();
let bytes = deposit.get_bytes();
Expand Down
9 changes: 5 additions & 4 deletions types/src/tx_type/full_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct FullExit {
#[validate]
pub oracle_prices: OraclePrices,
pub serial_id: u64,
pub eth_hash: H256,
/// Transaction hash of linea/zksync/starket etc
pub l2_hash: H256,
}

impl FullExit {
Expand All @@ -44,7 +45,7 @@ impl GetBytes for FullExit {
let bytes_len = self.bytes_len();
let mut out = Vec::with_capacity(bytes_len);
out.extend(self.serial_id.to_be_bytes());
out.extend_from_slice(self.eth_hash.as_bytes());
out.extend_from_slice(self.l2_hash.as_bytes());
assert_eq!(out.len(), bytes_len);
out
}
Expand All @@ -65,7 +66,7 @@ mod test {
fn test_full_exit_get_bytes() {
let address =
ZkLinkAddress::from_str("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9").unwrap();
let eth_hash =
let l2_hash =
H256::from_str("0xe35f3a39d542f6d276c2f203e8fd64fcb8bf5db062b71ccacf45d5ecd9d456f3")
.unwrap();
let default_oracle_price = OraclePrices::default();
Expand All @@ -79,7 +80,7 @@ mod test {
contract_prices: default_oracle_price.contract_prices,
margin_prices: default_oracle_price.margin_prices,
serial_id: 100,
eth_hash,
l2_hash,
};
let full_exit = builder.build();
let bytes = full_exit.get_bytes();
Expand Down

0 comments on commit e0203c9

Please sign in to comment.