@raac/rpc 2.0.0-beta.10 → 2.0.0-beta.11

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.
@@ -149,6 +149,7 @@ const claimRewardFromPool_1 = __importDefault(require("./contracts/feeCollector/
149
149
  const getState_1 = require("./contracts/rentStreamer/getState");
150
150
  const previewRelease_1 = require("./contracts/rentStreamer/previewRelease");
151
151
  const getScheduled_1 = require("./contracts/rentStreamer/getScheduled");
152
+ const getEpochProjection_1 = require("./contracts/rentStreamer/getEpochProjection");
152
153
  const sync_1 = require("./contracts/rentStreamer/sync");
153
154
  const release_1 = require("./contracts/rentStreamer/release");
154
155
  const getRawUserDepositsInLendingPool_1 = __importDefault(require("./pools/lendingPool/getRawUserDepositsInLendingPool"));
@@ -526,6 +527,7 @@ class RPCLibrary {
526
527
  getState: getState_1.getState,
527
528
  previewRelease: previewRelease_1.previewRelease,
528
529
  getScheduled: getScheduled_1.getScheduled,
530
+ getEpochProjection: getEpochProjection_1.getEpochProjection,
529
531
  sync: sync_1.sync,
530
532
  release: release_1.release,
531
533
  },
@@ -0,0 +1,91 @@
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.getEpochProjection = void 0;
7
+ const ethers_1 = require("ethers");
8
+ const chains_1 = __importDefault(require("../../configs/chains"));
9
+ const artifacts_1 = require("../../utils/artifacts");
10
+ const amount_1 = require("../../utils/amount");
11
+ const _helpers_1 = require("./_helpers");
12
+ /** FeeStreamer.WEEK: buckets and the gauge controller's epochs both start on this grid. */
13
+ const WEEK = 7n * 24n * 3600n;
14
+ const BOUNTY_PRECISION = 10n ** BigInt(amount_1.WAD_DECIMALS);
15
+ /** Bucket `index` of `amount` split from a start epoch, as FeeStreamer._append does: equal splits, the remainder in the last. */
16
+ const bucketShare = (amount, numEpochs, index) => {
17
+ if (index >= numEpochs)
18
+ return 0n;
19
+ const split = amount / numEpochs;
20
+ return index < numEpochs - 1n ? split : amount - split * (numEpochs - 1n);
21
+ };
22
+ /**
23
+ * Projects what the RWA gauge system receives from the RentStreamer in the current and the next epoch, counting
24
+ * token sent to the streamer but not yet synced as it will be split: from the current epoch when the current epoch
25
+ * is not released yet, otherwise from the next one. Reads every value at one block.
26
+ *
27
+ * Assumes the current epoch is released before it ends; an unreleased bucket rolls into the next release. Rent sent
28
+ * after this read adds to later buckets, so the next epoch's figure can still grow.
29
+ *
30
+ * @param chainId - The chain/network to use.
31
+ * @param provider - Optional ethers.js Provider instance.
32
+ * @returns Both epochs' deposited, pending and projected amounts in the streamed token's base units.
33
+ */
34
+ const getEpochProjection = async (chainId, provider) => {
35
+ if (!provider) {
36
+ const rpc = chains_1.default[chainId].rpcs[0];
37
+ provider = new ethers_1.ethers.JsonRpcProvider(rpc);
38
+ }
39
+ const block = await provider.getBlock("latest");
40
+ if (!block)
41
+ throw new Error("Could not read the latest block");
42
+ const overrides = { blockTag: block.number };
43
+ const currentEpoch = (BigInt(block.timestamp) / WEEK) * WEEK;
44
+ const nextEpoch = currentEpoch + WEEK;
45
+ const streamer = await (0, _helpers_1.getRentStreamerContract)(chainId, provider);
46
+ const [token, distributorAddress, numEpochs, maxBountyFee, killed, processed, lastEpochProcessed, preview, scheduledNext] = await Promise.all([
47
+ streamer.TOKEN(overrides),
48
+ streamer.GAUGE_REWARD_DISTRIBUTOR(overrides),
49
+ streamer.numEpochs(overrides),
50
+ streamer.maxBountyFee(overrides),
51
+ streamer.killed(overrides),
52
+ streamer.processed(overrides),
53
+ streamer.lastEpochProcessed(overrides),
54
+ streamer.previewRelease(overrides),
55
+ streamer.scheduled(nextEpoch, overrides),
56
+ ]);
57
+ const erc20 = new ethers_1.ethers.Contract(token, ["function balanceOf(address) view returns (uint256)"], provider);
58
+ const distributor = new ethers_1.ethers.Contract(distributorAddress, (0, artifacts_1.getABI)("gaugerewardsdistributor"), provider);
59
+ const [balance, depositedCurrent] = await Promise.all([
60
+ erc20.balanceOf(await streamer.getAddress(), overrides),
61
+ distributor.depositedAtEpoch(token, currentEpoch, overrides),
62
+ ]);
63
+ const epochs = BigInt(numEpochs);
64
+ const unsynced = balance - processed;
65
+ const currentReleased = lastEpochProcessed >= currentEpoch;
66
+ // previewRelease already counts every unreleased bucket up to now plus the first split of the unsynced amount.
67
+ const pendingCurrent = killed ? 0n : preview[0];
68
+ // Unsynced token is split from the current epoch at its release, or from the next epoch once the current one is released.
69
+ const pendingNext = killed ? 0n : scheduledNext + bucketShare(unsynced, epochs, currentReleased ? 0n : 1n);
70
+ const amount = (value) => (0, amount_1.toTokenAmount)(chainId, token, value);
71
+ const project = (epoch, deposited, pending) => {
72
+ const maxBounty = (pending * maxBountyFee) / BOUNTY_PRECISION;
73
+ return {
74
+ epoch: Number(epoch),
75
+ deposited: amount(deposited),
76
+ pending: amount(pending),
77
+ maxBounty: amount(maxBounty),
78
+ projected: amount(deposited + pending - maxBounty),
79
+ };
80
+ };
81
+ return {
82
+ token,
83
+ numEpochs: Number(numEpochs),
84
+ killed,
85
+ currentReleased,
86
+ unsynced: amount(unsynced),
87
+ current: project(currentEpoch, depositedCurrent, pendingCurrent),
88
+ next: project(nextEpoch, 0n, pendingNext),
89
+ };
90
+ };
91
+ exports.getEpochProjection = getEpochProjection;
@@ -91,6 +91,7 @@ import claimRewardFromPool from "./contracts/feeCollector/claimRewardFromPool";
91
91
  import { getState as getStateRentStreamer } from "./contracts/rentStreamer/getState";
92
92
  import { previewRelease as previewReleaseRentStreamer } from "./contracts/rentStreamer/previewRelease";
93
93
  import { getScheduled as getScheduledRentStreamer } from "./contracts/rentStreamer/getScheduled";
94
+ import { getEpochProjection as getEpochProjectionRentStreamer } from "./contracts/rentStreamer/getEpochProjection";
94
95
  import { sync as syncRentStreamer } from "./contracts/rentStreamer/sync";
95
96
  import { release as releaseRentStreamer } from "./contracts/rentStreamer/release";
96
97
  import getRawUserDepositsInLendingPool from "./pools/lendingPool/getRawUserDepositsInLendingPool";
@@ -408,6 +409,7 @@ declare class RPCLibrary {
408
409
  getState: typeof getStateRentStreamer;
409
410
  previewRelease: typeof previewReleaseRentStreamer;
410
411
  getScheduled: typeof getScheduledRentStreamer;
412
+ getEpochProjection: typeof getEpochProjectionRentStreamer;
411
413
  sync: typeof syncRentStreamer;
412
414
  release: typeof releaseRentStreamer;
413
415
  };
@@ -0,0 +1,40 @@
1
+ import { Provider } from "ethers";
2
+ import { ChainId } from "../../configs/chains";
3
+ import { TokenAmount } from "../../utils/amount";
4
+ export interface RentStreamerEpochAmount {
5
+ /** Epoch start timestamp. */
6
+ epoch: number;
7
+ /** Already deposited into the RWA GaugeRewardsDistributor for this epoch, by any depositor. Always zero for the next epoch. */
8
+ deposited: TokenAmount;
9
+ /** What the streamer will release into this epoch from what it holds now, before the keeper bounty. */
10
+ pending: TokenAmount;
11
+ /** Keeper bounty on `pending` at the maximum fee. */
12
+ maxBounty: TokenAmount;
13
+ /** `deposited + pending - maxBounty`: the least this epoch receives from what is deposited or held now. */
14
+ projected: TokenAmount;
15
+ }
16
+ export interface RentStreamerEpochProjection {
17
+ /** Streamed token (USDC). */
18
+ token: string;
19
+ numEpochs: number;
20
+ killed: boolean;
21
+ /** Whether the current epoch's release already happened. */
22
+ currentReleased: boolean;
23
+ /** Token sent to the streamer and not yet split into buckets: balance - processed. */
24
+ unsynced: TokenAmount;
25
+ current: RentStreamerEpochAmount;
26
+ next: RentStreamerEpochAmount;
27
+ }
28
+ /**
29
+ * Projects what the RWA gauge system receives from the RentStreamer in the current and the next epoch, counting
30
+ * token sent to the streamer but not yet synced as it will be split: from the current epoch when the current epoch
31
+ * is not released yet, otherwise from the next one. Reads every value at one block.
32
+ *
33
+ * Assumes the current epoch is released before it ends; an unreleased bucket rolls into the next release. Rent sent
34
+ * after this read adds to later buckets, so the next epoch's figure can still grow.
35
+ *
36
+ * @param chainId - The chain/network to use.
37
+ * @param provider - Optional ethers.js Provider instance.
38
+ * @returns Both epochs' deposited, pending and projected amounts in the streamed token's base units.
39
+ */
40
+ export declare const getEpochProjection: (chainId: ChainId, provider?: Provider) => Promise<RentStreamerEpochProjection>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raac/rpc",
3
- "version": "2.0.0-beta.10",
3
+ "version": "2.0.0-beta.11",
4
4
  "description": "RPC Library for RAAC",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",