Skip to content

Commit

Permalink
Add Basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkeating committed Mar 20, 2021
1 parent 2557efb commit 6acce54
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 11 deletions.
22 changes: 14 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@project/contracts": "file:contracts",
"@superfluid-finance/js-sdk": "^0.4.1",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
Expand Down
13 changes: 10 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { useRef } from "react";
import "./App.css";
import Header from "./components/Header";
import NftForm from "./components/NftForm";
import PutForRentForm from "./components/PutForRentForm";
import RentNft from "./components/RentNft";
import ReturnNft from "./components/ReturnNft";
import Dropzone from "./dropzone/Dropzone";
import useWeb3Modal from "./hooks/useWeb3Modal";



function App() {

const [provider, loadWeb3Modal, logoutOfWeb3Modal] = useWeb3Modal();
console.log("Here");
console.log(provider);
Expand All @@ -25,7 +25,14 @@ function App() {
<p className="title">Drag and Drop Your NFT Image </p>
<div className="content">
<Dropzone />
<h1>Mint NFT</h1>
<NftForm />
<h1>Put NFT for rent</h1>
<PutForRentForm />
<h1>Rent an NFT</h1>
<RentNft provider={provider} />
<h1>Return NFT</h1>
<ReturnNft provider={provider} />
</div>
</div>
);
Expand Down
78 changes: 78 additions & 0 deletions src/components/PutForRentForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useState } from "react";
// import { Form, Button } from "react-bootstrap";
import { abis } from "@project/contracts";
import { Formik, Field, Form, ErrorMessage, useField } from "formik";
import * as Yup from "yup";

const Web3 = require("web3");

const Input: FunctionalComponent<any> = ({ label, ...props }) => {
// useField() returns [formik.getFieldProps(), formik.getFieldMeta()]
// which we can spread on <input>. We can use field meta to show an error
// message if the field is invalid and it has been touched (i.e. visited)
const [field, meta] = useField(props);

return (
<div className="input--container">
<label htmlFor={props.id || props.name}>{label}</label>
<input className="input--input" {...field} {...props} />
{meta.touched && meta.error ? (
<div className="input--error">* {meta.error}</div>
) : null}
</div>
);
};

const PutForRentForm = () => {
const web3 = new Web3(window.ethereum);
const config = {
erc721: "0x76E195437534620106a2Ef736F8C8491159dC640",
};
const putForRent = async (id, price) => {
// const nftCid = await getNFTStorageClient().storeBlob(file);
console.log("hi");
const c = new web3.eth.Contract(abis.erc721.abi, config.erc721);

const tx = await c.methods
.putForRent(id, price)
.send({ from: window.ethereum.selectedAddress });

console.log(tx);
};

return (
<Formik
initialValues={{ nftId: 0 }}
validationSchema={Yup.object({
nftId: Yup.number()
.positive()
.typeError("Must be a number")
.required("Required"),
price: Yup.number()
.positive()
.typeError("Must be a number")
.required("Required"),
})}
onSubmit={async (values, { setSubmitting }) => {
console.log(values);
await putForRent(values.nftId, values.price);
}}
>
{(formProps) => (
<Form className="createSubscriptionPage--formContainer">
<Input label="NFT Id" name="nftId" type="text" />
<Input label="Price" name="price" type="text" />
<div className="createSubscriptionPage--submitContainer">
<button
className="createSubscriptionPage--submitButton"
type="submit"
>
Submit For Rent
</button>
</div>
</Form>
)}
</Formik>
);
};
export default PutForRentForm;
116 changes: 116 additions & 0 deletions src/components/RentNft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from "react";
// import { Form, Button } from "react-bootstrap";
import { abis } from "@project/contracts";
import { Formik, Form, useField } from "formik";
import * as Yup from "yup";
import { getSuperClient } from "../utils";

const Web3 = require("web3");

const Input: FunctionalComponent<any> = ({ label, ...props }) => {
// useField() returns [formik.getFieldProps(), formik.getFieldMeta()]
// which we can spread on <input>. We can use field meta to show an error
// message if the field is invalid and it has been touched (i.e. visited)
const [field, meta] = useField(props);

return (
<div className="input--container">
<label htmlFor={props.id || props.name}>{label}</label>
<input className="input--input" {...field} {...props} />
{meta.touched && meta.error ? (
<div className="input--error">* {meta.error}</div>
) : null}
</div>
);
};

