-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web3.js
69 lines (62 loc) · 3.1 KB
/
Web3.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
let Transactions, walletConnected = false;
const App = {
web3,
loadWeb3 : async() => {
if (window.ethereum) {
//
window.web3 = new Web3(window.ethereum);
await window.ethereum.enable()
} else if(window.web3){
window.web3 = new Web3(window.web3.currentProvider);
} else {
window.alert("Please install metamask");
}
},
loadBlockchainData: async() => {
transactions = await (await fetch("../build/contracts/Transactions.json")).json()
const networkId = await web3.eth.net.getId();
const networkData = transactions.networks[networkId];
if (networkData) {
Transactions = new web3.eth.Contract(await transactions.abi, networkData.address);
console.log(Transactions);
} else{
console.log('Transaction contract not deployed on the network')
}
return Transactions
},
connectWallet: async() =>{
App.account = await window.ethereum.enable()
console.log(App.account)
walletConnected = true;
document.getElementById('account__address').innerText = `${App.account[0].slice(0,5)}.....${App.account[0].slice(App.account[0].length - 4, App.account[0].length)}`
document.getElementById('eth__card__account__address').innerText = `${App.account[0].slice(0,5)}.....${App.account[0].slice(App.account[0].length - 4, App.account[0].length)}`
document.getElementById("connect__wallet").style.display = "none";
},
sendEth: async() => {
let ethAddress = document.getElementById("eth__address").value
let ethAmount = document.getElementById("eth__amount").value
let ethMessage = document.getElementById("eth__message").value
let parsedAmount = web3.utils.toWei(ethAmount)
await web3.eth.sendTransaction({from: App.account.toString(), to: ethAddress.toString(), value:`${web3.utils.toHex(parsedAmount)}`})
.on('receipt', (receipt) => {console.log(receipt)})
.on('confirmation', (confirmationNumber,receipt) => {console.log(confirmationNumber)})
let transactionHash = await Transactions.methods.addToBlockchain(ethAddress, parsedAmount, ethMessage).send(
{from:`${App.account}`}
)
console.log(await transactionHash)
await App.getBalance()
},
getBalance: async() => {
let account = await window.ethereum.enable()
let balance = await web3.eth.getBalance(account[0])
balance = web3.utils.fromWei(balance,"ether")
document.getElementById("eth__balance").innerText = balance
},
getAllTransactions: async() => {
let transactionsContract =await App.loadBlockchainData()
let transactions = await Transactions.methods.getTransactions().call()
let transactionCount = await Transactions.methods.getTransactionsCount().call()
console.log(transactions)
transactions = transactions.reverse().map((transaction) => {return {sender:transaction.sender, receiver: transaction.receiver, amount: transaction.amount, amount: transaction.amount, message: transaction.message}})
}
}