Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs rewrite #98

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ git submodule add [email protected]:distribute-protocol/abis.git frontend/src/abi/b
git submodule update --init --recursive
```
If you receive the error that `'frontend/src/abi/build' already exists in the index` then you can move to the next command
Updating submodules can be achieved easily from the root folder using
```
git submodule foreach git pull origin master
```

*Step 2:*

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- "0.0.0.0:8545:8545"
container_name: ganache-cli
image: 'trufflesuite/ganache-cli:latest'
command: -i 5777
ipfs:
image: jbenet/go-ipfs
ports:
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/abi/DistributeToken.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let obj = require('./build/contracts/DistributeToken.json')
let obj = require('./build/contracts/HyphaToken.json')

const DistributeTokenAddress = obj.networks['5777'].address
const HyphaTokenAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const DistributeTokenABI = JSON.stringify(obj.abi)
const HyphaTokenABI = JSON.stringify(obj.abi)

const DistributeTokenBytecode = obj.bytecode
const HyphaTokenBytecode = obj.bytecode

module.exports = {
DistributeTokenAddress,
DistributeTokenABI,
DistributeTokenBytecode
HyphaTokenAddress,
HyphaTokenABI,
HyphaTokenBytecode
}
2 changes: 1 addition & 1 deletion frontend/src/abi/ProjectLibrary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let obj = require('./build/contracts/ProjectLibrary.json')

const ProjectLibraryAddress = obj.networks['5777'].address
const ProjectLibraryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making network id an environment var?


const ProjectLibraryABI = JSON.stringify(obj.abi)

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/abi/ProjectRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// export const ProjectRegistryAddress = '0xef1ac57f437de18c8e5ec168fbe855fc55e238f1'
let obj = require('./build/contracts/ProjectRegistry.json')

const ProjectRegistryAddress = obj.networks['5777'].address
const ProjectRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const ProjectRegistryABI = JSON.stringify(obj.abi)

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/abi/ReputationRegistry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let obj = require('./build/contracts/ReputationRegistry.json')

const ReputationRegistryAddress = obj.networks['5777'].address
const ReputationRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const ReputationRegistryABI = JSON.stringify(obj.abi)

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/abi/TokenRegistry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let obj = require('./build/contracts/TokenRegistry.json')

const TokenRegistryAddress = obj.networks['5777'].address
const TokenRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const TokenRegistryABI = JSON.stringify(obj.abi)

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/utilities/blockchain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenRegistryABI, TokenRegistryAddress } from '../abi/TokenRegistry'
import { ReputationRegistryABI, ReputationRegistryAddress } from '../abi/ReputationRegistry'
import { ProjectRegistryABI, ProjectRegistryAddress } from '../abi/ProjectRegistry'
import { DistributeTokenABI, DistributeTokenAddress } from '../abi/DistributeToken'
import { HyphaTokenABI, HyphaTokenAddress } from '../abi/HyphaToken'
import { ProjectABI } from '../abi/Project'
import { TaskABI } from '../abi/Task'
import { ProjectLibraryABI, ProjectLibraryAddress } from '../abi/ProjectLibrary'
Expand Down Expand Up @@ -36,9 +36,9 @@ const PR = contract({abi: JSON.parse(ProjectRegistryABI)})
PR.setProvider(window.web3.currentProvider)
export const pr = PR.at(ProjectRegistryAddress)

const DT = contract({abi: JSON.parse(DistributeTokenABI)})
const DT = contract({abi: JSON.parse(HyphaTokenABI)})
DT.setProvider(window.web3.currentProvider)
export const dt = DT.at(DistributeTokenAddress)
export const dt = DT.at(HyphaTokenAddress)

const PL = contract({abi: JSON.parse(ProjectLibraryABI)})
PL.setProvider(window.web3.currentProvider)
Expand Down
13 changes: 0 additions & 13 deletions server/abi/DistributeToken.js

This file was deleted.

13 changes: 13 additions & 0 deletions server/abi/HyphaToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let obj = require('./build/contracts/HyphaToken.json')
//This address lookup is broken...the network id will be different based on where it is being deployed, check this before deployment
const HyphaTokenAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address
//console.log(Object.keys(obj.networks))
const HyphaTokenABI = obj.abi

const HyphaTokenBytecode = obj.bytecode

module.exports = {
HyphaTokenAddress,
HyphaTokenABI,
HyphaTokenBytecode
}
2 changes: 1 addition & 1 deletion server/abi/Project.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let obj = require('./build/contracts/Project.json')

const ProjectABI = JSON.stringify(obj.abi)
const ProjectABI = obj.abi

const ProjectBytecode = obj.bytecode

Expand Down
4 changes: 2 additions & 2 deletions server/abi/ProjectLibrary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let obj = require('./build/contracts/ProjectLibrary.json')

const ProjectLibraryAddress = obj.networks['5777'].address
const ProjectLibraryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const ProjectLibraryABI = JSON.stringify(obj.abi)
const ProjectLibraryABI = obj.abi

const ProjectLibraryBytecode = obj.deployedBytecode

Expand Down
4 changes: 2 additions & 2 deletions server/abi/ProjectRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// export const ProjectRegistryAddress = '0xef1ac57f437de18c8e5ec168fbe855fc55e238f1'
let obj = require('./build/contracts/ProjectRegistry.json')

const ProjectRegistryAddress = obj.networks['5777'].address
const ProjectRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const ProjectRegistryABI = JSON.stringify(obj.abi)
const ProjectRegistryABI = obj.abi

const ProjectRegistryBytecode = obj.bytecode

Expand Down
4 changes: 2 additions & 2 deletions server/abi/ReputationRegistry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let obj = require('./build/contracts/ReputationRegistry.json')

const ReputationRegistryAddress = obj.networks['5777'].address
const ReputationRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const ReputationRegistryABI = JSON.stringify(obj.abi)
const ReputationRegistryABI = obj.abi

const ReputationRegistryBytecode = obj.bytecode

Expand Down
2 changes: 1 addition & 1 deletion server/abi/Task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let obj = require('./build/contracts/Task.json')

const TaskABI = JSON.stringify(obj.abi)
const TaskABI = obj.abi

const TaskBytecode = obj.bytecode

Expand Down
4 changes: 2 additions & 2 deletions server/abi/TokenRegistry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let obj = require('./build/contracts/TokenRegistry.json')

const TokenRegistryAddress = obj.networks['5777'].address
const TokenRegistryAddress = Object.keys(obj.networks).sort()[Object.keys(obj.networks).length - 1].address

const TokenRegistryABI = JSON.stringify(obj.abi)
const TokenRegistryABI = obj.abi

const TokenRegistryBytecode = obj.bytecode

Expand Down
5 changes: 3 additions & 2 deletions server/config/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const oneSecondMS = 1000;

// const ganacheUrl = process.env.GANACHE_URL || 'ws://127.0.0.1:8545'
// const mongoUrl = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/hypha'
const ganacheUrl = process.env.GANACHE_URL || 'ws://ganache-cli:8545'
const mongoUrl = process.env.MONGODB_URI || 'mongodb://mongo:27017/distribute'
const mongoUrl = process.env.MONGODB_URI || 'mongodb://mongo:27017/hypha'
const isProd = process.env.NODE_ENV === 'prod' ? true : false;

const defaultMongoOptions = {
Expand All @@ -13,7 +15,6 @@ const defaultMongoOptions = {
reconnectInterval: 5 * oneSecondMS
};


module.exports = {
isProd,
mongoUrl,
Expand Down
95 changes: 35 additions & 60 deletions server/data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const User = require('../models/user')
const Validation = require('../models/validation')
const Vote = require('../models/vote')
const PrelimTaskList = require('../models/prelimTaskList')
const { VoteRecord } = require('../models/voteRecord')
const _ = require('lodash')
// The resolvers
const resolvers = {
Expand Down Expand Up @@ -52,19 +51,16 @@ const resolvers = {
tasks: (user) => Task.find({claimer: user.id}).then(tasks => tasks),
tokenChanges: (user) => Token.find({userId: user.id}).then(tokens => tokens),
validations: (user) => Validation.find({userId: user.id}).then(validations => validations),
votes: (user) => Vote.find({userId: user.id}).then(votes => votes),
voteRecords: (user) => User.findOne({_id: user.id}).then(userDoc => userDoc.voteRecords)
},
VoteRecord: {
voter: (vote) => User.findOne({account: vote.voter}).then(user => user),
task: (vote) => Task.findById(vote.task).then(voteRecord => voteRecord)
votes: (user) => Vote.find({voter: user.id}).then(votes => votes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact, you don't need the code after then then unless it's tranforming the result in some way. You can just write Vote.find({voter: user.id})

},

Validation: {
task: (validation) => Task.findById(validation.task).then(task => task)
task: (validation) => Task.findById(validation.task).then(task => task),
project: (validation) => Project.findById(validation.project).then(project => project)
},
Vote: {
task: (vote) => Task.findById(vote.taskId).then(vote => vote),
user: (vote) => User.findById(vote.userId).then(user => user)
task: (vote) => Task.findById(vote.task).then(task => task),
voter: (vote) => User.findById(vote.voter).then(user => user)
},
Query: {
network: () => Network.findOne({}).then(status => status),
Expand All @@ -87,18 +83,19 @@ const resolvers = {
verifiedPrelimTaskLists: (_, args) => PrelimTaskList.find({address: args.address.toLowerCase(), verified: true}).then(prelimTaskLists => prelimTaskLists),
userPrelimTaskLists: (_, args) => PrelimTaskList.findOne({submitter: args.account.toLowerCase()}).then(prelimTaskLists => prelimTaskLists),
taskValidations: (_, args) => Project.findOne({address: args.address.toLowerCase()}).then(project => Task.find({project: project.id}).then(task => Validation.find({task: task.id})).then(validations => validations)),
userVotes: (account) => [{}],
userVoteRecords: (_, args) => User.findOne({account: args.account}).then(user => user.voteRecords),
userVotes: (_, args) => User.findOne({account: args.account}).then(user => Vote.find({voter: user.id}).then(votes => votes)),
taskVotes: (address) => [{}],
findFinalTaskHash: (_, args) => PrelimTaskList.findOne({hash: args.topTaskHash, address: args.address.toLowerCase()}).then(prelimTaskList => prelimTaskList),
findTaskByIndex: (_, args) => Project.findOne({address: args.address.toLowerCase()}).then(project => Task.findOne({project: project.id, index: args.index})).then(task => task),
allTasksinProject: (_, args) => Project.findOne({address: args.address.toLowerCase()}).then(project => Task.find({project: project.id})).then(tasks => tasks),
getValidations: (_, args) => Project.findOne({address: args.address.toLowerCase()}).then(project => Task.findOne({project: project.id, index: args.index})).then(task => Validation.find({task: task.id})).then(validations => validations),
getUserValidationsinProject: (_, args) => Validation.find({projAddress: args.address.toLowerCase(), user: args.user.toLowerCase()}).then(validations => validations),
getPrevPollID: (obj, args) => User.findOne({account: args.account}).then((user) => {
let insertIndex = _.sortedIndexBy(user.voteRecords, {amount: args.amount}, (o) => o.amount)
let prevPollID = insertIndex < 1 ? 0 : user.voteRecords[insertIndex - 1].pollID
return prevPollID
getPrevPollID: (obj, args) => User.findOne({account: args.account}).then(user => {
Vote.find({voter: user.id, revealed: false, rescued: false}).sort({amount: 1}).then(votes => {
let insertIndex = _.sortedIndexBy(votes, {amount: args.amount}, (o) => o.amount)
let prevPollID = insertIndex < 1 ? 0 : votes[insertIndex - 1].pollID
return prevPollID
})
})
},
Mutation: {
Expand Down Expand Up @@ -174,50 +171,28 @@ const resolvers = {
}
})
},
addVote: (obj, args) => {
User.findOne({account: args.voter}).exec((err, user) => {
if (err) {
console.error(err)
} else {
Project.findOne({address: args.projectAddress}).exec((err, project) => {
if (err) { console.error(err) }
Task.findOne({project: project.id, index: args.taskIndex}).exec((err, task) => {
if (err) { console.error(err) }
let userVoteObj = new VoteRecord({
_id: new mongoose.Types.ObjectId(),
amount: args.amount,
pollID: args.pollID,
project: args.projectAddress,
revealed: false,
rescued: false,
salt: args.salt,
task: task.id,
type: args.type,
vote: args.vote,
voter: user.id
})
let index = _.sortedIndexBy(user.voteRecords, userVoteObj, (o) => o.amount)
user.voteRecords.splice(index, 0, userVoteObj)
user.markModified('voteRecords')
user.save(err => {
if (err) return console.log(err)
return userVoteObj
})
})
})

// userVoteObj.save((err, vote) => {
// if (err) return console.log(err)
// let index = _.sortedIndexBy(user.voteRecords, vote, (o) => o.amount)
// user.voteRecords.splice(index, 0, vote)
// user.markModified('voteRecords')
// user.save((err) => {
// if (err) return console.log(err)
// return vote
// })
// })
}
})
addVote: async (obj, args) => {
try {
const user = await User.findOne({account: args.voter})
const project = await Project.findOne({account: args.projectAddress})
const task = await Task.findOne({project: project.id, index: args.taskIndex})
const vote = await Vote.findOneAndUpdate({
amount: args.amount,
pollID: args.pollID,
project: project.id,
revealed: false,
rescued: false,
task: task.id,
type: args.type,
voter: user.id
}, { $set: {
salt: args.salt,
vote: args.vote
}}, {upsert: true})
if (!vote) console.error('vote not added successfully')
} catch (err) {
console.error('Error Adding Vote')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if you want to rethrow here, so it bubbles up to what gets returned by graphql? What does this mutation return?

}
}
// let prelimTaskListSubmitted = new PrelimTaskList({
// _id: new mongoose.Types.ObjectId(),
Expand Down
18 changes: 3 additions & 15 deletions server/data/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ const typeDefs = gql`
tasks: [Task]
tokenBalance: Int
tokenChanges: [Token]
voteRecords: [VoteRecord]
validations: [Validation]
votes: [Vote]
wallets: [String]
weiBalance: Int
}

