From f557e5ca57fc7a8b6c6471b6478da7ab757970d1 Mon Sep 17 00:00:00 2001 From: Luis Perrone Date: Wed, 20 Nov 2024 17:35:25 -0500 Subject: [PATCH] Modify pending rewards (#736) * Add lifetime rewards to each entity for pending rewards * Dont add a breaking change * Reduce & reuse * Attempt to fix tests --- packages/distributor-oracle/src/client.ts | 87 ++++++++++++++++++++--- 1 file changed, 78 insertions(+), 9 deletions(-) diff --git a/packages/distributor-oracle/src/client.ts b/packages/distributor-oracle/src/client.ts index 588f70d96..dea80c542 100644 --- a/packages/distributor-oracle/src/client.ts +++ b/packages/distributor-oracle/src/client.ts @@ -107,20 +107,14 @@ export async function getBulkRewards( }); } -export async function getPendingRewards( +export async function getRecipients( program: Program, lazyDistributor: PublicKey, dao: PublicKey, entityKeys: string[], encoding: BufferEncoding | "b58" = "b58", forceRequery = false -): Promise> { - const oracleRewards = await getBulkRewards( - program, - lazyDistributor, - entityKeys - ); - +) { const hemProgram = await init(program.provider as AnchorProvider); const cache = await getSingleton(hemProgram.provider.connection); const keyToAssetKs = entityKeys.map((entityKey) => { @@ -161,12 +155,37 @@ export async function getPendingRewards( false, forceRequery ); - const withRecipients = recipients.map((recipient, index) => { + + return recipients.map((recipient, index) => { return { entityKey: entityKeys[index], recipientAcc: recipient?.info, }; }); +} + +export async function getPendingRewards( + program: Program, + lazyDistributor: PublicKey, + dao: PublicKey, + entityKeys: string[], + encoding: BufferEncoding | "b58" = "b58", + forceRequery = false +): Promise> { + const oracleRewards = await getBulkRewards( + program, + lazyDistributor, + entityKeys + ); + + const withRecipients = await getRecipients( + program, + lazyDistributor, + dao, + entityKeys, + encoding, + forceRequery + ); return withRecipients.reduce((acc, { entityKey, recipientAcc }) => { const sortedOracleRewards = oracleRewards @@ -184,6 +203,56 @@ export async function getPendingRewards( }, {} as Record); } +export async function getPendingAndLifetimeRewards( + program: Program, + lazyDistributor: PublicKey, + dao: PublicKey, + entityKeys: string[], + encoding: BufferEncoding | "b58" = "b58", + forceRequery = false +): Promise< + Record< + string, + { + pendingRewards: string; + lifetimeRewards: string; + } + > +> { + const oracleRewards = await getBulkRewards( + program, + lazyDistributor, + entityKeys + ); + + const withRecipients = await getRecipients( + program, + lazyDistributor, + dao, + entityKeys, + encoding, + forceRequery + ); + + return withRecipients.reduce((acc, { entityKey, recipientAcc }) => { + const sortedOracleRewards = oracleRewards + .map((rew) => rew.currentRewards[entityKey] || new BN(0)) + .sort((a, b) => new BN(a).sub(new BN(b)).toNumber()); + + const oracleMedian = new BN( + sortedOracleRewards[Math.floor(sortedOracleRewards.length / 2)] + ); + + const subbed = oracleMedian.sub(recipientAcc?.totalRewards || new BN(0)); + acc[entityKey] = { + pendingRewards: subbed.toString(), + lifetimeRewards: recipientAcc?.totalRewards?.toString() || "0", + }; + + return acc; + }, {} as Record); +} + export async function formBulkTransactions({ program: lazyDistributorProgram, rewardsOracleProgram,