@broxus/js-core
Version:
MobX-based JavaScript Core library
282 lines (281 loc) • 14.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoteEscrowUtils = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../../constants");
const contracts_1 = require("../../models/vote-escrow/contracts");
const utils_1 = require("../../utils");
class VoteEscrowUtils {
static voteEpoch(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.voteEpoch({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
},
votes: params.votes.map(([addr, amount]) => ([(0, utils_1.resolveTvmAddress)(addr), amount])),
})
.sendDelayed({
amount: (0, utils_1.toInt)(3.2, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
...args,
});
}
static endVoting(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.endVoting({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
},
})
.sendDelayed({
amount: (0, utils_1.toInt)(10.2, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
...args,
});
}
static withdraw(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.withdraw({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
},
})
.sendDelayed({
amount: (0, utils_1.toInt)(1.6, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: (0, utils_1.resolveTvmAddress)(params.meta.sendGasTo),
...args,
});
}
static castVote(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.castVote({
proposal_id: params.proposalId,
support: params.support,
})
.sendDelayed({
amount: (0, utils_1.toInt)(11.5, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
...args,
});
}
static castVoteWithReason(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.castVoteWithReason({
proposal_id: params.proposalId,
reason: params.reason,
support: params.support,
})
.sendDelayed({
amount: (0, utils_1.toInt)(11.5, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
...args,
});
}
static tryUnlockCastedVotes(provider, voteEscrowAddress, params, args) {
const minTotalGas = (0, utils_1.toInt)(11, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS);
const gasToUnlockEachVote = (0, utils_1.toInt)(0.2, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS);
const buffer = (0, utils_1.toInt)(1.5, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS);
const amount = (0, bignumber_js_1.default)(gasToUnlockEachVote).times(params.proposalIds.length).plus(buffer);
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.tryUnlockCastedVotes({ proposal_ids: params.proposalIds })
.sendDelayed({
amount: bignumber_js_1.default.max(amount, minTotalGas).toFixed(),
bounce: true,
...args,
});
}
static tryUnlockVoteTokens(provider, voteEscrowAddress, params, args) {
return (0, contracts_1.voteEscrowContract)(provider, voteEscrowAddress)
.methods.tryUnlockVoteTokens({ proposal_id: params.proposalId })
.sendDelayed({
amount: (0, utils_1.toInt)(11.5, constants_1.DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
...args,
});
}
static async encodeDepositPayload(connection, voteEscrowAddress, params, cachedState) {
return (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.encodeDepositPayload({
call_id: params.callId,
deposit_owner: (0, utils_1.resolveTvmAddress)(params.depositOwner),
lock_time: params.lockTime,
nonce: params.nonce,
})
.call({ cachedState })
.then(result => result.payload);
}
static async encodeWhitelistPayload(connection, voteEscrowAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.encodeWhitelistPayload({
call_id: params.callId,
nonce: params.nonce,
whitelist_addr: (0, utils_1.resolveTvmAddress)(params.whitelistAddress),
})
.call({ cachedState: state });
return result.payload;
}
/**
* Real-time calculating vote escrow average
* @param connection
* @param voteEscrowAddress
* @param cachedState
*/
static async calculateAverage(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.calculateAverage()
.call({ cachedState: state });
return {
lastUpdateTime: Number(result._lastUpdateTime),
veQubeAverage: result._veQubeAverage,
veQubeAveragePeriod: Number(result._veQubeAveragePeriod),
veQubeBalance: result._veQubeBalance,
};
}
static async calculateGasForEndVoting(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.calculateGasForEndVoting()
.call({ cachedState: state });
return result.min_gas;
}
static async calculateVeMint(connection, voteEscrowAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.calculateVeMint({
lock_time: params.lockTime,
qube_amount: params.qubeAmount,
})
.call({ cachedState: state });
return result.ve_amount;
}
static async currentVotingVotes(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
return (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.currentVotingVotes()
.call({ cachedState: state });
}
static async distributionSchedule(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.distributionSchedule()
.call({ cachedState: state });
return result.distributionSchedule;
}
static async distributionScheme(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.distributionScheme()
.call({ cachedState: state });
return result.distributionScheme;
}
static async gaugeDaoApproved(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.gaugeDaoApproved()
.call({ cachedState: state });
return result.gaugeDaoApproved;
}
static async gaugeWhitelist(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.gaugeWhitelist()
.call({ cachedState: state });
return result.gaugeWhitelist;
}
static async getCurrentEpochDetails(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.getCurrentEpochDetails()
.call({ cachedState: state });
return {
currentEpoch: Number(result._currentEpoch),
currentEpochEndTime: Number(result._currentEpochEndTime),
currentEpochStartTime: Number(result._currentEpochStartTime),
currentVotingEndTime: Number(result._currentVotingEndTime),
currentVotingStartTime: Number(result._currentVotingStartTime),
currentVotingTotalVotes: Number(result._currentVotingTotalVotes),
};
}
static async getDetails(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.getDetails()
.call({ cachedState: state });
return {
dao: result._dao,
distributionSupply: result._distributionSupply,
emergency: result._emergency,
emissionDebt: result._emissionDebt,
gaugeWhitelistPrice: result._gaugeWhitelistPrice,
initialized: result._initialized,
lastUpdateTime: Number(result._lastUpdateTime),
manager: result._manager,
owner: result._owner,
paused: result._paused,
qube: result._qube,
qubeBalance: result._qubeBalance,
qubeMaxLockTime: Number(result._qubeMaxLockTime),
qubeMinLockTime: Number(result._qubeMinLockTime),
qubeWallet: result._qubeWallet,
teamTokens: result._teamTokens,
treasuryTokens: result._treasuryTokens,
veQubeBalance: result._veQubeBalance,
whitelistPayments: result._whitelistPayments,
};
}
static async getVotingDetails(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.getVotingDetails()
.call({ cachedState: state });
return {
epochTime: Number(result._epochTime),
gaugeMaxDowntime: Number(result._gaugeMaxDowntime),
gaugeMaxVotesRatio: Number(result._gaugeMaxVotesRatio),
gaugeMinVotesRatio: Number(result._gaugeMinVotesRatio),
gaugesNum: Number(result._gaugesNum),
maxGaugesPerVote: Number(result._maxGaugesPerVote),
timeBeforeVoting: Number(result._timeBeforeVoting),
votingNormalizing: Number(result._votingNormalizing),
votingTime: Number(result._votingTime),
};
}
static async getNormalizedVoting(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.getNormalizedVoting()
.call({ cachedState: state });
return {
distribution: result._distribution,
emissionDebt: result._emissionDebt,
normalizedVotes: result._normalizedVotes,
toDistributeTeam: result.to_distribute_team,
toDistributeTotal: result.to_distribute_total,
toDistributeTreasury: result.to_distribute_treasury,
votes: result._votes,
};
}
static async getVoteEscrowAccountAddress(connection, voteEscrowAddress, ownerAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, voteEscrowAddress);
const result = await (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress)
.methods.getVoteEscrowAccountAddress({ answerId: 0, user: (0, utils_1.resolveTvmAddress)(ownerAddress) })
.call({ cachedState: state, responsible: true });
return result.value0;
}
static decodeTransactionEvents(connection, voteEscrowAddress, transaction) {
return (0, contracts_1.voteEscrowContract)(connection, voteEscrowAddress).decodeTransactionEvents({ transaction });
}
}
exports.VoteEscrowUtils = VoteEscrowUtils;