|
| 1 | +import { providers } from 'ethers'; |
| 2 | +import BaseService from '../commons/BaseService'; |
| 3 | +import { UiIncentiveDataProviderValidator } from '../commons/validators/methodValidators'; |
| 4 | +import { isEthAddress } from '../commons/validators/paramValidators'; |
| 5 | +import { ReservesHelperInput, UserReservesHelperInput } from '../index'; |
| 6 | +import { UiIncentiveDataProviderV3 } from './typechain/UiIncentiveDataProviderV3'; |
| 7 | +import { UiIncentiveDataProviderV3__factory } from './typechain/UiIncentiveDataProviderV3__factory'; |
| 8 | +import { |
| 9 | + FullReservesIncentiveDataResponse, |
| 10 | + IncentiveData, |
| 11 | + IncentiveDataHumanized, |
| 12 | + ReservesIncentiveData, |
| 13 | + ReservesIncentiveDataHumanized, |
| 14 | + RewardInfo, |
| 15 | + UserIncentiveData, |
| 16 | + UserIncentiveDataHumanized, |
| 17 | + UserReservesIncentivesData, |
| 18 | + UserReservesIncentivesDataHumanized, |
| 19 | + UserRewardInfo, |
| 20 | +} from './types'; |
| 21 | +export * from './types'; |
| 22 | + |
| 23 | +export interface UiIncentiveDataProviderInterface { |
| 24 | + getFullReservesIncentiveData: ( |
| 25 | + args: UserReservesHelperInput, |
| 26 | + ) => Promise<FullReservesIncentiveDataResponse>; |
| 27 | + getReservesIncentivesData: ( |
| 28 | + args: ReservesHelperInput, |
| 29 | + ) => Promise<ReservesIncentiveData[]>; |
| 30 | + getUserReservesIncentivesData: ( |
| 31 | + args: UserReservesHelperInput, |
| 32 | + ) => Promise<UserReservesIncentivesData[]>; |
| 33 | + getReservesIncentivesDataHumanized: ( |
| 34 | + args: ReservesHelperInput, |
| 35 | + ) => Promise<ReservesIncentiveDataHumanized[]>; |
| 36 | + getUserReservesIncentivesDataHumanized: ( |
| 37 | + args: UserReservesHelperInput, |
| 38 | + ) => Promise<UserReservesIncentivesDataHumanized[]>; |
| 39 | +} |
| 40 | +export interface FeedResultSuccessful { |
| 41 | + rewardTokenAddress: string; |
| 42 | + answer: string; |
| 43 | + updatedAt: number; |
| 44 | + decimals: number; |
| 45 | +} |
| 46 | + |
| 47 | +export interface UiIncentiveDataProviderContext { |
| 48 | + uiIncentiveDataProviderAddress: string; |
| 49 | + provider: providers.Provider; |
| 50 | + chainId: number; |
| 51 | +} |
| 52 | + |
| 53 | +export class UiIncentiveDataProvider |
| 54 | + extends BaseService<UiIncentiveDataProviderV3> |
| 55 | + implements UiIncentiveDataProviderInterface |
| 56 | +{ |
| 57 | + readonly uiIncentiveDataProviderAddress: string; |
| 58 | + |
| 59 | + readonly chainId: number; |
| 60 | + |
| 61 | + /** |
| 62 | + * Constructor |
| 63 | + * @param context The ui incentive data provider context |
| 64 | + */ |
| 65 | + public constructor({ |
| 66 | + provider, |
| 67 | + uiIncentiveDataProviderAddress, |
| 68 | + chainId, |
| 69 | + }: UiIncentiveDataProviderContext) { |
| 70 | + super(provider, UiIncentiveDataProviderV3__factory); |
| 71 | + this.uiIncentiveDataProviderAddress = uiIncentiveDataProviderAddress; |
| 72 | + this.chainId = chainId; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the full reserve incentive data for the lending pool and the user |
| 77 | + * @param user The user address |
| 78 | + */ |
| 79 | + @UiIncentiveDataProviderValidator |
| 80 | + public async getFullReservesIncentiveData( |
| 81 | + @isEthAddress('user') |
| 82 | + @isEthAddress('lendingPoolAddressProvider') |
| 83 | + { user, lendingPoolAddressProvider }: UserReservesHelperInput, |
| 84 | + ): Promise<FullReservesIncentiveDataResponse> { |
| 85 | + const uiIncentiveContract = this.getContractInstance( |
| 86 | + this.uiIncentiveDataProviderAddress, |
| 87 | + ); |
| 88 | + |
| 89 | + return uiIncentiveContract.getFullReservesIncentiveData( |
| 90 | + lendingPoolAddressProvider, |
| 91 | + user, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the reserve incentive data for the lending pool |
| 97 | + */ |
| 98 | + @UiIncentiveDataProviderValidator |
| 99 | + public async getReservesIncentivesData( |
| 100 | + @isEthAddress('lendingPoolAddressProvider') |
| 101 | + { lendingPoolAddressProvider }: ReservesHelperInput, |
| 102 | + ): Promise<ReservesIncentiveData[]> { |
| 103 | + const uiIncentiveContract = this.getContractInstance( |
| 104 | + this.uiIncentiveDataProviderAddress, |
| 105 | + ); |
| 106 | + |
| 107 | + return uiIncentiveContract.getReservesIncentivesData( |
| 108 | + lendingPoolAddressProvider, |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the reserve incentive data for the user |
| 114 | + * @param user The user address |
| 115 | + */ |
| 116 | + @UiIncentiveDataProviderValidator |
| 117 | + public async getUserReservesIncentivesData( |
| 118 | + @isEthAddress('user') |
| 119 | + @isEthAddress('lendingPoolAddressProvider') |
| 120 | + { user, lendingPoolAddressProvider }: UserReservesHelperInput, |
| 121 | + ): Promise<UserReservesIncentivesData[]> { |
| 122 | + const uiIncentiveContract = this.getContractInstance( |
| 123 | + this.uiIncentiveDataProviderAddress, |
| 124 | + ); |
| 125 | + |
| 126 | + return uiIncentiveContract.getUserReservesIncentivesData( |
| 127 | + lendingPoolAddressProvider, |
| 128 | + user, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + @UiIncentiveDataProviderValidator |
| 133 | + public async getReservesIncentivesDataHumanized( |
| 134 | + @isEthAddress('lendingPoolAddressProvider') |
| 135 | + { lendingPoolAddressProvider }: ReservesHelperInput, |
| 136 | + ): Promise<ReservesIncentiveDataHumanized[]> { |
| 137 | + const response: ReservesIncentiveData[] = |
| 138 | + await this.getReservesIncentivesData({ lendingPoolAddressProvider }); |
| 139 | + |
| 140 | + return response.map(r => ({ |
| 141 | + id: `${this.chainId}-${r.underlyingAsset}-${lendingPoolAddressProvider}`.toLowerCase(), |
| 142 | + underlyingAsset: r.underlyingAsset.toLowerCase(), |
| 143 | + aIncentiveData: this._formatIncentiveData(r.aIncentiveData), |
| 144 | + vIncentiveData: this._formatIncentiveData(r.vIncentiveData), |
| 145 | + })); |
| 146 | + } |
| 147 | + |
| 148 | + @UiIncentiveDataProviderValidator |
| 149 | + public async getUserReservesIncentivesDataHumanized( |
| 150 | + @isEthAddress('user') |
| 151 | + @isEthAddress('lendingPoolAddressProvider') |
| 152 | + { user, lendingPoolAddressProvider }: UserReservesHelperInput, |
| 153 | + ): Promise<UserReservesIncentivesDataHumanized[]> { |
| 154 | + const response: UserReservesIncentivesData[] = |
| 155 | + await this.getUserReservesIncentivesData({ |
| 156 | + user, |
| 157 | + lendingPoolAddressProvider, |
| 158 | + }); |
| 159 | + |
| 160 | + return response.map(r => ({ |
| 161 | + id: `${this.chainId}-${user}-${r.underlyingAsset}-${lendingPoolAddressProvider}`.toLowerCase(), |
| 162 | + underlyingAsset: r.underlyingAsset.toLowerCase(), |
| 163 | + aTokenIncentivesUserData: this._formatUserIncentiveData( |
| 164 | + r.aTokenIncentivesUserData, |
| 165 | + ), |
| 166 | + vTokenIncentivesUserData: this._formatUserIncentiveData( |
| 167 | + r.vTokenIncentivesUserData, |
| 168 | + ), |
| 169 | + })); |
| 170 | + } |
| 171 | + |
| 172 | + private _formatIncentiveData(data: IncentiveData): IncentiveDataHumanized { |
| 173 | + return { |
| 174 | + tokenAddress: data.tokenAddress, |
| 175 | + incentiveControllerAddress: data.incentiveControllerAddress, |
| 176 | + rewardsTokenInformation: data.rewardsTokenInformation.map( |
| 177 | + (rawRewardInfo: RewardInfo) => ({ |
| 178 | + precision: rawRewardInfo.precision, |
| 179 | + rewardTokenAddress: rawRewardInfo.rewardTokenAddress, |
| 180 | + rewardTokenDecimals: rawRewardInfo.rewardTokenDecimals, |
| 181 | + emissionPerSecond: rawRewardInfo.emissionPerSecond.toString(), |
| 182 | + incentivesLastUpdateTimestamp: |
| 183 | + rawRewardInfo.incentivesLastUpdateTimestamp.toNumber(), |
| 184 | + tokenIncentivesIndex: rawRewardInfo.tokenIncentivesIndex.toString(), |
| 185 | + emissionEndTimestamp: rawRewardInfo.emissionEndTimestamp.toNumber(), |
| 186 | + rewardTokenSymbol: rawRewardInfo.rewardTokenSymbol, |
| 187 | + rewardOracleAddress: rawRewardInfo.rewardOracleAddress, |
| 188 | + rewardPriceFeed: rawRewardInfo.rewardPriceFeed.toString(), |
| 189 | + priceFeedDecimals: rawRewardInfo.priceFeedDecimals, |
| 190 | + }), |
| 191 | + ), |
| 192 | + }; |
| 193 | + } |
| 194 | + |
| 195 | + private _formatUserIncentiveData( |
| 196 | + data: UserIncentiveData, |
| 197 | + ): UserIncentiveDataHumanized { |
| 198 | + return { |
| 199 | + tokenAddress: data.tokenAddress, |
| 200 | + incentiveControllerAddress: data.incentiveControllerAddress, |
| 201 | + userRewardsInformation: data.userRewardsInformation.map( |
| 202 | + (userRewardInformation: UserRewardInfo) => ({ |
| 203 | + rewardTokenAddress: userRewardInformation.rewardTokenAddress, |
| 204 | + rewardTokenDecimals: userRewardInformation.rewardTokenDecimals, |
| 205 | + tokenIncentivesUserIndex: |
| 206 | + userRewardInformation.tokenIncentivesUserIndex.toString(), |
| 207 | + userUnclaimedRewards: |
| 208 | + userRewardInformation.userUnclaimedRewards.toString(), |
| 209 | + rewardTokenSymbol: userRewardInformation.rewardTokenSymbol, |
| 210 | + rewardOracleAddress: userRewardInformation.rewardOracleAddress, |
| 211 | + rewardPriceFeed: userRewardInformation.rewardPriceFeed.toString(), |
| 212 | + priceFeedDecimals: userRewardInformation.priceFeedDecimals, |
| 213 | + }), |
| 214 | + ), |
| 215 | + }; |
| 216 | + } |
| 217 | +} |
0 commit comments