Skip to content

Commit

Permalink
Modify pending rewards (#736)
Browse files Browse the repository at this point in the history
* Add lifetime rewards to each entity for pending rewards

* Dont add a breaking change

* Reduce & reuse

* Attempt to fix tests
  • Loading branch information
Perronef5 authored Nov 20, 2024
1 parent aa81433 commit f557e5c
Showing 1 changed file with 78 additions and 9 deletions.
87 changes: 78 additions & 9 deletions packages/distributor-oracle/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,14 @@ export async function getBulkRewards(
});
}

export async function getPendingRewards(
export async function getRecipients(
program: Program<LazyDistributor>,
lazyDistributor: PublicKey,
dao: PublicKey,
entityKeys: string[],
encoding: BufferEncoding | "b58" = "b58",
forceRequery = false
): Promise<Record<string, string>> {
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) => {
Expand Down Expand Up @@ -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>,
lazyDistributor: PublicKey,
dao: PublicKey,
entityKeys: string[],
encoding: BufferEncoding | "b58" = "b58",
forceRequery = false
): Promise<Record<string, 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
Expand All @@ -184,6 +203,56 @@ export async function getPendingRewards(
}, {} as Record<string, string>);
}

export async function getPendingAndLifetimeRewards(
program: Program<LazyDistributor>,
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<string, { pendingRewards: string; lifetimeRewards: string }>);
}

export async function formBulkTransactions({
program: lazyDistributorProgram,
rewardsOracleProgram,
Expand Down

0 comments on commit f557e5c

Please sign in to comment.