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

181 lines
4.7 KiB
TypeScript

import { EIP155_CHAINS, TEIP155Chain } from '@/data/EIP155Data'
import ModalStore from '@/store/ModalStore'
import { truncate } from '@/utils/HelperUtil'
import { approveEIP155Request, rejectEIP155Request } from '@/utils/RequestHandlerUtil'
import { walletConnectClient } from '@/utils/WalletConnectUtil'
import { wallet } from '@/utils/WalletUtil'
import {
Avatar,
Button,
Col,
Container,
Divider,
Link,
Loading,
Modal,
Row,
Text
} from '@nextui-org/react'
import { providers } from 'ethers'
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 { name, icons, url } = requestSession.peer.metadata
const transaction = params[0]
// Handle approve action
async function onApprove() {
if (requestEvent) {
setLoading(true)
const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc)
const connectedWallet = wallet.connect(provider)
const response = await approveEIP155Request(requestEvent.request, connectedWallet)
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>
</Modal.Header>
<Modal.Body>
<Container css={{ padding: 0 }}>
<Row align="center">
<Col span={3}>
<Avatar src={icons[0]} />
</Col>
<Col span={14}>
<Text h5>{name}</Text>
<Link href={url}>{url}</Link>
</Col>
</Row>
<Divider y={2} />
<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}>
{loading ? <Loading size="sm" color="success" /> : 'Approve'}
</Button>
</Modal.Footer>
</Fragment>
)
}