AMM

Calculating Derivative and LP Tokens Prices

Requires pool address and ABI available in CompliFi github repositories; ethers js library is employed to make requests to Ethereum blockchain.

const COMPLIFI_POOL_ABI = [...]; // CompliFi Pool ABI https://github.com/CompliFi/complifi-amm/blob/master/abis/1/Pool.json
const COMPLIFI_VAULT_ABI = [...]; // CompliFi Vault ABI https://github.com/CompliFi/complifi-protocol/blob/master/abis/1/Vault.json
const POOL_ADDRESS = ['0x3eAd43ca1ED431446841e75400215937E2a91Acc']; // BTCx5 June 2021 Pool
// initializing a pool
const pool = new ethers.Contract(poolInfo.address, COMPLIFI_POOL_ABI)
// getting a vault address
const vaultAddress = await pool.derivativeVault();
// initializing the vault
const vault = new ethers.Contract(vaultAddress, COMPLIFI_VAULT_ABI);
// getting primary (UP) derivative token
const primaryAddress = await vault.primaryToken();
// complement (DOWN) derivative token
const complementAddress = await vault.complementToken();
// current primary token leverage coefficient in the pool
const primaryLeverage = (await pool.getLeverage(primaryAddress)) / 10 ** 18;
// current primary token balance in the pool
const primaryAmount = (await pool.getBalance(primaryAddress)) / 10 ** 6;
// current complement token leverage coefficient in the pool
const complementLeverage = (await pool.getLeverage(complementAddress)) / 10 ** 18;
// current complement token balance in the pool
const complementAmount = (await pool.getBalance(complementAddress)) / 10 ** 6;
// derivative denomination can be set as a constant under current implementation (=2 for all x5), but we can also be taken from vault's derivative specification
const derivativeDenomination = 2;
// current LP tokens total supply
const poolTotalSupply = (await pool.totalSupply()) / 10 ** 18;
// calculating leveraged balance for primary token
const primaryLeveragedBalance = primaryLeverage * primaryAmount;
// calculating leveraged balance for complement tokens
const complementLeveragedBalance = complementLeverage * complementAmount;
// calculating sum of leveraged balances divided by denomination
const sumOfLeveragedBalances =
(primaryLeveragedBalance + complementLeveragedBalance) /
derivativeDenomination;
// calculating primary token spot price
const primaryPrice = complementLeveragedBalance / sumOfLeveragedBalances;
// calculating complement token spot price
const complementPrice = primaryLeveragedBalance / sumOfLeveragedBalances;
// calculating pool token(LP) spot price
const lpTokenPrice = (primaryAmount / poolTotalSupply) * primaryPrice +
(complementAmount / poolTotalSupply) * complementPrice;

Reading Data With View Contract

/// @title Reading key data from specified derivative trading Pool
contract PoolView {
/// @notice Contains key information about a derivative token
struct TokenRecord {
address self;
uint256 balance;
uint256 leverage;
uint8 decimals;
uint256 userBalance;
}
/// @notice Contains key information about arbitrary ERC20 token
struct Token {
address self;
uint256 totalSupply;
uint8 decimals;
uint256 userBalance;
}
/// @notice Contains key information about a Pool's configuration
/// @dev For the x5 leveraged token, repricerParam1 and
/// repricerParam2 are the implied volatility settings for the long and
/// short call options respectively
struct Config {
address derivativeVault;
address dynamicFee;
address repricer;
uint256 exposureLimitPrimary;
uint256 exposureLimitComplement;
uint256 repricerParam1;
uint256 repricerParam2;
uint256 pMin;
uint256 qMin;
uint8 qMinDecimals;
uint256 baseFee;
uint256 maxFee;
uint256 feeAmpPrimary;
uint256 feeAmpComplement;
uint8 decimals;
bool isPaused;
}
/// @notice Getting information about Pool configuration, it's derivative and pool(LP) tokens
/// @param _pool the vault address
/// @return primary pool's primary token metadata
/// @return complement pool' complement token metadata
/// @return poolToken pool's own token metadata
/// @return config pool configuration
function getPoolInfo(address _pool)
external
view
returns (
TokenRecord memory primary,
TokenRecord memory complement,
Token memory poolToken,
Config memory config
)
{...}
/// @notice Getting current state of Pool, token balances and leverages, LP token supply
/// @param _pool vault address
/// @return primary pool's primary token address
/// @return primaryBalance pool's primary token balance
/// @return primaryLeverage pool's primary token leverage
/// @return primaryDecimals pool's primary token decimals
/// @return complement pool's complement token address
/// @return complementBalance pool's complement token balance
/// @return complementLeverage pool's complement token leverage
/// @return complementDecimals pool's complement token decimals
/// @return lpTotalSupply pool's LP token total supply
/// @return lpDecimals pool's LP token decimals
function getPoolTokenData(address _pool)
external
view
returns (
address primary,
uint256 primaryBalance,
uint256 primaryLeverage,
uint8 primaryDecimals,
address complement,
uint256 complementBalance,
uint256 complementLeverage,
uint8 complementDecimals,
uint256 lpTotalSupply,
uint8 lpDecimals
)
{...}
/// @notice Getting Pool configuration only to reduce data loading time
/// @dev For the x5 leveraged token, repricerParam1 and
/// repricerParam2 are the implied volatility settings for the long and
/// short call options respectively
function getPoolConfig(address _pool)
external
view
returns (
address derivativeVault,
address dynamicFee,
address repricer,
uint256 exposureLimitPrimary,
uint256 exposureLimitComplement,
uint256 repricerParam1,
uint256 repricerParam2,
uint256 pMin,
uint256 qMin,
uint256 baseFee,
uint256 maxFee,
uint256 feeAmpPrimary,
uint256 feeAmpComplement
)
{...}
}