Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct BridgeCall<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(Some(call.data.len())),
space = 8 + OutgoingMessage::space::<Call>(call.data.len()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

use crate::{
common::{bridge::Bridge, BRIDGE_SEED, SOL_VAULT_SEED},
solana_to_base::{internal::bridge_sol::bridge_sol_internal, Call, OutgoingMessage},
solana_to_base::{internal::bridge_sol::bridge_sol_internal, Call, OutgoingMessage, Transfer},
};

/// Accounts struct for the bridge_sol instruction that transfers native SOL from Solana to Base
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct BridgeSol<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(call.as_ref().map(|c| c.data.len())),
space = 8 + OutgoingMessage::space::<Transfer>(call.map(|c| c.data.len()).unwrap_or_default()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

use crate::{
common::{bridge::Bridge, BRIDGE_SEED, TOKEN_VAULT_SEED},
solana_to_base::{internal::bridge_spl::bridge_spl_internal, Call, OutgoingMessage},
solana_to_base::{internal::bridge_spl::bridge_spl_internal, Call, OutgoingMessage, Transfer},
};

/// Accounts struct for the bridge_spl instruction that transfers SPL tokens from Solana to Base along
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct BridgeSpl<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(call.as_ref().map(|c| c.data.len())),
space = 8 + OutgoingMessage::space::<Transfer>(call.as_ref().map(|c| c.data.len()).unwrap_or_default()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anchor_spl::{
use crate::{
common::{bridge::Bridge, BRIDGE_SEED},
solana_to_base::{
internal::bridge_wrapped_token::bridge_wrapped_token_internal, Call, OutgoingMessage,
internal::bridge_wrapped_token::bridge_wrapped_token_internal, Call, OutgoingMessage, Transfer,
},
};

Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct BridgeWrappedToken<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(call.as_ref().map(|c| c.data.len())),
space = 8 + OutgoingMessage::space::<Transfer>(call.as_ref().map(|c| c.data.len()).unwrap_or_default()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct BridgeCallBuffered<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(Some(call_buffer.data.len())),
space = 8 + OutgoingMessage::space::<Call>(call_buffer.data.len()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_lang::prelude::*;
use crate::{
common::{bridge::Bridge, BRIDGE_SEED, SOL_VAULT_SEED},
solana_to_base::{
internal::bridge_sol::bridge_sol_internal, Call, CallBuffer, OutgoingMessage,
internal::bridge_sol::bridge_sol_internal, Call, CallBuffer, OutgoingMessage, Transfer,
},
};

Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct BridgeSolWithBufferedCall<'info> {
/// - Created fresh for each bridge; address determined by the provided keypair
/// - Funded by `payer`
/// - Space: 8-byte Anchor discriminator + serialized `OutgoingMessage`
#[account(init, payer = payer, space = 8 + OutgoingMessage::space(Some(call_buffer.data.len())))]
#[account(init, payer = payer, space = 8 + OutgoingMessage::space::<Transfer>(call_buffer.data.len()))]
pub outgoing_message: Account<'info, OutgoingMessage>,

/// System program required for account creation and the SOL transfer CPI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
use crate::{
common::{bridge::Bridge, BRIDGE_SEED, TOKEN_VAULT_SEED},
solana_to_base::{
internal::bridge_spl::bridge_spl_internal, Call, CallBuffer, OutgoingMessage,
internal::bridge_spl::bridge_spl_internal, Call, CallBuffer, OutgoingMessage, Transfer,
},
};

Expand Down Expand Up @@ -84,7 +84,7 @@ pub struct BridgeSplWithBufferedCall<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(Some(call_buffer.data.len())),
space = 8 + OutgoingMessage::space::<Transfer>(call_buffer.data.len()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
common::{bridge::Bridge, BRIDGE_SEED},
solana_to_base::{
internal::bridge_wrapped_token::bridge_wrapped_token_internal, Call, CallBuffer,
OutgoingMessage,
OutgoingMessage, Transfer,
},
};

Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct BridgeWrappedTokenWithBufferedCall<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(Some(call_buffer.data.len())),
space = 8 + OutgoingMessage::space::<Transfer>(call_buffer.data.len()),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct WrapToken<'info> {
#[account(
init,
payer = payer,
space = 8 + OutgoingMessage::space(Some(REGISTER_REMOTE_TOKEN_DATA_LEN)),
space = 8 + OutgoingMessage::space::<Call>(REGISTER_REMOTE_TOKEN_DATA_LEN),
)]
pub outgoing_message: Account<'info, OutgoingMessage>,

Expand Down
24 changes: 13 additions & 11 deletions solana/programs/bridge/src/solana_to_base/state/outgoing_message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use anchor_lang::prelude::*;

/// Trait for calculating the space required for a message.
pub trait MessageSpace {
fn space(data_len: usize) -> usize;
}

/// Represents a token transfer from Solana to Base with optional contract execution.
/// This struct contains all the information needed to bridge tokens between chains
/// and optionally execute additional logic on the destination chain after the transfer.
Expand All @@ -26,13 +31,13 @@ pub struct Transfer {
pub call: Option<Call>,
}

impl Transfer {
pub fn space(data_len: Option<usize>) -> usize {
impl MessageSpace for Transfer {
fn space(data_len: usize) -> usize {
20 + // to
32 + // local_token
20 + // remote_token
8 + // amount
1 + Call::space(data_len.unwrap_or_default()) // option_flag + call
1 + Call::space(data_len) // option_flag + call
}
}

Expand Down Expand Up @@ -66,8 +71,8 @@ pub struct Call {
pub data: Vec<u8>,
}

impl Call {
pub fn space(data_len: usize) -> usize {
impl MessageSpace for Call {
fn space(data_len: usize) -> usize {
CallType::INIT_SPACE + // call type
20 + // to
16 + // value
Expand Down Expand Up @@ -127,13 +132,10 @@ impl OutgoingMessage {
}

/// Returns the serialized size of an `OutgoingMessage` payload, excluding the 8-byte Anchor
/// account discriminator. Uses the `Transfer` variant for sizing because it is larger than
/// `Call` (it embeds an optional `Call`), ensuring sufficient capacity for either variant.
pub fn space(data_len: Option<usize>) -> usize {
/// account discriminator.
pub fn space<T: MessageSpace>(data_len: usize) -> usize {
8 + // nonce
32 + // sender

// TODO: Accept the message type as a parameter, so we can use the correct space calculation.
1 + Transfer::space(data_len) // variant + transfer (the transfer variant is always bigger as it embeds an optional call)
1 + T::space(data_len) // message (variant + space)
}
}