import BigNumberJs from 'bignumber.js'; import { providers } from 'ethers'; import BaseService from '../commons/BaseService'; import { eEthereumTxType, EthereumTransactionTypeExtended, tEthereumAddress, transactionType, } from '../commons/types'; import { isEthAddress, isValidTokenIdArray, } from '../commons/validators/paramValidators'; import { NToken } from './typechain/NToken'; import { NToken__factory } from './typechain/NToken__factory'; const MULTIPLIER_UNIT = 10 ** 18; export type DelegateParams = { user: tEthereumAddress; delegate: tEthereumAddress; tokenIds: string[]; enable: boolean; }; export class NTokenService extends BaseService { instance: NToken; constructor(provider: providers.Provider, address: string) { super(provider, NToken__factory); this.instance = this.getContractInstance(address); this.delegateForToken = this.delegateForToken.bind(this); } public async getUserAvgMultiplier(user: string): Promise { const num = await this.instance.avgMultiplierOf(user); return new BigNumberJs(num.toString()).div(MULTIPLIER_UNIT); } public async getTraitMultiplier(tokenId: string): Promise { const num = await this.instance.getTraitMultiplier(tokenId); return new BigNumberJs(num.toString()).div(MULTIPLIER_UNIT); } public delegateForToken( @isEthAddress('user') @isEthAddress('delegate') @isValidTokenIdArray('tokenIds') { user, delegate, tokenIds, enable }: DelegateParams, ): EthereumTransactionTypeExtended { const tx: () => Promise = this.generateTxCallback({ rawTxMethod: async () => this.instance.populateTransaction.delegateForToken( delegate, tokenIds, enable, ), from: user, }); return { tx, txType: eEthereumTxType.DELEGATE, gas: this.generateTxPriceEstimation([], tx), }; } }