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

156 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-03-04 10:24:15 +00:00
import ProjectInfoCard from '@/components/ProjectInfoCard'
2022-02-16 12:02:39 +00:00
import { EIP155_CHAINS, TEIP155Chain } from '@/data/EIP155Data'
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 { truncate } from '@/utils/HelperUtil'
import { walletConnectClient } from '@/utils/WalletConnectUtil'
2022-03-04 10:24:15 +00:00
import { Button, Col, Container, Divider, Loading, Modal, Row, 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
const { chainId } = requestEvent
const { method, params } = requestEvent.request
const { protocol } = requestSession.relay
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>
<Modal.Header>
<Text h3>Send / Sign Transaction</Text>
2022-02-16 12:02:39 +00:00
</Modal.Header>
<Modal.Body>
<Container css={{ padding: 0 }}>
2022-03-04 10:24:15 +00:00
<ProjectInfoCard metadata={requestSession.peer.metadata} />
2022-02-16 12:02:39 +00:00
<Row>
<Col>
<Text h5>From</Text>
<Text color="$gray400">{truncate(transaction.from, 30)}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>To</Text>
<Text color="$gray400">{truncate(transaction.to, 30)}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Value</Text>
<Text color="$gray400">{transaction.value}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Gas Price</Text>
<Text color="$gray400">{transaction.gasPrice}</Text>
</Col>
<Col>
<Text h5>Gas Limit</Text>
<Text color="$gray400">{transaction.gasLimit}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Nonce</Text>
<Text color="$gray400">{transaction.nonce}</Text>
</Col>
<Col>
<Text h5>Data</Text>
<Text color="$gray400">{transaction.data}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Blockchain</Text>
<Text color="$gray400">
{EIP155_CHAINS[chainId as TEIP155Chain]?.name ?? chainId}
</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Method</Text>
<Text color="$gray400">{method}</Text>
</Col>
</Row>
<Divider y={2} />
<Row>
<Col>
<Text h5>Relay Protocol</Text>
<Text color="$gray400">{protocol}</Text>
</Col>
</Row>
</Container>
</Modal.Body>
<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>
)
}