|
1 | | -import { Contract } from 'ethers' |
2 | | -import { BigNumber } from '@ethersproject/bignumber' |
3 | | -import { Claim } from '@generationsoftware/pt-v5-utils-js' |
| 1 | +import { Contract } from "ethers"; |
| 2 | +import { BigNumber } from "@ethersproject/bignumber"; |
| 3 | +import { Claim } from "@generationsoftware/pt-v5-utils-js"; |
4 | 4 |
|
5 | 5 | export interface TierPrizeAmounts { |
6 | 6 | [tier: string]: BigNumber; |
7 | 7 | } |
8 | 8 |
|
9 | 9 | interface amountsAsString { |
10 | 10 | [key: string]: string; |
11 | | -}; |
| 11 | +} |
12 | 12 |
|
13 | 13 | interface claimTiers { |
14 | 14 | [key: string]: []; |
15 | | -}; |
| 15 | +} |
16 | 16 |
|
17 | 17 | interface ClaimWithAmount extends Claim { |
18 | | - amount: string |
| 18 | + amount: string; |
19 | 19 | } |
20 | 20 |
|
21 | 21 | interface ClaimWithAmountAndTwabs extends ClaimWithAmount { |
22 | | - userTwab: string |
23 | | - totalSupplyTwab: string |
| 22 | + userTwab: string; |
| 23 | + totalSupplyTwab: string; |
24 | 24 | } |
25 | 25 |
|
26 | 26 | export function sumPrizeAmounts(tierPrizeAmounts: TierPrizeAmounts): string { |
27 | 27 | return Object.values(tierPrizeAmounts) |
28 | 28 | .reduce((a, b) => a.add(b), BigNumber.from(0)) |
29 | | - .toString() |
| 29 | + .toString(); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | export function mapTierPrizeAmountsToString(tierPrizeAmounts: TierPrizeAmounts) { |
33 | 33 | const obj: amountsAsString = {}; |
34 | | - |
| 34 | + |
35 | 35 | for (const entry of Object.entries(tierPrizeAmounts)) { |
36 | 36 | const [key, value] = entry; |
37 | 37 | obj[key] = BigNumber.from(value).toString(); |
38 | 38 | } |
39 | 39 |
|
40 | 40 | return obj; |
41 | | -}; |
| 41 | +} |
42 | 42 |
|
43 | | -export function addTierPrizeAmountsToClaims(claims: Claim[], tierPrizeAmounts: TierPrizeAmounts): ClaimWithAmount[] { |
44 | | - // const claimsByTier = groupByTier(claims, tierPrizeAmounts) |
45 | | - const claimsWithAmounts:ClaimWithAmount[] = [] |
| 43 | +export function addTierPrizeAmountsToClaims( |
| 44 | + claims: Claim[], |
| 45 | + tierPrizeAmounts: TierPrizeAmounts |
| 46 | +): ClaimWithAmount[] { |
| 47 | + const claimsWithAmounts: ClaimWithAmount[] = []; |
46 | 48 |
|
47 | 49 | const tierAmountPerPrize: amountsAsString = {}; |
48 | 50 | for (const tier of Object.keys(tierPrizeAmounts)) { |
49 | | - tierAmountPerPrize[tier] = '0'; |
| 51 | + tierAmountPerPrize[tier] = "0"; |
50 | 52 | } |
51 | 53 |
|
52 | 54 | for (const tier of Object.entries(tierPrizeAmounts)) { |
53 | | - const [key, value] = tier |
54 | | - tierAmountPerPrize[key] = BigNumber.from(value).toString() |
55 | | - |
56 | | - // I don't believe we want to divide the amount since the getTierPrizeSize is the |
57 | | - // size of each prize, not the shared amount for the tier |
58 | | - // |
59 | | - // const numberOfPrizes = claimsByTier[key].length |
60 | | - // if (numberOfPrizes > 0) { |
61 | | - // tierAmountPerPrize[key] = BigNumber.from(value).div(numberOfPrizes).toString() |
62 | | - // } |
| 55 | + const [key, value] = tier; |
| 56 | + tierAmountPerPrize[key] = BigNumber.from(value).toString(); |
63 | 57 | } |
64 | 58 |
|
65 | 59 | for (const claim of claims) { |
66 | | - const claimWithAmount = { ...claim, amount: tierAmountPerPrize[claim.tier.toString()] } |
67 | | - claimsWithAmounts.push(claimWithAmount) |
| 60 | + const claimWithAmount = { ...claim, amount: tierAmountPerPrize[claim.tier.toString()] }; |
| 61 | + claimsWithAmounts.push(claimWithAmount); |
68 | 62 | } |
69 | 63 |
|
70 | | - return claimsWithAmounts |
71 | | -} |
72 | | - |
73 | | -const groupByTier = (claims: any, tierPrizeAmounts: TierPrizeAmounts) => { |
74 | | - const initialClaims: claimTiers = {}; |
75 | | - for (const tier of Object.keys(tierPrizeAmounts)) { |
76 | | - initialClaims[tier] = []; |
77 | | - } |
78 | | - |
79 | | - return claims.reduce(function (accumulator: any, value: any) { |
80 | | - accumulator[value.tier] = accumulator[value.tier] || []; |
81 | | - accumulator[value.tier].push(value); |
82 | | - return accumulator; |
83 | | - }, initialClaims); |
| 64 | + return claimsWithAmounts; |
84 | 65 | } |
85 | 66 |
|
86 | 67 | export async function addUserAndTotalSupplyTwabsToClaims( |
87 | 68 | claimsWithAmounts: ClaimWithAmount[], |
88 | 69 | tierAccrualDurationsInDraws: Record<string, BigNumber>, |
89 | 70 | prizePoolContract: Contract |
90 | 71 | ): Promise<ClaimWithAmountAndTwabs[]> { |
91 | | - const claimsWithAmountAndTwabs: ClaimWithAmountAndTwabs[] = [] |
| 72 | + const claimsWithAmountAndTwabs: ClaimWithAmountAndTwabs[] = []; |
92 | 73 |
|
93 | 74 | for (const claim of claimsWithAmounts) { |
94 | | - const tierDrawDuration = tierAccrualDurationsInDraws[claim.tier.toString()] |
| 75 | + const tierDrawDuration = tierAccrualDurationsInDraws[claim.tier.toString()]; |
95 | 76 |
|
96 | 77 | const twabs = await prizePoolContract.getVaultUserBalanceAndTotalSupplyTwab( |
97 | 78 | claim.vault, |
98 | 79 | claim.winner, |
99 | 80 | tierDrawDuration |
100 | | - ) |
| 81 | + ); |
101 | 82 |
|
102 | | - const claimWithAmountAndTwab = { ...claim, userTwab: twabs[0].toString(), totalSupplyTwab: twabs[1].toString() } |
103 | | - |
104 | | - claimsWithAmountAndTwabs.push(claimWithAmountAndTwab) |
| 83 | + const claimWithAmountAndTwab = { |
| 84 | + ...claim, |
| 85 | + userTwab: twabs[0].toString(), |
| 86 | + totalSupplyTwab: twabs[1].toString(), |
| 87 | + }; |
| 88 | + |
| 89 | + claimsWithAmountAndTwabs.push(claimWithAmountAndTwab); |
105 | 90 | } |
106 | 91 |
|
107 | | - return claimsWithAmountAndTwabs |
| 92 | + return claimsWithAmountAndTwabs; |
108 | 93 | } |
109 | | - |
0 commit comments