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

Commit

Permalink
feat: add MiniMe PNK contract
Browse files Browse the repository at this point in the history
  • Loading branch information
satello committed Jun 12, 2018
1 parent cf29afb commit b85cca2
Show file tree
Hide file tree
Showing 16 changed files with 1,019 additions and 145 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@
"babel-runtime": "^6.26.0",
"eth-sig-util": "^1.4.2",
"kleros": "^0.0.6",
"kleros-interaction": "^0.0.8",
"kleros-interaction": "^0.0.17",
"lodash": "^4.17.4",
"minimetoken": "^0.2.0",
"truffle-contract": "^2.0.5",
"web3": "^0.20.1",
"web3-eth-personal": "^1.0.0-beta.34"
Expand Down
2 changes: 1 addition & 1 deletion src/constants/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const LOCALHOST_STORE_PROVIDER = 'https://kleros.in'
export const NULL_ADDRESS = '0x'

export const TRANSACTION = {
GAS: 4400000,
GAS: 6400000,
VALUE: 0
}
15 changes: 10 additions & 5 deletions src/contracts/abstractions/Arbitrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ class Arbitrator extends AbstractContract {
return this.getDisputesForUserFromStore(account)
}

/**
* Get disputes from the store.
* @param {string} account - The users account.
* @returns {object[]} The dispute objects.
*/
getDisputesForUserFromStore = async account => {
const aribtratorAddress = this._contractImplementation.getContractAddress()
return Promise.all(
(await this._StoreProvider.getDisputes(account)).filter(dispute =>
dispute.arbitratorAddress === aribtratorAddress
).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
(await this._StoreProvider.getDisputes(account))
.filter(dispute => dispute.arbitratorAddress === aribtratorAddress)
.map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
)
}

Expand Down
88 changes: 88 additions & 0 deletions src/contracts/implementations/PNK/MiniMePinakion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import PinakionPOCArtifact from 'kleros-interaction/build/contracts/MiniMeTokenERC20'
import _ from 'lodash'

import * as ethConstants from '../../../constants/eth'
import * as errorConstants from '../../../constants/error'
import ContractImplementation from '../../ContractImplementation'
import deployContractAsync from '../../../utils/deployContractAsync'
import isRequired from '../../../utils/isRequired'

/**
* Provides interaction with a PinakionPOC contract deployed on the blockchain.
*/
class MiniMePinakion extends ContractImplementation {
/**
* Constructor PinakionPOC.
* @param {object} web3Provider - web3 instance.
* @param {string} contractAddress - of the contract (optionnal).
*/
constructor(web3Provider, contractAddress) {
super(web3Provider, PinakionPOCArtifact, contractAddress)
}

/**
* Deploy a new instance of PinakionPOC.
* @param {string} account - account of user
* @param {object} web3Provider - web3 provider object
* @param {string} tokenFactoryAddress - The address of the MiniMeTokenFactory contract that will create the Clone token contracts
* @param {string} parentTokenAddress - Address of the parent token, set to 0x0 if it is a new token
* @param {number} parentSnapshotBlock - Block of the parent token that will determine the initial distribution of the clone token, set to 0 if it is a new token
* @param {string} tokenName - Name of the token.
* @param {number} decimalUnits - Number of decimal units token is divisible by.
* @param {string} tokenSymbol - Abreviated symbol to represent the token.
* @param {bool} transfersEnabled - If users can transfer tokens.
* @returns {object} - 'truffle-contract' Object | err The contract object or error deploy.
*/
static deploy = async (
account = isRequired('account'),
web3Provider = isRequired('web3Provider'),
tokenFactoryAddress = isRequired('tokenFactoryAddress'),
parentTokenAddress = '0x0',
parentSnapshotBlock = 0,
tokenName = 'Pinakion',
decimalUnits = 18,
tokenSymbol = 'PNK',
transfersEnabled = true
) => {
const contractDeployed = await deployContractAsync(
account,
0, // value
PinakionPOCArtifact,
web3Provider,
tokenFactoryAddress, // args
parentTokenAddress,
parentSnapshotBlock,
tokenName,
decimalUnits,
tokenSymbol,
transfersEnabled
)

return contractDeployed
}

/**
* Transfer ownership of the PNK contract to the kleros POC contract.
* @param {string} klerosAddress - Address of Kleros POC contract.
* @param {string} account - Address of user.
* @returns {object} - The result transaction object.
*/
changeController = async (
klerosAddress,
account = this._Web3Wrapper.getAccount(0)
) => {
await this.loadContract()

try {
return this.contractInstance.changeController(klerosAddress, {
from: account,
gas: ethConstants.TRANSACTION.GAS
})
} catch (err) {
console.error(err)
throw new Error(errorConstants.UNABLE_TO_TRANSFER_OWNERSHIP)
}
}
}

