on df0c310 help sign in
bridge/src/store/lib/useStarReleaseStore.js
70 lines 2.0 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 getLockupKind,
11} from 'lib/starRelease';
13export default function useStarRelease() {
14 const { contracts } = useNetwork();
15 const { wallet } = useWallet();
17 const [starReleaseDetails, _setDetails] = useState(Nothing());
19 const [batchLimits, setBatchLimits] = useState([]);
21 const syncStarReleaseDetails = useCallback(async () => {
22 const _wallet = wallet.getOrElse(null);
23 const _contracts = contracts.getOrElse(null);
24 if (!(_contracts && _wallet)) {
25 return;
26 }
27 _setDetails(Nothing());
29 const address = _wallet.address;
31 try {
32 const [linear, conditional, kind] = await Promise.all([
33 getLinear(_contracts, address),
34 getConditional(_contracts, address),
35 getLockupKind(_contracts, address),
36 ]);
38 const keys = Object.keys(linear);
39 const result = keys.reduce(
40 (acc, key) => ({ ...acc, [key]: linear[key] + conditional[key] }),
41 {}
42 );
43 result.kind = kind;
44 result.linear = { approvedTransferTo: linear.approvedTransferTo };
45 result.conditional = {
46 approvedTransferTo: conditional.approvedTransferTo,
47 };
48 setBatchLimits(conditional.batchLimits);
50 _setDetails(Just(result));
51 } catch (e) {
52 _setDetails(Just(null));
53 console.error('error fetching star release details', e);
54 }
55 }, [contracts, wallet]);
57 const withdraw = useCallback(
58 (amount, to) => {
59 const _wallet = wallet.getOrElse(null);
60 const _contracts = contracts.getOrElse(null);
61 if (!(_contracts && _wallet)) {
62 return [];
63 }
64 return generateWithdrawTxs(_contracts, batchLimits, amount, to);
65 },
66 [batchLimits, contracts, wallet]
67 );
69 return { starReleaseDetails, syncStarReleaseDetails, withdraw };