|
| 1 | +use alloy::{ |
| 2 | + primitives::{Address, U256}, |
| 3 | + signers::Signer, |
| 4 | +}; |
| 5 | +use chrono::Utc; |
| 6 | +use eyre::Error; |
| 7 | +use signet_rpc::TxCache; |
| 8 | +use signet_types::UnsignedOrder; |
| 9 | +use signet_zenith::RollupOrders::{Input, Order, Output}; |
| 10 | + |
| 11 | +/// Helper fn to convert from a human readable amount to a U256 token amount. |
| 12 | +fn token_amount(amount: u64, decimals: u32) -> U256 { |
| 13 | + U256::from(amount * 10u64.pow(decimals)) |
| 14 | +} |
| 15 | + |
| 16 | +/// Empty main to silence clippy. |
| 17 | +fn main() {} |
| 18 | + |
| 19 | +/// Example code demonstrating API usage and patterns for signing an Order. |
| 20 | +#[derive(Debug)] |
| 21 | +pub struct SendOrder<S: Signer> { |
| 22 | + /// The signer to use for signing the order. |
| 23 | + signer: S, |
| 24 | + /// The transaction cache endpoint. |
| 25 | + tx_cache: TxCache, |
| 26 | + /// The address of the Order contract on the rollup. |
| 27 | + ru_order_contract: Address, |
| 28 | + /// The address of USDC on the rollup. |
| 29 | + ru_usdc_address: Address, |
| 30 | + /// The address of USDC on the host. |
| 31 | + host_usdc_address: Address, |
| 32 | + /// The chain id of the rollup. |
| 33 | + ru_chain_id: u64, |
| 34 | + /// The chain id of the host. |
| 35 | + host_chain_id: u64, |
| 36 | +} |
| 37 | + |
| 38 | +impl<S> SendOrder<S> |
| 39 | +where |
| 40 | + S: Signer, |
| 41 | +{ |
| 42 | + /// Create a new SendOrder instance. |
| 43 | + pub const fn new( |
| 44 | + signer: S, |
| 45 | + tx_cache: TxCache, |
| 46 | + ru_order_contract: Address, |
| 47 | + ru_usdc_address: Address, |
| 48 | + host_usdc_address: Address, |
| 49 | + ru_chain_id: u64, |
| 50 | + host_chain_id: u64, |
| 51 | + ) -> Self { |
| 52 | + Self { |
| 53 | + signer, |
| 54 | + tx_cache, |
| 55 | + ru_order_contract, |
| 56 | + ru_usdc_address, |
| 57 | + host_usdc_address, |
| 58 | + ru_chain_id, |
| 59 | + host_chain_id, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Construct a simple example Order, sign it, and send it. |
| 64 | + pub async fn run(&self) -> Result<(), Error> { |
| 65 | + // get an example order |
| 66 | + let order = self.example_order(); |
| 67 | + |
| 68 | + // sign and send the order |
| 69 | + self.sign_and_send_order(order).await |
| 70 | + } |
| 71 | + |
| 72 | + /// Sign an Order and send it to the transaction cache to be Filled. |
| 73 | + pub async fn sign_and_send_order(&self, order: Order) -> Result<(), Error> { |
| 74 | + // make an UnsignedOrder from the Order |
| 75 | + let unsigned = UnsignedOrder::from(&order); |
| 76 | + |
| 77 | + // sign it |
| 78 | + let signed = unsigned |
| 79 | + .with_chain(self.ru_chain_id, self.ru_order_contract) |
| 80 | + .sign(&self.signer) |
| 81 | + .await?; |
| 82 | + |
| 83 | + // send the SignedOrder to the transaction cache |
| 84 | + self.tx_cache.forward_order(signed).await |
| 85 | + } |
| 86 | + |
| 87 | + /// Get an example Order which swaps 1 USDC on the rollup for 1 USDC on the host. |
| 88 | + fn example_order(&self) -> Order { |
| 89 | + let usdc_decimals: u32 = 6; |
| 90 | + let one_usdc = token_amount(1, usdc_decimals); |
| 91 | + |
| 92 | + // input is 1 USDC on the rollup |
| 93 | + let input = Input { token: self.ru_usdc_address, amount: one_usdc }; |
| 94 | + |
| 95 | + // output is 1 USDC on the host chain |
| 96 | + let output = Output { |
| 97 | + token: self.host_usdc_address, |
| 98 | + amount: one_usdc, |
| 99 | + chainId: self.host_chain_id as u32, |
| 100 | + recipient: self.signer.address(), |
| 101 | + }; |
| 102 | + |
| 103 | + // deadline 60 seconds (or ~5 blocks) from now |
| 104 | + let deadline = Utc::now().timestamp() + 60; |
| 105 | + |
| 106 | + // construct the order |
| 107 | + Order { inputs: vec![input], outputs: vec![output], deadline: U256::from(deadline) } |
| 108 | + } |
| 109 | +} |
0 commit comments