UNPKG

@hypernetlabs/web-ui

Version:
214 lines 7.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFund = void 0; const objects_1 = require("@hypernetlabs/objects"); const contexts_1 = require("../contexts"); const ethers_1 = require("ethers"); const react_1 = require("react"); const react_alert_1 = require("react-alert"); var EActionTypes; (function (EActionTypes) { EActionTypes["FETCHED"] = "FETCHED"; EActionTypes["TOKEN_SELECTED"] = "TOKEN_SELECTED"; EActionTypes["SUCCESS"] = "SUCCESS"; EActionTypes["ERROR"] = "ERROR"; EActionTypes["AMOUNT_CHANGED"] = "AMOUNT_CHANGED"; EActionTypes["DESTINATION_ADDRESS_CHANGED"] = "DESTINATION_ADDRESS_CHANGED"; EActionTypes["FETCHED_STATE_CHANNELS"] = "FETCHED_STATE_CHANNELS"; EActionTypes["STATE_CHANNEL_SELECTED"] = "STATE_CHANNEL_SELECTED"; })(EActionTypes || (EActionTypes = {})); function useFund() { const { coreProxy, UIData, defaultGovernanceChainId } = (0, contexts_1.useStoreContext)(); const { setLoading } = (0, contexts_1.useLayoutContext)(); const alert = (0, react_alert_1.useAlert)(); const initialState = { error: null, tokenSelectorOptions: [], selectedPaymentToken: undefined, amount: "1", destinationAddress: (0, objects_1.EthereumAccountAddress)(""), activeStateChannels: [], stateChannelsFetched: false, }; const [state, dispatch] = (0, react_1.useReducer)((state, action) => { switch (action.type) { case EActionTypes.FETCHED: return { ...state, error: false, tokenSelectorOptions: action.payload, dataFetched: true }; case EActionTypes.TOKEN_SELECTED: return { ...state, error: false, selectedPaymentToken: action.payload, }; case EActionTypes.AMOUNT_CHANGED: return { ...state, error: false, amount: action.payload, }; case EActionTypes.DESTINATION_ADDRESS_CHANGED: return { ...state, error: false, destinationAddress: action.payload, }; case EActionTypes.FETCHED_STATE_CHANNELS: return { ...state, error: false, activeStateChannels: action.payload, stateChannelsFetched: true }; case EActionTypes.STATE_CHANNEL_SELECTED: return { ...state, error: false, selectedStateChennel: action.payload, }; case EActionTypes.ERROR: return { ...state, error: true, }; case EActionTypes.SUCCESS: return { ...state, error: false, }; default: return { ...state }; } }, initialState); (0, react_1.useEffect)(() => { let cancelRequest = false; const fetchData = async () => { setLoading(true); try { if (cancelRequest) return; // get data from coreProxy coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.payments.getTokenInformation().map((tokenInformation) => { const tokenInformationList = tokenInformation.filter((tokenInfo) => tokenInfo.chainId == defaultGovernanceChainId); // prepare balances setLoading(false); dispatch({ type: EActionTypes.FETCHED, payload: tokenInformationList, }); }); coreProxy.getEthereumAccounts().match((accounts) => { dispatch({ type: EActionTypes.DESTINATION_ADDRESS_CHANGED, payload: accounts[0], }); }, (err) => { console.log("err: ", err); }); coreProxy.payments.getActiveStateChannels().match((stateChannels) => { dispatch({ type: EActionTypes.FETCHED_STATE_CHANNELS, payload: stateChannels, }); dispatch({ type: EActionTypes.STATE_CHANNEL_SELECTED, payload: UIData.getSelectedStateChannel(), }); }, (err) => { console.log("err: ", err); }); } catch (error) { setLoading(false); if (cancelRequest) return; dispatch({ type: EActionTypes.ERROR }); } }; fetchData(); return function cleanup() { cancelRequest = true; }; }, []); const setSelectedPaymentToken = (selectedOption) => { dispatch({ type: EActionTypes.TOKEN_SELECTED, payload: selectedOption, }); }; const setAmount = (enteredAmount) => { dispatch({ type: EActionTypes.AMOUNT_CHANGED, payload: enteredAmount, }); }; const setDestinationAddress = (enteredAddress) => { dispatch({ type: EActionTypes.AMOUNT_CHANGED, payload: (0, objects_1.EthereumAccountAddress)(enteredAddress), }); }; const depositFunds = (tokenAddress, amount, stateChannelAddress) => { setLoading(true); coreProxy.payments .depositFunds(stateChannelAddress, tokenAddress, (0, objects_1.BigNumberString)(ethers_1.ethers.utils.parseEther(amount || "1").toString())) .match((balances) => { alert.success("Your fund deposit has succeeded!"); setLoading(false); dispatch({ type: EActionTypes.SUCCESS, }); }, (err) => { alert.error(err.message || "Your fund deposit has failed"); setLoading(false); dispatch({ type: EActionTypes.ERROR, }); }); }; const withdrawFunds = (tokenAddress, amount, stateChannelAddress) => { setLoading(true); coreProxy.payments .withdrawFunds(stateChannelAddress, tokenAddress, (0, objects_1.BigNumberString)(ethers_1.ethers.utils.parseEther(amount).toString()), state.destinationAddress) .match(() => { alert.success("Your funds withdrawl has succeeded!"); setLoading(false); }, (err) => { alert.error(err.message || "Your funds withdrawl has failed"); setLoading(false); }); }; const mintTokens = () => { setLoading(true); coreProxy.payments .mintTestToken((0, objects_1.BigNumberString)(ethers_1.ethers.utils.parseEther(state.amount || "1").toString())) .match(() => { alert.success("mint tokens has succeeded"); setLoading(false); dispatch({ type: EActionTypes.SUCCESS, }); }, (err) => { alert.error(err.message || "mint tokens has failed"); setLoading(false); dispatch({ type: EActionTypes.ERROR, }); }); }; return { ...state, setSelectedPaymentToken, setDestinationAddress, depositFunds, withdrawFunds, mintTokens, setAmount, }; } exports.useFund = useFund; //# sourceMappingURL=useFund.js.map