-
Notifications
You must be signed in to change notification settings - Fork 37
feat(bridge): add Axelar cross-chain bridge stubs and design doc #232
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
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| .PHONY: sandbox sandbox-stop sandbox-logs | ||
|
|
||
| sandbox: | ||
| @bash scripts/sandbox.sh | ||
|
|
||
| sandbox-stop: | ||
| @docker stop skillsphere-sandbox 2>/dev/null || true | ||
|
|
||
| sandbox-logs: | ||
| @docker logs -f skillsphere-sandbox |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| //! # IBC Cross-Chain Bridge (Issue #217) | ||
| //! | ||
| //! Enables cross-chain USDC payments from Ethereum/Polygon via Axelar. | ||
| //! Users can pay with USDC on EVM chains and receive equivalent tokens on Stellar. | ||
|
|
||
| #![allow(unused_imports)] | ||
|
|
||
| use soroban_sdk::{ | ||
| contract, contractimpl, contracterror, contracttype, symbol_short, Address, Env, String, BytesN, | ||
| }; | ||
|
|
||
| #[contracterror] | ||
| #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
| #[repr(u32)] | ||
| pub enum BridgeError { | ||
| Unauthorized = 1, | ||
| ReplayDetected = 2, | ||
| InvalidChain = 3, | ||
| InsufficientBalance = 4, | ||
| } | ||
|
|
||
| #[contracttype] | ||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct BridgeMessage { | ||
| pub source_chain: String, | ||
| pub source_address: String, | ||
| pub token: Address, | ||
| pub amount: i128, | ||
| pub recipient: Address, | ||
| pub nonce: u128, | ||
| } | ||
|
|
||
| #[contracttype] | ||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct RefundRequest { | ||
| pub source_chain: String, | ||
| pub source_address: String, | ||
| pub token: Address, | ||
| pub amount: i128, | ||
| pub requested_at: u64, | ||
| } | ||
|
|
||
| #[contract] | ||
| pub struct BridgeContract; | ||
|
|
||
| #[contractimpl] | ||
| impl BridgeContract { | ||
| /// Validates and processes an incoming bridge message from Axelar. | ||
| /// | ||
| /// # Arguments | ||
| /// * `env` — The Soroban environment | ||
| /// * `msg` — The bridge message containing payment details | ||
| /// | ||
| /// # Errors | ||
| /// * `BridgeError::Unauthorized` — If sender is not the authorized relayer | ||
| /// * `BridgeError::ReplayDetected` — If the nonce has already been used | ||
| /// * `BridgeError::InvalidChain` — If source chain is not supported | ||
| /// | ||
| /// # Events | ||
| /// * Emits `bridge_message_received` with (source_chain, recipient, token, amount, nonce) | ||
| pub fn receive_bridge_message(env: Env, msg: BridgeMessage) -> Result<(), BridgeError> { | ||
| // TODO: Validate sender is authorized Axelar relayer address | ||
| // let relayer = env | ||
| // .storage() | ||
| // .instance() | ||
| // .get(&DataKey::BridgeRelayer) | ||
| // .ok_or(BridgeError::Unauthorized)?; | ||
| // if env.invoker_contract() != relayer { | ||
| // return Err(BridgeError::Unauthorized); | ||
| // } | ||
|
|
||
| // TODO: Check nonce for replay protection | ||
| // let nonce_key = DataKey::BridgeNonce(msg.source_chain.clone(), msg.source_address.clone(), msg.nonce); | ||
| // if env.storage().persistent().has(&nonce_key) { | ||
| // return Err(BridgeError::ReplayDetected); | ||
| // } | ||
|
|
||
| // TODO: Validate source chain is whitelisted | ||
| // let supported_chains: Vec<String> = env.storage().instance().get(&DataKey::SupportedChains).unwrap_or(Vec::new(&env)); | ||
| // if !supported_chains.contains(&msg.source_chain) { | ||
| // return Err(BridgeError::InvalidChain); | ||
| // } | ||
|
|
||
| todo!() | ||
| } | ||
|
|
||
| /// Initiates an outbound bridge transfer to another chain. | ||
| /// | ||
| /// # Arguments | ||
| /// * `env` — The Soroban environment | ||
| /// * `destination_chain` — Target chain name (e.g., "Ethereum", "Polygon") | ||
| /// * `recipient` — Destination address on the target chain | ||
| /// * `amount` — Amount of tokens to bridge | ||
| /// | ||
| /// # Errors | ||
| /// * `BridgeError::Unauthorized` — If caller is not authorized | ||
| /// * `BridgeError::InsufficientBalance` — If contract lacks sufficient tokens | ||
| /// | ||
| /// # Events | ||
| /// * Emits `bridge_out_initiated` with (destination_chain, recipient, amount) | ||
| pub fn initiate_bridge_out( | ||
| env: Env, | ||
| destination_chain: String, | ||
| recipient: String, | ||
| amount: i128, | ||
| ) -> Result<(), BridgeError> { | ||
| // TODO: Validate caller is authorized (could be admin or any user with allowance) | ||
|
|
||
| // TODO: Lock/burn tokens held by the contract | ||
| // let token = env.current_contract_address(); // or separate token address | ||
| // let token_client = token::Client::new(&env, &token); | ||
| // if token_client.balance(&env.current_contract_address()) < amount { | ||
| // return Err(BridgeError::InsufficientBalance); | ||
| // } | ||
|
|
||
| // TODO: Call Axelar gateway to initiate cross-chain transfer | ||
|
|
||
| todo!() | ||
| } | ||
|
|
||
| /// Requests a refund for a failed bridge message. | ||
| /// | ||
| /// # Arguments | ||
| /// * `env` — The Soroban environment | ||
| /// * `source_chain` — The original source chain | ||
| /// * `source_address` — The original sender address | ||
| /// * `nonce` — The nonce from the original bridge message | ||
| /// | ||
| /// # Errors | ||
| /// * `BridgeError::InvalidChain` — If refund request is invalid | ||
| /// | ||
| /// # Events | ||
| /// * Emits `bridge_refund` with (source_chain, source_address, nonce, amount) | ||
| pub fn request_refund( | ||
| env: Env, | ||
| source_chain: String, | ||
| source_address: String, | ||
| nonce: u128, | ||
| ) -> Result<i128, BridgeError> { | ||
| todo!() | ||
| } | ||
|
|
||
| /// Sets the authorized Axelar relayer address (admin only). | ||
| /// | ||
| /// # Arguments | ||
| /// * `env` — The Soroban environment | ||
| /// * `relayer` — The new relayer address | ||
| /// | ||
| /// # Events | ||
| /// * Emits `relayer_updated` with the new relayer address | ||
| pub fn set_relayer(env: Env, relayer: Address) -> Result<(), BridgeError> { | ||
| todo!() | ||
| } | ||
|
|
||
| /// Gets the current authorized relayer address. | ||
| pub fn get_relayer(env: Env) -> Option<Address> { | ||
| todo!() | ||
| } | ||
|
|
||
| /// Checks if an incoming bridge message has already been processed. | ||
| pub fn is_nonce_used(env: Env, source_chain: String, source_address: String, nonce: u128) -> bool { | ||
| todo!() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
| use soroban_sdk::testutils::Address as _; | ||
| use soroban_sdk::{Address, Env, String}; | ||
|
|
||
| #[test] | ||
| fn test_bridge_message_structure() { | ||
| let env = Env::default(); | ||
| let msg = BridgeMessage { | ||
| source_chain: String::from_str(&env, "Ethereum"), | ||
| source_address: String::from_str(&env, "0x1234...abcd"), | ||
| token: Address::generate(&env), | ||
| amount: 1000, | ||
| recipient: Address::generate(&env), | ||
| nonce: 1, | ||
| }; | ||
| assert_eq!(msg.amount, 1000); | ||
| assert_eq!(msg.nonce, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_refund_request_structure() { | ||
| let env = Env::default(); | ||
| let req = RefundRequest { | ||
| source_chain: String::from_str(&env, "Ethereum"), | ||
| source_address: String::from_str(&env, "0x1234...abcd"), | ||
| token: Address::generate(&env), | ||
| amount: 1000, | ||
| requested_at: 12345, | ||
| }; | ||
| assert_eq!(req.amount, 1000); | ||
| assert_eq!(req.requested_at, 12345); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bridge_error_values() { | ||
| assert_eq!(BridgeError::Unauthorized as u32, 1); | ||
| assert_eq!(BridgeError::ReplayDetected as u32, 2); | ||
| assert_eq!(BridgeError::InvalidChain as u32, 3); | ||
| assert_eq!(BridgeError::InsufficientBalance as u32, 4); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return a typed contract error from the bridge stubs instead of panicking.
All public entry points currently abort via
todo!(), so callers get an opaque trap rather than aBridgeError. Since this module is already exported, that makes the bridge API look supported while every invocation hard-fails at runtime. Either hide the module until it is implemented, or add a temporaryNotImplementedvariant and return that explicitly from each stub.Proposed stopgap
pub enum BridgeError { Unauthorized = 1, ReplayDetected = 2, InvalidChain = 3, InsufficientBalance = 4, + NotImplemented = 5, } ... - todo!() + Err(BridgeError::NotImplemented)Also applies to: 101-119, 134-163
🤖 Prompt for AI Agents