@ajna-finance/sdk
Version:
A typescript SDK that can be used to create Dapps in Ajna ecosystem.
255 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DistributionPeriod = void 0;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const common_1 = require("../constants/common");
const grant_fund_1 = require("../contracts/grant-fund");
const types_1 = require("../types");
const utils_1 = require("../utils");
const grant_fund_2 = require("../utils/grant-fund");
const ContractBase_1 = require("./ContractBase");
/**
* Class used to iteract with distribution periods.
*/
class DistributionPeriod extends ContractBase_1.ContractBase {
constructor(signerOrProvider, id, isActive, startBlock, startDate, endBlock, endDate, fundsAvailable, votesCount, fundedSlateHash) {
super(signerOrProvider);
this.id = id;
this.isActive = isActive;
this.startBlock = startBlock;
this.startDate = startDate;
this.endBlock = endBlock;
this.endDate = endDate;
this.fundsAvailable = fundsAvailable;
this.votesCount = votesCount;
this.fundedSlateHash = fundedSlateHash;
}
toString() {
return `distribution period #${this.id}
is active: ${this.isActive ? 'yes' : 'no'}
start block: ${this.startBlock}
start date: ${new Date(this.startDate)}
end block: ${this.endBlock}
end date: ${new Date(this.endDate)}
funds available: ${(0, utils_1.fromWad)(this.fundsAvailable)}
votes count: ${(0, utils_1.fromWad)(this.votesCount)}
funded slate hash: ${(0, utils_1.fromWad)(this.fundedSlateHash)}
`;
}
/**
* Retrieve a bytes32 hash of the current distribution period stage.
*/
getStage() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getStage)(this.getProvider());
});
}
/**
* Get current distribution period stage.
* @returns string, distribution period stage
*/
distributionPeriodStage() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const distributionPeriodStage = yield this.getStage();
switch (distributionPeriodStage) {
case common_1.SCREENING_STAGE:
return types_1.DistributionPeriodStage.SCREENING;
case common_1.FUNDING_STAGE:
return types_1.DistributionPeriodStage.FUNDING;
case common_1.CHALLENGE_STAGE:
return types_1.DistributionPeriodStage.CHALLENGE;
default:
return types_1.DistributionPeriodStage.FINALIZE;
}
});
}
/**
* Get the voter's voting power in the screening stage of a distribution period.
* @param distributionId the distributionId of the distribution period to check
* @param address the address of the voter to check
* @returns the voter's voting power
*/
getScreeningVotingPower(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getVotesScreening)(this.getProvider(), this.id, address);
});
}
/**
* Get the remaining quadratic voting power available to the voter in the funding stage of a distribution period.
* @param distributionId the distributionId of the distribution period to check
* @param address the address of the voter to check
* @returns the voter's remaining quadratic voting power
*/
getFundingVotingPower(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getVotesFunding)(this.getProvider(), this.id, address);
});
}
/**
* Get the voter's voting power based on current distribution period stage.
* @param address the address of the voter to check
* @returns the voter's voting power
*/
getVotingPower(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const distributionPeriodStage = yield this.distributionPeriodStage();
if (distributionPeriodStage === types_1.DistributionPeriodStage.SCREENING) {
const initialVotingPower = yield this.getScreeningVotingPower(address);
const votingPowerUsed = yield this.getScreeningVotesCast(address);
const remainingVotingPower = ethers_1.BigNumber.from(initialVotingPower).sub(votingPowerUsed);
return remainingVotingPower;
}
else if (distributionPeriodStage === types_1.DistributionPeriodStage.FUNDING) {
return yield this.getFundingVotingPower(address);
}
else {
throw new types_1.SdkError("Couldn't get voting power. Distribution Period is already finalized");
}
});
}
/**
* Get the current state of a given voter in the funding stage.
* @param distributionId the distributionId of the distribution period to check.
* @param address the address of the voter to check.
* @return {@link VoterInfo} * voter's voting information.
*/
getVoterInfo(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getVoterInfo)(this.getProvider(), this.id, address);
});
}
/**
* Get the number of screening votes cast by an account in a given distribution period.
* @param distributionId The distributionId of the distribution period to check.
* @param account The address of the voter to check.
* @return The number of screening votes successfully cast the voter.
*/
getScreeningVotesCast(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getScreeningVotesCast)(this.getProvider(), this.id, address);
});
}
/**
* Get the list of funding votes cast by an account in a given distribution period.
* @param distributionId_ The distributionId of the distribution period to check.
* @param account_ The address of the voter to check.
* @return FundingVoteParams The list of FundingVoteParams structs that have been successfully cast the voter.
*/
getFundingVotesCast(address) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return yield (0, grant_fund_1.getFundingVotesCast)(this.getProvider(), this.id, address);
});
}
/**
* Cast an array of screening votes in one transaction.
* @param signer voter
* @param {@link VoteParams} * the array of votes on proposals to cast.
* @return promise to transaction
*/
screeningVote(signer, votes) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return (0, grant_fund_1.screeningVote)(signer, votes);
});
}
/**
* Cast an array of funding votes in one transaction.
* @param signer voter
* @param {@link VoteParams} * the array of votes on proposals to cast.
* @return promise to transaction
*/
fundingVote(signer, votes) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return (0, grant_fund_1.fundingVote)(signer, votes);
});
}
/**
* Cast an array of screening or funding votes (based on current distribution period stage).
* @param signer voter
* @param {@link VoteParams} * the array of votes on proposals to cast.
* @returns promise to transaction
*/
castVotes(signer, votes) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const distributionPeriodStage = yield this.distributionPeriodStage();
const isDistributionPeriodOnScreeningStage = distributionPeriodStage === types_1.DistributionPeriodStage.SCREENING;
// Format to what's expected by the contract.
const formattedVotes = yield Promise.all(votes.map(([proposalId, vote]) => {
if (isDistributionPeriodOnScreeningStage) {
return [ethers_1.BigNumber.from(proposalId), ethers_1.BigNumber.from((0, utils_1.toWad)(vote))];
}
else {
// Calculates square root of absolute value and multiplies by -1 if it was a negative vote before
const voteRoot = Number(vote) < 0 ? (0, utils_1.wsqrt)((0, utils_1.toWad)(vote).abs()).mul(-1) : (0, utils_1.wsqrt)((0, utils_1.toWad)(vote));
return [ethers_1.BigNumber.from(proposalId), ethers_1.BigNumber.from(voteRoot)];
}
}));
if (isDistributionPeriodOnScreeningStage) {
return (0, grant_fund_1.screeningVote)(signer, formattedVotes);
}
else {
return (0, grant_fund_1.fundingVote)(signer, formattedVotes);
}
});
}
/**
* Get top ten proposals on funding stage.
* @param distributionId the distributionId of the distribution period to check
* @returns top ten proposals on funding stage
*/
getTopTenProposals() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const toTenProposals = yield (0, grant_fund_1.getTopTenProposals)(this.getProvider(), this.id);
const formattedToTenProposals = toTenProposals.map(id => id.toString());
return formattedToTenProposals;
});
}
/**
* Check if a slate of proposals meets requirements, and maximizes votes. If so, set the provided proposal slate as the new top slate of proposals.
* @param proposals Array of proposals to check.
* @returns promise to transaction
*/
updateSlate(signer, proposals) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const proposalsIds = proposals.map(proposalId => ethers_1.BigNumber.from(proposalId));
return (0, grant_fund_1.updateSlate)(signer, proposalsIds, this.id);
});
}
/**
* Get the funded proposal slate for a given distributionId, and slate hash.
* @returns The array of proposalIds that are in the funded slate hash.
*/
getFundedProposalSlate() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const proposals = yield (0, grant_fund_1.getFundedProposalSlate)(this.getProvider(), this.fundedSlateHash);
const proposalIds = proposals.map(id => id.toString());
return proposalIds;
});
}
/**
* Get best proposals based on the combination of votes received and tokens requested over tokens available.
* @param tokensAvailable treasury.
* @returns proposals[] a new slate of proposals
*/
getOptimalProposals(proposalIds, tokensAvailable) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let bestProposals;
let proposals = [];
const getEachProposalInfo = (proposalId) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const proposalInfo = yield (0, grant_fund_1.getProposalInfo)(this.getProvider(), ethers_1.BigNumber.from(proposalId));
return (0, grant_fund_2.formatProposalInfo)(proposalInfo);
});
proposals = yield Promise.all(proposalIds.map(getEachProposalInfo));
if (proposals.length > 0) {
bestProposals = (0, grant_fund_2.findBestProposals)(proposals, Number((0, utils_1.fromWad)(tokensAvailable)));
}
else {
throw new types_1.SdkError('There is no funded proposal slate');
}
const optimalProposalsIds = bestProposals.flatMap(proposal => proposal.proposalId.toString());
return optimalProposalsIds;
});
}
}
exports.DistributionPeriod = DistributionPeriod;
//# sourceMappingURL=DistributionPeriod.js.map