import { BigNumber, BigNumberish, constants, providers } from 'ethers'; import BaseService from '../commons/BaseService'; import { eEthereumTxType, EthereumTransactionTypeExtended, transactionType, } from '../commons/types'; import { IncentivesValidator } from '../commons/validators/methodValidators'; import { isEthAddress, isEthAddressArray, } from '../commons/validators/paramValidators'; import { IRewardsController } from './typechain/IRewardsController'; import { IRewardsControllerFactory } from './typechain/IRewardsControllerFactory'; export type ClaimRewardsV2MethodType = { user: string; assets: string[]; reward: string; to?: string; incentivesControllerAddress: string; }; export type getAllUserRewardsReq = { user: string; assets: string[]; incentivesControllerAddress: string; }; export type getUserRewardsReq = { user: string; assets: string[]; reward: string; incentivesControllerAddress: string; }; export type getAllUserRewardsRsp = { rewardsList: string[]; unclaimedAmounts: BigNumber[]; }; export type ClaimAllRewardsV2MethodType = { user: string; assets: string[]; to?: string; incentivesControllerAddress: string; }; export interface RewardsControllerInterface { claimRewards: ( args: ClaimRewardsV2MethodType, ) => EthereumTransactionTypeExtended[]; claimAllRewards: ( args: ClaimAllRewardsV2MethodType, ) => EthereumTransactionTypeExtended[]; getAllUserRewards: ( args: getAllUserRewardsReq, ) => Promise; getUserRewards: (args: getUserRewardsReq) => Promise; } export class RewardsController extends BaseService implements RewardsControllerInterface { constructor(provider: providers.Provider) { super(provider, IRewardsControllerFactory); } @IncentivesValidator public claimRewards( @isEthAddress('user') @isEthAddress('incentivesControllerAddress') @isEthAddress('to') @isEthAddress('reward') @isEthAddressArray('assets') { user, assets, to, incentivesControllerAddress, reward, }: ClaimRewardsV2MethodType, ): EthereumTransactionTypeExtended[] { const incentivesContract: IRewardsController = this.getContractInstance( incentivesControllerAddress, ); const txCallback: () => Promise = this.generateTxCallback({ rawTxMethod: async () => incentivesContract.populateTransaction.claimRewards( assets, constants.MaxUint256.toString(), to ?? user, reward, ), from: user, }); return [ { tx: txCallback, txType: eEthereumTxType.REWARD_ACTION, gas: this.generateTxPriceEstimation([], txCallback), }, ]; } @IncentivesValidator public claimAllRewards( @isEthAddress('user') @isEthAddress('incentivesControllerAddress') @isEthAddress('to') @isEthAddressArray('assets') { user, assets, to, incentivesControllerAddress, }: ClaimAllRewardsV2MethodType, ): EthereumTransactionTypeExtended[] { const incentivesContract: IRewardsController = this.getContractInstance( incentivesControllerAddress, ); const txCallback: () => Promise = this.generateTxCallback({ rawTxMethod: async () => incentivesContract.populateTransaction.claimAllRewards( assets, to ?? user, ), from: user, }); return [ { tx: txCallback, txType: eEthereumTxType.REWARD_ACTION, gas: this.generateTxPriceEstimation([], txCallback), }, ]; } public async getUserRewards( @isEthAddress('user') @isEthAddress('incentivesControllerAddress') @isEthAddress('reward') @isEthAddressArray('assets') { user, assets, incentivesControllerAddress, reward }: getUserRewardsReq, ): Promise { const incentivesContract: IRewardsController = this.getContractInstance( incentivesControllerAddress, ); return incentivesContract.getUserRewards(assets, user, reward); } public async getAllUserRewards( @isEthAddress('user') @isEthAddress('incentivesControllerAddress') @isEthAddressArray('assets') { user, assets, incentivesControllerAddress }: getAllUserRewardsReq, ): Promise { const incentivesContract: IRewardsController = this.getContractInstance( incentivesControllerAddress, ); const { 0: rewardsList, 1: unclaimedAmounts } = await incentivesContract.getAllUserRewards(assets, user); return { rewardsList, unclaimedAmounts, }; } }