Skip to content

Commit

Permalink
clean up submit code
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Jul 22, 2020
1 parent bc09a06 commit d8fa190
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 57 deletions.
4 changes: 2 additions & 2 deletions www/js/contract.js
@@ -1,7 +1,7 @@
'use strict'

const address = '0x2812bCb0f907111BFAe3af46d30f79Fd613d4F2a';
const abi = [
const contractAddress = '0x2812bCb0f907111BFAe3af46d30f79Fd613d4F2a';
const contractAbi = [
{
"inputs": [
{
Expand Down
128 changes: 74 additions & 54 deletions www/js/script.js
Expand Up @@ -22,30 +22,15 @@ const last = async (iterator) => {
}

document.addEventListener('DOMContentLoaded', async () => {
const node = await Ipfs.create({ repo: 'ipfs-' + Math.random() });
window.node = node;

const status = node.isOnline() ? 'online' : 'offline';

console.log(`Node status: ${status}`);
if (typeof Web3 == "undefined") {
// todo
}

console.log("Web3:");
console.log(Web3);
console.log("Web3.givenProvider:");
console.log(Web3.givenProvider);

var web3 = new Web3(Web3.givenProvider);

console.log('web3');
console.log(web3);
web3.eth.getAccounts(console.log);

window.web3 = web3;

var contract = new web3.eth.Contract(abi, address);
console.log(contract);

var message = await contract.methods.message().call();
console.log(message);

enableSubmitButton();
})

Expand All @@ -62,49 +47,84 @@ function enableSubmitButton() {
}

async function submit() {
console.assert(contractAddress);
console.assert(contractAbi);
console.assert(Ipfs);
console.assert(Web3);

disableSubmitButton();

let node = window.node;
const node = await Ipfs.create({ repo: 'ipfs-' + Math.random() });

var msginput = document.getElementById("msg-input").value;
const status = node.isOnline() ? 'online' : 'offline';
console.log(`Node status: ${status}`);

if (!node.isOnline()) {
// todo
}

if (Web3.givenProvider == null) {
// todo
}

var web3 = new Web3(Web3.givenProvider);

console.log('web3:');
console.log(web3);

console.log(msginput);
var contract = new web3.eth.Contract(contractAbi, contractAddress);
console.log("contract:");
console.log(contract);

var message = await contract.methods.message().call();
console.log(message);

var msginput = document.getElementById("msg-input");
console.assert(msginput);
var msginput = msginput.value;

var addedNode = node.add({
path: 'message.txt',
content: msginput
});

console.log(addedNode);
var addedNode = await addedNode;
var cid = addedNode.cid.toString();

console.log('Added file:', addedNode.path, addedNode.cid.toString());

//test
var contract = new web3.eth.Contract(abi, address);
contract.methods.setMessage(addedNode.cid.toString());

console.log('web3.eth.requestaccounts: ');
var addresses = await web3.eth.requestAccounts();

console.log('accounts[0] -- '+ addresses[0]);

contract.methods.setMessage(addedNode.cid.toString()).send({from: addresses[0]})
.on('transactionHash', function(hash){
console.log('transactionHash');
console.log(hash);
})
.on('receipt', function(receipt){
console.log('receipt');
console.log(receipt);
})
.on('confirmation', function(confirmationNumber, receipt){
console.log('confirmation');
console.log(confirmationNumber);
console.log(receipt);
})
.on('error', function(error){
console.log('error');
console.log(error);
});
console.log('Added file:', addedNode.path, addedNode.cid);

var accounts = await web3.eth.requestAccounts();

console.log("accounts:");
console.log(accounts);

if (accounts.length < 1) {
// todo
}

var account = accounts[0];
console.log("account:");
console.log(account);

contract.methods
.setMessage(cid)
.send({from: account})
.on('transactionHash', function(hash){
console.log('transactionHash');
console.log(hash);
})
.on('receipt', function(receipt){
console.log('receipt');
console.log(receipt);
})
.on('confirmation', function(confirmationNumber, receipt){
console.log('confirmation');
console.log(confirmationNumber);
console.log(receipt);
})
.on('error', function(error){
console.log('error');
console.log(error);
});

console.log("waiting on ethereum");
}
2 changes: 1 addition & 1 deletion www/update.html
Expand Up @@ -41,7 +41,7 @@ <h1>Update The Big Announcement</h1>
</div>

<div>
<p><button type="button" onclick="submit()" id="submit-button" disabled="true">Submit</button></p>
<p><button type="button" onclick="submit()" id="submit-button" value="disabled" disabled>Submit</button></p>
</div>

<div id="update-messages">
Expand Down

0 comments on commit d8fa190

Please sign in to comment.