> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cross-chain treasury rebalancing

> Build a service that watches per-chain utilization and publishes Routes intents to rebalance USDC across chains, with cryptographic execution guarantees.

This recipe builds a server-side rebalancer that programmatically moves treasury USDC between chains based on a threshold (e.g. utilization, yield differential, opportunity).

## When to use

* Multi-chain protocols managing distributed treasury
* Yield managers chasing the highest opportunity across chains
* Operational treasuries that want to remove manual rebalancing

## 1. Define the trigger

```typescript theme={null}
interface RebalancePolicy {
  sourceChain: number;
  destChain: number;
  token: string;            // USDC on dest
  threshold: bigint;        // Trigger amount
  minTransfer: bigint;
}

async function shouldRebalance(p: RebalancePolicy): Promise<bigint | null> {
  const sourceBal = await getBalance(p.sourceChain);
  const destBal = await getBalance(p.destChain);
  const delta = sourceBal - destBal;
  if (delta < p.threshold) return null;
  return delta / 2n;        // Move half the gap
}
```

## 2. Get a quote for the rebalance

A treasury rebalance is a simple transfer between two of your wallets. Use the V3 quote API with the source treasury as `funder` and the destination treasury as `recipient`.

```typescript theme={null}
const res = await fetch('https://quotes.eco.com/api/v3/quotes/single', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    dAppID: 'treasury-rebalancer',
    quoteRequest: {
      sourceChainID: policy.sourceChain,
      destinationChainID: policy.destChain,
      sourceToken: policy.sourceToken,
      destinationToken: policy.destToken,
      sourceAmount: amount.toString(),
      funder: SOURCE_TREASURY,
      recipient: DEST_TREASURY,
    },
  }),
});
const quote = await res.json();
```

## 3. Approve, publish, fund

Approve the reward token to `quote.data.contracts.sourcePortal`, then call `publishAndFund(destinationChainID, encodedRoute, reward, false)`, same exact pattern as [Sending USDC across chains](/recipes/send-usdc-cross-chain).

## 4. Run on a schedule

```typescript theme={null}
async function tick() {
  for (const policy of POLICIES) {
    const amount = await shouldRebalance(policy);
    if (amount && amount > policy.minTransfer) {
      await publishRebalance(policy, amount);
    }
  }
}

setInterval(tick, 60_000);
```

You've successfully built an atomic, auditable cross-chain rebalancer.
