import React, { PureComponent } from 'react' import { StoreState } from '../types/store'; import { getWeb3State } from '../store/blockchain/selectors'; import { Web3State } from '../types/blockchain'; import { connect } from 'react-redux'; import { BalancesContainer } from './account/balances_container'; import { Button, Container, Form, InputGroup, Modal } from 'react-bootstrap'; import { startPaymasterSetFee, startPaymasterSetForwarder, startPaymasterSetGasUsedByPost, startPaymasterSetMinBalance, startPaymasterSetMinGas, startPaymasterSetPaymentToken, startPaymasterSetRelayHub, startPaymasterSetTarget, startPaymasterWhitelisToken, startPaymasterWithdraw } from '../store/actions'; import { ether } from '@opengsn/common'; import { ethers } from 'ethers'; enum Errors { SetMinBalance, SetPaymentToken, SetFee, Whitelist, SetGasUsedByPost, SetMinGas, SetTarget, SetRelayHub, SetForwarder, Withdraw } interface OwnProps { errorMessage: string | null, errorType: Errors | null, minBalance: string, paymentToken: string, fee: string, whitelistTokenAdd: string, whitelistToken: boolean, gasUsedByPost: string, minGas: string, target: string, relayHub: string, forwarder: string, withdrawReceiver: string, withdrawAmount: string } interface StateProps { web3State: Web3State } interface DispatchProps { setMinBalance: (amount: string) => void setPaymentToken: (address: string) => void setFee: (amount: string) => void whitelistToken: (address: string, whitelist: boolean) => void setGasUsedByPost: (amount: string) => void setMinGas: (amount: string) => void setTarget: (address: string) => void setRelayHub: (address: string) => void setForwarder: (address: string) => void withdraw: (amount: string, receiver: string) => void } type Props = StateProps & DispatchProps; class Admin extends PureComponent{ state = { errorMessage: null, minBalance: "", errorType: null, paymentToken: "", fee: "", whitelistTokenAdd: "", whitelistToken: true, gasUsedByPost: "", minGas: "", target: "", relayHub: "", forwarder: "", withdrawReceiver: "", withdrawAmount: "" } onSetMinBalance = () => { const { minBalance } = this.state; if (isNaN(Number(minBalance))) return this.setState({ errorMessage: "Invalid Number", errorType: Errors.SetMinBalance }) this.props.setMinBalance(minBalance) } onSetPaymentToken = () => { const { paymentToken } = this.state if(!ethers.utils.isAddress(paymentToken.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.SetPaymentToken }) } this.props.setPaymentToken(paymentToken) } onSetFee = () => { const { fee } = this.state; if (isNaN(Number(fee))) return this.setState({ errorMessage: "Invalid Number", errorType: Errors.SetFee }) this.props.setFee(fee) } onWhitelist = () => { const { whitelistToken, whitelistTokenAdd } = this.state; if(!ethers.utils.isAddress(whitelistTokenAdd.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.Whitelist }) } this.props.whitelistToken(whitelistTokenAdd, whitelistToken); } onSetGasUsedByPost = () => { const { gasUsedByPost } = this.state; if (isNaN(Number(gasUsedByPost))) return this.setState({ errorMessage: "Invalid Number", errorType: Errors.SetGasUsedByPost }) this.props.setGasUsedByPost(gasUsedByPost) } onSetMinGas = () => { const { minGas } = this.state; if (isNaN(Number(minGas))) return this.setState({ errorMessage: "Invalid Number", errorType: Errors.SetMinGas }) this.props.setMinGas(minGas) } onSetTarget = () => { const { target } = this.state if(!ethers.utils.isAddress(target.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.SetTarget }) } this.props.setTarget(target); } onSetRelayHub = () => { const { relayHub } = this.state if(!ethers.utils.isAddress(relayHub.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.SetRelayHub }) } this.props.setRelayHub(relayHub); } onSetForwarder = () => { const { forwarder } = this.state if(!ethers.utils.isAddress(forwarder.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.SetForwarder }) } this.props.setForwarder(forwarder); } onWithdraw = () => { const { withdrawAmount, withdrawReceiver} = this.state; if(!ethers.utils.isAddress(withdrawReceiver.trim())) { return this.setState({ errorMessage: "Invalid Address", errorType: Errors.Withdraw }) } if (isNaN(Number(withdrawAmount))) return this.setState({ errorMessage: "Invalid Number", errorType: Errors.Withdraw }) this.props.withdraw(withdrawAmount, withdrawReceiver) } renderSetMinBalance = () => { return (
this.setState({ minBalance: target.value, errorMessage: null, errorType: null })} />
Sets Min Balance the Token Paymaster contract should have
) } renderSetPaymentToken = () => { return (
this.setState({ paymentToken: target.value, errorMessage: null, errorType: null })} /> Fee Payment Token
) } renderSetFee = () => { return (
this.setState({ fee: target.value, errorMessage: null, errorType: null })} /> Fee charged in any swap
) } renderwhitelistToken = () => { return (
this.setState({whitelistToken: target.checked, errorMessage: null, errorType: null}) } /> this.setState({ whitelistTokenAdd: target.value, errorMessage: null, errorType: null })} /> Whitelist ERC20 Token for swapping
) } renderSetGasUsedByPost = () => { return (
this.setState({ gasUsedByPost: target.value, errorMessage: null, errorType: null })} /> Gas used when the call is relayed back, around 60000 Gas Minumim
) } renderSetMinGas = () => { return (
this.setState({ minGas: target.value, errorMessage: null, errorType: null })} /> Minimum gas user has to send in the transaction
) } renderSetTarget = () => { return (
this.setState({ target: target.value, errorMessage: null, errorType: null })} />
Target is the Token Swap Contract
) } renderSetRelayHub = () => { return (
this.setState({ relayHub: target.value, errorMessage: null, errorType: null })} /> Set Relay Hub Contract
) } renderSetForwarder = () => { return (
this.setState({ forwarder: target.value, errorMessage: null, errorType: null })} /> Set Forwarder Contract
) } renderWithdraw = () => { return(
this.setState({ withdrawAmount: target.value, errorMessage: null, errorType: null })} /> this.setState({ withdrawReceiver: target.value, errorMessage: null, errorType: null })} /> Withdraws ETH balance from Paymaster Contract
) } renderErrorModal = () => { return ( this.setState({errorMessage: null})}> Error {this.state.errorMessage} ) } render() { return (
{this.renderErrorModal()} {this.renderWithdraw()} {this.renderSetMinBalance()} {this.renderSetPaymentToken()} {this.renderSetFee()} {this.renderwhitelistToken()} {this.renderSetGasUsedByPost()} {this.renderSetMinGas()} {this.renderSetTarget()} {this.renderSetRelayHub()} {this.renderSetForwarder()}
) } } const mapStateToProps = (state: StoreState): StateProps => { return { web3State: getWeb3State(state) } } const mapDispatchToProps = (dispatch: any): DispatchProps => { return { setMinBalance: (amount: string) => dispatch(startPaymasterSetMinBalance(amount)), setPaymentToken: (address: string) => dispatch(startPaymasterSetPaymentToken(address)), setFee: (amount: string) => dispatch(startPaymasterSetFee(amount)), whitelistToken: (address: string, whitelist: boolean) => dispatch(startPaymasterWhitelisToken(address, whitelist)), setGasUsedByPost: (amount: string) => dispatch(startPaymasterSetGasUsedByPost(amount)), setMinGas: (amount: string) => dispatch(startPaymasterSetMinGas(amount)), setTarget: (address: string) => dispatch(startPaymasterSetTarget(address)), setRelayHub: (address: string) => dispatch(startPaymasterSetRelayHub(address)), setForwarder: (address: string) => dispatch(startPaymasterSetForwarder(address)), withdraw: (amount: string, receiver: string) => dispatch(startPaymasterWithdraw(amount, receiver)) } } const AdminContainer = connect(mapStateToProps, mapDispatchToProps)(Admin); export { AdminContainer }