Page cover

Example: Using Cross-Chain Bridges

Example: Using Cross-Chain Bridges

JuliaOS includes real implementations of multiple cross-chain bridge protocols (Wormhole, Axelar, LayerZero) for transferring assets between different blockchain networks. Here's how to use them:

# Using the CLI
./scripts/run-cli.sh  # or node packages/cli/interactive.cjs

# Select "🌉 Bridge Operations" from the menu
# Select "Bridge Tokens" from the submenu
# Choose a bridge protocol (Wormhole, Axelar, LayerZero)
# Follow the prompts to specify source chain, target chain, token, amount, and recipient
import asyncio
from juliaos import JuliaOS

async def bridge_tokens():
    # Initialize JuliaOS
    juliaos_client = JuliaOS(host="localhost", port=8052)
    await juliaos_client.connect()

    # Get supported bridges
    bridges = await juliaos_client.bridges.get_supported_bridges()
    print(f"Supported bridges: {bridges}")

    # Get supported chains for Wormhole
    chains = await juliaos_client.bridges.get_supported_chains("wormhole")
    print(f"Supported chains for Wormhole: {chains}")

    # Get supported tokens for a chain pair
    tokens = await juliaos_client.bridges.get_supported_tokens(
        bridge="wormhole",
        source_chain="ethereum",
        destination_chain="solana"
    )
    print(f"Supported tokens: {tokens}")

    # Estimate fee for a transfer
    fee = await juliaos_client.bridges.estimate_fee(
        bridge="wormhole",
        source_chain="ethereum",
        destination_chain="solana",
        token="USDC",
        amount="10.0"
    )
    print(f"Estimated fee: {fee}")

    # Create a wallet for the source chain
    wallet = await juliaos_client.wallets.create_wallet(
        name="Bridge Wallet",
        wallet_type="local",
        network="ethereum"
    )

    # Bridge tokens from Ethereum to Solana
    result = await juliaos_client.bridges.bridge_tokens(
        bridge="wormhole",
        source_chain="ethereum",
        destination_chain="solana",
        token="USDC",
        amount="10.0",
        recipient="9ywX...",  # Solana recipient address
        wallet_id=wallet["id"]
    )

    print(f"Bridge transaction initiated: {result['transaction_hash']}")

    # Check transaction status
    status = await juliaos_client.bridges.check_transaction_status(
        bridge="wormhole",
        source_chain="ethereum",
        transaction_hash=result["transaction_hash"]
    )

    print(f"Transaction status: {status['status']}")

    # Get transaction history
    history = await juliaos_client.bridges.get_transaction_history(
        wallet_id=wallet["id"],
        limit=10
    )

    print(f"Transaction history: {len(history['transactions'])} transactions found")

    await juliaos_client.disconnect()

# Run the bridge operation
asyncio.run(bridge_tokens())
# Using Julia directly
using JuliaOS.Bridge

# Get supported bridges
bridges = Bridge.get_supported_bridges()
println("Supported bridges: ", bridges)

# Get supported chains for Wormhole
chains = Bridge.get_supported_chains("wormhole")
println("Supported chains for Wormhole: ", chains)

# Create a wallet for the source chain
wallet = JuliaOS.Wallet.create_wallet(
    "Bridge Wallet",
    "local",
    "ethereum"
)

# Bridge tokens from Ethereum to Solana using Wormhole
result = Bridge.bridge_tokens(
    "wormhole",
    "ethereum",
    "solana",
    "USDC",
    "10.0",
    Dict(
        "recipient" => "9ywX...",  # Solana recipient address
        "wallet_id" => wallet["id"]
    )
)

# Check transaction status
status = Bridge.check_transaction_status(
    "wormhole",
    "ethereum",
    result["transaction_hash"]
)

println("Transaction status: ", status["status"])

Last updated