UNPKG

@ajna-finance/sdk

Version:

A typescript SDK that can be used to create Dapps in Ajna ecosystem.

80 lines 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatProposalInfo = exports.optimize = void 0; exports.findBestProposals = findBestProposals; const types_1 = require("../types"); const numeric_1 = require("./numeric"); /** * Scale the votes to use the full voting power based on a set of votes provided * @param {BigNumber} votingPower Voting power * @param {Array<Votes>} votes Array of proposalIds and vote used * @returns {Array<Votes>} Array of proposalIds and new votes calculated */ const optimize = (votingPower, votes) => { let currentVotes = (0, numeric_1.toWad)('0'); // Format votes to WADs representations const formattedVotes = votes.map(([id, vote]) => { return [id, (0, numeric_1.toWad)(vote)]; }); // Sum absolute value of votes formattedVotes.forEach(([, vote]) => { currentVotes = currentVotes.add(vote.abs()); }); if (currentVotes.eq(0)) { throw new types_1.SdkError('Constraint not satisfied: all votes are 0'); } // Calculates the scale factor of the votes based on voting power and sum of votes const scaleFactor = (0, numeric_1.wdiv)(votingPower, currentVotes); // Calculates new votes and format votes again to a string for UI usage const result = formattedVotes.map(([id, vote]) => { const scaledVote = (0, numeric_1.wmul)(vote, scaleFactor); return [id, (0, numeric_1.fromWad)(scaledVote)]; }); return result; }; exports.optimize = optimize; function findBestProposals(proposals, tokensAvailable) { if (!proposals || proposals.length === 0) return []; // 90% of tokens available should be considered for this calculation const tokensAvailableToTake = (tokensAvailable * 9) / 10; // Function to generate all combinations of proposals function* generateCombinations(arr, start, len) { if (len === 0) { yield []; return; } if (start === arr.length) return; for (let i = start; i <= arr.length - len; i++) { const head = arr.slice(i, i + 1); const tailCombinations = generateCombinations(arr, i + 1, len - 1); for (const tail of tailCombinations) { yield head.concat(tail); } } } let bestCombo = []; let bestVotes = 0; // Check combinations of 1 proposal, 2 proposals... for (let len = 1; len <= proposals.length; len++) { for (const combo of generateCombinations(proposals, 0, len)) { const totalVotes = combo.reduce((sum, proposal) => sum + Number((0, numeric_1.fromWad)(proposal.votesReceived)), 0); const totalTokens = combo.reduce((sum, proposal) => sum + Number((0, numeric_1.fromWad)(proposal.tokensRequested)), 0); if (totalTokens <= tokensAvailableToTake && totalVotes > bestVotes) { bestVotes = totalVotes; bestCombo = combo; } } } return bestCombo; } const formatProposalInfo = (proposalInfo) => { return { proposalId: proposalInfo[0], votesReceived: proposalInfo[2], tokensRequested: proposalInfo[3], }; }; exports.formatProposalInfo = formatProposalInfo; //# sourceMappingURL=grant-fund.js.map