@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
92 lines • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWithdrawableBalance = exports.useTimeRemaining = exports.getRemainingTime = void 0;
exports.useCantonAcceptOrRejectOffer = useCantonAcceptOrRejectOffer;
const react_1 = require("react");
const bridge_1 = require("../../bridge");
const coin_canton_1 = require("@ledgerhq/coin-canton");
const helpers_1 = require("@ledgerhq/ledger-wallet-framework/account/helpers");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function useCantonAcceptOrRejectOffer({ currency, account, partyId, }) {
const cantonBridge = (0, bridge_1.getCurrencyBridge)(currency);
const transferInstruction = (0, react_1.useCallback)(({ contractId, deviceId, reason }, type) => {
return cantonBridge.transferInstruction(currency, deviceId, account, partyId, contractId, type, reason);
}, [cantonBridge, currency, account, partyId]);
return transferInstruction;
}
const getRemainingTime = (diff) => {
if (diff <= 0) {
return "";
}
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
const startIndex = days > 0 ? 0 : hours > 0 ? 1 : minutes > 0 ? 2 : 3;
const units = [
[days, "d"],
[hours, "h"],
[minutes, "m"],
[seconds, "s"],
];
return units
.slice(startIndex)
.map(([value, suffix]) => `${value.toString().padStart(2, "0")}${suffix}`)
.join(" ");
};
exports.getRemainingTime = getRemainingTime;
const useTimeRemaining = (expiresAtMicros = 0, isExpired = false) => {
const [timeRemaining, setTimeRemaining] = (0, react_1.useState)("");
(0, react_1.useEffect)(() => {
if (expiresAtMicros <= 0 || isExpired) {
setTimeRemaining("");
return;
}
const updateTimeRemaining = () => {
const now = Date.now();
const expiresAt = expiresAtMicros / 1000;
const diff = expiresAt - now;
setTimeRemaining((0, exports.getRemainingTime)(diff));
};
updateTimeRemaining();
const interval = setInterval(updateTimeRemaining, 1000);
return () => clearInterval(interval);
}, [expiresAtMicros, isExpired]);
return timeRemaining;
};
exports.useTimeRemaining = useTimeRemaining;
const hasCantonResources = (account) => {
if (account.type === "Account") {
return (0, coin_canton_1.isCantonAccount)(account);
}
return "cantonResources" in account && !!account.cantonResources;
};
/**
* Hook to calculate withdrawable balance from expired outgoing offers.
* Withdrawable balance is the sum of amounts from offers the user sent that have expired.
*/
const useWithdrawableBalance = (account, accounts) => {
return (0, react_1.useMemo)(() => {
if (!hasCantonResources(account))
return new bignumber_js_1.default(0);
const proposals = account.cantonResources?.pendingTransferProposals ?? [];
// For token accounts, use parent account's xpub; for main accounts, use account's xpub
const parentAccount = (0, helpers_1.getParentAccount)(account, accounts);
const mainAccount = parentAccount ?? account;
const accountXpub = "xpub" in mainAccount ? mainAccount.xpub ?? "" : "";
const currentTime = Date.now();
return proposals.reduce((sum, proposal) => {
const isOutgoing = proposal.sender === accountXpub;
const isExpired = currentTime > proposal.expires_at_micros / 1000;
if (isOutgoing && isExpired) {
return sum.plus(new bignumber_js_1.default(proposal.amount));
}
return sum;
}, new bignumber_js_1.default(0));
}, [account, accounts]);
};
exports.useWithdrawableBalance = useWithdrawableBalance;
//# sourceMappingURL=react.js.map