UNPKG

@q-dev/q-js-sdk

Version:

Typescript Library to interact with Q System Contracts

279 lines 12.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QVaultInstance = void 0; const unit_converter_1 = require("../../utils/unit-converter"); const bn_js_1 = __importDefault(require("bn.js")); const CompoundRateKeeperInstance_1 = require("../common/CompoundRateKeeperInstance"); const ERC20HelperInstance_1 = require("../ERC20HelperInstance"); /** * QVault instance to interact with QVault contract. * See [onchain documentation](@system-contracts-repo/@network/QVault/) for more details. * An instance of this class for a deployed network can be obtained via {@link ContractRegistryInstance.qVault} */ class QVaultInstance extends ERC20HelperInstance_1.ERC20HelperInstance { constructor(signerOrProvider, address) { super(signerOrProvider, QVaultInstance.abi, address); this.c = new unit_converter_1.UnitConverter(); } /** * [External documentation](@system-contracts-repo/@network/QVault/#aggregatednormalizedbalance) */ async aggregatedNormalizedBalance() { return (await this.instance.aggregatedNormalizedBalance()).toString(); } /** @deprecated use getCompoundRateKeeper */ async compoundRateKeeper() { return this.instance.compoundRateKeeper(); } /** * [External documentation](@system-contracts-repo/@network/QVault/#getcompoundratekeeper) */ async getCompoundRateKeeper() { const compoundRateKeeperAddress = await this.instance.compoundRateKeeper(); return new CompoundRateKeeperInstance_1.CompoundRateKeeperInstance(this.adapter.signerOrProvider, compoundRateKeeperAddress); } /** * [External documentation](@system-contracts-repo/@network/QVault/#deposit) */ async deposit(txOptions) { return this.submitTransaction('deposit', [], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#lock) */ async lock(amount, txOptions) { return this.submitTransaction('lock', [amount], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#announceunlock) */ async announceUnlock(amount, txOptions) { return this.submitTransaction('announceUnlock', [amount], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#unlock) */ async unlock(amount, txOptions) { return this.submitTransaction('unlock', [amount], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#withdraw) */ async withdraw(amount, txOptions) { return this.submitTransaction('withdraw', [amount], txOptions); } /** * Withdraw all available amount. * Use {@link QVaultInstance.getReadyToWithdrawBalance} * @param user account. * @returns Transaction receipt. */ async withdrawEntireBalance(user) { return this.withdraw(await this.getReadyToWithdrawBalance(user), { from: user }); } /** * [External documentation](@system-contracts-repo/@network/QVault/#updatecompoundrate) */ async updateCompoundRate(txOptions) { return this.submitTransaction('updateCompoundRate', [], txOptions); } /** @deprecated compound rate has to be updated via ValidationRewardPoolsInstance */ async updateValidatorsCompoundRate(_validator, _txOptions) { throw new Error('This has to be set via ValidationRewardPoolsInstance'); } /** * [External documentation](@system-contracts-repo/@network/QVault/#delegatestake) */ async delegateStake(delegatedTo, stakes, txOptions) { return this.submitTransaction('delegateStake', [delegatedTo, stakes], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#transferandcall) */ async transferAndCall(to, value, data, txOptions) { return this.submitTransaction('transferAndCall', [to, value, data], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#withdrawto) */ async withdrawTo(recipient, amount, txOptions) { return this.submitTransaction('withdrawTo', [recipient, amount], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#depositto) */ async depositTo(recipient, txOptions) { return this.submitTransaction('depositTo', [recipient], txOptions); } /** * Token amount that can be withdrawn at current moment. * This method takes into account all locks of QVault * @param user account * @returns Withdrawable amount */ async getReadyToWithdrawBalance(user) { const currentTime = new bn_js_1.default((await this.adapter.provider.getBlock('latest')).timestamp); const votingWeightInfo = await this.votingWeightProxy.getBaseVotingWeightInfo(user, currentTime.toString()); const votingAgentLockUntil = new bn_js_1.default(votingWeightInfo.lockedUntil); const lockInfo = await this.getLockInfo(user); const currentBalance = new bn_js_1.default(await this.balanceOf(user)); const nonLockedAmount = currentBalance.sub(new bn_js_1.default(lockInfo.lockedAmount)).sub(new bn_js_1.default(lockInfo.pendingUnlockAmount)); const minimumFromTimeLocks = new bn_js_1.default(await this.getMinimumBalance(user, currentTime.toString())); const minimumFromStakeDelegation = new bn_js_1.default(await this.getTotalDelegatedStake(user)); const minimumBalance = bn_js_1.default.max(minimumFromStakeDelegation, minimumFromTimeLocks); let readyToWithdrawBalance = currentBalance.sub(minimumBalance); if (currentBalance.toString() == '0') { return '0'; } if (readyToWithdrawBalance.lte(nonLockedAmount)) { return readyToWithdrawBalance.toString(); } const amountToUnlock = readyToWithdrawBalance.sub(nonLockedAmount); if (currentTime.gte(new bn_js_1.default(lockInfo.pendingUnlockTime))) { const pendingUnlockAmount = new bn_js_1.default(lockInfo.pendingUnlockAmount); if (amountToUnlock.lte(pendingUnlockAmount)) { return readyToWithdrawBalance.toString(); } if (currentTime.gte(new bn_js_1.default(lockInfo.lockedUntil)) && currentTime.gte(votingAgentLockUntil)) { return readyToWithdrawBalance.toString(); } readyToWithdrawBalance = readyToWithdrawBalance.add(pendingUnlockAmount); } readyToWithdrawBalance = readyToWithdrawBalance.sub(amountToUnlock); return readyToWithdrawBalance.toString(); } /** * Calculate total delegation stake for chosen account. * @param user account. * @returns total delegated stake. */ async getTotalDelegatedStake(user) { const validatorInfos = await this.instance.getDelegationsList(user); let totalDelegatedStake = new bn_js_1.default(0); for (let i = 0; i < validatorInfos.length; i++) { totalDelegatedStake = totalDelegatedStake.add(new bn_js_1.default(validatorInfos[i].actualStake.toString())); } return totalDelegatedStake.toString(); } /** * [External documentation](@system-contracts-repo/@network/QVault/#getdelegationslist) */ async getDelegationsList(user) { const validatorInfos = await this.instance.getDelegationsList(user); return validatorInfos.map(i => ({ validator: i.validator, actualStake: i.actualStake.toString(), normalizedStake: i.normalizedStake.toString(), compoundRate: i.compoundRate.toString(), latestUpdateOfCompoundRate: i.latestUpdateOfCompoundRate.toString(), idealStake: i.idealStake.toString(), claimableReward: i.claimableReward.toString() })); } /** * [External documentation](@system-contracts-repo/@network/QVault/#getnormalizedbalance) */ async getNormalizedBalance(userAddress) { return (await this.instance.getNormalizedBalance(userAddress)).toString(); } /** * [External documentation](@system-contracts-repo/@network/QVault/#claimstakedelegatorreward) */ async claimStakeDelegatorReward(txOptions) { return this.submitTransaction('claimStakeDelegatorReward', [], txOptions); } /** * [External documentation](@system-contracts-repo/@network/QVault/#depositfrompool) */ async depositFromPool(txOptions) { return this.submitTransaction('depositFromPool', [], txOptions); } /** @deprecated user balance has to be received via balanceOf */ async getUserBalance(user) { return (await this.instance.balanceOf(user)).toString(); } /** * [External documentation](@system-contracts-repo/@network/QVault/#getbalancedetails) */ async getBalanceDetails(user) { const details = await this.instance.getBalanceDetails({ from: user }); return { currentBalance: details.currentBalance.toString(), normalizedBalance: details.normalizedBalance.toString(), compoundRate: details.compoundRate.toString(), lastUpdateOfCompoundRate: details.lastUpdateOfCompoundRate.toString(), interestRate: details.interestRate.toString(), }; } /** * [External documentation](@system-contracts-repo/@network/QVault/#getlockinfo) */ async getLockInfo(user) { const result = await this.instance.getLockInfo({ from: user }); return { lockedAmount: result.lockedAmount.toString(), lockedUntil: result.lockedUntil.toString(), pendingUnlockAmount: result.pendingUnlockAmount.toString(), pendingUnlockTime: result.pendingUnlockTime.toString(), }; } /** * Calculate available to delegate amount * @param user account * @returns delegatable amount */ async getDelegatableAmount(user) { const balance = await this.balanceOf(user); const alreadyDelegated = this.c.toBigNumber(await this.getTotalDelegatedStake(user)); const delegatableAmount = this.c.toBigNumber(balance).minus(alreadyDelegated); return this.c.fromBigNumber(delegatableAmount); } /** * Calculate available to lock amount * @param user account * @returns lockable amount */ async getLockableAmount(user) { const balance = await this.balanceOf(user); const lockInfo = await this.getLockInfo(user); const alreadyLocked = this.c.toBigNumber(lockInfo.lockedAmount).plus(this.c.toBigNumber(lockInfo.pendingUnlockAmount)); const lockableAmount = this.c.toBigNumber(balance).minus(alreadyLocked); return this.c.fromBigNumber(lockableAmount); } /** * {@link TimeLockHelperInstance.depositOnBehalfOf | Internal documentation} */ async depositOnBehalfOf(account, start, end, txOptions) { return this.submitTransaction('depositOnBehalfOf', [account, start, end], txOptions); } /** * {@link TimeLockHelperInstance.getTimeLocks | Internal documentation} */ async getTimeLocks(account) { const timelocks = await this.instance.getTimeLocks(account); return timelocks.map(i => ({ amount: i.amount.toString(), releaseStart: i.releaseStart.toString(), releaseEnd: i.releaseEnd.toString() })); } /** * {@link TimeLockHelperInstance.getMinimumBalance | Internal documentation} */ async getMinimumBalance(account, timestamp) { return (await this.instance.getMinimumBalance(account, timestamp)).toString(); } /** * {@link TimeLockHelperInstance.purgeTimeLocks | Internal documentation} */ async purgeTimeLocks(account, txOptions) { return this.submitTransaction('purgeTimeLocks', [account], txOptions); } } exports.QVaultInstance = QVaultInstance; QVaultInstance.registryKey = 'tokeneconomics.qVault'; QVaultInstance.abi = 'QVault.json'; //# sourceMappingURL=QVaultInstance.js.map