Skip to content

Commit

Permalink
anchoring ipfs data on contract
Browse files Browse the repository at this point in the history
incr gas price for görli
  • Loading branch information
elmariachi111 committed Aug 12, 2020
1 parent 27a34f3 commit ca6839a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
46 changes: 46 additions & 0 deletions .openzeppelin/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,43 @@
],
"storageDiff": []
}
},
"DocExchange": {
"address": "0x1E6C6D4c33FfB5763562DDA25F260e84Be69d2C7",
"constructorCode": "608060405234801561001057600080fd5b50610458806100206000396000f3fe",
"bodyBytecodeHash": "167057e1f0e73f4632f0d9c828320dc88f39b7468ad91b99836fc4ebe297285e",
"localBytecodeHash": "e027048d52e4df22ba208f5e508a8bbb2ad41d5496ee54ac8a4266f74c781cdb",
"deployedBytecodeHash": "e027048d52e4df22ba208f5e508a8bbb2ad41d5496ee54ac8a4266f74c781cdb",
"types": {
"t_bytes": {
"id": "t_bytes",
"kind": "elementary",
"label": "bytes"
},
"t_mapping<t_bytes>": {
"id": "t_mapping<t_bytes>",
"valueType": "t_bytes",
"label": "mapping(key => bytes)",
"kind": "mapping"
}
},
"storage": [
{
"contract": "DocExchange",
"path": "contracts/DocExchange.sol",
"label": "documents",
"astId": 2138,
"type": "t_mapping<t_bytes>",
"src": "105:35:13"
}
],
"warnings": {
"hasConstructor": false,
"hasSelfDestruct": false,
"hasDelegateCall": false,
"hasInitialValuesInDeclarations": false,
"uninitializedBaseContracts": []
}
}
},
"solidityLibs": {},
Expand All @@ -307,6 +344,15 @@
"admin": "0x5c19BF66CC2c9386Cdf06d346F32F140A4559be9",
"kind": "Upgradeable"
}
],
"ledger-academy/DocExchange": [
{
"address": "0xcB4c372940117184A3fe3eBDe36d118b6f3A454e",
"version": "1.0.0",
"implementation": "0x1E6C6D4c33FfB5763562DDA25F260e84Be69d2C7",
"admin": "0x5c19BF66CC2c9386Cdf06d346F32F140A4559be9",
"kind": "Upgradeable"
}
]
},
"manifestVersion": "2.2",
Expand Down
3 changes: 2 additions & 1 deletion .openzeppelin/project.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"manifestVersion": "2.2",
"contracts": {
"ADIToken": "ADIToken"
"ADIToken": "ADIToken",
"DocExchange": "DocExchange"
},
"dependencies": {
"@openzeppelin/contracts-ethereum-package": "^3.0.0"
Expand Down
17 changes: 17 additions & 0 deletions contracts/DocExchange.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.6.0;


contract DocExchange {
event DocumentAdded(address owner, bytes cid);

mapping(address => bytes) documents;

function addDocument(bytes memory cid) public {
documents[msg.sender] = cid;
emit DocumentAdded(msg.sender, cid);
}

function getMyDocument() public view returns (bytes memory cid) {
return documents[msg.sender];
}
}
69 changes: 65 additions & 4 deletions src/IpfsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
import React, { useState, useEffect } from "react";
import { useWeb3React } from "@web3-react/core";
import Web3 from "web3";
import DocExchange from "./contracts/DocExchange.json";

import IPFS from "ipfs";
import IPFS, { Buffer, CID } from "ipfs";
import _secrets from "../.secrets.json";

const IpfsPage: React.FC = () => {
const { account, library: web3 } = useWeb3React<Web3>();
const [ipfsNode, setIpfsNode] = useState();

const [files, setFiles] = useState([]);
const [myDocumentContent, setMyDocumentContent] = useState();

const contract = new web3.eth.Contract(
DocExchange.abi,
_secrets.docExchangeAddress
);

async function anchorOnChain(cid) {
const bytes = [...cid.buffer];

const promiEvent = contract.methods.addDocument(bytes).send({
from: account,
gasPrice: 21 * 1e5,
gas: 30 * 1e5,
});

promiEvent.on("transactionHash", console.log);
promiEvent.on("receipt", console.log);
promiEvent.on("confirmation", (number, confirmation) => {
console.debug(confirmation);
});
}

async function getMyDocumentFromChainAndResolve() {
const resultBytes = await contract.methods.getMyDocument().call({
from: account,
});
console.log("cid stored on chain", resultBytes);

const bytes = Web3.utils.hexToBytes(resultBytes);
const cid = new CID(Buffer.from(bytes));
console.log("CID recovered from contract", cid.toString());

const contentChunks = await ipfsNode!.cat(cid);
const awaitedChunks: any[] = [];
// eslint-disable-next-line no-restricted-syntax
for await (const chunk of contentChunks) {
awaitedChunks.push(chunk);
}
const result = Buffer.concat(awaitedChunks).toString();
console.log("content: ", result);
setMyDocumentContent(result);
return result;
}

async function addToIpfs(content: string | any[]): Promise<any[]> {
const addResult = ipfsNode!.add(content);
Expand All @@ -25,6 +73,8 @@ const IpfsPage: React.FC = () => {
const ipfsResult = await addToIpfs(_currentContent);
console.log(ipfsResult);

anchorOnChain(ipfsResult[0].cid);

setFiles([...files, ipfsResult[0]]);
};

Expand All @@ -39,12 +89,18 @@ const IpfsPage: React.FC = () => {
return (
<div>
<form onSubmit={submit}>
<textarea name="thecontent">test</textarea>
<textarea
name="thecontent"
rows={10}
cols={40}
defaultValue="type anything you'd like to store forever..."
></textarea>{" "}
<br />
<button type="submit" value="store!">
store!
store forever!
</button>
</form>
<h2>Files:</h2>
<h2>History:</h2>
<ul>
{files.map((f) => (
<li>
Expand All @@ -57,6 +113,11 @@ const IpfsPage: React.FC = () => {
</li>
))}
</ul>
<h2>My Document:</h2>
<button onClick={getMyDocumentFromChainAndResolve}>
get my document.
</button>
<p>{myDocumentContent}</p>
</div>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/contracts/DocExchange.json

0 comments on commit ca6839a

Please sign in to comment.