@broxus/js-core
Version:
MobX-based JavaScript Core library
275 lines (274 loc) • 13 kB
JavaScript
import { BigNumber } from 'bignumber.js';
import { DEFAULT_NATIVE_CURRENCY_DECIMALS } from '../../constants';
import { voteEscrowContract } from '../../models/vote-escrow/contracts';
import { getFullContractState, resolveTvmAddress, toInt } from '../../utils';
export class VoteEscrowUtils {
static voteEpoch(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.voteEpoch({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: resolveTvmAddress(params.meta.sendGasTo),
},
votes: params.votes.map(([addr, amount]) => ([resolveTvmAddress(addr), amount])),
})
.sendDelayed({
amount: toInt(3.2, DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: resolveTvmAddress(params.meta.sendGasTo),
...args,
});
}
static endVoting(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.endVoting({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: resolveTvmAddress(params.meta.sendGasTo),
},
})
.sendDelayed({
amount: toInt(10.2, DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: resolveTvmAddress(params.meta.sendGasTo),
...args,
});
}
static withdraw(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.withdraw({
meta: {
call_id: params.meta.callId,
nonce: params.meta.nonce,
send_gas_to: resolveTvmAddress(params.meta.sendGasTo),
},
})
.sendDelayed({
amount: toInt(1.6, DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
from: resolveTvmAddress(params.meta.sendGasTo),
...args,
});
}
static castVote(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.castVote({
proposal_id: params.proposalId,
support: params.support,
})
.sendDelayed({
amount: toInt(11.5, DEFAULT_NATIVE_CURRENCY_DECIMALS),
...args,
});
}
static castVoteWithReason(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.castVoteWithReason({
proposal_id: params.proposalId,
reason: params.reason,
support: params.support,
})
.sendDelayed({
amount: toInt(11.5, DEFAULT_NATIVE_CURRENCY_DECIMALS),
...args,
});
}
static tryUnlockCastedVotes(provider, voteEscrowAddress, params, args) {
const minTotalGas = toInt(11, DEFAULT_NATIVE_CURRENCY_DECIMALS);
const gasToUnlockEachVote = toInt(0.2, DEFAULT_NATIVE_CURRENCY_DECIMALS);
const buffer = toInt(1.5, DEFAULT_NATIVE_CURRENCY_DECIMALS);
const amount = BigNumber(gasToUnlockEachVote).times(params.proposalIds.length).plus(buffer);
return voteEscrowContract(provider, voteEscrowAddress)
.methods.tryUnlockCastedVotes({ proposal_ids: params.proposalIds })
.sendDelayed({
amount: BigNumber.max(amount, minTotalGas).toFixed(),
bounce: true,
...args,
});
}
static tryUnlockVoteTokens(provider, voteEscrowAddress, params, args) {
return voteEscrowContract(provider, voteEscrowAddress)
.methods.tryUnlockVoteTokens({ proposal_id: params.proposalId })
.sendDelayed({
amount: toInt(11.5, DEFAULT_NATIVE_CURRENCY_DECIMALS),
bounce: true,
...args,
});
}
static async encodeDepositPayload(connection, voteEscrowAddress, params, cachedState) {
return voteEscrowContract(connection, voteEscrowAddress)
.methods.encodeDepositPayload({
call_id: params.callId,
deposit_owner: 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 getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.encodeWhitelistPayload({
call_id: params.callId,
nonce: params.nonce,
whitelist_addr: 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 getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.calculateGasForEndVoting()
.call({ cachedState: state });
return result.min_gas;
}
static async calculateVeMint(connection, voteEscrowAddress, params, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
return voteEscrowContract(connection, voteEscrowAddress)
.methods.currentVotingVotes()
.call({ cachedState: state });
}
static async distributionSchedule(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.distributionSchedule()
.call({ cachedState: state });
return result.distributionSchedule;
}
static async distributionScheme(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.distributionScheme()
.call({ cachedState: state });
return result.distributionScheme;
}
static async gaugeDaoApproved(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.gaugeDaoApproved()
.call({ cachedState: state });
return result.gaugeDaoApproved;
}
static async gaugeWhitelist(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.gaugeWhitelist()
.call({ cachedState: state });
return result.gaugeWhitelist;
}
static async getCurrentEpochDetails(connection, voteEscrowAddress, cachedState) {
const state = cachedState ?? await getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
const result = await 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 getFullContractState(connection, voteEscrowAddress);
const result = await voteEscrowContract(connection, voteEscrowAddress)
.methods.getVoteEscrowAccountAddress({ answerId: 0, user: resolveTvmAddress(ownerAddress) })
.call({ cachedState: state, responsible: true });
return result.value0;
}
static decodeTransactionEvents(connection, voteEscrowAddress, transaction) {
return voteEscrowContract(connection, voteEscrowAddress).decodeTransactionEvents({ transaction });
}
}