const RentNft = ({ provider }) => {
const web3 = new Web3(window.ethereum);
const config = {
erc721: "0x76E195437534620106a2Ef736F8C8491159dC640",
};
const rentNft = async (id, flowRate) => {
console.log(provider);
const superClient = await getSuperClient(provider);
console.log(superClient);
console.log("Client");

console.log(id, web3.utils.toWei("1"));
console.log(
"ENCODE PARAMETERS: ",
web3.eth.abi.encodeParameters(
["uint256", "uint256"],
[id, web3.utils.toWei("1")]
)
);

console.log("SENDER: ", window.ethereum.selectedAddress);
console.log("SUPERTOKEN: ", superClient.tokens.fDAIx.address);
// function getRentList() external view returns (RentInfo[] memory)
const c = new web3.eth.Contract(abis.erc721.abi, config.erc721);
const rentList = await c.methods
.getRentList()
.call({ from: window.ethereum.selectedAddress });
const correctNFT = rentList.filter((i, element) => {
console.log(i);
console.log(element);
console.log(id);
console.log(i[0]);
console.log(i[2]);
console.log(i[0] === `${id}`);
if (i[0] === `${id}`) {
return element;
}
});
console.log("Correct");
console.log(correctNFT);

const price = correctNFT[0][0];
console.log(price);
const flowPrice = 277777777777778 * price;
console.log(flowPrice);
console.log("Flow Price");
const flowTx = await superClient.cfa.createFlow({
superToken: superClient.tokens.fDAIx.address,
sender: window.ethereum.selectedAddress,
receiver: config.erc721,
flowRate: `${flowPrice}`,
userData: web3.eth.abi.encodeParameters(
["uint256", "uint256"],
[id, web3.utils.toWei("1")]
),
});
console.log(flowTx);
};

return (
<Formik
initialValues={{ id: 0 }}
validationSchema={Yup.object({
nftId: Yup.number()
.positive()
.typeError("Must be a number")
.required("Required"),
})}
onSubmit={async (values, { setSubmitting }) => {
console.log(values);
await rentNft(values.nftId, values.flowRate);
}}
>
{(formProps) => (
<Form className="createSubscriptionPage--formContainer">
<Input label="NFT Id" name="nftId" type="text" />
<div className="createSubscriptionPage--submitContainer">
<button
className="createSubscriptionPage--submitButton"
type="submit"
>
Submit For Rent
</button>
</div>
</Form>
)}
</Formik>
);
};
export default RentNft;
46 changes: 46 additions & 0 deletions src/components/ReturnNft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from "react";
// import { Form, Button } from "react-bootstrap";
import { abis } from "@project/contracts";
import { Formik, Form, useField } from "formik";
import * as Yup from "yup";
import { getSuperClient } from "../utils";

const Web3 = require("web3");

const Input: FunctionalComponent<any> = ({ label, ...props }) => {
// useField() returns [formik.getFieldProps(), formik.getFieldMeta()]
// which we can spread on <input>. We can use field meta to show an error
// message if the field is invalid and it has been touched (i.e. visited)
const [field, meta] = useField(props);

return (
<div className="input--container">
<label htmlFor={props.id || props.name}>{label}</label>
<input className="input--input" {...field} {...props} />
{meta.touched && meta.error ? (
<div className="input--error">* {meta.error}</div>
) : null}
</div>
);
};

const ReturnNft = ({ provider }) => {
const config = {
erc721: "0x76E195437534620106a2Ef736F8C8491159dC640",
};
// How does this know the NFT
const returnNft = async () => {
const superClient = await getSuperClient(provider);

const flowTx = await superClient.cfa.deleteFlow({
superToken: superClient.tokens.fDAIx.address,
sender: window.ethereum.selectedAddress,
receiver: config.erc721,
by: window.ethereum.selectedAddress,
});
console.log(flowTx);
};

return <button onClick={returnNft}>Return NFT</button>;
};
export default ReturnNft;
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SuperfluidSDK = require("@superfluid-finance/js-sdk");

export const getSuperClient = async (provider) => {
console.log("PROVIDER: ", provider);
const sf = new SuperfluidSDK.Framework({
ethers: provider,
tokens: ["fDAI"],
});
await sf.initialize();
return sf;
};

0 comments on commit 6acce54

Please sign in to comment.