wallet-connect-web-examples/wallets/react-wallet-v2/src/views/SessionSendTransactionModal.tsx

86 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-03-04 10:24:15 +00:00
import ProjectInfoCard from '@/components/ProjectInfoCard'
2022-03-04 10:37:17 +00:00
import RequestDataCard from '@/components/RequestDataCard'
import RequesDetailsCard from '@/components/RequestDetalilsCard'
import RequestMethodCard from '@/components/RequestMethodCard'
import RequestModalContainer from '@/components/RequestModalContainer'
2022-02-16 12:02:39 +00:00
import ModalStore from '@/store/ModalStore'
2022-02-24 10:23:52 +00:00
import { approveEIP155Request, rejectEIP155Request } from '@/utils/EIP155RequestHandlerUtil'
2022-02-16 12:02:39 +00:00
import { walletConnectClient } from '@/utils/WalletConnectUtil'
import { Button, Divider, Loading, Modal, Text } from '@nextui-org/react'
2022-02-16 12:02:39 +00:00
import { Fragment, useState } from 'react'
export default function SessionSendTransactionModal() {
const [loading, setLoading] = useState(false)
// Get request and wallet data from store
const requestEvent = ModalStore.state.data?.requestEvent
const requestSession = ModalStore.state.data?.requestSession
// Ensure request and wallet are defined
if (!requestEvent || !requestSession) {
return <Text>Missing request data</Text>
}
// Get required proposal data
2022-02-16 12:02:39 +00:00
const { method, params } = requestEvent.request
const transaction = params[0]
// Handle approve action
async function onApprove() {
if (requestEvent) {
setLoading(true)
2022-02-21 13:53:39 +00:00
const response = await approveEIP155Request(requestEvent)
2022-02-16 12:02:39 +00:00
await walletConnectClient.respond({
topic: requestEvent.topic,
response
})
ModalStore.close()
}
}
// Handle reject action
async function onReject() {
if (requestEvent) {
const response = rejectEIP155Request(requestEvent.request)
await walletConnectClient.respond({
topic: requestEvent.topic,
response
})
ModalStore.close()
}
}
return (
<Fragment>
<RequestModalContainer title="Send / Sign Transaction">
<ProjectInfoCard metadata={requestSession.peer.metadata} />
2022-02-16 12:02:39 +00:00
<Divider y={2} />
2022-02-16 12:02:39 +00:00
<RequestDataCard data={transaction} />
2022-02-16 12:02:39 +00:00
<Divider y={2} />
2022-02-16 12:02:39 +00:00
<RequesDetailsCard
chains={[requestEvent.chainId ?? '']}
protocol={requestSession.relay.protocol}
/>
2022-02-16 12:02:39 +00:00
<Divider y={2} />
2022-02-16 12:02:39 +00:00
<RequestMethodCard methods={[method]} />
</RequestModalContainer>
2022-02-16 12:02:39 +00:00
<Modal.Footer>
<Button auto flat color="error" onClick={onReject} disabled={loading}>
Reject
</Button>
<Button auto flat color="success" onClick={onApprove} disabled={loading}>
2022-02-16 13:46:42 +00:00
{loading ? <Loading size="sm" color="success" /> : 'Approve'}
2022-02-16 12:02:39 +00:00
</Button>
</Modal.Footer>
</Fragment>
)
}