Skip to content

Commit

Permalink
simple IPFS uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
elmariachi111 committed Aug 12, 2020
1 parent b65f532 commit f49c7c1
Show file tree
Hide file tree
Showing 3 changed files with 4,151 additions and 92 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@web3-react/core": "^6.0.9",
"@web3-react/injected-connector": "^6.0.7",
"ethers": "^4.0.47",
"ipfs": "^0.44.0",
"react": "^16.8",
"react-dom": "^16.8",
"web3": "^1.2.7"
Expand Down
55 changes: 49 additions & 6 deletions src/IpfsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
import React, { useState, useEffect } from "react";
import { useWeb3Context } from "web3-react";
import ADIToken from "./contracts/ADIToken.json";
import TransferForm from "./TransferForm";
import IPFS, { Buffer, CID } from "ipfs";
import _secrets from "../.secrets.json";

import Web3 from "web3";

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

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

async function addToIpfs(content: string | any[]): Promise<any[]> {
const addResult = ipfsNode!.add(content);
const results = [];

for await (const result of addResult) {
results.push(result);
}
return results;
}

const submit = async (e) => {
e.preventDefault();
e.stopPropagation();
const _currentContent = e.target["thecontent"].value;
const ipfsResult = await addToIpfs(_currentContent);
console.log(ipfsResult);

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

useEffect(() => {
(async () => {})();
}, [web3]);
(async () => {
const _ipfsNode = await IPFS.create();
console.log("ipfs node is running");
setIpfsNode(_ipfsNode);
})();
}, []);

return (
<div>
<p>
oh hai <b>{account}</b>
</p>
<form onSubmit={submit}>
<textarea name="thecontent">test</textarea>
<button type="submit" value="store!">
store!
</button>
</form>
<h2>Files:</h2>
<ul>
{files.map((f) => (
<li>
<a
href={`https://ipfs.io/ipfs/${f.cid.toString()}`}
target="_blank"
>
{f.cid.toString()}
</a>
</li>
))}
</ul>
</div>
);
};
Expand Down
Loading

0 comments on commit f49c7c1

Please sign in to comment.