import React from 'react';
import './wallet.scss';
import {useService} from '@xstate/react';
import {formatEther} from '@ethersproject/units';
import {Button, Heading, Flex, Text, Link, Loader} from 'rimble-ui';
import {DomainBudget, BN} from '@statechannels/wallet-core';
import {track} from '../segment-analytics';
import {getAmountsFromBudget} from './selectors';
import {CloseLedgerAndWithdrawService} from '../workflows/close-ledger-and-withdraw';
import {TARGET_NETWORK} from '../config';
interface Props {
service: CloseLedgerAndWithdrawService;
}
export const CloseLedgerAndWithdraw = (props: Props) => {
const [current, _send] = useService(props.service);
const send = (event: 'USER_APPROVES_CLOSE' | 'USER_REJECTS_CLOSE') => () => {
track(event, {domain: current.context.domain});
_send(event);
};
const waitForUserApproval = ({waiting, budget}: {waiting: boolean; budget: DomainBudget}) => {
const {playerAmount} = getAmountsFromBudget(budget);
return (
Withdraw Funds
{budget.domain}
Close your hub connection with {budget.domain} and withdraw your funds?
You will receive {formatEther(BN.from(playerAmount))} ETH and the budget will be closed
with the channel hub.
Cancel
);
};
const talkingToHub = (
Withdraw funds
Communicating with the hub
);
const working = (
Withdraw funds
Working...
);
const withdrawSubmitTransaction = (
Withdraw funds
Please approve the transaction in metamask
);
const withdrawWaitMining = ({transactionId}: {transactionId: string}) => (
Withdraw funds
Waiting for your transaction to be mined.
Click{' '}
here
{' '}
to follow the progress on etherscan.
);
if (current.matches('waitForUserApproval')) {
const {budget} = current.context;
return waitForUserApproval({waiting: false, budget});
} else if (
current.matches('createObjective') ||
current.matches('fetchBudget') ||
current.matches('clearBudget')
) {
return working;
} else if (
current.matches({closeLedger: 'constructFinalState'}) ||
current.matches({closeLedger: 'supportState'})
) {
return talkingToHub;
} else if (current.matches({withdraw: 'submitTransaction'})) {
return withdrawSubmitTransaction;
} else if (current.matches({withdraw: 'waitMining'})) {
return withdrawWaitMining(current.context);
} else if (current.matches('done')) {
// workflow hides ui, so user shouldn't ever see this
return
Success! Returning to app..
;
} else if (current.matches('budgetFailure')) {
// workflow hides ui, so user shouldn't ever see this
return Failed :(. Returning to app..
;
} else {
return Todo
;
}
};