@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
62 lines • 2.46 kB
JavaScript
import { useEffect, useMemo, useState } from "react";
import { bakers } from "@ledgerhq/coin-tezos/network/index";
export function useBakers(whitelistAddresses) {
const [whitelistedBakers, setWhitelistedBakers] = useState(() => bakers.listBakersWithDefault(whitelistAddresses));
useEffect(() => {
bakers.listBakers(whitelistAddresses).then(setWhitelistedBakers);
}, [whitelistAddresses]);
return whitelistedBakers;
}
export function useDelegation(account) {
const [delegation, setDelegation] = useState(() => bakers.getAccountDelegationSync(account));
useEffect(() => {
let cancelled = false;
bakers.loadAccountDelegation(account).then(delegation => {
if (cancelled)
return;
setDelegation(delegation);
});
return () => {
cancelled = true;
};
}, [account]);
return delegation;
}
export function useBaker(addr) {
const [baker, setBaker] = useState(() => bakers.getBakerSync(addr));
bakers.loadBaker(addr).then(setBaker);
return baker;
}
// select a random baker for the mount time (assuming bakers length don't change)
export function useRandomBaker(bakers) {
const randomBakerIndex = useMemo(() => {
const nonFullBakers = bakers.filter(b => b.capacityStatus !== "full");
if (nonFullBakers.length > 0) {
// if there are non full bakers, we pick one
const i = Math.floor(Math.random() * nonFullBakers.length);
return bakers.indexOf(nonFullBakers[i]);
}
// fallback on random between only full bakers
return Math.floor(Math.random() * bakers.length); // for perf, we only want to re-calc on bakers.length changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [bakers.length]);
return bakers[randomBakerIndex];
}
export function useStakingPositions(account) {
const delegation = useDelegation(account);
return useMemo(() => {
if (account.type !== "Account" || !delegation?.address)
return [];
return [
{
uid: account.freshAddress,
address: account.freshAddress,
delegate: delegation.address,
state: "active",
asset: { type: "native" },
amount: BigInt(account.balance.toString()),
},
];
}, [account, delegation]);
}
//# sourceMappingURL=react.js.map