Cross-Border Trade Settlement

What it does:
Automates cross-border payments, currency conversions, and settlement between international buyers and suppliers on-chain.

Why it matters:
Reduces delays, eliminates intermediaries, minimizes FX fees, ensures transparent and auditable settlement, and mitigates settlement risks in global trade.

How it works:

  • Buyers deposit payment in their preferred currency or stablecoin

  • Smart contracts handle currency conversion via integrated on-chain oracles

  • Payments are released to suppliers automatically once delivery or contractual milestones are verified

  • Supports partial payments, escrow, and milestone-based settlements

  • Integrates with Just-In-Time Payment Contracts, Supply Chain Traceability, and Automated Trade Finance

  • Transaction history and settlements are publicly auditable

  • Dashboards show payment status, exchange rates applied, and pending settlements

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @title CrossBorderSettlement
 * @author Nam
 * @notice Automates cross-border payments and settlements on-chain
 */
contract CrossBorderSettlement is Ownable {

    struct Trade {
        address payable buyer;
        address payable supplier;
        uint256 amount; // in stablecoin or wei
        string currency; // e.g., "USD", "EUR"
        bool delivered;
        bool paid;
    }

    mapping(uint256 => Trade) public trades;
    uint256 public tradeCount;

    mapping(address => bool) public authorizedVerifiers;
    mapping(string => uint256) public exchangeRates; // e.g., "USD" => rate in wei

    // -------------------- EVENTS --------------------

    event TradeCreated(uint256 indexed tradeId, address buyer, address supplier, uint256 amount, string currency);
    event DeliveryConfirmed(uint256 indexed tradeId);
    event PaymentReleased(uint256 indexed tradeId, uint256 amount, string currency);
    event VerifierApproved(address verifier);
    event ExchangeRateUpdated(string currency, uint256 rate);

    // -------------------- VERIFIER MANAGEMENT --------------------

    function approveVerifier(address _verifier) external onlyOwner {
        authorizedVerifiers[_verifier] = true;
        emit VerifierApproved(_verifier);
    }

    function revokeVerifier(address _verifier) external onlyOwner {
        authorizedVerifiers[_verifier] = false;
    }

    modifier onlyVerifier() {
        require(authorizedVerifiers[msg.sender], "Not authorized verifier");
        _;
    }

    // -------------------- EXCHANGE RATE MANAGEMENT --------------------

    function setExchangeRate(string calldata _currency, uint256 _rate) external onlyOwner {
        exchangeRates[_currency] = _rate;
        emit ExchangeRateUpdated(_currency, _rate);
    }

    // -------------------- TRADE MANAGEMENT --------------------

    function createTrade(address payable _supplier, string calldata _currency) external payable {
        require(msg.value > 0, "Payment required");
        require(exchangeRates[_currency] > 0, "Exchange rate not set");

        tradeCount += 1;
        trades[tradeCount] = Trade({
            buyer: payable(msg.sender),
            supplier: _supplier,
            amount: msg.value,
            currency: _currency,
            delivered: false,
            paid: false
        });

        emit TradeCreated(tradeCount, msg.sender, _supplier, msg.value, _currency);
    }

    function confirmDelivery(uint256 _tradeId) external onlyVerifier {
        Trade storage t = trades[_tradeId];
        require(!t.delivered, "Already confirmed");

        t.delivered = true;
        emit DeliveryConfirmed(_tradeId);

        // Automatically release payment
        if (!t.paid) {
            t.paid = true;
            t.supplier.transfer(t.amount); // In practice, integrate token transfer for stablecoins
            emit PaymentReleased(_tradeId, t.amount, t.currency);
        }
    }

    // -------------------- VIEW FUNCTIONS --------------------

    function isTradeSettled(uint256 _tradeId) external view returns (bool) {
        return trades[_tradeId].paid;
    }

    function getTradeDetails(uint256 _tradeId) external view returns (Trade memory) {
        return trades[_tradeId];
    }

    // -------------------- FUNDING --------------------

    receive() external payable {}
}