2022-05-08 14:49:18 +02:00
|
|
|
import React, { ReactElement, useState } from 'react'
|
2020-01-13 22:07:21 +01:00
|
|
|
import { parseEther } from '@ethersproject/units'
|
2018-10-13 20:37:13 +02:00
|
|
|
import InputGroup from './InputGroup'
|
2019-11-22 21:26:50 +01:00
|
|
|
import Alert, { getTransactionMessage } from './Alert'
|
2021-03-23 01:09:32 +01:00
|
|
|
import { web3 as styleWeb3 } from './index.module.css'
|
2022-09-16 00:48:55 +02:00
|
|
|
import { useSendTransaction, usePrepareSendTransaction } from 'wagmi'
|
2022-05-08 14:49:18 +02:00
|
|
|
import { ConnectButton } from '@rainbow-me/rainbowkit'
|
2018-10-09 23:48:25 +02:00
|
|
|
|
2020-05-22 14:38:19 +02:00
|
|
|
export default function Web3Donation({
|
|
|
|
address
|
|
|
|
}: {
|
|
|
|
address: string
|
|
|
|
}): ReactElement {
|
2022-09-16 00:48:55 +02:00
|
|
|
const { config } = usePrepareSendTransaction({ request: {} })
|
|
|
|
const { sendTransactionAsync } = useSendTransaction(config)
|
2022-05-08 14:49:18 +02:00
|
|
|
|
2021-03-23 21:38:06 +01:00
|
|
|
const [message, setMessage] = useState<{ status: string; text: string }>()
|
|
|
|
const [transactionHash, setTransactionHash] = useState<string>()
|
2019-11-21 21:32:55 +01:00
|
|
|
|
2022-05-08 14:49:18 +02:00
|
|
|
async function handleSendTransaction(amount: string) {
|
2019-11-21 21:32:55 +01:00
|
|
|
setMessage({
|
|
|
|
status: 'loading',
|
2019-11-22 21:26:50 +01:00
|
|
|
text: getTransactionMessage().waitingForUser
|
2019-11-21 21:32:55 +01:00
|
|
|
})
|
2018-10-09 23:48:25 +02:00
|
|
|
|
2020-01-13 22:07:48 +01:00
|
|
|
try {
|
2022-05-08 14:49:18 +02:00
|
|
|
const tx = await sendTransactionAsync({
|
2022-09-16 00:48:55 +02:00
|
|
|
recklesslySetUnpreparedRequest: {
|
2022-05-08 14:49:18 +02:00
|
|
|
to: address,
|
|
|
|
value: parseEther(amount) // ETH -> Wei
|
|
|
|
}
|
2020-01-13 22:07:48 +01:00
|
|
|
})
|
|
|
|
setTransactionHash(tx.hash)
|
|
|
|
setMessage({
|
|
|
|
status: 'loading',
|
|
|
|
text: getTransactionMessage().waitingConfirmation
|
|
|
|
})
|
2018-10-30 18:08:07 +01:00
|
|
|
|
2020-01-13 22:07:48 +01:00
|
|
|
await tx.wait()
|
2019-11-23 18:19:41 +01:00
|
|
|
|
2020-01-13 22:07:48 +01:00
|
|
|
setMessage({
|
|
|
|
status: 'success',
|
|
|
|
text: getTransactionMessage().success
|
|
|
|
})
|
|
|
|
} catch (error) {
|
2021-03-23 21:38:06 +01:00
|
|
|
setMessage(null)
|
2020-01-13 22:07:48 +01:00
|
|
|
}
|
2018-10-09 23:48:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:32:55 +01:00
|
|
|
return (
|
2021-03-23 01:09:32 +01:00
|
|
|
<div className={styleWeb3}>
|
2022-05-08 14:49:18 +02:00
|
|
|
<ConnectButton chainStatus="icon" showBalance={false} />
|
|
|
|
|
2021-03-23 01:09:32 +01:00
|
|
|
{message ? (
|
|
|
|
<Alert message={message} transactionHash={transactionHash} />
|
2019-11-21 21:32:55 +01:00
|
|
|
) : (
|
2022-05-08 14:49:18 +02:00
|
|
|
<InputGroup sendTransaction={handleSendTransaction} />
|
2019-11-21 21:32:55 +01:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
2018-10-09 23:48:25 +02:00
|
|
|
}
|