on c19b2a8 help sign in
bridge/src/store/lib/useStarReleaseStore.js
63 lines 1.7 kB history raw
1import { useState, useCallback } from 'react';
2import { Nothing, Just } from 'folktale/maybe';
4import { useNetwork } from 'store/network';
5import { useWallet } from 'store/wallet';
6import {
7 generateWithdrawTxs,
8 getLinear,
9 getConditional,
10} from 'lib/starRelease';
12export default function useStarRelease() {
13 const { contracts } = useNetwork();
14 const { wallet } = useWallet();
16 const [starReleaseDetails, _setDetails] = useState(Nothing());
18 const [batchLimits, setBatchLimits] = useState([]);
20 const syncStarReleaseDetails = useCallback(async () => {
21 const _wallet = wallet.getOrElse(null);
22 const _contracts = contracts.getOrElse(null);
23 if (!(_contracts && _wallet)) {
24 return;
25 }
26 _setDetails(Nothing());
28 const address = _wallet.address;
30 try {
31 const [linear, conditional] = await Promise.all([
32 getLinear(_contracts, address),
33 getConditional(_contracts, address),
34 ]);
36 const keys = Object.keys(linear);
37 const result = keys.reduce(
38 (acc, key) => ({ ...acc, [key]: linear[key] + conditional[key] }),
39 {}
40 );
41 setBatchLimits(conditional.batchLimits);
43 _setDetails(Just(result));
44 } catch (e) {
45 _setDetails(Nothing());
46 console.error(e);
47 }
48 }, [contracts, wallet]);
50 const withdraw = useCallback(
51 (amount, to) => {
52 const _wallet = wallet.getOrElse(null);
53 const _contracts = contracts.getOrElse(null);
54 if (!(_contracts && _wallet)) {
55 return [];
56 }
57 return generateWithdrawTxs(_contracts, batchLimits, amount, to);
58 },
59 [batchLimits, contracts, wallet]
60 );
62 return { starReleaseDetails, syncStarReleaseDetails, withdraw };