@ardier16/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
265 lines • 11.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidatorsInstance = void 0;
const SystemContractInstance_1 = require("../../SystemContractInstance");
const TimeLockHelperInstance_1 = require("../../TimeLockHelperInstance");
/**
* Validators instance to interact with Validators contract.
* See [onchain documentation](@system-contracts-repo/@network/Validators/) for more details.
* An instance of this class for a deployed network can be obtained via {@link ContractRegistryInstance.validators}
*/
class ValidatorsInstance extends SystemContractInstance_1.SystemContractInstance {
constructor(web3, address) {
super(web3, ValidatorsInstance.abi, address);
this.timeLockHelper = new TimeLockHelperInstance_1.TimeLockHelperInstance(web3, ValidatorsInstance.abi, address);
}
/**
* @deprecated Use commitStake, instead
*/
async commitCollateral(txOptions) {
console.warn('ValidatorsInstance.commitCollateral is deprecated. Use commitStake, instead.');
return this.submitTransaction(this.instance.methods.commitStake(), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#commitstake)
*/
async commitStake(txOptions) {
return this.submitTransaction(this.instance.methods.commitStake(), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#announcewithdrawal)
*/
async announceWithdrawal(amount, txOptions) {
return this.submitTransaction(this.instance.methods.announceWithdrawal(amount), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#withdraw)
*/
async withdraw(amount, payTo, txOptions) {
return this.submitTransaction(this.instance.methods.withdraw(amount, payTo), txOptions);
}
/**
* Validators list.
* @returns validator addresses.
*/
async getLongList() {
const longlistAddress = await this.instance.methods.longList().call();
const stakes = new SystemContractInstance_1.SystemContractInstance(this.adapter.web3, 'AddressStorageStakes.json', longlistAddress);
const longList = await stakes.instance.methods.getAddresses().call();
return longList;
}
/**
* Validators list of addresses and balances.
* @returns validator addresses and balances.
*/
async getLongListWithBalance() {
const longlistAddress = await this.instance.methods.longList().call();
const stakes = new SystemContractInstance_1.SystemContractInstance(this.adapter.web3, 'AddressStorageStakes.json', longlistAddress);
const longList = await stakes.instance.methods.getAddresses().call();
const longListWithBalances = longList.map(async (e) => {
return {
address: e,
balance: this.adapter.web3.utils.fromWei(await this.instance.methods.getAccountableTotalStake(e).call())
};
});
return Promise.all(longListWithBalances);
}
/**
* Validators recent list of addresses and balances.
* @returns recent used validator addresses and balances.
*/
async getShortList() {
const shortlist = await this.instance.methods.getValidatorShortList().call();
const listEntries = shortlist.map(e => {
return {
address: e[0],
balance: this.adapter.web3.utils.fromWei(e[1])
};
});
return listEntries;
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getshortlistmaxlength)
*/
async getShortListMaxLength() {
return this.instance.methods.getShortListMaxLength().call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#entershortlist)
*/
async enterShortList(txOptions) {
return this.submitTransaction(this.instance.methods.enterShortList(), txOptions);
}
/** @deprecated The interest rate has to be set on the ValidationRewardPoolsInstance */
async setInterestRate(_qiv, _txOptions) {
throw new Error('The interest rate has to be set on the ValidationRewardPoolsInstance');
}
/** @deprecated The delegators share has to be set on the ValidationRewardPoolsInstance */
async setDelegatorsShare(_qsv, _txOptions) {
throw new Error('The delegators share has to be set on the ValidationRewardPoolsInstance');
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#isinshortlist)
*/
async isInShortList(user) {
return this.instance.methods.isInShortList(user).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#isinlonglist)
*/
async isInLongList(user) {
return this.instance.methods.isInLongList(user).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getslashingproposalids)
*/
async getSlashingProposalIds(validator) {
return this.instance.methods.getSlashingProposalIds(validator).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getlockinfo)
*/
async getLockInfo(who) {
return await this.votingWeightProxy.getLockInfo(this.address, who);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#makesnapshot)
*/
async makeSnapshot(txOptions) {
return this.submitTransaction(this.instance.methods.makeSnapshot(), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#applyslashing)
*/
async applySlashing(proposalId, validator, amountToSlash, txOptions) {
return this.submitTransaction(this.instance.methods.applySlashing(proposalId, validator, amountToSlash), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#addslashingproposal)
*/
async addSlashingProposal(validator, slashingProposalId, txOptions) {
return this.submitTransaction(this.instance.methods.addSlashingProposal(validator, slashingProposalId), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#calculatependingslashingsamount)
*/
async calculatePendingSlashingsAmount(validator, endTime) {
return this.instance.methods.calculatePendingSlashingsAmount(validator, endTime).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getvalidatorslist)
*/
async getValidatorsList() {
return this.instance.methods.getValidatorsList().call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#purgependingslashings)
*/
async purgePendingSlashings(validator, txOptions) {
return this.submitTransaction(this.instance.methods.purgePendingSlashings(validator), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#refreshdelegatedstake)
*/
async refreshDelegatedStake(delegatedTo, txOptions) {
return this.submitTransaction(this.instance.methods.refreshDelegatedStake(delegatedTo), txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#validatorsinfos)
*/
async getWithdrawalInfo(address) {
const withdrawalInfo = await this.instance.methods.validatorsInfos(address).call();
return withdrawalInfo;
}
/** @deprecated Use getAccountableSelfStake , instead*/
async getValidatorsOwnStake(address) {
return this.instance.methods.getAccountableSelfStake(address).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getaccountableselfstake)
*/
async getAccountableSelfStake(address) {
return this.instance.methods.getAccountableSelfStake(address).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getaccountabletotalstake)
*/
async getAccountableTotalStake(address) {
return this.instance.methods.getAccountableTotalStake(address).call();
}
/** @deprecated Use getSelfStake , instead*/
async getValidatorsOwnStakeWithoutPendingWithdrawal(address) {
return this.instance.methods.getSelfStake(address).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getselfstake)
*/
async getSelfStake(address) {
return this.instance.methods.getSelfStake(address).call();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getvalidatortotalstake)
*/
async getValidatorTotalStake(address) {
return this.instance.methods.getValidatorTotalStake(address).call();
}
/** @deprecated has to be called via ValidationRewardPoolsInstance */
async getInterestRate(_address) {
throw new Error('interest rate has to be called via ValidationRewardPoolsInstance');
}
/** @deprecated has to be called via ValidationRewardPoolsInstance */
async getDelegatorsShare(_address) {
throw new Error('delegators share has to be called via ValidationRewardPoolsInstance');
}
/**
* Retrieve information about Validators'stake
* @param address Validator
* @returns General information about Validator's stakes {@link ValidatorInfo}
*/
async getValidatorInfo(address) {
const selfStake = this.adapter.web3.utils.fromWei(await this.instance.methods.getAccountableSelfStake(address).call());
const pendingWithdrawal = await this.getWithdrawalInfo(address);
const pendingStake = this.adapter.web3.utils.fromWei(pendingWithdrawal.amount);
const delegatedStake = this.adapter.web3.utils.fromWei(await this.instance.methods.getValidatorDelegatedStake(address).call());
const totalStake = this.adapter.web3.utils.fromWei(await this.instance.methods.getValidatorTotalStake(address).call());
const accountableStake = this.adapter.web3.utils.fromWei(await this.instance.methods.getAccountableTotalStake(address).call());
const currentDelegationFactor = Number.parseFloat(totalStake) / Number.parseFloat(selfStake);
return {
address,
selfStake,
pendingStake,
delegatedStake,
totalStake,
currentDelegationFactor,
accountableStake
};
}
/**
* {@link TimeLockHelperInstance.depositOnBehalfOf | Internal documentation}
*/
async depositOnBehalfOf(account, start, end, txOptions) {
return this.timeLockHelper.depositOnBehalfOf(account, start, end, txOptions);
}
/**
* {@link TimeLockHelperInstance.getTimeLocks | Internal documentation}
*/
async getTimeLocks(account) {
return this.timeLockHelper.getTimeLocks(account);
}
/**
* {@link TimeLockHelperInstance.getMinimumBalance | Internal documentation}
*/
async getMinimumBalance(account, timestamp) {
return this.timeLockHelper.getMinimumBalance(account, timestamp);
}
/**
* {@link TimeLockHelperInstance.purgeTimeLocks | Internal documentation}
*/
async purgeTimeLocks(account, txOptions) {
return this.timeLockHelper.purgeTimeLocks(account, txOptions);
}
}
exports.ValidatorsInstance = ValidatorsInstance;
ValidatorsInstance.registryKey = 'governance.validators';
ValidatorsInstance.abi = 'Validators.json';
//# sourceMappingURL=ValidatorsInstance.js.map