export default MiniMePinakion
50 changes: 50 additions & 0 deletions src/contracts/implementations/PNK/TokenFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
MiniMeTokenFactoryAbi,
MiniMeTokenFactoryByteCode
} from 'minimetoken/build/MiniMeToken.sol'
import _ from 'lodash'

import ContractImplementation from '../../ContractImplementation'
import deployContractAsync from '../../../utils/deployContractAsync'

/**
* Provides interaction with a PinakionPOC contract deployed on the blockchain.
*/
class TokenFactory extends ContractImplementation {
/**
* Constructor PinakionPOC.
* @param {object} web3Provider - web3 instance.
* @param {string} contractAddress - of the contract (optionnal).
*/
constructor(web3Provider, contractAddress) {
const artifact = {
abi: MiniMeTokenFactoryAbi,
bytecode: MiniMeTokenFactoryByteCode
}
super(web3Provider, artifact, contractAddress)
}

/**
* Deploy a new instance of PinakionPOC.
* @param {string} account - account of user
* @param {object} web3Provider - web3 provider object
* @returns {object} - 'truffle-contract' Object | err The contract object or error deploy.
*/
static deploy = async (account, web3Provider) => {
const artifact = {
abi: MiniMeTokenFactoryAbi,
bytecode: MiniMeTokenFactoryByteCode
}

const contractDeployed = await deployContractAsync(
account, // account
0, // value
artifact, // artifact
web3Provider // web3
)

return contractDeployed
}
}

export default TokenFactory
4 changes: 3 additions & 1 deletion src/contracts/implementations/PNK/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import PinakionPOC from './PinakionPOC'
import MiniMePinakion from './MiniMePinakion'
import TokenFactory from './TokenFactory'

export { PinakionPOC }
export { PinakionPOC, MiniMePinakion, TokenFactory }
21 changes: 9 additions & 12 deletions src/contracts/implementations/arbitrator/KlerosPOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,11 @@ class KlerosPOC extends ContractImplementation {
}

