Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 57 additions & 18 deletions client/src/components/Welcome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { TransactionContext } from "../context/TransactionContext";
import { shortenAddress } from "../utils/shortenAddress";
import { Loader } from ".";

const companyCommonStyles = "min-h-[70px] sm:px-0 px-2 sm:min-w-[120px] flex justify-center items-center border-[0.5px] border-gray-400 text-sm font-light text-white";
const companyCommonStyles =
"min-h-[70px] sm:px-0 px-2 sm:min-w-[120px] flex justify-center items-center border-[0.5px] border-gray-400 text-sm font-light text-white";

const Input = ({ placeholder, name, type, value, handleChange }) => (
<input
Expand All @@ -21,7 +22,17 @@ const Input = ({ placeholder, name, type, value, handleChange }) => (
);

const Welcome = () => {
const { currentAccount, connectWallet, handleChange, sendTransaction, formData, isLoading } = useContext(TransactionContext);
const {
currentAccount,
connectWallet,
accountBalance,
handleChange,
sendTransaction,
formData,
isLoading,
} = useContext(TransactionContext);

console.log(accountBalance);

const handleSubmit = (e) => {
const { addressTo, amount, keyword, message } = formData;
Expand All @@ -41,7 +52,8 @@ const Welcome = () => {
Send Crypto <br /> across the world
</h1>
<p className="text-left mt-5 text-white font-light md:w-9/12 w-11/12 text-base">
Explore the crypto world. Buy and sell cryptocurrencies easily on Krypto.
Explore the crypto world. Buy and sell cryptocurrencies easily on
Krypto.
</p>
{!currentAccount && (
<button
Expand Down Expand Up @@ -84,6 +96,13 @@ const Welcome = () => {
<BsInfoCircle fontSize={17} color="#fff" />
</div>
<div>
{!accountBalance ? (
""
) : (
<p className="flex justify-between items-start text-white shadow-xl text-base text-[19px]">
{accountBalance} <SiEthereum fontSize={20} color="#fff" />
</p>
)}
<p className="text-white font-light text-sm">
{shortenAddress(currentAccount)}
</p>
Expand All @@ -94,24 +113,44 @@ const Welcome = () => {
</div>
</div>
<div className="p-5 sm:w-96 w-full flex flex-col justify-start items-center blue-glassmorphism">
<Input placeholder="Address To" name="addressTo" type="text" handleChange={handleChange} />
<Input placeholder="Amount (ETH)" name="amount" type="number" handleChange={handleChange} />
<Input placeholder="Keyword (Gif)" name="keyword" type="text" handleChange={handleChange} />
<Input placeholder="Enter Message" name="message" type="text" handleChange={handleChange} />
<Input
placeholder="Address To"
name="addressTo"
type="text"
handleChange={handleChange}
/>
<Input
placeholder="Amount (ETH)"
name="amount"
type="number"
handleChange={handleChange}
/>
<Input
placeholder="Keyword (Gif)"
name="keyword"
type="text"
handleChange={handleChange}
/>
<Input
placeholder="Enter Message"
name="message"
type="text"
handleChange={handleChange}
/>

<div className="h-[1px] w-full bg-gray-400 my-2" />

{isLoading
? <Loader />
: (
<button
type="button"
onClick={handleSubmit}
className="text-white w-full mt-2 border-[1px] p-2 border-[#3d4f7c] hover:bg-[#3d4f7c] rounded-full cursor-pointer"
>
Send now
</button>
)}
{isLoading ? (
<Loader />
) : (
<button
type="button"
onClick={handleSubmit}
className="text-white w-full mt-2 border-[1px] p-2 border-[#3d4f7c] hover:bg-[#3d4f7c] rounded-full cursor-pointer"
>
Send now
</button>
)}
</div>
</div>
</div>
Expand Down
90 changes: 66 additions & 24 deletions client/src/context/TransactionContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ const { ethereum } = window;
const createEthereumContract = () => {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const transactionsContract = new ethers.Contract(contractAddress, contractABI, signer);
const transactionsContract = new ethers.Contract(
contractAddress,
contractABI,
signer
);

return transactionsContract;
};

export const TransactionsProvider = ({ children }) => {
const [formData, setformData] = useState({ addressTo: "", amount: "", keyword: "", message: "" });
const [formData, setformData] = useState({
addressTo: "",
amount: "",
keyword: "",
message: "",
});
const [currentAccount, setCurrentAccount] = useState("");
const [accountBalance, setAccountBalance] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [transactionCount, setTransactionCount] = useState(localStorage.getItem("transactionCount"));
const [transactionCount, setTransactionCount] = useState(
localStorage.getItem("transactionCount")
);
const [transactions, setTransactions] = useState([]);

const handleChange = (e, name) => {
Expand All @@ -31,16 +43,21 @@ export const TransactionsProvider = ({ children }) => {
if (ethereum) {
const transactionsContract = createEthereumContract();

const availableTransactions = await transactionsContract.getAllTransactions();

const structuredTransactions = availableTransactions.map((transaction) => ({
addressTo: transaction.receiver,
addressFrom: transaction.sender,
timestamp: new Date(transaction.timestamp.toNumber() * 1000).toLocaleString(),
message: transaction.message,
keyword: transaction.keyword,
amount: parseInt(transaction.amount._hex) / (10 ** 18)
}));
const availableTransactions =
await transactionsContract.getAllTransactions();

const structuredTransactions = availableTransactions.map(
(transaction) => ({
addressTo: transaction.receiver,
addressFrom: transaction.sender,
timestamp: new Date(
transaction.timestamp.toNumber() * 1000
).toLocaleString(),
message: transaction.message,
keyword: transaction.keyword,
amount: parseInt(transaction.amount._hex) / 10 ** 18,
})
);

console.log(structuredTransactions);

Expand All @@ -53,13 +70,22 @@ export const TransactionsProvider = ({ children }) => {
}
};

const fetchBalance = async (address) => {
const balance = await ethereum
.request({ method: "eth_getBalance", params: [address, "latest"] })
.then((bal) => ethers.utils.formatEther(bal));

setAccountBalance(balance);
};

const checkIfWalletIsConnect = async () => {
try {
if (!ethereum) return alert("Please install MetaMask.");

const accounts = await ethereum.request({ method: "eth_accounts" });

if (accounts.length) {
fetchBalance(accounts[0]);
setCurrentAccount(accounts[0]);

getAllTransactions();
Expand All @@ -75,9 +101,13 @@ export const TransactionsProvider = ({ children }) => {
try {
if (ethereum) {
const transactionsContract = createEthereumContract();
const currentTransactionCount = await transactionsContract.getTransactionCount();
const currentTransactionCount =
await transactionsContract.getTransactionCount();

window.localStorage.setItem("transactionCount", currentTransactionCount);
window.localStorage.setItem(
"transactionCount",
currentTransactionCount
);
}
} catch (error) {
console.log(error);
Expand All @@ -90,8 +120,11 @@ export const TransactionsProvider = ({ children }) => {
try {
if (!ethereum) return alert("Please install MetaMask.");

const accounts = await ethereum.request({ method: "eth_requestAccounts", });
const accounts = await ethereum.request({
method: "eth_requestAccounts",
});

fetchBalance(accounts[0]);
setCurrentAccount(accounts[0]);
window.location.reload();
} catch (error) {
Expand All @@ -110,23 +143,31 @@ export const TransactionsProvider = ({ children }) => {

await ethereum.request({
method: "eth_sendTransaction",
params: [{
from: currentAccount,
to: addressTo,
gas: "0x5208",
value: parsedAmount._hex,
}],
params: [
{
from: currentAccount,
to: addressTo,
gas: "0x5208",
value: parsedAmount._hex,
},
],
});

const transactionHash = await transactionsContract.addToBlockchain(addressTo, parsedAmount, message, keyword);
const transactionHash = await transactionsContract.addToBlockchain(
addressTo,
parsedAmount,
message,
keyword
);

setIsLoading(true);
console.log(`Loading - ${transactionHash.hash}`);
await transactionHash.wait();
console.log(`Success - ${transactionHash.hash}`);
setIsLoading(false);

const transactionsCount = await transactionsContract.getTransactionCount();
const transactionsCount =
await transactionsContract.getTransactionCount();

setTransactionCount(transactionsCount.toNumber());
window.location.reload();
Expand Down Expand Up @@ -156,6 +197,7 @@ export const TransactionsProvider = ({ children }) => {
sendTransaction,
handleChange,
formData,
accountBalance,
}}
>
{children}
Expand Down