Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

This PR adds initial core-js SDK integration #11

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39,740 changes: 23,179 additions & 16,561 deletions client/package-lock.json

Large diffs are not rendered by default.

97 changes: 48 additions & 49 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { useUserAddress } from "eth-hooks";
import { useUserProvider, useContractLoader, useBalance } from "./hooks";
import { NETWORKS } from "./networks";
import { Text, Page, Tabs, Row, Col, Spacer } from "@geist-ui/react";
import {generateSignature} from "./lib/signerConnect"
import {generateIDX} from "./lib/identity"
import {definitions} from "./utils/config.json";
import {loginUserWithChallenge} from "./utils/threadDB"
import {getLoginUser, getSafeData} from './lib/safientDB'
import {PrivateKey} from "@textile/hub"
import {getSigner} from "./lib/signerConnect"
import Archon from '@kleros/archon';

import ContractsNotDeployed from './components/ContractsNotDeployed/ContractsNotDeployed';
Expand All @@ -27,8 +22,10 @@ import Safes from './components/Safes/Safes';
import Profile from "./components/Profile/Profile";
import Content from "./components/Safes/Content";
import Web3 from "web3";
import {SafientSDK} from '@safient/core'
import { set } from "ramda";

const targetNetwork = NETWORKS['kovan'];
const targetNetwork = NETWORKS['localhost'];
const localProviderUrl = targetNetwork.rpcUrl;
const localProvider = new StaticJsonRpcProvider(localProviderUrl);

