Avalanche Pseudo-Delta-Neutral Leveraged Farming

This guide is intended for developers who wish to interact with Aperture contracts directly on Avalanche. Regular users are advised to use the official Aperture web app.

Please consult the Deployed Contracts page for contract addresses on various networks.

Query Position IDs For Wallet Address

Aperture uses position id to keep track of users' investment position. To get a list of position ids for an address, we need to first query Aperture Manager (see Deployed Contracts for contract address).

const managerABI =
    ["function getPositionsExt(address) view returns (tuple(uint128,uint16,uint64)[])"];
const apertureManager = new ethers.Contract(addr, managerABI, provider);
// `getPositionsExt` returns a list of tuple of:
//    uint128 positionId -> This is the position id.
//    uint16 chainId -> Used for cross chain communication. Likely not relevant for partner integration.
//    uint64 strategyId -> The strategy id (a.k.a. vault id) to identify which
//        strategy a position belongs to.
const positions = await apertureManager.getPositionsExt(ownerAddr);

Once we have position ids and strategy ids from Aperture Manager contract, we are ready to query individual vault to get detailed information for each position.

First step is to look up which vault to query against by using strategy id (see Deployed Contracts for mapping). Next, look up position value expressed in AVAX value and convert it to be denominated by the input token of the vault.

const vaultABI = [
    "function vaultState() view returns (uint256, uint256)",
    "function getEquityETHValue() view returns (uint256)",
    "function pendingRewardStable() view returns (uint256)",
    "function positions(uint16,uint128) view returns (uint256)"
    "function pairInfo() view returns (address, address, address, address)"];

const AVAXChainId = 6; // Aperture uses Wormhole chain id.
// `getETHPx` returns the price of a token in the native token. For Ethereum, it would be
// ether. For Avalanche, it would be AVAX token. The final price is scaled to 2^112.
const oracleABI = ["function getETHPx(address) view returns (uint256)"];
const vault = new ethers.Contract(addr, vaultABI, provider);
const oracle = new ethers.Contract(addr, oracleABI, provider);
// 2^112, a very large number used by oracle.
const multiplier = BigNumber.from(2).pow(112);

// Get total shares from the vault.
const vaultShare = (await vault.vaultState())[0]; // The first item is total vault shares.
// Get how many shares a position owns.
const positionShare = await vault.position(AVAXChainId, positionId);
// This is the token address user deposits into the vault.
const mainTokenAddr = (await vault.pairInfo())[0]; // First item is the main token address.

// Retrieve vault equity expressed in AVAX token. Note that this is different from TVL of
// the vault. TVL includes leverage, however, equity is (total_position_worth - liability).
// For example, equity of $1 is equivalent to $1 * leverage in terms of TVL of the vault.
const equityETH = await vault.getEquityETHValue();

// Convert equity to be dominated in the main vault token.
const mainTokenETHPrice = await oracle.getETHPx(mainTokenAddr);
const pendingRewards = await vault.pendingRewardStable();

// Vault's equity expressed in main token.
const equityMainTotal = equityETH.mul(multiplier).div(mainTokenETHPrice).add(pendingRewards);
const equityMain = equityMainTotal.mul(positionShare).div(vaultShare);
// Compute the final position value expressed in the main vault token.
const result = ethers.utils.formatUnits(equityMain, mainTokenDecimal);

Last updated