-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionContext.js
148 lines (119 loc) · 4.41 KB
/
TransactionContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useEffect, useState } from "react";
import { ethers } from "ethers";
// import { contractABI, contractAddress } from "../utils/constants";
contractABI = "";
contractAddress = "";
export const TransactionContext = React.createContext();
const getEthereumContract = () => {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const transactionContract = new ethers.Contract(
contractAddress,
contractABI,
signer
);
return transactionContract;
};
export const TransactionProvider = ({ children }) => {
const [currentAccount, setCurrentAccount] = useState();
const [connectCounter, setConnectCounter] = useState(0);
const [formData, setFormData] = useState({addressTo: ''});
const [studentData, setStudentData] = useState({jsonURI: '', addressFrom: ''});
const handleChange = (e, name) =>{
setFormData((prevState) => ({...prevState, [name]: e.target.value}));
}
const handleStudent = (e, name) =>{
setStudentData((prevState) => ({...prevState,[name]: e.target.value}));
}
// Initial Startup function
const checkIfWalletIsConnected = async () => {
if (!ethereum) return alert("Please install metamask");
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
setCurrentAccount(accounts);
if (currentAccount == null) {
setCurrentAccount(accounts[0]);
}
if (accounts.length) {
setCurrentAccount(accounts[0]);
} else {
console.log("No accounts");
}
};
// Custom Connect Button
const connectWallet = async () => {
try {
if (!ethereum) return alert("Please install metamask");
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
setCurrentAccount(accounts[0]);
if (currentAccount) {
if (connectCounter == 0) {
setConnectCounter(connectCounter + 1);
return alert("Wallet Connected");
} else if (connectCounter >= 1) {
getEthereumContract();
setConnectCounter(connectCounter + 1);
return alert("Wallet already connected");
}
}
} catch (error) {
console.log(error);
}
};
const sendTransaction = async () =>{
try {
if(!ethereum) return alert("Please install metamask");
const {addressTo} = formData;
const transactionContract = getEthereumContract();
const parsedAmount = ethers.utils.parseEther("0")
await ethereum.request({
method:'eth_sendTransaction',
params:[{
from: currentAccount,
to: contractAddress,
gas: '0x186A0',
value: parsedAmount._hex,
}]
});
const transactionHash = await transactionContract.issueDegree(addressTo);
await transactionHash.wait();
} catch (error) {
console.log(error);
}
}
const claimDegree = async () =>{
try {
if(!ethereum) return alert("Please install metamask");
const {jsonURI} = studentData;
const ipfsdata = "https://ipfs.io/ipfs/"+jsonURI;
const transactionContract = getEthereumContract();
await ethereum.request({
method: 'eth_sendTransaction',
params:[{
from: currentAccount,
to: contractAddress,
gas: '0x5208',
}]
});
const transactionHash = await transactionContract.claimDegree(ipfsdata);
await transactionHash.wait();
} catch (error) {
console.log(error);
}
}
// Startup
useEffect(() => {
checkIfWalletIsConnected();
}, []);
// Return fuction and passing all values
return (
<TransactionContext.Provider
value={{ connectWallet, connectCounter, currentAccount, formData, setFormData, handleChange, handleStudent, sendTransaction, claimDegree, studentData, setStudentData}}
>
{children}
</TransactionContext.Provider>
);
};