Skip to content

feat: example code to sign & send Orders #44

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

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions crates/constants/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ impl SignetSystemConstants {
self.host.is_system_contract(address)
}

/// Get the Order contract address for the given chain id.
pub const fn orders_for(&self, chain_id: u64) -> Option<Address> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is the most idiomatic way to do this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fn signature is idiomatic, but it's kinda weird in that there are exactly 2 choices, and the caller will always know which they want, so why do we allow passing an arbitrary chain id? caller should probably use .host_orders() or .rollup_orders() directly instead?

Copy link
Member

@prestwich prestwich May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's kinda fine this way I guess? the agg may contain invalid or junk data and this is one of the better ways to handle that I suppose

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i wish we didn't have an arbitrary chain id here, but james's argument is sound

if chain_id == self.host_chain_id() {
Some(self.host_orders())
} else if chain_id == self.ru_chain_id() {
Some(self.ru_orders())
} else {
None
}
}

/// Get the host chain ID.
pub const fn host_chain_id(&self) -> u64 {
self.host.chain_id()
Expand Down
5 changes: 5 additions & 0 deletions crates/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ tokio = { workspace = true, features = ["macros"] }
tokio-util = "0.7.13"
tower-http = { version = "0.6.2", features = ["cors"] }
tracing.workspace = true

[dev-dependencies]
signet-zenith.workspace = true
signet-constants.workspace = true
chrono.workspace = true
55 changes: 20 additions & 35 deletions crates/rpc/examples/filler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy::{
eips::Encodable2718,
network::{Ethereum, EthereumWallet, TransactionBuilder},
primitives::{Address, Bytes},
primitives::Bytes,
providers::{
fillers::{
BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller,
Expand All @@ -14,6 +14,7 @@ use alloy::{
};
use eyre::{eyre, Error};
use signet_bundle::SignetEthBundle;
use signet_constants::SignetSystemConstants;
use signet_tx_cache::{client::TxCache, types::TxCacheSendBundleResponse};
use signet_types::{AggregateOrders, SignedFill, SignedOrder, UnsignedFill};
use std::{collections::HashMap, slice::from_ref};
Expand All @@ -34,9 +35,6 @@ type Provider = FillProvider<
Ethereum,
>;

/// Empty main to silence clippy.
fn main() {}

/// Example code demonstrating API usage and patterns for Signet Fillers.
#[derive(Debug)]
pub struct Filler<S: Signer> {
Expand All @@ -46,13 +44,8 @@ pub struct Filler<S: Signer> {
ru_provider: Provider,
/// The transaction cache endpoint.
tx_cache: TxCache,
/// A HashMap of the Order contract addresses for each chain.
/// MUST contain an address for both Host and Rollup.
order_contracts: HashMap<u64, Address>,
/// The chain id of the rollup.
ru_chain_id: u64,
/// The chain id of the host.
host_chain_id: u64,
/// The system constants.
constants: SignetSystemConstants,
}

impl<S> Filler<S>
Expand All @@ -64,11 +57,9 @@ where
signer: S,
ru_provider: Provider,
tx_cache: TxCache,
order_contracts: HashMap<u64, Address>,
ru_chain_id: u64,
host_chain_id: u64,
constants: SignetSystemConstants,
) -> Self {
Self { signer, ru_provider, tx_cache, order_contracts, ru_chain_id, host_chain_id }
Self { signer, ru_provider, tx_cache, constants }
}

/// Query the transaction cache to get all possible orders.
Expand Down Expand Up @@ -125,7 +116,7 @@ where
let txs = self.sign_and_encode_txns(tx_requests).await?;

// get the aggregated host fill for the Bundle, if any
let host_fills = signed_fills.remove(&self.host_chain_id);
let host_fills = signed_fills.remove(&self.constants.host().chain_id());

// set the Bundle to only be valid if mined in the next rollup block
let block_number = self.ru_provider.get_block_number().await? + 1;
Expand Down Expand Up @@ -165,8 +156,12 @@ where
let mut unsigned_fill = UnsignedFill::from(&agg);
// populate the Order contract addresses for each chain
for chain_id in agg.destination_chain_ids() {
unsigned_fill =
unsigned_fill.with_chain(chain_id, self.order_contract_address_for(chain_id)?);
unsigned_fill = unsigned_fill.with_chain(
chain_id,
self.constants
.orders_for(chain_id)
.ok_or(eyre!("invalid target chain id {}", chain_id))?,
);
}
// sign the UnsignedFill, producing a SignedFill for each target chain
Ok(unsigned_fill.sign(&self.signer).await?)
Expand Down Expand Up @@ -194,17 +189,17 @@ where
// Note that `fill` transactions MUST be mined *before* the corresponding Order(s) `initiate` transactions in order to cound
// Host `fill` transactions are always considered to be mined "before" the rollup block is processed,
// but Rollup `fill` transactions MUST take care to be ordered before the Orders are `initiate`d
if let Some(rollup_fill) = signed_fills.get(&self.ru_chain_id) {
if let Some(rollup_fill) = signed_fills.get(&self.constants.rollup().chain_id()) {
// add the fill tx to the rollup txns
let ru_fill_tx = rollup_fill.to_fill_tx(self.ru_order_contract()?);
let ru_fill_tx = rollup_fill.to_fill_tx(self.constants.rollup().orders());
tx_requests.push(ru_fill_tx);
}

// next, add a transaction to initiate each SignedOrder
for signed_order in orders {
// add the initiate tx to the rollup txns
let ru_initiate_tx =
signed_order.to_initiate_tx(self.signer.address(), self.ru_order_contract()?);
let ru_initiate_tx = signed_order
.to_initiate_tx(self.signer.address(), self.constants.rollup().orders());
tx_requests.push(ru_initiate_tx);
}

Expand Down Expand Up @@ -238,17 +233,7 @@ where
}
Ok(encoded_txs)
}

/// Get the Order contract address for the given chain id.
fn order_contract_address_for(&self, chain_id: u64) -> Result<Address, Error> {
self.order_contracts
.get(&chain_id)
.copied()
.ok_or(eyre!("No Order contract address configured for chain id {}", chain_id))
}

/// Get the Order contract address for the rollup.
fn ru_order_contract(&self) -> Result<Address, Error> {
self.order_contract_address_for(self.ru_chain_id)
}
}

/// Empty main to silence clippy.
fn main() {}
80 changes: 80 additions & 0 deletions crates/rpc/examples/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use alloy::{
primitives::{uint, U256},
signers::Signer,
};
use chrono::Utc;
use eyre::Error;
use signet_constants::SignetSystemConstants;
use signet_tx_cache::client::TxCache;
use signet_types::UnsignedOrder;
use signet_zenith::RollupOrders::{Input, Order, Output};

const ONE_USDC: U256 = uint!(1_000_000_U256);

/// Example code demonstrating API usage and patterns for signing an Order.
#[derive(Debug)]
pub struct SendOrder<S: Signer> {
/// The signer to use for signing the order.
signer: S,
/// The transaction cache endpoint.
tx_cache: TxCache,
/// The system constants.
constants: SignetSystemConstants,
}

impl<S> SendOrder<S>
where
S: Signer,
{
/// Create a new SendOrder instance.
pub const fn new(signer: S, tx_cache: TxCache, constants: SignetSystemConstants) -> Self {
Self { signer, tx_cache, constants }
}

/// Construct a simple example Order, sign it, and send it.
pub async fn run(&self) -> Result<(), Error> {
// get an example order
let order = self.example_order();

// sign and send the order
self.sign_and_send_order(order).await
}

/// Sign an Order and send it to the transaction cache to be Filled.
pub async fn sign_and_send_order(&self, order: Order) -> Result<(), Error> {
// make an UnsignedOrder from the Order
let unsigned = UnsignedOrder::from(&order);

// sign it
let signed = unsigned
.with_chain(self.constants.rollup().chain_id(), self.constants.rollup().orders())
.sign(&self.signer)
.await?;

// send the SignedOrder to the transaction cache
self.tx_cache.forward_order(signed).await
}

/// Get an example Order which swaps 1 USDC on the rollup for 1 USDC on the host.
fn example_order(&self) -> Order {
// input is 1 USDC on the rollup
let input = Input { token: self.constants.rollup().tokens().usdc(), amount: ONE_USDC };

// output is 1 USDC on the host chain
let output = Output {
token: self.constants.host().tokens().usdc(),
amount: ONE_USDC,
chainId: self.constants.host().chain_id() as u32,
recipient: self.signer.address(),
};

// deadline 60 seconds (or ~5 blocks) from now
let deadline = Utc::now().timestamp() + 60;

// construct the order
Order { inputs: vec![input], outputs: vec![output], deadline: U256::from(deadline) }
}
}

/// Empty main to silence clippy.
fn main() {}
Loading