@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
141 lines • 5.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUnfreezeData = exports.formatVotes = exports.getNextRewardDate = exports.getLastVotedDate = exports.useTronSuperRepresentatives = exports.SR_MAX_VOTES = exports.SR_THRESHOLD = exports.MIN_TRANSACTION_AMOUNT = void 0;
exports.useTronPowerLoading = useTronPowerLoading;
exports.useSortedSr = useSortedSr;
const bignumber_js_1 = require("bignumber.js");
const invariant_1 = __importDefault(require("invariant"));
const constants_1 = require("@ledgerhq/coin-tron/logic/constants");
const network_1 = require("@ledgerhq/coin-tron/network");
const react_1 = require("react");
const react_2 = require("../../bridge/react");
exports.MIN_TRANSACTION_AMOUNT = constants_1.ONE_TRX;
exports.SR_THRESHOLD = 27;
exports.SR_MAX_VOTES = 5;
let __lastSeenSR = [];
/** Fetch the list of super representatives */
const useTronSuperRepresentatives = () => {
const [sr, setSr] = (0, react_1.useState)(__lastSeenSR);
(0, react_1.useEffect)(() => {
let unsub = false;
(0, network_1.getTronSuperRepresentatives)().then((sr) => {
__lastSeenSR = sr;
if (unsub)
return;
setSr(sr);
});
return () => {
unsub = true;
};
}, []);
return sr;
};
exports.useTronSuperRepresentatives = useTronSuperRepresentatives;
/** Get last time voted */
const getLastVotedDate = (account) => {
return account.tronResources && account.tronResources.lastVotedDate
? account.tronResources.lastVotedDate
: null;
};
exports.getLastVotedDate = getLastVotedDate;
/** Get next available date to claim rewards */
const getNextRewardDate = (account) => {
const lastWithdrawnRewardDate = account.tronResources && account.tronResources.lastWithdrawnRewardDate
? account.tronResources.lastWithdrawnRewardDate
: null;
if (lastWithdrawnRewardDate) {
// add 24hours
const nextDate = lastWithdrawnRewardDate.getTime() + 24 * 60 * 60 * 1000;
if (nextDate > Date.now())
return nextDate;
}
return null;
};
exports.getNextRewardDate = getNextRewardDate;
/** format votes with superrepresentatives data */
const formatVotes = (votes, superRepresentatives) => {
return votes && superRepresentatives
? votes.map(({ address, voteCount }) => {
const srIndex = superRepresentatives.findIndex(sp => sp.address === address);
return {
validator: superRepresentatives[srIndex],
rank: srIndex + 1,
isSR: srIndex < exports.SR_THRESHOLD,
address,
voteCount,
};
})
: [];
};
exports.formatVotes = formatVotes;
// wait an effect of a tron freeze until it effectively change
function useTronPowerLoading(account) {
const tronPower = (account.tronResources && account.tronResources.tronPower) || 0;
const initialTronPower = (0, react_1.useRef)(tronPower);
const initialAccount = (0, react_1.useRef)(account);
const [isLoading, setLoading] = (0, react_1.useState)(true);
(0, react_1.useEffect)(() => {
if (initialTronPower.current !== tronPower) {
setLoading(false);
}
}, [tronPower]);
const sync = (0, react_2.useBridgeSync)();
(0, react_1.useEffect)(() => {
if (!isLoading)
return;
const interval = setInterval(() => {
sync({
type: "SYNC_ONE_ACCOUNT",
priority: 10,
accountId: initialAccount.current.id,
reason: "tron-power-load",
});
}, 5000);
return () => clearInterval(interval);
}, [initialAccount, sync, isLoading]);
return isLoading;
}
/** Search filters for SR list */
const searchFilter = (query) => ({ name, address }) => {
if (!query)
return true;
const terms = `${name || ""} ${address}`;
return terms.toLowerCase().includes(query.toLowerCase().trim());
};
/** Hook to search and sort SR list according to initial votes and query */
function useSortedSr(search, superRepresentatives, votes) {
const { current: initialVotes } = (0, react_1.useRef)(votes.map(({ address }) => address));
const SR = (0, react_1.useMemo)(() => superRepresentatives.map((sr, rank) => ({
sr,
name: sr.name,
address: sr.address,
rank: rank + 1,
isSR: rank < exports.SR_THRESHOLD,
})), [superRepresentatives]);
const sortedVotes = (0, react_1.useMemo)(() => SR.filter(({ address }) => initialVotes.includes(address)).concat(SR.filter(({ address }) => !initialVotes.includes(address))), [SR, initialVotes]);
const sr = (0, react_1.useMemo)(() => (search ? SR.filter(searchFilter(search)) : sortedVotes), [search, SR, sortedVotes]);
return sr;
}
/** format account to retrieve unfreeze data */
const getUnfreezeData = (account) => {
const { tronResources } = account;
(0, invariant_1.default)(tronResources, "getUnfreezeData: tron account is expected");
const frozen = tronResources?.frozen ?? { bandwidth: null, energy: null };
const bandwidth = frozen.bandwidth;
const energy = frozen.energy;
const unfreezeBandwidth = new bignumber_js_1.BigNumber(bandwidth ? bandwidth.amount : 0);
const canUnfreezeBandwidth = unfreezeBandwidth.gt(0);
const unfreezeEnergy = new bignumber_js_1.BigNumber(energy ? energy.amount : 0);
const canUnfreezeEnergy = unfreezeEnergy.gt(0);
return {
unfreezeBandwidth,
unfreezeEnergy,
canUnfreezeBandwidth,
canUnfreezeEnergy,
};
};
exports.getUnfreezeData = getUnfreezeData;
//# sourceMappingURL=react.js.map