|
| 1 | +import { storiesOf } from '@storybook/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { Provider, useWallet, useContract } from '..'; |
| 4 | +import { Button, Input, Grid, GridItem } from '@chakra-ui/react'; |
| 5 | +import { ethers } from 'ethers'; |
| 6 | + |
| 7 | +const ADDRESS = '0x7e1D33FcF1C6b6fd301e0B7305dD40E543CF7135'; // Rinkeby |
| 8 | +const ABI = [ |
| 9 | + { |
| 10 | + inputs: [ |
| 11 | + { |
| 12 | + internalType: 'string', |
| 13 | + name: '_greeting', |
| 14 | + type: 'string', |
| 15 | + }, |
| 16 | + ], |
| 17 | + stateMutability: 'nonpayable', |
| 18 | + type: 'constructor', |
| 19 | + }, |
| 20 | + { |
| 21 | + inputs: [], |
| 22 | + name: 'greet', |
| 23 | + outputs: [ |
| 24 | + { |
| 25 | + internalType: 'string', |
| 26 | + name: '', |
| 27 | + type: 'string', |
| 28 | + }, |
| 29 | + ], |
| 30 | + stateMutability: 'view', |
| 31 | + type: 'function', |
| 32 | + }, |
| 33 | + { |
| 34 | + inputs: [ |
| 35 | + { |
| 36 | + internalType: 'string', |
| 37 | + name: '_greeting', |
| 38 | + type: 'string', |
| 39 | + }, |
| 40 | + ], |
| 41 | + name: 'setGreeting', |
| 42 | + outputs: [], |
| 43 | + stateMutability: 'nonpayable', |
| 44 | + type: 'function', |
| 45 | + }, |
| 46 | + { |
| 47 | + inputs: [ |
| 48 | + { |
| 49 | + internalType: 'address payable', |
| 50 | + name: '_to', |
| 51 | + type: 'address', |
| 52 | + }, |
| 53 | + ], |
| 54 | + name: 'transferTo', |
| 55 | + outputs: [], |
| 56 | + stateMutability: 'payable', |
| 57 | + type: 'function', |
| 58 | + }, |
| 59 | +]; |
| 60 | + |
| 61 | +const Default = () => { |
| 62 | + const { connectWallet, disconnectWallet, connected } = useWallet(); |
| 63 | + const contract = useContract(ADDRESS, ABI); |
| 64 | + const [state, setState] = React.useState({ |
| 65 | + newGreeting: '', |
| 66 | + toAddress: '', |
| 67 | + amount: '0', |
| 68 | + }); |
| 69 | + |
| 70 | + const handleGreet = async () => alert(await contract.greet()); |
| 71 | + const handleChangeState = |
| 72 | + (stateName: string) => |
| 73 | + ({ target: { value } }) => { |
| 74 | + setState({ ...state, [stateName]: value }); |
| 75 | + }; |
| 76 | + const handleSetGreeting = async () => { |
| 77 | + await contract.setGreeting(state.newGreeting); |
| 78 | + setState({ ...state, newGreeting: '' }); |
| 79 | + }; |
| 80 | + const handleTransferTo = async () => { |
| 81 | + await contract.transferTo(state.toAddress, { value: ethers.utils.parseEther(state.amount) }); |
| 82 | + setState({ ...state, toAddress: '', amount: '0' }); |
| 83 | + }; |
| 84 | + |
| 85 | + if (connected) { |
| 86 | + return ( |
| 87 | + <div> |
| 88 | + <Button onClick={disconnectWallet}>Disconnect wallet</Button> |
| 89 | + <h3>Contract Methods</h3> |
| 90 | + <Grid templateColumns='repeat(5, 1fr)' columnGap={5}> |
| 91 | + <GridItem colSpan={5}> |
| 92 | + <Button onClick={handleGreet}>greet</Button> |
| 93 | + </GridItem> |
| 94 | + <GridItem colSpan={5}> |
| 95 | + <Button disabled={!state.newGreeting} onClick={handleSetGreeting}> |
| 96 | + setGreeting |
| 97 | + </Button> |
| 98 | + <Input |
| 99 | + value={state.newGreeting} |
| 100 | + placeholder='New Greeting!' |
| 101 | + onChange={handleChangeState('newGreeting')} |
| 102 | + /> |
| 103 | + </GridItem> |
| 104 | + <GridItem colSpan={5}> |
| 105 | + <Button disabled={!(state.toAddress && state.amount)} onClick={handleTransferTo}> |
| 106 | + transferTo |
| 107 | + </Button> |
| 108 | + <Input |
| 109 | + value={state.toAddress} |
| 110 | + placeholder='0xjA123....' |
| 111 | + onChange={handleChangeState('toAddress')} |
| 112 | + /> |
| 113 | + <Input placeholder='0.2' value={state.amount} onChange={handleChangeState('amount')} /> |
| 114 | + </GridItem> |
| 115 | + </Grid> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + return ( |
| 121 | + <> |
| 122 | + <Button onClick={connectWallet}>Connect Wallet</Button> |
| 123 | + </> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +storiesOf('Hooks/useContract', module).add('Default', () => ( |
| 128 | + <Provider network='rinkeby'> |
| 129 | + <Default /> |
| 130 | + </Provider> |
| 131 | +)); |
0 commit comments