@raac/rpc 2.0.0-beta.17 → 2.0.0-beta.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getPoolState = void 0;
|
|
7
|
+
const ethers_1 = require("ethers");
|
|
8
|
+
const chains_1 = __importDefault(require("../../../configs/chains"));
|
|
9
|
+
const amount_1 = require("../../../utils/amount");
|
|
10
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
11
|
+
const _helpers_1 = require("./_helpers");
|
|
12
|
+
const getPools_1 = require("./getPools");
|
|
13
|
+
const COMMON = [
|
|
14
|
+
"function balances(uint256) view returns (uint256)",
|
|
15
|
+
"function totalSupply() view returns (uint256)",
|
|
16
|
+
"function get_virtual_price() view returns (uint256)",
|
|
17
|
+
"function fee() view returns (uint256)",
|
|
18
|
+
"function A() view returns (uint256)",
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Price getters per flavour. twocrypto-ng has one price and no index; tricrypto-ng and stableswap-ng take k for
|
|
22
|
+
* coin k + 1. All price in coin 0.
|
|
23
|
+
*/
|
|
24
|
+
const PRICE_ABI = {
|
|
25
|
+
twocrypto: [
|
|
26
|
+
"function price_oracle() view returns (uint256)",
|
|
27
|
+
"function last_prices() view returns (uint256)",
|
|
28
|
+
"function price_scale() view returns (uint256)",
|
|
29
|
+
"function lp_price() view returns (uint256)",
|
|
30
|
+
"function mid_fee() view returns (uint256)",
|
|
31
|
+
"function out_fee() view returns (uint256)",
|
|
32
|
+
],
|
|
33
|
+
tricrypto: [
|
|
34
|
+
"function price_oracle(uint256) view returns (uint256)",
|
|
35
|
+
"function last_prices(uint256) view returns (uint256)",
|
|
36
|
+
"function price_scale(uint256) view returns (uint256)",
|
|
37
|
+
"function lp_price() view returns (uint256)",
|
|
38
|
+
"function mid_fee() view returns (uint256)",
|
|
39
|
+
"function out_fee() view returns (uint256)",
|
|
40
|
+
],
|
|
41
|
+
stableswap: ["function price_oracle(uint256) view returns (uint256)", "function get_p(uint256) view returns (uint256)"],
|
|
42
|
+
};
|
|
43
|
+
const FEE_DECIMALS = 10;
|
|
44
|
+
const ONE = BigInt(10) ** BigInt(amount_1.WAD_DECIMALS);
|
|
45
|
+
/**
|
|
46
|
+
* Reads a Curve pool's composition, prices and fees in one go: balances, LP supply, virtual and LP price, the
|
|
47
|
+
* oracle, spot and (crypto) scale price of every coin in coin 0, each balance's value in coin 0, and the fee and A.
|
|
48
|
+
*
|
|
49
|
+
* @param chainId - The chain/network to use.
|
|
50
|
+
* @param poolAddressOrId - The pool's key in the chain config's `curve` section, or its address.
|
|
51
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
52
|
+
* @returns The pool's state; values and TVL are in coin 0's units at the oracle price.
|
|
53
|
+
*/
|
|
54
|
+
const getPoolState = async (chainId, poolAddressOrId, provider) => {
|
|
55
|
+
const entry = (0, _helpers_1.resolveCurvePool)(chainId, poolAddressOrId);
|
|
56
|
+
const pool = (0, getPools_1.getPool)(chainId, entry.id);
|
|
57
|
+
if (!provider)
|
|
58
|
+
provider = new ethers_1.ethers.JsonRpcProvider(chains_1.default[chainId].rpcs[0]);
|
|
59
|
+
const contract = new ethers_1.ethers.Contract(entry.contract, [...COMMON, ...PRICE_ABI[entry.flavour]], provider);
|
|
60
|
+
const crypto = entry.flavour !== "stableswap";
|
|
61
|
+
const others = pool.coins.slice(1).map((_, k) => k);
|
|
62
|
+
// twocrypto-ng's price getters take no index; it has a single other coin.
|
|
63
|
+
const each = (method) => Promise.all(others.map((k) => (entry.flavour === "twocrypto" ? contract[method]() : contract[method](k))));
|
|
64
|
+
try {
|
|
65
|
+
const [balances, supply, virtualPrice, fee, A, oracle, spot, scale, lpPrice, mid, out] = await Promise.all([
|
|
66
|
+
Promise.all(pool.coins.map((_, i) => contract.balances(i))),
|
|
67
|
+
contract.totalSupply(),
|
|
68
|
+
contract.get_virtual_price(),
|
|
69
|
+
contract.fee(),
|
|
70
|
+
contract.A(),
|
|
71
|
+
each("price_oracle"),
|
|
72
|
+
each(crypto ? "last_prices" : "get_p"),
|
|
73
|
+
crypto ? each("price_scale") : Promise.resolve(null),
|
|
74
|
+
crypto ? contract.lp_price() : Promise.resolve(null),
|
|
75
|
+
crypto ? contract.mid_fee() : Promise.resolve(null),
|
|
76
|
+
crypto ? contract.out_fee() : Promise.resolve(null),
|
|
77
|
+
]);
|
|
78
|
+
const unit = pool.coins[0];
|
|
79
|
+
const prices = (p) => [ONE, ...p].map((v) => (0, amount_1.toAmount)(v, amount_1.WAD_DECIMALS, unit.symbol));
|
|
80
|
+
const oraclePrices = prices(oracle);
|
|
81
|
+
// balance (coin decimals) * price (1e18) -> coin 0 decimals
|
|
82
|
+
const values = balances.map((b, i) => (0, amount_1.toAmount)((b * oraclePrices[i].value * BigInt(10) ** BigInt(unit.decimals)) / (BigInt(10) ** BigInt(pool.coins[i].decimals) * ONE), unit.decimals, unit.symbol));
|
|
83
|
+
const tvl = values.reduce((sum, v) => sum + v.value, BigInt(0));
|
|
84
|
+
const lp = lpPrice ?? (supply > BigInt(0) ? (tvl * BigInt(10) ** BigInt(amount_1.WAD_DECIMALS + amount_1.WAD_DECIMALS - unit.decimals)) / supply : BigInt(0));
|
|
85
|
+
return {
|
|
86
|
+
pool,
|
|
87
|
+
balances: balances.map((b, i) => (0, amount_1.toAmount)(b, pool.coins[i].decimals, pool.coins[i].symbol)),
|
|
88
|
+
totalSupply: (0, amount_1.toAmount)(supply, amount_1.WAD_DECIMALS, "LP"),
|
|
89
|
+
virtualPrice: (0, amount_1.toAmount)(virtualPrice, amount_1.WAD_DECIMALS),
|
|
90
|
+
lpPrice: (0, amount_1.toAmount)(lp, amount_1.WAD_DECIMALS, unit.symbol),
|
|
91
|
+
oraclePrices,
|
|
92
|
+
spotPrices: prices(spot),
|
|
93
|
+
priceScale: scale ? prices(scale) : null,
|
|
94
|
+
values,
|
|
95
|
+
tvl: (0, amount_1.toAmount)(tvl, unit.decimals, unit.symbol),
|
|
96
|
+
fee: (0, amount_1.toScaled)(fee, FEE_DECIMALS),
|
|
97
|
+
feeRange: mid !== null && out !== null ? { mid: (0, amount_1.toScaled)(mid, FEE_DECIMALS), out: (0, amount_1.toScaled)(out, FEE_DECIMALS) } : null,
|
|
98
|
+
A,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
throw (0, rpcError_1.toRpcError)("curve.common.getPoolState", error, { chainId, address: entry.contract });
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
exports.getPoolState = getPoolState;
|
|
@@ -20,6 +20,7 @@ __exportStar(require("./calcWithdrawOneCoin"), exports);
|
|
|
20
20
|
__exportStar(require("./exchange"), exports);
|
|
21
21
|
__exportStar(require("./getDy"), exports);
|
|
22
22
|
__exportStar(require("./getPools"), exports);
|
|
23
|
+
__exportStar(require("./getPoolState"), exports);
|
|
23
24
|
__exportStar(require("./getVirtualPrice"), exports);
|
|
24
25
|
__exportStar(require("./removeLiquidity"), exports);
|
|
25
26
|
__exportStar(require("./removeLiquidityOneCoin"), exports);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Provider } from "ethers";
|
|
2
|
+
import { ChainId } from "../../../configs/chains";
|
|
3
|
+
import { ScaledValue, TokenAmount } from "../../../utils/amount";
|
|
4
|
+
import { CurvePool } from "./getPools";
|
|
5
|
+
export interface CurvePoolState {
|
|
6
|
+
pool: CurvePool;
|
|
7
|
+
/** Coin balances in pool index order, each in its own decimals and symbol. */
|
|
8
|
+
balances: TokenAmount[];
|
|
9
|
+
/** LP tokens in circulation (the pool is its own LP token, 18 decimals). */
|
|
10
|
+
totalSupply: TokenAmount;
|
|
11
|
+
/** `get_virtual_price()`: underlying per LP token, only grows with fees. */
|
|
12
|
+
virtualPrice: TokenAmount;
|
|
13
|
+
/** Value of one LP token in coin 0: `lp_price()` on crypto pools, the balances at oracle prices on stableswap pools. */
|
|
14
|
+
lpPrice: TokenAmount;
|
|
15
|
+
/** Price of each coin in coin 0, 18 decimals, from the pool's EMA oracle; `oraclePrices[0]` is 1. */
|
|
16
|
+
oraclePrices: TokenAmount[];
|
|
17
|
+
/** Price of each coin in coin 0 right now: `last_prices` on crypto pools, `get_p` on stableswap pools. */
|
|
18
|
+
spotPrices: TokenAmount[];
|
|
19
|
+
/** Crypto pools only: where liquidity is concentrated (`price_scale`), in coin 0. Null on stableswap pools. */
|
|
20
|
+
priceScale: TokenAmount[] | null;
|
|
21
|
+
/** Each balance valued in coin 0 at the oracle price; their sum is the pool's TVL in coin 0. */
|
|
22
|
+
values: TokenAmount[];
|
|
23
|
+
tvl: TokenAmount;
|
|
24
|
+
/** Current swap fee as a fraction (Curve's 1e10 scale). */
|
|
25
|
+
fee: ScaledValue;
|
|
26
|
+
/** Crypto pools: the fee range the current fee moves in, as fractions. Null on stableswap pools. */
|
|
27
|
+
feeRange: {
|
|
28
|
+
mid: ScaledValue;
|
|
29
|
+
out: ScaledValue;
|
|
30
|
+
} | null;
|
|
31
|
+
/** Amplification, as the pool's `A()` reports it (crypto pools scale it by 10000). */
|
|
32
|
+
A: bigint;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Reads a Curve pool's composition, prices and fees in one go: balances, LP supply, virtual and LP price, the
|
|
36
|
+
* oracle, spot and (crypto) scale price of every coin in coin 0, each balance's value in coin 0, and the fee and A.
|
|
37
|
+
*
|
|
38
|
+
* @param chainId - The chain/network to use.
|
|
39
|
+
* @param poolAddressOrId - The pool's key in the chain config's `curve` section, or its address.
|
|
40
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
41
|
+
* @returns The pool's state; values and TVL are in coin 0's units at the oracle price.
|
|
42
|
+
*/
|
|
43
|
+
export declare const getPoolState: (chainId: ChainId, poolAddressOrId: string, provider?: Provider) => Promise<CurvePoolState>;
|
|
@@ -4,6 +4,7 @@ export * from "./calcWithdrawOneCoin";
|
|
|
4
4
|
export * from "./exchange";
|
|
5
5
|
export * from "./getDy";
|
|
6
6
|
export * from "./getPools";
|
|
7
|
+
export * from "./getPoolState";
|
|
7
8
|
export * from "./getVirtualPrice";
|
|
8
9
|
export * from "./removeLiquidity";
|
|
9
10
|
export * from "./removeLiquidityOneCoin";
|