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

chore(bridge-contracts): add snapshot test for memo #1753

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/astria-bridge-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ serde = { workspace = true }
serde_json = { workspace = true }
tendermint = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
43 changes: 32 additions & 11 deletions crates/astria-bridge-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,8 @@ where
.expect("must be set if this method is entered"),
);

let memo = memo_to_json(&memos::v1::Ics20WithdrawalFromRollup {
memo: event.memo.clone(),
rollup_block_number,
rollup_return_address: event.sender.encode_hex(),
rollup_withdrawal_event_id,
})
.map_err(GetWithdrawalActionsError::encode_memo)?;
let memo = memo_to_json(&event, rollup_block_number, rollup_withdrawal_event_id)
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
.map_err(GetWithdrawalActionsError::encode_memo)?;

let amount = calculate_amount(&event, self.asset_withdrawal_divisor)
.map_err(GetWithdrawalActionsError::calculate_withdrawal_amount)?;
Expand Down Expand Up @@ -663,9 +658,22 @@ struct EncodeMemoError {
source: serde_json::Error,
}

fn memo_to_json<T: prost::Name + serde::Serialize>(memo: &T) -> Result<String, EncodeMemoError> {
serde_json::to_string(memo).map_err(|source| EncodeMemoError {
proto_message: T::full_name(),
fn memo_to_json(
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
event: &Ics20WithdrawalFilter,
rollup_block_number: u64,
rollup_withdrawal_event_id: String,
) -> Result<String, EncodeMemoError> {
use prost::Name as _;
type Memo = memos::v1::Ics20WithdrawalFromRollup;

let withdrawal = Memo {
memo: event.memo.clone(),
rollup_block_number,
rollup_return_address: event.sender.encode_hex(),
rollup_withdrawal_event_id,
};
serde_json::to_string(&withdrawal).map_err(|source| EncodeMemoError {
proto_message: Memo::full_name(),
source,
})
}
Expand All @@ -688,9 +696,22 @@ fn timeout_in_5_min() -> u64 {

#[cfg(test)]
mod tests {
use super::max_timeout_height;
use super::*;

#[test]
fn max_timeout_height_does_not_panic() {
max_timeout_height();
}

#[test]
fn memo_snapshot() {
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
let event = Ics20WithdrawalFilter {
sender: ethers::core::types::Address::from_low_u64_be(999),
amount: ethers::core::types::U256::one(),
destination_chain_address: "destination address".to_string(),
memo: "a memo".to_string(),
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
};
let json_encoded_memo = memo_to_json(&event, 999, "event ID".to_string()).unwrap();
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
insta::assert_snapshot!("memo_snapshot", json_encoded_memo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-bridge-contracts/src/lib.rs
expression: "memo_to_json(&event, rollup_block_number, rollup_withdrawal_event_id).unwrap()"
---
{"rollupBlockNumber":"999","rollupWithdrawalEventId":"event ID","rollupReturnAddress":"0x00000000000000000000000000000000000000000000000000000000000003e7","memo":"a memo"}
Fraser999 marked this conversation as resolved.
Show resolved Hide resolved
Loading