@q-dev/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
240 lines • 10.2 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 TimeLockHelperInstance_1.TimeLockHelperInstance {
constructor(signerOrProvider, address) {
super(signerOrProvider, ValidatorsInstance.abi, address);
}
/**
* @deprecated Use commitStake, instead
*/
async commitCollateral(txOptions) {
console.warn('ValidatorsInstance.commitCollateral is deprecated. Use commitStake, instead.');
return this.submitTransaction('commitStake', [], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#commitstake)
*/
async commitStake(txOptions) {
return this.submitTransaction('commitStake', [], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#announcewithdrawal)
*/
async announceWithdrawal(amount, txOptions) {
return this.submitTransaction('announceWithdrawal', [amount], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#withdraw)
*/
async withdraw(amount, payTo, txOptions) {
return this.submitTransaction('withdraw', [amount, payTo], txOptions);
}
/**
* Validators list.
* @returns validator addresses.
*/
async getLongList() {
const longlistAddress = await this.instance.longList();
const stakes = new SystemContractInstance_1.SystemContractInstance(this.adapter.signerOrProvider, 'AddressStorageStakes.json', longlistAddress);
return stakes.instance.getAddresses();
}
/**
* Validators list of addresses and balances.
* @returns validator addresses and balances.
*/
async getLongListWithBalance() {
const longList = await this.getLongList();
const longListWithBalances = longList.map(async (e) => {
return {
address: e,
balance: this.adapter.fromWei(await this.instance.getAccountableTotalStake(e))
};
});
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.getValidatorShortList();
return shortlist.map(e => ({
address: e.validator,
balance: this.adapter.fromWei(e.amount)
}));
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getshortlistmaxlength)
*/
async getShortListMaxLength() {
return (await this.instance.getShortListMaxLength()).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#entershortlist)
*/
async enterShortList(txOptions) {
return this.submitTransaction('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.isInShortList(user);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#isinlonglist)
*/
async isInLongList(user) {
return this.instance.isInLongList(user);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getslashingproposalids)
*/
async getSlashingProposalIds(validator) {
return (await this.instance.getSlashingProposalIds(validator))
.map(i => i.toString());
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getlockinfo)
*/
async getLockInfo(who) {
return this.votingWeightProxy.getLockInfo(this.address, who);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#makesnapshot)
*/
async makeSnapshot(txOptions) {
return this.submitTransaction('makeSnapshot', [], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#applyslashing)
*/
async applySlashing(proposalId, validator, amountToSlash, txOptions) {
return this.submitTransaction('applySlashing', [proposalId, validator, amountToSlash], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#addslashingproposal)
*/
async addSlashingProposal(validator, slashingProposalId, txOptions) {
return this.submitTransaction('addSlashingProposal', [validator, slashingProposalId], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#calculatependingslashingsamount)
*/
async calculatePendingSlashingsAmount(validator, endTime) {
return (await this.instance.callStatic.calculatePendingSlashingsAmount(validator, endTime)).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getvalidatorslist)
*/
async getValidatorsList() {
return this.instance.getValidatorsList();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#purgependingslashings)
*/
async purgePendingSlashings(validator, txOptions) {
return this.submitTransaction('purgePendingSlashings', [validator], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#refreshdelegatedstake)
*/
async refreshDelegatedStake(delegatedTo, txOptions) {
return this.submitTransaction('refreshDelegatedStake', [delegatedTo], txOptions);
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#validatorsinfos)
*/
async getWithdrawalInfo(address) {
const withdrawalInfo = await this.instance.validatorsInfos(address);
return {
endTime: withdrawalInfo.endTime.toString(),
amount: withdrawalInfo.amount.toString(),
};
}
/** @deprecated Use getAccountableSelfStake , instead*/
async getValidatorsOwnStake(address) {
return (await this.instance.getAccountableSelfStake(address)).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getaccountableselfstake)
*/
async getAccountableSelfStake(address) {
return (await this.instance.getAccountableSelfStake(address)).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getaccountabletotalstake)
*/
async getAccountableTotalStake(address) {
return (await this.instance.getAccountableTotalStake(address)).toString();
}
/** @deprecated Use getSelfStake , instead*/
async getValidatorsOwnStakeWithoutPendingWithdrawal(address) {
return (await this.instance.getSelfStake(address)).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getselfstake)
*/
async getSelfStake(address) {
return (await this.instance.getSelfStake(address)).toString();
}
/**
* [External documentation](@system-contracts-repo/@network/Validators/#getvalidatortotalstake)
*/
async getValidatorTotalStake(address) {
return (await this.instance.getValidatorTotalStake(address)).toString();
}
/** @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 [selfStakeBN, withdrawalInfo, delegatedStakeBN, totalStakeBN, accountableStakeBN,] = await Promise.all([
this.instance.getAccountableSelfStake(address),
this.getWithdrawalInfo(address),
this.instance.getValidatorDelegatedStake(address),
this.instance.getValidatorTotalStake(address),
this.instance.getAccountableTotalStake(address)
]);
const selfStake = this.adapter.fromWei(selfStakeBN);
const totalStake = this.adapter.fromWei(totalStakeBN);
return {
address,
selfStake,
pendingStake: this.adapter.fromWei(withdrawalInfo.amount),
delegatedStake: this.adapter.fromWei(delegatedStakeBN),
totalStake,
currentDelegationFactor: Number.parseFloat(totalStake) / Number.parseFloat(selfStake),
accountableStake: this.adapter.fromWei(accountableStakeBN),
};
}
}
exports.ValidatorsInstance = ValidatorsInstance;
ValidatorsInstance.registryKey = 'governance.validators';
ValidatorsInstance.abi = 'Validators.json';
//# sourceMappingURL=ValidatorsInstance.js.map