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

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: get data directly from implementation
  • Loading branch information
satello committed Jul 25, 2018
1 parent 12040dd commit 5ff4aee
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 93 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -68,8 +68,9 @@
"babel-runtime": "^6.26.0",
"eth-sig-util": "^1.4.2",
"ethereumjs-util": "^5.2.0",
"ethjs": "^0.4.0",
"kleros": "^0.0.6",
"kleros-interaction": "^0.0.17",
"kleros-interaction": "^0.0.18",
"lodash": "^4.17.4",
"minimetoken": "^0.2.0",
"truffle-contract": "^2.0.5",
Expand Down
42 changes: 6 additions & 36 deletions src/contracts/abstractions/Arbitrable.js
@@ -1,4 +1,5 @@
import AbstractContract from '../AbstractContract'
import Eth from 'ethjs'

import getContractAddress from '../../utils/getContractAddress'

Expand All @@ -19,34 +20,29 @@ class ArbitrableContract extends AbstractContract {
* @param {string} partyB - Ethereum address of the other party in the contract.
* @param {bytes} arbitratorExtraData - Extra data for the arbitrator.
* @param {string} email - Email address of the contract creator (default empty string).
* @param {string} title - Title of the contract (default empty string).
* @param {string} description - Description of what the contract is about (default empty string).
* @param {...any} args - Extra arguments for the contract.
* @returns {object | Error} - The contract object or an error.
*/
deploy = async (
account,
value,
hashContract,
arbitratorAddress,
timeout,
partyB,
arbitratorExtraData = '',
email = '',
title = '',
description = '',
metaEvidence = {}
metaEvidence = {},
...args
) => {
const web3Provider = this._contractImplementation.getWeb3Provider()
const eth = new Eth(web3Provider)
const txCount = await eth.getTransactionCount(account)
// determine the contract address WARNING if the nonce changes this will produce a different address
const contractAddress = getContractAddress(account, web3Provider)
const contractAddress = getContractAddress(account, txCount)
const metaEvidenceUri = this._StoreProvider.getMetaEvidenceUri(account, contractAddress)

const contractInstance = await this._contractImplementation.constructor.deploy(
account,
value,
hashContract,
arbitratorAddress,
timeout,
partyB,
Expand All @@ -57,20 +53,17 @@ class ArbitrableContract extends AbstractContract {
)

if (contractInstance.address !== contractAddress)
raise new Error('Contract address does not match meta-evidence uri')
throw new Error('Contract address does not match meta-evidence uri')

const newContract = await this._StoreProvider.updateContract(
account,
contractInstance.address,
{
hashContract,
partyA: account,
partyB,
arbitrator: arbitratorAddress,
timeout,
email,
title,
description,
metaEvidence
}
)
Expand Down Expand Up @@ -100,16 +93,11 @@ class ArbitrableContract extends AbstractContract {
)
// construct the unique URI
const evidenceUri = this._StoreProvider.getEvidenceUri(account, contractAddress, evidenceIndex)

const txHash = await this._contractImplementation.submitEvidence(
account,
name,
description,
evidenceUri
)



return txHash
}

Expand Down Expand Up @@ -138,24 +126,6 @@ class ArbitrableContract extends AbstractContract {
this._contractImplementation.contractAddress
)
}

/**
* Get data from the store and contract for Arbitrable Contract.
* @param {string} account - ETH address of user.
* @returns {object} - Contract data.
*/
getData = async account => {
const contractData = await this._contractImplementation.getData()

let storeData = {}
if (account)
storeData = await this._StoreProvider.getContractByAddress(
account,
this._contractImplementation.contractAddress
)

return Object.assign({}, storeData, contractData)
}
}

