CompliFi
Search…
AMM

Calculating Derivative, LP and PLP Tokens Prices

Requires pool address and ABI available in CompliFi github repositories; ethers js library is employed to make requests to EVM-compatible blockchain.
1
const COMPLIFI_PLP_ABI = [...]; // CompliFi PLP ABI https://github.com/CompliFi/complifi-amm/blob/master/abis/137/PermanentLiquidityPool.json
2
const COMPLIFI_POOL_ABI = [...]; // CompliFi Pool ABI https://github.com/CompliFi/complifi-amm/blob/master/abis/1/Pool.json
3
const COMPLIFI_VAULT_ABI = [...]; // CompliFi Vault ABI https://github.com/CompliFi/complifi-protocol/blob/master/abis/1/Vault.json
4
const VAULT_COLLATERAL_TOKEN_DECIMALS = ...;
5
const PLP_ADDRESS = '0xa51110FF610eC9523dacf13F2bB03e05aFCBc624'; // BTCx5-USDC
6
7
// initializing a plp
8
const plp = new ethers.Contract(PLP_ADDRESS, COMPLIFI_PLP_ABI)
9
10
// getting current designated pool address
11
const poolAddress = await plp.designatedPool();
12
13
14
// initializing a pool
15
const pool = new ethers.Contract(poolAddress, COMPLIFI_POOL_ABI)
16
17
// getting a vault address
18
const vaultAddress = await pool.derivativeVault();
19
20
// initializing the vault
21
const vault = new ethers.Contract(vaultAddress, COMPLIFI_VAULT_ABI);
22
23
// getting primary (UP) derivative token
24
const primaryAddress = await vault.primaryToken();
25
26
// complement (DOWN) derivative token
27
const complementAddress = await vault.complementToken();
28
29
// current primary token leverage coefficient in the pool
30
const primaryLeverage = (await pool.getLeverage(primaryAddress)) / 10 ** 26;
31
32
// current primary token balance in the pool
33
const primaryAmount = (await pool.getBalance(primaryAddress)) / 10 ** VAULT_COLLATERAL_TOKEN_DECIMALS;
34
35
// current complement token leverage coefficient in the pool
36
const complementLeverage = (await pool.getLeverage(complementAddress)) / 10 ** 26;
37
38
// current complement token balance in the pool
39
const complementAmount = (await pool.getBalance(complementAddress)) / 10 ** VAULT_COLLATERAL_TOKEN_DECIMALS;
40
41
// derivative denomination can be set as a constant under current implementation (=2 for all x5 and =1 for all CollOptions), but we can also be taken from vault's derivative specification
42
const derivativeDenomination = 2; // 1 for CallOptions
43
44
// current LP tokens total supply
45
const poolTotalSupply = (await pool.totalSupply()) / 10 ** 26;
46
47
// calculating leveraged balance for primary token
48
const primaryLeveragedBalance = primaryLeverage * primaryAmount;
49
50
// calculating leveraged balance for complement tokens
51
const complementLeveragedBalance = complementLeverage * complementAmount;
52
53
// calculating sum of leveraged balances divided by denomination
54
const sumOfLeveragedBalances =
55
(primaryLeveragedBalance + complementLeveragedBalance) /
56
derivativeDenomination;
57
58
// calculating primary token spot price
59
const primaryPrice = complementLeveragedBalance / sumOfLeveragedBalances;
60
61
// calculating complement token spot price
62
const complementPrice = primaryLeveragedBalance / sumOfLeveragedBalances;
63
64
// calculating pool token(LP) spot price
65
const lpTokenPrice = (primaryAmount / poolTotalSupply) * primaryPrice +
66
(complementAmount / poolTotalSupply) * complementPrice;
67
68
69
// getting plp token total sypply
70
const plpTotal = await plp.totalSupply();
71
72
// getting LP token balance of the plp token contract
73
const poolBalance = await pool.balanceOf(PLP_ADDRESS);
74
75
// calculating plp token price
76
const plpTokenPrice = lpTokenPrice * poolBalance / plpTotal;
Copied!

