Files
hanzo.ai/app/(marketing)/blockchain/defi/page.tsx
zeekay 11265a21fd Highlight every code sample with shiki in the code theme
Code on hanzo.ai was plain monospace: 285 blocks across 200 pages, one of
them hand-inked in four greys. components/code now renders each sample
at build with shiki and @hanzo/ui's codeTheme (Dracula dark, GitHub
Light light), every colour written as light-dark() so the page's
color-scheme picks it, on the theme's own ground. The page ships the
coloured spans and no highlighter; e2e/gates/code.spec.ts holds both.

A client page that shows code is split into a server page.tsx that owns
the sample and a client view.tsx that takes the highlighted node:
ProductLanding's code.source, the blockchain pages' codeExamples, the
models kit Sample, the guest-key card, realtime, analytics, payments,
sdks and the rest. Transcripts, agent prompts and ASCII diagrams are not
code and keep their markup.

Takes @hanzo/ui 8.27.83 for codeTheme and @hanzo/design 0.5.35 with it,
regenerates app/gui.css for the new theme keys, and lowers the raw-colour
allowance to 58: the samples' literal inks are gone.
2026-10-05 01:31:58 -07:00

349 lines
8.9 KiB
TypeScript

import { sample } from '@/components/blockchain/sample'
import HanzoDeFi from './view'
const EXAMPLES = [
{
language: "Solidity",
filename: "YieldVault.sol",
code: `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@hanzo/defi/IHanzoRouter.sol";
import "@hanzo/defi/IHanzoStaking.sol";
import "@hanzo/defi/IHanzoLending.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract YieldOptimizer {
IHanzoRouter public router;
IHanzoStaking public staking;
IHanzoLending public lending;
constructor(address _router, address _staking, address _lending) {
router = IHanzoRouter(_router);
staking = IHanzoStaking(_staking);
lending = IHanzoLending(_lending);
}
// Swap tokens using best route
function swap(
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256 minAmountOut
) external returns (uint256 amountOut) {
IERC20(tokenIn).transferFrom(msg.sender, address(this), amountIn);
IERC20(tokenIn).approve(address(router), amountIn);
amountOut = router.swapExactTokensForTokens(
tokenIn, tokenOut, amountIn, minAmountOut, msg.sender
);
}
// Stake tokens for yield
function stake(address pool, uint256 amount, uint256 lockPeriod) external {
IERC20(staking.stakingToken(pool)).transferFrom(msg.sender, address(this), amount);
staking.stake(pool, amount, lockPeriod, msg.sender);
}
// Supply to lending market
function supply(address market, uint256 amount) external {
address asset = lending.marketAsset(market);
IERC20(asset).transferFrom(msg.sender, address(this), amount);
IERC20(asset).approve(address(lending), amount);
lending.supply(market, amount, msg.sender);
}
// Get best yield across protocols
function getBestYield(address token) external view returns (
uint256 stakingAPY,
uint256 lendingAPY
) {
stakingAPY = staking.getAPY(token);
lendingAPY = lending.getSupplyRate(token);
}
}`,
},
{
language: "Node",
filename: "defi.ts",
code: `import { HanzoDeFi } from "@hanzo/blockchain";
const defi = new HanzoDeFi({
apiKey: process.env.HANZO_API_KEY,
});
// Get best swap quote across DEXs
const quote = await defi.swap.getQuote({
fromToken: "ETH",
toToken: "USDC",
amount: "1000000000000000000", // 1 ETH
chainId: 1,
});
// Execute gasless swap (user pays in USDC)
const tx = await defi.swap.execute({
quote,
wallet: userWallet,
gasless: true,
});
// Stake tokens with lock period
await defi.staking.stake({
pool: "0x...",
amount: "1000000000000000000",
lockPeriod: 30 * 24 * 60 * 60, // 30 days
wallet: userWallet,
});
// Get user's DeFi positions across protocols
const positions = await defi.portfolio.getPositions({
address: "0x...",
protocols: ["aave", "compound", "uniswap"],
});`,
},
{
language: "Python",
filename: "defi.py",
code: `from hanzo.blockchain import DeFi
import os
defi = DeFi(api_key=os.environ["HANZO_API_KEY"])
# Get best swap quote across DEXs
quote = defi.swap.get_quote(
from_token="ETH",
to_token="USDC",
amount="1000000000000000000", # 1 ETH
chain_id=1,
)
# Execute gasless swap (user pays in USDC)
tx = defi.swap.execute(
quote=quote,
wallet=user_wallet,
gasless=True,
)
# Stake tokens with lock period
defi.staking.stake(
pool="0x...",
amount="1000000000000000000",
lock_period=30 * 24 * 60 * 60, # 30 days
wallet=user_wallet,
)
# Get user's DeFi positions across protocols
positions = defi.portfolio.get_positions(
address="0x...",
protocols=["aave", "compound", "uniswap"],
)`,
},
{
language: "Go",
filename: "defi.go",
code: `package main
import (
"os"
"github.com/hanzoai/blockchain-go/defi"
)
func main() {
client := defi.NewClient(os.Getenv("HANZO_API_KEY"))
// Get best swap quote across DEXs
quote, _ := client.Swap.GetQuote(&defi.QuoteRequest{
FromToken: "ETH",
ToToken: "USDC",
Amount: "1000000000000000000", // 1 ETH
ChainID: 1,
})
// Execute gasless swap (user pays in USDC)
tx, _ := client.Swap.Execute(&defi.ExecuteRequest{
Quote: quote,
Wallet: userWallet,
Gasless: true,
})
// Stake tokens with lock period
client.Staking.Stake(&defi.StakeRequest{
Pool: "0x...",
Amount: "1000000000000000000",
LockPeriod: 30 * 24 * 60 * 60, // 30 days
Wallet: userWallet,
})
// Get user's DeFi positions across protocols
positions, _ := client.Portfolio.GetPositions(&defi.PositionsRequest{
Address: "0x...",
Protocols: []string{"aave", "compound", "uniswap"},
})
}`,
},
{
language: "Rust",
filename: "defi.rs",
code: `use hanzo_blockchain::defi::{DeFi, QuoteRequest, StakeRequest};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = DeFi::new(&env::var("HANZO_API_KEY")?);
// Get best swap quote across DEXs
let quote = client.swap.get_quote(QuoteRequest {
from_token: "ETH".into(),
to_token: "USDC".into(),
amount: "1000000000000000000".into(), // 1 ETH
chain_id: 1,
}).await?;
// Execute gasless swap (user pays in USDC)
let tx = client.swap.execute(quote, user_wallet, true).await?;
// Stake tokens with lock period
client.staking.stake(StakeRequest {
pool: "0x...".into(),
amount: "1000000000000000000".into(),
lock_period: 30 * 24 * 60 * 60, // 30 days
wallet: user_wallet,
}).await?;
// Get user's DeFi positions across protocols
let positions = client.portfolio.get_positions(
"0x...",
vec!["aave", "compound", "uniswap"],
).await?;
Ok(())
}`,
},
{
language: "C",
filename: "defi.c",
code: `#include <hanzo/blockchain/defi.h>
#include <stdlib.h>
int main() {
hanzo_defi_t *defi = hanzo_defi_new(getenv("HANZO_API_KEY"));
// Get best swap quote across DEXs
hanzo_quote_t *quote = hanzo_swap_get_quote(defi,
"ETH", // from_token
"USDC", // to_token
"1000000000000000000", // amount (1 ETH)
1 // chain_id
);
// Execute gasless swap (user pays in USDC)
hanzo_tx_t *tx = hanzo_swap_execute(defi,
quote,
user_wallet,
1 // gasless = true
);
// Stake tokens with lock period
hanzo_staking_stake(defi,
"0x...", // pool
"1000000000000000000", // amount
30 * 24 * 60 * 60, // lock_period (30 days)
user_wallet
);
// Get user's DeFi positions across protocols
const char *protocols[] = {"aave", "compound", "uniswap", NULL};
hanzo_positions_t *positions = hanzo_portfolio_get_positions(
defi, "0x...", protocols
);
hanzo_defi_free(defi);
return 0;
}`,
},
{
language: "C++",
filename: "defi.cpp",
code: `#include <hanzo/blockchain/defi.hpp>
#include <cstdlib>
int main() {
auto defi = hanzo::DeFi(std::getenv("HANZO_API_KEY"));
// Get best swap quote across DEXs
auto quote = defi.swap.get_quote({
.from_token = "ETH",
.to_token = "USDC",
.amount = "1000000000000000000", // 1 ETH
.chain_id = 1,
});
// Execute gasless swap (user pays in USDC)
auto tx = defi.swap.execute({
.quote = quote,
.wallet = user_wallet,
.gasless = true,
});
// Stake tokens with lock period
defi.staking.stake({
.pool = "0x...",
.amount = "1000000000000000000",
.lock_period = 30 * 24 * 60 * 60, // 30 days
.wallet = user_wallet,
});
// Get user's DeFi positions across protocols
auto positions = defi.portfolio.get_positions(
"0x...",
{"aave", "compound", "uniswap"}
);
return 0;
}`,
},
{
language: "Ruby",
filename: "defi.rb",
code: `require 'hanzo/blockchain'
defi = Hanzo::Blockchain::DeFi.new(
api_key: ENV['HANZO_API_KEY']
)
# Get best swap quote across DEXs
quote = defi.swap.get_quote(
from_token: 'ETH',
to_token: 'USDC',
amount: '1000000000000000000', # 1 ETH
chain_id: 1
)
# Execute gasless swap (user pays in USDC)
tx = defi.swap.execute(
quote: quote,
wallet: user_wallet,
gasless: true
)
# Stake tokens with lock period
defi.staking.stake(
pool: '0x...',
amount: '1000000000000000000',
lock_period: 30 * 24 * 60 * 60, # 30 days
wallet: user_wallet
)
# Get user's DeFi positions across protocols
positions = defi.portfolio.get_positions(
address: '0x...',
protocols: %w[aave compound uniswap]
)`,
},
]
/** The page, with every language's sample highlighted where it is built (components/code). */
export default function Page() {
return <HanzoDeFi codeExamples={EXAMPLES.map(sample)} />
}