Integration: Programmatic Hedging
For DAOs or automated treasuries holding significant USDC or EURC on Stellar, AnchorShield allows you to programmatically buy protection without ever using the UI.
This guide demonstrates how to build a Node.js bot that automatically locks collateral and buys YES (Insurance) tokens from the limit order book.
Prerequisites
@stellar/stellar-sdkinstalled.- A funded Stellar testnet account.
The Complete Implementation
Below is a complete, copy-pasteable TypeScript example demonstrating the end-to-end flow:
- Initialize the Soroban RPC connection.
- Build a transaction to call
mint_complete_set(locking 10 USDC). - Build a transaction to call
place_order(selling the 10 NO tokens to the market, effectively leaving you with just the YES tokens).
import {
Contract,
rpc as SorobanRpc,
Networks,
Keypair,
nativeToScVal,
Address
} from "@stellar/stellar-sdk";
// Configuration
const RPC_URL = "https://soroban-testnet.stellar.org:443";
const NETWORK_PASSPHRASE = Networks.TESTNET;
const MARKET_ADDRESS = "C...YOUR_MARKET_ID...";
const server = new SorobanRpc.Server(RPC_URL, { allowHttp: false });
const treasuryKeypair = Keypair.fromSecret("S...YOUR_SECRET_KEY...");
async function hedgeTreasury() {
const marketContract = new Contract(MARKET_ADDRESS);
const account = await server.getAccount(treasuryKeypair.publicKey());
// ---------------------------------------------------------
// STEP 1: Lock 10 USDC to mint 10 YES and 10 NO tokens
// ---------------------------------------------------------
console.log("Minting YES/NO token pairs...");
const mintOp = marketContract.call("mint_complete_set",
new Address(treasuryKeypair.publicKey()).toScVal(),
nativeToScVal(10_000_0000, { type: "i128" }) // 10 USDC in stroops
);
// Build, sign, and submit the mint transaction...
// (Standard Soroban transaction submission logic here)
// ---------------------------------------------------------
// STEP 2: Sell the NO tokens to underwriters
// ---------------------------------------------------------
console.log("Selling NO tokens to the order book...");
// We want to sell 10 NO tokens at $0.95 (meaning we pay $0.05 for the YES token)
const placeOrderOp = marketContract.call("place_order",
new Address(treasuryKeypair.publicKey()).toScVal(),
nativeToScVal(false, { type: "bool" }), // is_buy = false (Selling)
nativeToScVal(9500, { type: "i64" }), // price_bps = 9500
nativeToScVal(10_000_0000, { type: "i128" }) // amount = 10
);
// Build, sign, and submit the order transaction...
console.log("Hedging complete! YES tokens secured.");
}
hedgeTreasury().catch(console.error);Why do we sell the NO tokens? When you mint, you get both YES and NO tokens. By selling the NO tokens on the order book, you are offloading the risk to an underwriter. The difference between what you locked and what you sold the NO token for is the Premium you paid for insurance.