|
| 1 | +use eyre::Result; |
| 2 | + |
| 3 | +mod signal_slot; |
| 4 | +use signal_slot::get_signal_slot; |
| 5 | + |
| 6 | +mod utils; |
| 7 | +use utils::{deploy_eth_bridge, deploy_signal_service, get_proofs, get_provider, SignalProof}; |
| 8 | + |
| 9 | +use alloy::hex::decode; |
| 10 | +use alloy::primitives::{Address, Bytes, FixedBytes, U256}; |
| 11 | +use std::fs; |
| 12 | + |
| 13 | +fn expand_vector(vec: Vec<Bytes>, name: &str) -> String { |
| 14 | + let mut expanded = String::new(); |
| 15 | + for (i, item) in vec.iter().enumerate() { |
| 16 | + expanded += format!("\t\t{}[{}] = hex\"{}\";\n", name, i, item) |
| 17 | + .replace("0x", "") |
| 18 | + .as_str(); |
| 19 | + } |
| 20 | + return expanded; |
| 21 | +} |
| 22 | + |
| 23 | +fn create_deposit_call( |
| 24 | + proof: SignalProof, |
| 25 | + nonce: usize, |
| 26 | + signer: Address, |
| 27 | + recipient: Address, |
| 28 | + amount: U256, |
| 29 | + data: &str, |
| 30 | + context: &str, |
| 31 | + id: &FixedBytes<32>, |
| 32 | +) -> String { |
| 33 | + let mut result = String::new(); |
| 34 | + result += format!("\n\t\t// Populate proof {}\n", nonce).as_str(); |
| 35 | + result += format!( |
| 36 | + "\t\taccountProof = new bytes[]({});\n", |
| 37 | + proof.account_proof.len() |
| 38 | + ) |
| 39 | + .as_str(); |
| 40 | + result += expand_vector(proof.account_proof, "accountProof").as_str(); |
| 41 | + result += format!( |
| 42 | + "\t\tstorageProof = new bytes[]({});\n", |
| 43 | + proof.storage_proof.len() |
| 44 | + ) |
| 45 | + .as_str(); |
| 46 | + result += expand_vector(proof.storage_proof, "storageProof").as_str(); |
| 47 | + result += format!("\t\tdeposit.nonce = {};\n", nonce).as_str(); |
| 48 | + result += format!("\t\tdeposit.from = address({});\n", signer).as_str(); |
| 49 | + result += format!("\t\tdeposit.to = address({});\n", recipient).as_str(); |
| 50 | + result += format!("\t\tdeposit.amount = {};\n", amount).as_str(); |
| 51 | + result += format!("\t\tdeposit.data = bytes(hex\"{}\");\n", data).as_str(); |
| 52 | + result += format!("\t\tdeposit.context = bytes(hex\"{}\");\n", context).as_str(); |
| 53 | + result += format!("\t\t_createDeposit(\n\t\t\taccountProof,\n\t\t\tstorageProof,\n\t\t\tdeposit,\n\t\t\tbytes32({}),\n\t\t\tbytes32({})\n\t\t);\n", proof.slot, id).as_str(); |
| 54 | + return result; |
| 55 | +} |
| 56 | + |
| 57 | +pub struct DepositSpecification { |
| 58 | + pub recipient: Address, |
| 59 | + pub amount: U256, |
| 60 | + pub data: String, |
| 61 | + pub context: String, |
| 62 | +} |
| 63 | + |
| 64 | +fn deposit_specification() -> Vec<DepositSpecification> { |
| 65 | + // This is an address on the destination chain, so it seems natural to use one generated there |
| 66 | + // In this case, the CrossChainDepositExists.sol test case defines _randomAddress("recipient"); |
| 67 | + let recipient = "0x99A270Be1AA5E97633177041859aEEB9a0670fAa"; |
| 68 | + // Use both zero and non-zero amounts (in this case 4 ether) |
| 69 | + let amounts = vec![0_u128, 4000000000000000000_u128]; |
| 70 | + // Use different calldata to try different functions and inputs |
| 71 | + let calldata = vec![ |
| 72 | + "", // empty |
| 73 | + "9b28f6fb00000000000000000000000000000000000000000000000000000000000004d2", // (valid) call to somePayableFunction(1234) |
| 74 | + "9b28f6fb00000000000000000000000000000000000000000000000000000000000004d3", // (invalid) call to somePayableFunction(1235) |
| 75 | + "5932a71200000000000000000000000000000000000000000000000000000000000004d2", // (valid) call to `someNonPayableFunction(1234)` |
| 76 | + ]; |
| 77 | + |
| 78 | + let mut specifications = vec![]; |
| 79 | + for amount in amounts { |
| 80 | + for data in calldata.iter() { |
| 81 | + specifications.push(DepositSpecification { |
| 82 | + recipient: recipient.parse().unwrap(), |
| 83 | + amount: U256::from(amount), |
| 84 | + data: data.to_string(), |
| 85 | + context: String::from(""), |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + return specifications; |
| 90 | +} |
| 91 | + |
| 92 | +#[tokio::main] |
| 93 | +async fn main() -> Result<()> { |
| 94 | + let (provider, _anvil, signer) = get_provider()?; |
| 95 | + let signal_service = deploy_signal_service(&provider).await?; |
| 96 | + let eth_bridge = deploy_eth_bridge(&provider, *signal_service.address()).await?; |
| 97 | + |
| 98 | + let deposits = deposit_specification(); |
| 99 | + assert!(deposits.len() > 0, "No deposits to prove"); |
| 100 | + let mut ids: Vec<FixedBytes<32>> = vec![]; |
| 101 | + // Perform all deposits |
| 102 | + for (_i, spec) in deposits.iter().enumerate() { |
| 103 | + let tx = eth_bridge |
| 104 | + .deposit( |
| 105 | + spec.recipient, |
| 106 | + decode(spec.data.clone())?.into(), |
| 107 | + decode(spec.context.clone())?.into(), |
| 108 | + ) |
| 109 | + .value(spec.amount) |
| 110 | + .send() |
| 111 | + .await? |
| 112 | + .get_receipt() |
| 113 | + .await?; |
| 114 | + let id = tx.logs().get(0).unwrap().data().clone().data; |
| 115 | + ids.push(FixedBytes::from_slice(&id[..32])); |
| 116 | + } |
| 117 | + |
| 118 | + let mut block_hash = FixedBytes::ZERO; |
| 119 | + let mut state_root = FixedBytes::ZERO; |
| 120 | + |
| 121 | + let mut populated_proofs = String::new(); |
| 122 | + for (i, id) in ids.iter().enumerate() { |
| 123 | + let slot = get_signal_slot(id, ð_bridge.address()); |
| 124 | + let proof = get_proofs(&provider, slot, &signal_service).await?; |
| 125 | + |
| 126 | + if i == 0 { |
| 127 | + block_hash = proof.block_hash; |
| 128 | + state_root = proof.state_root; |
| 129 | + } else { |
| 130 | + assert!(proof.block_hash == block_hash); |
| 131 | + assert!(proof.state_root == state_root); |
| 132 | + } |
| 133 | + |
| 134 | + let d = &deposits[i]; |
| 135 | + populated_proofs += create_deposit_call( |
| 136 | + proof, |
| 137 | + i, |
| 138 | + signer.address(), |
| 139 | + d.recipient, |
| 140 | + d.amount, |
| 141 | + d.data.as_str(), |
| 142 | + d.context.as_str(), |
| 143 | + id, |
| 144 | + ) |
| 145 | + .as_str(); |
| 146 | + } |
| 147 | + |
| 148 | + let template = fs::read_to_string("offchain/sample_deposit_proof.tmpl")?; |
| 149 | + let formatted = template |
| 150 | + .replace( |
| 151 | + "{signal_service_address}", |
| 152 | + signal_service.address().to_string().as_str(), |
| 153 | + ) |
| 154 | + .replace( |
| 155 | + "{bridge_address}", |
| 156 | + eth_bridge.address().to_string().as_str(), |
| 157 | + ) |
| 158 | + .replace("{block_hash}", block_hash.to_string().as_str()) |
| 159 | + .replace("{state_root}", state_root.to_string().as_str()) |
| 160 | + .replace("{populate_proofs}", populated_proofs.as_str()); |
| 161 | + println!("{}", formatted); |
| 162 | + Ok(()) |
| 163 | +} |
0 commit comments