|
10 | 10 | let etherGram;
|
11 | 11 | const ethergramABI = require("./ethergram_abi.js");
|
12 | 12 | const scAddress = "0x9aa9c91c79e22882139f59a322173eb349783484";
|
13 |
| - const myAddress = "0x519Ff9BEFa4127688900C31922350103aA5495e6"; |
| 13 | + let callerAddress; |
14 | 14 |
|
15 | 15 | var postsFromSC = [];
|
16 | 16 |
|
|
35 | 35 | const ipfsHash = e.detail.path;
|
36 | 36 | //convert ipfsHash to bytes32 to fit the SC
|
37 | 37 | const hashToSend = hashToBytes32(ipfsHash);
|
38 |
| - etherGram.methods.upload(hashToSend).send({from: myAddress}); |
| 38 | + etherGram.methods.upload(hashToSend).send({from: callerAddress}); |
39 | 39 | getPostsFromSC(e);
|
40 | 40 | }
|
41 | 41 |
|
42 | 42 | const clap = (e) => {
|
43 | 43 | e.preventDefault()
|
44 | 44 | const imageHash = hashToBytes32(e.detail);
|
45 |
| - etherGram.methods.clap(imageHash).send({from: myAddress}) |
| 45 | + etherGram.methods.clap(imageHash).send({from: callerAddress}) |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | const comment = (e) => {
|
49 | 49 | const commentHash = web3.utils.asciiToHex(e.detail.elements[0].value);
|
50 | 50 | const imageHash = hashToBytes32(e.detail.name);
|
51 |
| - etherGram.methods.comment(imageHash, commentHash).send({from: myAddress}); |
| 51 | + etherGram.methods.comment(imageHash, commentHash).send({from: callerAddress}); |
52 | 52 | }
|
53 | 53 |
|
54 | 54 | const getPostsFromSC = async (e) => {
|
55 | 55 | e.preventDefault()
|
56 | 56 | setPage("uploading");
|
57 |
| - const result = await etherGram.methods.getAllPosts().call({from: myAddress}); |
| 57 | + const result = await etherGram.methods.getAllPosts().call({from: callerAddress}); |
58 | 58 | for (let i=0; i < result.length; i++) {
|
59 | 59 | //default ipfs values for first 2 bytes: function: 0x12=sha2, size: 0x20=256 bits
|
60 | 60 | //cut the "0x" off
|
|
76 | 76 | }
|
77 | 77 |
|
78 | 78 | const getClapCountFromSC = async (imageHash) => {
|
79 |
| - return etherGram.methods.getClapCount(imageHash).call({from: myAddress}) |
| 79 | + return etherGram.methods.getClapCount(imageHash).call({from: callerAddress}) |
80 | 80 | }
|
81 | 81 |
|
82 | 82 | const getComment = async (imageHash) => {
|
83 |
| - const result = await etherGram.methods.getComments(imageHash).call({from: myAddress}) |
| 83 | + const result = await etherGram.methods.getComments(imageHash).call({from: callerAddress}) |
84 | 84 | var processedComments=[];
|
85 | 85 | result.forEach(commentHash => {
|
86 | 86 | processedComments.push(web3.utils.toAscii(commentHash));
|
|
92 | 92 | etherGram = new web3.eth.Contract(ethergramABI,scAddress);
|
93 | 93 | }
|
94 | 94 |
|
95 |
| - window.addEventListener('load', function() { |
| 95 | + window.addEventListener('load', async function() { |
96 | 96 | // Checking if Web3 has been injected by the browser (Mist/MetaMask)
|
97 | 97 | if (typeof web3 !== 'undefined') {
|
98 | 98 | // Use Mist/MetaMask's provider
|
99 | 99 | console.log("Use MetaMask as provider");
|
100 |
| - web3 = new Web3(web3.currentProvider); |
101 |
| - |
| 100 | + web3 = new Web3(window.web3.currentProvider); |
102 | 101 | } else {
|
103 | 102 | console.log("Error! There is no provider to use!!!");
|
104 | 103 | }
|
105 | 104 |
|
106 | 105 | // Now you can start your app & access web3js freely:
|
107 | 106 | startApp()
|
| 107 | +
|
| 108 | + var accounts = await web3.eth.getAccounts(); |
| 109 | + callerAddress = accounts[0]; |
| 110 | +
|
| 111 | + var accountInterval = setInterval(function() { |
| 112 | + if (web3.eth.accounts[0] !== callerAddress) { |
| 113 | + callerAddress = web3.eth.accounts[0]; |
| 114 | + } |
| 115 | + }, 100); |
108 | 116 | })
|
109 | 117 |
|
110 | 118 |
|
|
0 commit comments