Reading Data With View Contract

1
/// @title Reading key data from specified derivative trading Pool
2
contract PoolView {
3
4
/// @notice Contains key information about a derivative token
5
struct TokenRecord {
6
address self;
7
uint256 balance;
8
uint256 leverage;
9
uint8 decimals;
10
uint256 userBalance;
11
}
12
13
/// @notice Contains key information about arbitrary ERC20 token
14
struct Token {
15
address self;
16
uint256 totalSupply;
17
uint8 decimals;
18
uint256 userBalance;
19
}
20
21
/// @notice Contains key information about a Pool's configuration
22
/// @dev For the x5 leveraged token, repricerParam1 and
23
/// repricerParam2 are the implied volatility settings for the long and
24
/// short call options respectively
25
struct Config {
26
address derivativeVault;
27
address dynamicFee;
28
address repricer;
29
uint256 exposureLimitPrimary;
30
uint256 exposureLimitComplement;
31
uint256 repricerParam1;
32
uint256 repricerParam2;
33
uint256 pMin;
34
uint256 qMin;
35
uint8 qMinDecimals;
36
uint256 baseFee;
37
uint256 maxFee;
38
uint256 feeAmpPrimary;
39
uint256 feeAmpComplement;
40
uint8 decimals;
41
bool isPaused;
42
}
43
44
/// @notice Getting information about Pool configuration, it's derivative and pool(LP) tokens
45
/// @param _pool the vault address
46
/// @return primary pool's primary token metadata
47
/// @return complement pool' complement token metadata
48
/// @return poolToken pool's own token metadata
49
/// @return config pool configuration
50
function getPoolInfo(address _pool)
51
external
52
view
53
returns (
54
TokenRecord memory primary,
55
TokenRecord memory complement,
56
Token memory poolToken,
57
Config memory config
58
)
59
{...}
60
61
/// @notice Getting current state of Pool, token balances and leverages, LP token supply
62
/// @param _pool vault address
63
/// @return primary pool's primary token address
64
/// @return primaryBalance pool's primary token balance
65
/// @return primaryLeverage pool's primary token leverage
66
/// @return primaryDecimals pool's primary token decimals
67
/// @return complement pool's complement token address
68
/// @return complementBalance pool's complement token balance
69
/// @return complementLeverage pool's complement token leverage
70
/// @return complementDecimals pool's complement token decimals
71
/// @return lpTotalSupply pool's LP token total supply
72
/// @return lpDecimals pool's LP token decimals
73
function getPoolTokenData(address _pool)
74
external
75
view
76
returns (
77
address primary,
78
uint256 primaryBalance,
79
uint256 primaryLeverage,
80
uint8 primaryDecimals,
81
address complement,
82
uint256 complementBalance,
83
uint256 complementLeverage,
84
uint8 complementDecimals,
85
uint256 lpTotalSupply,
86
uint8 lpDecimals
87
)
88
{...}
89
90
/// @notice Getting Pool configuration only to reduce data loading time
91
/// @dev For the x5 leveraged token, repricerParam1 and
92
/// repricerParam2 are the implied volatility settings for the long and
93
/// short call options respectively
94
function getPoolConfig(address _pool)
95
external
96
view
97
returns (
98
address derivativeVault,
99
address dynamicFee,
100
address repricer,
101
uint256 exposureLimitPrimary,
102
uint256 exposureLimitComplement,
103
uint256 repricerParam1,
104
uint256 repricerParam2,
105
uint256 pMin,
106
uint256 qMin,
107
uint256 baseFee,
108
uint256 maxFee,
109
uint256 feeAmpPrimary,
110
uint256 feeAmpComplement
111
)
112
{...}
113
}
Copied!
Last modified 1mo ago