Tenzro
Tutorial — DeFi

Build a portfolio rebalancer

A rebalancer reads your positions across SVM and EVM, computes the drift from target weights, and dispatches the smallest set of trades and bridges to restore the target.
Level
Advanced
Time
~45 min
Prerequisites
Holdings on multiple chains
Stack
TypeScript
01

Define the target allocation

Specify weights and a rebalance band.

const target = { ETH: 0.4, SOL: 0.3, USDC: 0.2, TNZO: 0.1 };
const band = 0.05;
02

Pull positions from each chain

The token registry on Tenzro indexes wrapped representations across VMs.

const tokens = await client.token.listTokens();
const positions = await Promise.all(
  tokens.tokens.map((t) => client.token.getTokenBalance(walletAddress, t.symbol)),
);
03

Compute trades

Use a minimal-trade solver — overshoot the band, not just touch it.

const trades = computeRebalanceTrades(positions, target, band);
04

Dispatch through the bridge router

Each trade picks the cheapest route via the bridge router.

for (const t of trades) {
  const routes = await client.bridge().getRoutes(t.from, t.to, t.asset);
  const best = routes[0];
  await client.bridge().bridgeTokens(t.from, t.to, t.asset, t.amount, recipient, best.adapter);
}
Related
← All tutorials