From 79f8a6263b35d9ddcdc0581b748b7f5b779f0a2b Mon Sep 17 00:00:00 2001 From: Vladislav Ivanov Date: Sun, 3 Jan 2021 17:45:57 +0200 Subject: [PATCH] Initial commit --- src/App.tsx | 134 +----------------------------------------- src/helpers/ethers.ts | 27 --------- 2 files changed, 3 insertions(+), 158 deletions(-) delete mode 100644 src/helpers/ethers.ts diff --git a/src/App.tsx b/src/App.tsx index 1cd589d..a5ebda4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,20 +4,12 @@ import styled from 'styled-components'; import Web3Modal from 'web3modal'; // @ts-ignore import WalletConnectProvider from '@walletconnect/web3-provider'; -import Button from './components/Button'; import Column from './components/Column'; import Wrapper from './components/Wrapper'; import Header from './components/Header'; import Loader from './components/Loader'; import ConnectButton from './components/ConnectButton'; -import { fonts } from './styles'; -import { - US_ELECTION_ROPSTEN_ADDRESS -} from './constants'; -import { getContract } from './helpers/ethers'; -import US_ELECTION from './constants/abis/USElection.json'; -import { logMsg } from './helpers/dev'; import { Web3Provider } from '@ethersproject/providers'; import { getChainData } from './helpers/utilities'; @@ -56,23 +48,6 @@ const SBalances = styled(SLanding)` } `; -const STestButtonContainer = styled.div` - width: 100%; - display: flex; - justify-content: center; - align-items: center; - flex-wrap: wrap; -`; - -const STestButton = styled(Button)` - border-radius: 8px; - font-size: ${ fonts.size.medium }; - height: 44px; - width: 100%; - max-width: 175px; - margin: 12px; -`; - interface IAppState { fetching: boolean; address: string; @@ -131,93 +106,15 @@ class App extends React.Component { const address = provider.selectedAddress ? provider.selectedAddress : provider?.accounts[0]; - const electionContract = getContract(US_ELECTION_ROPSTEN_ADDRESS, US_ELECTION.abi, library, address); - await this.setState({ provider, library, chainId: network.chainId, address, - connected: true, - electionContract + connected: true }); }; - public currentLeader = async () => { - const { electionContract } = this.state; - - if (!electionContract) { - return; - } - - const currentLeader = await electionContract.currentLeader(); - logMsg('Current leader: ', currentLeader); - - await this.setState({ info: { message: `Current Leader: ${ currentLeader }`, link: '#' } }); - }; - - public submitElectionResult = async () => { - const { electionContract } = this.state; - - if (!electionContract) { - return; - } - - const dataArr = [ - 'Dupnitsa', - 1, - 50, - 24 - ]; - - try { - await this.setState({ fetching: true }); - const transaction = await electionContract.submitStateResult(dataArr); - - logMsg(`Wait for the transaction... State Result Submission Transaction: ${ transaction.hash }`); - - const {chainId} = this.state; - const chainData = chainId ? getChainData(chainId) : null; - await this.setState({ info: { message: `Etherscan tx: ${transaction.hash}`, link: `${chainData.explorer}/tx/${transaction.hash}` } }); - - const transactionReceipt = await transaction.wait(); - if (transactionReceipt.status !== 1) { - logMsg('Transaction was not successful'); - } - - await this.setState({ fetching: false }); - } catch (e) { - await this.setState({ fetching: false }); - await this.setState({ info: { message: e, link: '#' } }); - } - }; - - public endElection = async () => { - const { electionContract } = this.state; - - if (!electionContract) { - return; - } - - try { - await this.setState({ fetching: true }); - - const endTransaction = await electionContract.endElection(); - logMsg('End Transaction: ', endTransaction.hash); - - const endTransactionReceipt = await endTransaction.wait(); - if (endTransactionReceipt.status !== 1) { - logMsg('Transaction was not successful'); - } - await this.setState({ info: { message: `Etherscan tx: ${endTransaction.hash}`, link: `${endTransaction.explorer}/tx/${endTransaction.hash}` } }); - - await this.setState({ fetching: false }); - } catch (e) { - await this.setState({ fetching: false }); - await this.setState({ info: { message: e, link: '#' } }); - } - }; - public getNetwork = () => getChainData(this.state.chainId).network; public getProviderOptions = () => { @@ -242,9 +139,7 @@ class App extends React.Component { address, connected, chainId, - fetching, - electionContract, - info + fetching } = this.state; return ( @@ -262,30 +157,7 @@ class App extends React.Component { - ) : electionContract ? ( - -

Contract Actions

- - - - Current Leader - - - - Submit Result - - - - End election - - - { info !== null ? ( - { info.message } - ) : null } - -
- ) : ( + ) :( diff --git a/src/helpers/ethers.ts b/src/helpers/ethers.ts deleted file mode 100644 index a7fc3fe..0000000 --- a/src/helpers/ethers.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { getAddress } from '@ethersproject/address'; -import { AddressZero } from '@ethersproject/constants'; -import { Contract } from '@ethersproject/contracts'; - -export function isAddress(value: string) { - try { - return getAddress(value); - } catch { - return false; - } -} - -export function getSigner(library: any, account: any) { - return library.getSigner(account).connectUnchecked(); -} - -export function getProviderOrSigner(library: any, account: any) { - return account ? getSigner(library, account) : library; -} - -export function getContract(address: string, ABI: any, library: any, account: any) { - if (!isAddress(address) || address === AddressZero) { - throw Error(`Invalid 'address' parameter '${ address }'.`); - } - - return new Contract(address, ABI, getProviderOrSigner(library, account)); -}