@raac/rpc 2.0.0-beta.16 → 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.
- package/dist/RPCLibrary.js +6 -0
- package/dist/configs/chains/11155111.json +15 -0
- package/dist/contracts/curve/common/getPoolState.js +105 -0
- package/dist/contracts/curve/common/index.js +1 -0
- package/dist/contracts/yearn/vault/_helpers.js +86 -0
- package/dist/contracts/yearn/vault/deposit.js +29 -0
- package/dist/contracts/yearn/vault/getApr.js +34 -0
- package/dist/contracts/yearn/vault/getUserInfo.js +48 -0
- package/dist/contracts/yearn/vault/getVaultInfo.js +71 -0
- package/dist/contracts/yearn/vault/getVaults.js +22 -0
- package/dist/contracts/yearn/vault/index.js +29 -0
- package/dist/contracts/yearn/vault/mint.js +29 -0
- package/dist/contracts/yearn/vault/previewDeposit.js +25 -0
- package/dist/contracts/yearn/vault/previewMint.js +25 -0
- package/dist/contracts/yearn/vault/previewRedeem.js +25 -0
- package/dist/contracts/yearn/vault/previewWithdraw.js +25 -0
- package/dist/contracts/yearn/vault/processReport.js +29 -0
- package/dist/contracts/yearn/vault/redeem.js +30 -0
- package/dist/contracts/yearn/vault/withdraw.js +30 -0
- package/dist/types/RPCLibrary.d.ts +4 -0
- package/dist/types/contracts/curve/common/getPoolState.d.ts +43 -0
- package/dist/types/contracts/curve/common/index.d.ts +1 -0
- package/dist/types/contracts/yearn/vault/_helpers.d.ts +24 -0
- package/dist/types/contracts/yearn/vault/deposit.d.ts +14 -0
- package/dist/types/contracts/yearn/vault/getApr.d.ts +14 -0
- package/dist/types/contracts/yearn/vault/getUserInfo.d.ts +29 -0
- package/dist/types/contracts/yearn/vault/getVaultInfo.d.ts +43 -0
- package/dist/types/contracts/yearn/vault/getVaults.d.ts +18 -0
- package/dist/types/contracts/yearn/vault/index.d.ts +13 -0
- package/dist/types/contracts/yearn/vault/mint.d.ts +14 -0
- package/dist/types/contracts/yearn/vault/previewDeposit.d.ts +13 -0
- package/dist/types/contracts/yearn/vault/previewMint.d.ts +13 -0
- package/dist/types/contracts/yearn/vault/previewRedeem.d.ts +13 -0
- package/dist/types/contracts/yearn/vault/previewWithdraw.d.ts +13 -0
- package/dist/types/contracts/yearn/vault/processReport.d.ts +14 -0
- package/dist/types/contracts/yearn/vault/redeem.d.ts +15 -0
- package/dist/types/contracts/yearn/vault/withdraw.d.ts +15 -0
- package/dist/types/user/computeEffectiveReturn.d.ts +3 -3
- package/dist/types/user/getUserPortfolio.d.ts +12 -2
- package/dist/types/user/getUserSavings.d.ts +30 -0
- package/dist/types/user/index.d.ts +1 -0
- package/dist/user/getUserPortfolio.js +16 -6
- package/dist/user/getUserSavings.js +35 -0
- package/dist/user/index.js +1 -0
- package/package.json +1 -1
package/dist/RPCLibrary.js
CHANGED
|
@@ -226,6 +226,7 @@ const controllerMethods = __importStar(require("./contracts/governance/gaugeCont
|
|
|
226
226
|
const gaugeSystemMethods = __importStar(require("./contracts/governance/gaugeSystems"));
|
|
227
227
|
// ===== Curve =====
|
|
228
228
|
const curveCommonMethods = __importStar(require("./contracts/curve/common"));
|
|
229
|
+
const yearnVaultMethods = __importStar(require("./contracts/yearn/vault"));
|
|
229
230
|
const faucetMethods = __importStar(require("./contracts/faucet"));
|
|
230
231
|
// ===== User aggregates (dashboard) =====
|
|
231
232
|
const userMethods = __importStar(require("./user"));
|
|
@@ -427,6 +428,8 @@ class RPCLibrary {
|
|
|
427
428
|
// prices
|
|
428
429
|
// Curve pools from the chain config's `curve` section
|
|
429
430
|
curve;
|
|
431
|
+
// Yearn V3 vaults from the chain config's `yearn` section
|
|
432
|
+
yearn;
|
|
430
433
|
faucet;
|
|
431
434
|
// Aggregate reads of one user's position and claimables, composed from the methods above
|
|
432
435
|
user;
|
|
@@ -918,6 +921,9 @@ class RPCLibrary {
|
|
|
918
921
|
this.curve = {
|
|
919
922
|
common: curveCommonMethods,
|
|
920
923
|
};
|
|
924
|
+
this.yearn = {
|
|
925
|
+
vault: yearnVaultMethods,
|
|
926
|
+
};
|
|
921
927
|
this.faucet = faucetMethods;
|
|
922
928
|
this.user = userMethods;
|
|
923
929
|
this.prices = {
|
|
@@ -253,6 +253,14 @@
|
|
|
253
253
|
"coins": ["pmusd", "raactoken", "leraac"]
|
|
254
254
|
}
|
|
255
255
|
},
|
|
256
|
+
"yearn": {
|
|
257
|
+
"spmusd": {
|
|
258
|
+
"id": "spmusd",
|
|
259
|
+
"name": "Savings pmUSD",
|
|
260
|
+
"contract": "0xAC7Db048FB762Dbe714aDE6D3903E6Dd992690a5",
|
|
261
|
+
"asset": "pmusd"
|
|
262
|
+
}
|
|
263
|
+
},
|
|
256
264
|
"nfts": {
|
|
257
265
|
"raacnft": {
|
|
258
266
|
"id": "raacnft",
|
|
@@ -295,6 +303,13 @@
|
|
|
295
303
|
"decimals": 18,
|
|
296
304
|
"contract": "0x0D63FFffF740A0B5DbcC6C70774FDA1456195a3c"
|
|
297
305
|
},
|
|
306
|
+
"spmusd": {
|
|
307
|
+
"id": "spmusd",
|
|
308
|
+
"name": "Savings pmUSD",
|
|
309
|
+
"symbol": "spmUSD",
|
|
310
|
+
"decimals": 18,
|
|
311
|
+
"contract": "0xAC7Db048FB762Dbe714aDE6D3903E6Dd992690a5"
|
|
312
|
+
},
|
|
298
313
|
"usdc": {
|
|
299
314
|
"id": "usdc",
|
|
300
315
|
"name": "USDC",
|
|
@@ -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,86 @@
|
|
|
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.getYearnVaults = exports.PROFIT_UNLOCKING_RATE_SCALE = exports.YEARN_VAULT_ABI = void 0;
|
|
7
|
+
exports.aprFromUnlockingRate = aprFromUnlockingRate;
|
|
8
|
+
exports.resolveYearnVault = resolveYearnVault;
|
|
9
|
+
exports.getYearnVaultContract = getYearnVaultContract;
|
|
10
|
+
const ethers_1 = require("ethers");
|
|
11
|
+
const chains_1 = __importDefault(require("../../../configs/chains"));
|
|
12
|
+
/**
|
|
13
|
+
* The Yearn V3 vault surface (VaultV3.vy, API 3.0.x) these methods use. ERC4626 calls use the standard
|
|
14
|
+
* signatures: Yearn's `withdraw` defaults to a 0 max loss and `redeem` to 100%, which only matter for a vault
|
|
15
|
+
* with strategies.
|
|
16
|
+
*/
|
|
17
|
+
exports.YEARN_VAULT_ABI = [
|
|
18
|
+
"function asset() view returns (address)",
|
|
19
|
+
"function decimals() view returns (uint8)",
|
|
20
|
+
"function apiVersion() view returns (string)",
|
|
21
|
+
"function totalAssets() view returns (uint256)",
|
|
22
|
+
"function totalSupply() view returns (uint256)",
|
|
23
|
+
"function totalIdle() view returns (uint256)",
|
|
24
|
+
"function totalDebt() view returns (uint256)",
|
|
25
|
+
"function pricePerShare() view returns (uint256)",
|
|
26
|
+
"function balanceOf(address) view returns (uint256)",
|
|
27
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
28
|
+
"function convertToAssets(uint256 shares) view returns (uint256)",
|
|
29
|
+
"function convertToShares(uint256 assets) view returns (uint256)",
|
|
30
|
+
"function previewDeposit(uint256 assets) view returns (uint256)",
|
|
31
|
+
"function previewMint(uint256 shares) view returns (uint256)",
|
|
32
|
+
"function previewWithdraw(uint256 assets) view returns (uint256)",
|
|
33
|
+
"function previewRedeem(uint256 shares) view returns (uint256)",
|
|
34
|
+
"function maxDeposit(address receiver) view returns (uint256)",
|
|
35
|
+
"function maxWithdraw(address owner) view returns (uint256)",
|
|
36
|
+
"function maxRedeem(address owner) view returns (uint256)",
|
|
37
|
+
"function profitMaxUnlockTime() view returns (uint256)",
|
|
38
|
+
"function fullProfitUnlockDate() view returns (uint256)",
|
|
39
|
+
"function profitUnlockingRate() view returns (uint256)",
|
|
40
|
+
"function lastProfitUpdate() view returns (uint256)",
|
|
41
|
+
"function unlockedShares() view returns (uint256)",
|
|
42
|
+
"function deposit_limit() view returns (uint256)",
|
|
43
|
+
"function isShutdown() view returns (bool)",
|
|
44
|
+
"function accountant() view returns (address)",
|
|
45
|
+
"function role_manager() view returns (address)",
|
|
46
|
+
"function roles(address) view returns (uint256)",
|
|
47
|
+
"function get_default_queue() view returns (address[])",
|
|
48
|
+
"function deposit(uint256 assets, address receiver) returns (uint256)",
|
|
49
|
+
"function mint(uint256 shares, address receiver) returns (uint256)",
|
|
50
|
+
"function withdraw(uint256 assets, address receiver, address owner) returns (uint256)",
|
|
51
|
+
"function redeem(uint256 shares, address receiver, address owner) returns (uint256)",
|
|
52
|
+
"function process_report(address strategy) returns (uint256, uint256)",
|
|
53
|
+
];
|
|
54
|
+
/** `profitUnlockingRate` is shares per second scaled by this (VaultV3.vy `MAX_BPS_EXTENDED`). */
|
|
55
|
+
exports.PROFIT_UNLOCKING_RATE_SCALE = 10n ** 12n;
|
|
56
|
+
const SECONDS_PER_YEAR = 365n * 24n * 60n * 60n;
|
|
57
|
+
/**
|
|
58
|
+
* The APR the vault pays right now: the assets its locked profit releases per second, annualised, over total
|
|
59
|
+
* assets, as a WAD. Zero once the last reported profit has fully unlocked. It reflects the current stream only.
|
|
60
|
+
*/
|
|
61
|
+
async function aprFromUnlockingRate(contract, profitUnlockingRate, fullProfitUnlockDate, totalAssets, now) {
|
|
62
|
+
if (fullProfitUnlockDate <= now || profitUnlockingRate === 0n || totalAssets === 0n)
|
|
63
|
+
return 0n;
|
|
64
|
+
const sharesPerYear = (profitUnlockingRate * SECONDS_PER_YEAR) / exports.PROFIT_UNLOCKING_RATE_SCALE;
|
|
65
|
+
const assetsPerYear = await contract.convertToAssets(sharesPerYear);
|
|
66
|
+
return (assetsPerYear * 10n ** 18n) / totalAssets;
|
|
67
|
+
}
|
|
68
|
+
const getYearnVaults = (chainId) => chains_1.default[chainId]?.yearn ?? {};
|
|
69
|
+
exports.getYearnVaults = getYearnVaults;
|
|
70
|
+
/** Resolves a Yearn vault by its config key (any casing) or its address. */
|
|
71
|
+
function resolveYearnVault(chainId, vaultAddressOrId) {
|
|
72
|
+
const vaults = (0, exports.getYearnVaults)(chainId);
|
|
73
|
+
const needle = vaultAddressOrId.toLowerCase();
|
|
74
|
+
const entry = vaults[needle] ?? Object.values(vaults).find((vault) => vault.contract.toLowerCase() === needle);
|
|
75
|
+
if (!entry)
|
|
76
|
+
throw new Error(`Yearn vault "${vaultAddressOrId}" not configured for chain ${chainId}`);
|
|
77
|
+
return entry;
|
|
78
|
+
}
|
|
79
|
+
/** The vault contract bound to `runner`, or to the chain's default RPC when none is given. */
|
|
80
|
+
function getYearnVaultContract(chainId, vaultAddressOrId, runner) {
|
|
81
|
+
const entry = resolveYearnVault(chainId, vaultAddressOrId);
|
|
82
|
+
if (!runner) {
|
|
83
|
+
runner = new ethers_1.ethers.JsonRpcProvider(chains_1.default[chainId].rpcs[0]);
|
|
84
|
+
}
|
|
85
|
+
return { entry, contract: new ethers_1.ethers.Contract(entry.contract, exports.YEARN_VAULT_ABI, runner) };
|
|
86
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deposit = void 0;
|
|
4
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
5
|
+
const _helpers_1 = require("./_helpers");
|
|
6
|
+
/**
|
|
7
|
+
* Deposits `assets` of the vault asset for shares. The signer approves the vault for the asset first
|
|
8
|
+
* (`wallet.assets.approveAsset(chainId, vault.asset, vault.contract, amount, signer)`).
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param assets - Vault asset amount, in its base units.
|
|
13
|
+
* @param receiver - Who receives the shares.
|
|
14
|
+
* @param signer - The signer sending the transaction and paying the assets.
|
|
15
|
+
* @returns The mined transaction receipt.
|
|
16
|
+
*/
|
|
17
|
+
const deposit = async (chainId, vaultAddressOrId, assets, receiver, signer) => {
|
|
18
|
+
if (!signer)
|
|
19
|
+
throw new Error("Signer is required");
|
|
20
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, signer);
|
|
21
|
+
try {
|
|
22
|
+
const tx = await contract.deposit(assets, receiver);
|
|
23
|
+
return await tx.wait();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw (0, rpcError_1.toRpcError)("yearn.deposit", error, { chainId, address: entry.contract });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.deposit = deposit;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getApr = void 0;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
/**
|
|
8
|
+
* The APR a Yearn V3 vault pays right now, from the profit still unlocking: assets released per second,
|
|
9
|
+
* annualised, over total assets. It is 0 once the last report has fully unlocked, so it reflects the current
|
|
10
|
+
* reward stream, not a historical average.
|
|
11
|
+
*
|
|
12
|
+
* @param chainId - The chain/network to use.
|
|
13
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
14
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
15
|
+
* @returns The APR as a WAD (0.05e18 = 5%).
|
|
16
|
+
*/
|
|
17
|
+
const getApr = async (chainId, vaultAddressOrId, provider) => {
|
|
18
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
19
|
+
try {
|
|
20
|
+
const [profitUnlockingRate, fullProfitUnlockDate, totalAssets, block] = await Promise.all([
|
|
21
|
+
contract.profitUnlockingRate(),
|
|
22
|
+
contract.fullProfitUnlockDate(),
|
|
23
|
+
contract.totalAssets(),
|
|
24
|
+
contract.runner.provider.getBlock("latest"),
|
|
25
|
+
]);
|
|
26
|
+
if (!block)
|
|
27
|
+
throw new Error("Latest block unavailable");
|
|
28
|
+
return (0, amount_1.toScaled)(await (0, _helpers_1.aprFromUnlockingRate)(contract, profitUnlockingRate, fullProfitUnlockDate, totalAssets, BigInt(block.timestamp)));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw (0, rpcError_1.toRpcError)("yearn.getApr", error, { chainId, address: entry.contract });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.getApr = getApr;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUserInfo = getUserInfo;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const amount_1 = require("../../../utils/amount");
|
|
6
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
7
|
+
const _helpers_1 = require("./_helpers");
|
|
8
|
+
const ERC20_READ_ABI = [
|
|
9
|
+
"function balanceOf(address) view returns (uint256)",
|
|
10
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Gets a user's position in a Yearn V3 vault, plus their vault asset balance and allowance.
|
|
14
|
+
*
|
|
15
|
+
* @param chainId - The chain/network to use.
|
|
16
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section (e.g. "spmusd"), or its address.
|
|
17
|
+
* @param account - Address of the user.
|
|
18
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
19
|
+
* @returns The user's shares, their asset value, balances, allowance and limits, in base units.
|
|
20
|
+
*/
|
|
21
|
+
async function getUserInfo(chainId, vaultAddressOrId, account, provider) {
|
|
22
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
23
|
+
const asset = (value) => (0, amount_1.toTokenAmount)(chainId, entry.asset, value);
|
|
24
|
+
const shares = (value) => (0, amount_1.toTokenAmount)(chainId, entry.contract, value);
|
|
25
|
+
try {
|
|
26
|
+
const token = new ethers_1.ethers.Contract(await contract.asset(), ERC20_READ_ABI, contract.runner);
|
|
27
|
+
const [userShares, assetBalance, assetAllowance, maxDeposit, maxWithdraw, maxRedeem] = await Promise.all([
|
|
28
|
+
contract.balanceOf(account),
|
|
29
|
+
token.balanceOf(account),
|
|
30
|
+
token.allowance(account, entry.contract),
|
|
31
|
+
contract.maxDeposit(account),
|
|
32
|
+
contract.maxWithdraw(account),
|
|
33
|
+
contract.maxRedeem(account),
|
|
34
|
+
]);
|
|
35
|
+
return {
|
|
36
|
+
shares: shares(userShares),
|
|
37
|
+
assets: asset(await contract.convertToAssets(userShares)),
|
|
38
|
+
assetBalance: asset(assetBalance),
|
|
39
|
+
assetAllowance: asset(assetAllowance),
|
|
40
|
+
maxDeposit: asset(maxDeposit),
|
|
41
|
+
maxWithdraw: asset(maxWithdraw),
|
|
42
|
+
maxRedeem: shares(maxRedeem),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw (0, rpcError_1.toRpcError)("yearn.getUserInfo", error, { chainId, address: entry.contract });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVaultInfo = getVaultInfo;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
8
|
+
/**
|
|
9
|
+
* Gets the vault-wide state of a Yearn V3 vault. For a user's position use `getUserInfo`.
|
|
10
|
+
*
|
|
11
|
+
* @param chainId - The chain/network to use.
|
|
12
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section (e.g. "spmusd"), or its address.
|
|
13
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
14
|
+
* @returns The vault state, with amounts in base units.
|
|
15
|
+
*/
|
|
16
|
+
async function getVaultInfo(chainId, vaultAddressOrId, provider) {
|
|
17
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
18
|
+
const asset = (value) => (0, amount_1.toTokenAmount)(chainId, entry.asset, value);
|
|
19
|
+
const shares = (value) => (0, amount_1.toTokenAmount)(chainId, entry.contract, value);
|
|
20
|
+
try {
|
|
21
|
+
const [assetAddress, apiVersion, totalAssets, totalSupply, totalIdle, totalDebt, pricePerShare, lockedShares, profitMaxUnlockTime, fullProfitUnlockDate, profitUnlockingRate, lastProfitUpdate, depositLimit, isShutdown, accountant, roleManager, strategies, block,] = await Promise.all([
|
|
22
|
+
contract.asset(),
|
|
23
|
+
contract.apiVersion(),
|
|
24
|
+
contract.totalAssets(),
|
|
25
|
+
contract.totalSupply(),
|
|
26
|
+
contract.totalIdle(),
|
|
27
|
+
contract.totalDebt(),
|
|
28
|
+
contract.pricePerShare(),
|
|
29
|
+
contract.balanceOf(entry.contract),
|
|
30
|
+
contract.profitMaxUnlockTime(),
|
|
31
|
+
contract.fullProfitUnlockDate(),
|
|
32
|
+
contract.profitUnlockingRate(),
|
|
33
|
+
contract.lastProfitUpdate(),
|
|
34
|
+
contract.deposit_limit(),
|
|
35
|
+
contract.isShutdown(),
|
|
36
|
+
contract.accountant(),
|
|
37
|
+
contract.role_manager(),
|
|
38
|
+
contract.get_default_queue(),
|
|
39
|
+
contract.runner.provider.getBlock("latest"),
|
|
40
|
+
]);
|
|
41
|
+
if (!block)
|
|
42
|
+
throw new Error("Latest block unavailable");
|
|
43
|
+
const [lockedProfit, apr] = await Promise.all([
|
|
44
|
+
contract.convertToAssets(lockedShares),
|
|
45
|
+
(0, _helpers_1.aprFromUnlockingRate)(contract, profitUnlockingRate, fullProfitUnlockDate, totalAssets, BigInt(block.timestamp)),
|
|
46
|
+
]);
|
|
47
|
+
return {
|
|
48
|
+
...entry,
|
|
49
|
+
assetAddress,
|
|
50
|
+
apiVersion,
|
|
51
|
+
totalAssets: asset(totalAssets),
|
|
52
|
+
totalSupply: shares(totalSupply),
|
|
53
|
+
totalIdle: asset(totalIdle),
|
|
54
|
+
totalDebt: asset(totalDebt),
|
|
55
|
+
pricePerShare: (0, amount_1.toScaled)(pricePerShare, (0, amount_1.getAssetDecimals)(chainId, entry.asset)),
|
|
56
|
+
apr: (0, amount_1.toScaled)(apr),
|
|
57
|
+
lockedProfit: asset(lockedProfit),
|
|
58
|
+
profitMaxUnlockTime: Number(profitMaxUnlockTime),
|
|
59
|
+
fullProfitUnlockDate: Number(fullProfitUnlockDate),
|
|
60
|
+
lastProfitUpdate: Number(lastProfitUpdate),
|
|
61
|
+
depositLimit: asset(depositLimit),
|
|
62
|
+
isShutdown,
|
|
63
|
+
accountant: accountant === ZERO_ADDRESS ? null : accountant,
|
|
64
|
+
roleManager,
|
|
65
|
+
strategies: [...strategies],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw (0, rpcError_1.toRpcError)("yearn.getVaultInfo", error, { chainId, address: entry.contract });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVault = exports.getVaults = void 0;
|
|
4
|
+
const _helpers_1 = require("./_helpers");
|
|
5
|
+
/**
|
|
6
|
+
* Lists the Yearn V3 vaults in the chain config. Reads the config only; no RPC call is made.
|
|
7
|
+
*
|
|
8
|
+
* @param chainId - The chain/network to use.
|
|
9
|
+
* @returns Every configured vault; empty when the chain has none.
|
|
10
|
+
*/
|
|
11
|
+
const getVaults = (chainId) => Object.values((0, _helpers_1.getYearnVaults)(chainId));
|
|
12
|
+
exports.getVaults = getVaults;
|
|
13
|
+
/**
|
|
14
|
+
* Looks up one Yearn V3 vault in the chain config. Reads the config only; no RPC call is made.
|
|
15
|
+
*
|
|
16
|
+
* @param chainId - The chain/network to use.
|
|
17
|
+
* @param vaultAddressOrId - The vault's config key or its address.
|
|
18
|
+
* @returns The vault entry.
|
|
19
|
+
* @throws If the vault is not configured on the chain.
|
|
20
|
+
*/
|
|
21
|
+
const getVault = (chainId, vaultAddressOrId) => (0, _helpers_1.resolveYearnVault)(chainId, vaultAddressOrId);
|
|
22
|
+
exports.getVault = getVault;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./deposit"), exports);
|
|
18
|
+
__exportStar(require("./getApr"), exports);
|
|
19
|
+
__exportStar(require("./getUserInfo"), exports);
|
|
20
|
+
__exportStar(require("./getVaultInfo"), exports);
|
|
21
|
+
__exportStar(require("./getVaults"), exports);
|
|
22
|
+
__exportStar(require("./mint"), exports);
|
|
23
|
+
__exportStar(require("./previewDeposit"), exports);
|
|
24
|
+
__exportStar(require("./previewMint"), exports);
|
|
25
|
+
__exportStar(require("./previewRedeem"), exports);
|
|
26
|
+
__exportStar(require("./previewWithdraw"), exports);
|
|
27
|
+
__exportStar(require("./processReport"), exports);
|
|
28
|
+
__exportStar(require("./redeem"), exports);
|
|
29
|
+
__exportStar(require("./withdraw"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mint = void 0;
|
|
4
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
5
|
+
const _helpers_1 = require("./_helpers");
|
|
6
|
+
/**
|
|
7
|
+
* Mints exactly `shares`, pulling the assets they cost (see `previewMint`). The signer approves the vault for the
|
|
8
|
+
* asset first.
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param shares - Share amount, in base units.
|
|
13
|
+
* @param receiver - Who receives the shares.
|
|
14
|
+
* @param signer - The signer sending the transaction and paying the assets.
|
|
15
|
+
* @returns The mined transaction receipt.
|
|
16
|
+
*/
|
|
17
|
+
const mint = async (chainId, vaultAddressOrId, shares, receiver, signer) => {
|
|
18
|
+
if (!signer)
|
|
19
|
+
throw new Error("Signer is required");
|
|
20
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, signer);
|
|
21
|
+
try {
|
|
22
|
+
const tx = await contract.mint(shares, receiver);
|
|
23
|
+
return await tx.wait();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw (0, rpcError_1.toRpcError)("yearn.mint", error, { chainId, address: entry.contract });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.mint = mint;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.previewDeposit = void 0;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
/**
|
|
8
|
+
* Shares a deposit of `assets` would mint now.
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param assets - Vault asset amount, in its base units.
|
|
13
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
14
|
+
* @returns The shares, in base units.
|
|
15
|
+
*/
|
|
16
|
+
const previewDeposit = async (chainId, vaultAddressOrId, assets, provider) => {
|
|
17
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
18
|
+
try {
|
|
19
|
+
return (0, amount_1.toTokenAmount)(chainId, entry.contract, await contract.previewDeposit(assets));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw (0, rpcError_1.toRpcError)("yearn.previewDeposit", error, { chainId, address: entry.contract });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.previewDeposit = previewDeposit;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.previewMint = void 0;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
/**
|
|
8
|
+
* Assets a mint of `shares` would pull now.
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param shares - Share amount, in base units.
|
|
13
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
14
|
+
* @returns The vault asset amount, in its base units.
|
|
15
|
+
*/
|
|
16
|
+
const previewMint = async (chainId, vaultAddressOrId, shares, provider) => {
|
|
17
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
18
|
+
try {
|
|
19
|
+
return (0, amount_1.toTokenAmount)(chainId, entry.asset, await contract.previewMint(shares));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw (0, rpcError_1.toRpcError)("yearn.previewMint", error, { chainId, address: entry.contract });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.previewMint = previewMint;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.previewRedeem = void 0;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
/**
|
|
8
|
+
* Assets a redemption of `shares` would pay out now.
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param shares - Share amount, in base units.
|
|
13
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
14
|
+
* @returns The vault asset amount, in its base units.
|
|
15
|
+
*/
|
|
16
|
+
const previewRedeem = async (chainId, vaultAddressOrId, shares, provider) => {
|
|
17
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
18
|
+
try {
|
|
19
|
+
return (0, amount_1.toTokenAmount)(chainId, entry.asset, await contract.previewRedeem(shares));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw (0, rpcError_1.toRpcError)("yearn.previewRedeem", error, { chainId, address: entry.contract });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.previewRedeem = previewRedeem;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.previewWithdraw = void 0;
|
|
4
|
+
const amount_1 = require("../../../utils/amount");
|
|
5
|
+
const rpcError_1 = require("../../../utils/rpcError");
|
|
6
|
+
const _helpers_1 = require("./_helpers");
|
|
7
|
+
/**
|
|
8
|
+
* Shares a withdrawal of `assets` would burn now.
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain/network to use.
|
|
11
|
+
* @param vaultAddressOrId - The vault's key in the chain config's `yearn` section, or its address.
|
|
12
|
+
* @param assets - Vault asset amount, in its base units.
|
|
13
|
+
* @param provider - Optional ethers.js Provider instance.
|
|
14
|
+
* @returns The shares, in base units.
|
|
15
|
+
*/
|
|
16
|
+
const previewWithdraw = async (chainId, vaultAddressOrId, assets, provider) => {
|
|
17
|
+
const { entry, contract } = (0, _helpers_1.getYearnVaultContract)(chainId, vaultAddressOrId, provider);
|
|
18
|
+
try {
|
|
19
|
+
return (0, amount_1.toTokenAmount)(chainId, entry.contract, await contract.previewWithdraw(assets));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw (0, rpcError_1.toRpcError)("yearn.previewWithdraw", error, { chainId, address: entry.contract });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.previewWithdraw = previewWithdraw;
|