/**
* Starting from the blockNumber of the dispute creation, find when and if it was ruled on
* @param {number} blockNumber - The block number that the dispute was created.
* @param {number} appeal - the number of appeals we need to fetch events for.
* Find when a ruling was made in a session
* @param {number} session - The session number.
* @returns {number[]} an array of timestamps
*/
getAppealRuledAtTimestamp = async (session) => {
getAppealRuledAtTimestamp = async session => {
const eventLog = await this._getNewPeriodEventLogForSession(
session,
arbitratorConstants.PERIOD.APPEAL
Expand All @@ -595,9 +594,8 @@ class KlerosPOC extends ContractImplementation {
}

/**
* Starting from the blockNumber of the dispute creation, find when and if it was ruled on
* @param {number} blockNumber - The block number that the dispute was created.
* @param {number} appeal - the number of appeals we need to fetch events for.
* Find the deadline for disputes in a session.
* @param {number} session - The session number.
* @returns {number[]} an array of timestamps
*/
getDisputeDeadlineTimestamp = async session => {
Expand All @@ -621,9 +619,8 @@ class KlerosPOC extends ContractImplementation {
}

/**
* Get the event log for the dispute creation.
* @param {number} blockNumber - The block number that the dispute was created.
* @param {number} numberOfAppeals - the number of appeals we need to fetch events for.
* Get the event log for an appeal creation
* @param {number} session - The session number.
* @returns {number[]} an array of timestamps
*/
getAppealCreationTimestamp = async session => {
Expand Down Expand Up @@ -709,9 +706,9 @@ class KlerosPOC extends ContractImplementation {
'NewPeriod',
0,
'latest',
{ _session: session }
{ _session: [session] }
)
for (let i=0; i<logs.length; i++) {
for (let i = 0; i < logs.length; i++) {
const eventLog = logs[i]
if (eventLog.args._period.toNumber() === periodNumber) return eventLog
}
Expand Down
11 changes: 5 additions & 6 deletions src/kleros.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ class Kleros {
* use when initializing KlerosPOC
* @param {string} arbitrableContractAddress - Address of the arbitrator contract we should
* use when initializing KlerosPOC
* @param {string} authToken - Signed token cooresponding to the user profile address.
*/
constructor(
ethereumProvider = isRequired('ethereumProvider'),
storeUri = isRequired('storeUri'),
arbitratorAddress,
arbitrableContractAddress,
authToken
arbitrableContractAddress
) {
/**
* We need a set of implementations that we expose to the outside api and a set we use
Expand Down Expand Up @@ -73,7 +71,7 @@ class Kleros {
// **************************** //
// KLEROS WRAPPERS
this.web3Wrapper = new Web3Wrapper(ethereumProvider)
this.storeWrapper = new StoreProviderWrapper(storeUri, authToken)
this.storeWrapper = new StoreProviderWrapper(storeUri)
// ARBITRATOR
this.arbitrator = new contracts.abstractions.Arbitrator(
_klerosPOC,
Expand Down Expand Up @@ -124,6 +122,7 @@ class Kleros {
* store for metadata.
* @param {string} account Address of the user
* @param {function} callback The function to be called once a notification
* @returns {Promise} the watcher promise so that user can wait for event watcher to start before taking other actions.
*/
watchForEvents = async (
account,
Expand All @@ -146,15 +145,15 @@ class Kleros {
// fetch last block for user
const fromBlock = await this.storeWrapper.getLastBlock(account)
// start event listener
this.eventListener.watchForEvents(fromBlock)
return this.eventListener.watchForEvents(fromBlock)
}

/**
* Stop watching for events on the Arbitrator initialized in the Kleros Instance.
*/
stopWatchingForEvents = () => {
if (this.eventListener)
this.eventListener.stopWatchingForEvents(this.arbitrator)
this.eventListener.removeContractInstance(this.arbitrator)
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/resources/Disputes.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ class Disputes {
*/
getDisputeDeadline = async (disputeId, appeal = 0) => {
const cachedDispute = this.disputeCache[disputeId] || {}
if (
cachedDispute.appealDeadlines &&
cachedDispute.appealDeadlines[appeal]
)
if (cachedDispute.appealDeadlines && cachedDispute.appealDeadlines[appeal])
return cachedDispute.appealDeadlines[appeal]

const dispute = await this._ArbitratorInstance.getDispute(disputeId)
Expand All @@ -219,11 +216,10 @@ class Disputes {
/**
* Get the timestamp on when the dispute's ruling was finalized.
* @param {number} disputeId - The index of the dispute.
* @param {string} account - The users address.
* @param {number} appeal - The appeal number. 0 if there have been no appeals.
* @returns {number} timestamp of the appeal
*/
getAppealRuledAt = async (disputeId, account, appeal = 0) => {
getAppealRuledAt = async (disputeId, appeal = 0) => {
const cachedDispute = this.disputeCache[disputeId]
if (
cachedDispute &&
Expand Down Expand Up @@ -386,6 +382,7 @@ class Disputes {

const appealCreatedAt = await this.getAppealCreatedAt(
dispute.disputeId,
account,
appeal
)
const appealDeadline = await this.getDisputeDeadline(
Expand Down
8 changes: 7 additions & 1 deletion src/utils/StoreProviderWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ class StoreProviderWrapper {
* @returns {number} The last block number.
*/
getLastBlock = async userAddress => {
const userProfile = await this.newUserProfile(userAddress)
let userProfile
try {
userProfile = await this.newUserProfile(userAddress)
// eslint-disable-next-line no-unused-vars
} catch (err) {
userProfile = {}
}

return userProfile.lastBlock || 0
}
Expand Down

0 comments on commit b85cca2

Please sign in to comment.