Expand All @@ -39,8 +36,10 @@ function App() {
const [idx, setIdx] = useState(null);
const [user, setUser] = useState(0);
const [userData, setUserData] =useState([]);
const [identity, setIdentity] = useState(null);
const [provider, setProvider] = useState(null);
const [connection, setConnection] = useState(null)
const [client, setClient] = useState(null);
const [threadId, setThreadId] = useState(null);
const [sc, setSc] = useState(null)
const [web3, setWeb3] = useState(null);
const [arbitrationFee, setArbitrationFee] = useState(null);
const userProvider = useUserProvider(injectedProvider, localProvider);
Expand All @@ -60,50 +59,43 @@ function App() {
}


useEffect(() => {
async function init() {
try {
const arbitrationFeeWei = await archon.arbitrator.getArbitrationCost(
writeContracts.AutoAppealableArbitrator.address
);

setArbitrationFee(arbitrationFeeWei)
} catch (e) {
console.log(e)
}
}
init();
}, [writeContracts]);
// useEffect(() => {
// async function init() {
// try {
// const arbitrationFeeWei = await archon.arbitrator.getArbitrationCost(
// writeContracts.AutoAppealableArbitrator.address
// );

// setArbitrationFee(arbitrationFeeWei)
// } catch (e) {
// console.log(e)
// }
// }
// init();
// }, [writeContracts]);


const connectUser = async (provider) => {
const connectUser = async () => {
console.log(process.env.REACT_APP_USER_API_KEY)
console.log('connect')
const {seed, web3Provider} = await generateSignature(provider);
setProvider(web3Provider)
const {idx, ceramic} = await generateIDX(seed);
setIdx(idx)
console.log(idx)
const identity = PrivateKey.fromRawEd25519Seed(Uint8Array.from(seed))
setIdentity(identity)
let threadData = null
const client = await loginUserWithChallenge(identity);
if (client !== null) {
//call middleWare
setCeramic(ceramic)
threadData = await getLoginUser(idx.id)
}
console.log(client)
const data = await idx.get(definitions.profile, idx.id)
setUserData(threadData)
setUser((threadData && data) ? 2 : 1)
return {idx, identity, threadData}
const signer = await getSigner();
console.log(signer)
let sdk = new SafientSDK(signer, 31337);
setSc(sdk);
const user = await sdk.safientCore.connectUser(process.env.REACT_APP_USER_API_KEY, process.env.REACT_USER_API_SECRET);
const userRes = await sdk.safientCore.getLoginUser(user, user.idx.id);
setUserData(userRes)
setConnection(user)
setIdx(user.idx);
setClient(user.client);
setThreadId(user.threadId)
}

const loadWeb3Modal = useCallback(() => {
async function setProvider() {
const provider = await web3Modal.connect();
setInjectedProvider(new Web3Provider(provider));
await connectUser(null);
await connectUser();
}
setProvider();
}, [setInjectedProvider]);
Expand Down Expand Up @@ -160,32 +152,39 @@ function App() {
<Safes writeContracts={writeContracts} />
</Tabs.Item>
<Tabs.Item label='claims' value='8'>
<Claims writeContracts={writeContracts} />
<Claims
idx ={idx}
sc = {sc}
connection = {connection}
/>
</Tabs.Item>
<Tabs.Item label="Safe" value="9">
<Content
idx={idx}
idx={idx}
sc={sc}
user={user}
userData={userData}
network={targetNetwork.name}
address={address}
writeContracts={writeContracts}
arbitrationFee={arbitrationFee}
injectedProvider={injectedProvider}
connection={connection}
/>
</Tabs.Item>
<Tabs.Item label="profile" value="10">
<Profile
ceramic={ceramic}
idx={idx}
identity={identity}
idx={idx}
sc= {sc}
client={client}
web3 = {web3}
user={user}
userData={userData}
setUser={setUser}
setUserData={setUserData}
address={address}
arbitrationFee={arbitrationFee}
connection = {connection}
/>
</Tabs.Item>
</Tabs>
Expand Down
91 changes: 21 additions & 70 deletions client/src/components/Claims/Claims.jsx
Original file line number Diff line number Diff line change
@@ -1,82 +1,33 @@
import React, { useEffect, useState } from 'react';
import { useToasts, Table, Tag } from '@geist-ui/react';
import { useToasts, Table, Tag, Input, Text, Spacer, Button } from '@geist-ui/react';

function Claims({ writeContracts }) {
const [claims, setClaims] = useState([]);
const [data, setData] = useState([]);
const [toasts, setToast] = useToasts();
function Claims({ idx, sc, connection }) {
const [disputeId, setDisputeId] = useState();
const [ruling, setRuling] = useState();

const showAlert = (alertMessage, alertColor) => {
setToast({
text: alertMessage,
type: alertColor,
});
};

useEffect(() => {
async function init() {
try {
const claimsCountArray = Array(Number(await writeContracts.SafexMain.claimsCount())).fill(0);
let claims = [];
claimsCountArray.forEach(async (_, i) => {
const claim = await writeContracts.SafexMain.claims(i);
claims.push(claim);
if (i === claimsCountArray.length - 1) {
setClaims(claims);
}
});
} catch (e) {
if (e.data !== undefined) {
const error = e.data.message.split(':')[2].split('revert ')[1];
showAlert(error + '!', 'warning');
} else {
showAlert('Error!', 'warning');
const handleSubmit = async () => {
if (idx) {
const res = await sc.safientCore.giveRuling(parseInt(disputeId), parseInt(ruling))
console.log(res)
}
}
}
init();
}, [writeContracts]);
}

useEffect(() => {
function init() {
let newData = [];
claims.map((claim) => {
const dataItem = {
safeId: String(claim.safeId),
disputeId: String(claim.disputeId),
claimedBy: claim.claimedBy.substr(0, 5) + '...' + claim.claimedBy.slice(claim.claimedBy.length - 5),
result:
(claim.result === 'Active' && (
<Tag type='secondary' invert>
Active
</Tag>
)) ||
(claim.result === 'Passed' && (
<Tag type='success' invert>
Passed
</Tag>
)) ||
(claim.result === 'Failed' && (
<Tag type='error' invert>
Failed
</Tag>
)),
};
newData.push(dataItem);
});
setData(newData);
}
init();
}, [claims]);


return (
<>
<Table data={data}>
<Table.Column prop='safeId' label='Safe Id' />
<Table.Column prop='disputeId' label='Dispute Id' />
<Table.Column prop='claimedBy' label='Claimed By' />
<Table.Column prop='result' label='Status' />
</Table>
<Input status="secondary" placeholder="Dispute Id" onChange={(e) => setDisputeId(e.target.value)} width="50%">
<Text b>Name</Text>
</Input>
<Spacer />
<Input status="secondary" placeholder="Ruling" onChange={(e) => setRuling(e.target.value)} width="50%">
<Text b>Email</Text>
</Input>
<Spacer y={2} />
<Button type="secondary" auto onClick={handleSubmit}>
Submit
</Button>
</>
);
}
Expand Down
41 changes: 24 additions & 17 deletions client/src/components/Profile/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import React, { useEffect, useState } from "react";
import { Divider, Spacer, Text, Link, Note, Input, Button, useToasts,Snippet } from "@geist-ui/react";
import {definitions} from "../../utils/config.json";
import {registerNewUser, checkEmailExists} from "../../lib/safientDB"
import {loginUserWithChallenge} from "../../utils/threadDB";
// import {loginUserWithChallenge} from "../../utils/threadDB";
import {generateCipherKey} from "../../utils/aes"


function Profile({ idx, identity, user, userData,setUser,setUserData, address }) {
function Profile({ idx, sc, client, user, setUser, address, connection }) {
const [email, setEmail] = useState('');
const [name, setName] = useState('');
const [modal, setModal] = useState(false);
const [loading, setLoading] = useState(false);
const [signUpToast, setToast] = useToasts(false);
const [userData, setUserData] = useState({})

// const showAlert = (alertMessage, alertColor) => {
// setLoading(false);
Expand All @@ -21,33 +22,39 @@ function Profile({ idx, identity, user, userData,setUser,setUserData, address }
// });
// };

useEffect(() => {
async function loadProfile() {
try {
const userRes = await sc.safientCore.getLoginUser(connection, idx.id);
if(userRes !== null){
setUserData(userRes)
setUser(2)
}else{
setUser(1)
}
} catch (e) {
console.log(e)
}
}
loadProfile();
}, [user]);


const handleSubmit = async () => {
//ceramic and threaddb
// const aesKey = await generateCipherKey();
if (idx) {
setLoading(true);

const client = await loginUserWithChallenge(identity);
if (client != null) {
const { status } = await checkEmailExists(email);
if (status) {

const ceramicRes = await idx.set(definitions.profile, {
name: name,
email: email,
});

const threadRes = await registerNewUser(idx.id, address, name, email, 0);

setUserData(threadRes);
if (ceramicRes && threadRes) {
const res = await sc.safientCore.registerNewUser(connection, name, email, 0, address);
if (res !== `${email} already registered.`) {
setLoading(false);
setModal(false);
setUser(2);
}
} else {
setToast({
text: 'This Email already Exists, Please try with new Email',
text: `${email} already registered.`,
type: 'warning',
delay: 5000,
});
Expand Down
Loading