Expand All @@ -143,25 +143,14 @@ const typeDefs = gql`
task: Task
user: String
state: Boolean
address: String
rewarded: Boolean
project: Project
}

type Vote {
id: ID
amount: Int
revealed: Boolean
rescued: Boolean
pollID: Int
hash: String
task: Task
type: String
user: User
}

type VoteRecord {
id: ID
amount: Int
pollID: Int
revealed: Boolean
rescued: Boolean
Expand Down Expand Up @@ -193,7 +182,6 @@ const typeDefs = gql`
userValidations(account: String): [Validation]
taskValidations(address: String): [Validation]
userVotes(account: String): [Vote]
userVoteRecords(account: String): [VoteRecord]
taskVotes(address: String): [Vote]
verifiedPrelimTaskLists(address: String): [PrelimTaskList]
userPrelimTaskLists(address: String): [PrelimTaskList]
Expand Down Expand Up @@ -224,7 +212,7 @@ const typeDefs = gql`
addUser(input: CredentialInput, account: String): User
addTaskList(input: String, address: String): Project
addPrelimTaskList(address: String, taskHash: String, submitter: String, weighting: String): Project
addVote(type: String, projectAddress: String, taskIndex: Int, amount: Int, vote: String, salt: String, pollID: Int, voter: String): VoteRecord
addVote(type: String, projectAddress: String, taskIndex: Int, amount: Int, vote: String, salt: String, pollID: Int, voter: String): Vote
}
`
// Put together a schema
Expand Down
Loading