export default ArbitrableContract
66 changes: 31 additions & 35 deletions src/contracts/implementations/arbitrable/ArbitrableTransaction.js
Expand Up @@ -37,11 +37,11 @@ class ArbitrableTransaction extends ContractImplementation {
static deploy = async (
account,
value = ethConstants.TRANSACTION.VALUE,
hashContract,
arbitratorAddress,
timeout,
partyB,
arbitratorExtraData = '',
metaEvidenceUri,
web3Provider
) => {
const contractDeployed = await deployContractAsync(
Expand All @@ -50,45 +50,30 @@ class ArbitrableTransaction extends ContractImplementation {
arbitrableTransactionArtifact,
web3Provider,
arbitratorAddress,
hashContract,
timeout,
partyB,
arbitratorExtraData
arbitratorExtraData,
metaEvidenceUri
)

return contractDeployed
}

/**
* Get the meta evidence for a contract dispute. Look up meta-evidence events
* Get the meta evidence for the contract. Arbitrable Transaction can only have
* one meta-evidence that is submitted on contract creation. Look up meta-evidence event
* and make an http request to the resource.
* @param {string} arbitratorAddress - The arbitrators address.
* @param {number} disputeId - The index of the dispute.
*/
getMetaEvidenceForDispute = async (arbitratorAddress, disputeId) => {
const metaEvidenceLinkLog = await EventListener.getEventLogs(
this,
'LinkMetaEvidence',
0,
'latest',
{ _disputeID: disputeId, _arbitrator: arbitratorAddress }
)

// No meta-evidence for dispute
if (!metaEvidenceLinkLog)
return {}

// Always use the first log
const metaEvidenceId = metaEvidenceLinkLog[0].args._metaEvidenceID
getMetaEvidence = async () => {
const metaEvidenceLog = await EventListener.getEventLogs(
this,
'MetaEvidence',
0,
'latest',
{ _metaEvidenceID: metaEvidenceId }
{ _metaEvidenceID: 0 }
)

if (!metaEvidenceLog)
if (!metaEvidenceLog[0])
return {} // NOTE better to throw errors for missing meta-evidence?

const metaEvidenceUri = metaEvidenceLog[0].args._evidence
Expand All @@ -104,10 +89,17 @@ class ArbitrableTransaction extends ContractImplementation {

/**
* Get the evidence submitted in a dispute.
* @param {string} arbitratorAddress - The arbitrators address.
* @param {number} disputeId - The index of the dispute.
*/
getEvidenceForDispute = async (arbitratorAddress, disputeId) => {
getEvidence = async () => {
await this.loadContract()
const arbitratorAddress = await this.contractInstance.arbitrator()
await this.loadContract()
const disputeId = (await this.contractInstance.disputeID()).toNumber()

// No evidence yet as there is no dispute
if (_.isNull(disputeId))
return []

const evidenceLogs = await EventListener.getEventLogs(
this,
'Evidence',
Expand All @@ -117,11 +109,11 @@ class ArbitrableTransaction extends ContractImplementation {
)

// TODO verify hash and data are valid if hash exists
return evidenceLogs.map(async evidenceLog => {
return Promise.all(evidenceLogs.map(async evidenceLog => {
const evidenceURI = evidenceLog.args._evidence
const evidence = await httpRequest(
'GET',
metaEvidenceUri
evidenceURI
)
const submittedAt = (
await this._Web3Wrapper.getBlock(evidenceLog.blockNumber)
Expand All @@ -130,7 +122,7 @@ class ArbitrableTransaction extends ContractImplementation {
...evidence.body,
...{ submittedBy: evidenceLog.args._party, submittedAt }
}
})
}))
}

/**
Expand Down Expand Up @@ -212,14 +204,12 @@ class ArbitrableTransaction extends ContractImplementation {
*/
submitEvidence = async (
account = this._Web3Wrapper.getAccount(0),
name,
description = '',
url
) => {
await this.loadContract()

const txHashObj = await this.contractInstance.submitEvidence(
JSON.stringify(name, description, url),
url,
{
from: account,
gas: ethConstants.TRANSACTION.GAS,
Expand Down Expand Up @@ -386,7 +376,9 @@ class ArbitrableTransaction extends ContractImplementation {
partyAFee,
partyBFee,
lastInteraction,
amount
amount,
evidence,
metaEvidence
] = await Promise.all([
this.contractInstance.arbitrator(),
this.contractInstance.arbitratorExtraData(),
Expand All @@ -400,7 +392,9 @@ class ArbitrableTransaction extends ContractImplementation {
this.contractInstance.partyAFee(),
this.contractInstance.partyBFee(),
this.contractInstance.lastInteraction(),
this.contractInstance.amount()
this.contractInstance.amount(),
this.getEvidence(),
this.getMetaEvidence()
])

return {
Expand All @@ -416,7 +410,9 @@ class ArbitrableTransaction extends ContractImplementation {
partyAFee: this._Web3Wrapper.fromWei(partyAFee, 'ether'),
partyBFee: this._Web3Wrapper.fromWei(partyBFee, 'ether'),
lastInteraction: lastInteraction.toNumber(),
amount: amount.toNumber()
amount: amount.toNumber(),
evidence,
metaEvidence
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/resources/Disputes.js
Expand Up @@ -314,14 +314,8 @@ class Disputes {
)
const [arbitrableContractData, evidence, metaEvidence] = await Promise.all([
this._ArbitrableInstance.getData(account),
this._ArbitrableInstance.getEvidenceForDispute(
arbitratorAddress,
dispute.disputeId
),
this._ArbitrableInstance.getMetaEvidenceForDispute(
arbitratorAddress,
dispute.disputeId
)
this._ArbitrableInstance.getEvidence(),
this._ArbitrableInstance.getMetaEvidence()
])
const contractStoreData = await this._StoreProviderInstance.getContractByAddress(
arbitrableContractData.partyA,
Expand Down
12 changes: 6 additions & 6 deletions src/utils/StoreProviderWrapper.js
Expand Up @@ -39,12 +39,12 @@ class StoreProviderWrapper {
this._storeQueue.fetch(() => httpRequest('GET', uri))


getMetaEvidenceUri = userAddress, contractAddress => (
getMetaEvidenceUri = (userAddress, contractAddress) => (
`${this._storeUri}/${userAddress}/contracts/${contractAddress}/meta-evidence`
)

getEvidenceUri = (address, contractAddress, evidenceIndex) => (
`${this._storeUri}/${userAddress}/contracts/${contractAddress}/evidence/evidenceIndex`
getEvidenceUri = (userAddress, contractAddress, evidenceIndex) => (
`${this._storeUri}/${userAddress}/contracts/${contractAddress}/evidence/${evidenceIndex}`
)

// **************************** //
Expand Down Expand Up @@ -245,7 +245,7 @@ class StoreProviderWrapper {
* @param {string} url - A link to the evidence.
* @returns {number} - The index of the evidence
*/
addEvidenceContract = (
addEvidenceContract = async (
contractAddress,
userAddress,
name,
Expand All @@ -271,8 +271,8 @@ class StoreProviderWrapper {
`${this._storeUri}/${userAddress}/contracts/${contractAddress}/evidence`
)

if (response.status !== 200)
throw new Error(errorConstants.REQUEST_FAILED('Unable to submit evidence')
if (response.status !== 201)
throw new Error(errorConstants.REQUEST_FAILED('Unable to submit evidence'))

return response.body.evidenceIndex
}
Expand Down
6 changes: 5 additions & 1 deletion src/utils/Web3Wrapper.js
Expand Up @@ -18,7 +18,11 @@ class Web3Wrapper {

getCoinbase = () => this._web3.eth.coinbase

getNonce = address => this._web3.eth.getTransactionCount(address)
getNonce = async address => {
console.log(this._web3.eth.blockNumber)
const nonce = await this._web3.eth.getTransactionCount(address)
return nonce
}

toWei = (amount, unit) => {
const newAmount = this._web3.toWei(amount, unit)
Expand Down
7 changes: 4 additions & 3 deletions src/utils/getContractAddress.js
@@ -1,6 +1,7 @@
import ethUtil from 'ethereumjs-util'

const getContractAddress (account, Web3Wrapper) => {
const currentNonce = Web3Wrapper.getNonce(account)
return ethUtil.bufferToHex(ethUtil.generateAddress(account, currentNonce))
const getContractAddress = (account, nonce) => {
return ethUtil.bufferToHex(ethUtil.generateAddress(account, nonce))
}

export default getContractAddress

0 comments on commit 5ff4aee

Please sign in to comment.