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

Feature/#1371 #1386

Merged
merged 6 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
83 changes: 47 additions & 36 deletions app/src/components/market/sections/market_verify/market_verify.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import { ItemTypes, gtcrEncode } from '@kleros/gtcr-encoder'
import { abi as _GeneralizedTCR } from '@kleros/tcr/build/contracts/GeneralizedTCR.json'
import { ethers } from 'ethers'
import React, { useCallback, useMemo, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import { RouteComponentProps, useHistory, withRouter } from 'react-router-dom'
import styled from 'styled-components'

import { ConnectedWeb3Context } from '../../../../hooks'
import { ConnectedWeb3Context, useCpk } from '../../../../hooks'
import { useKlerosCuration } from '../../../../hooks/useKlerosCuration'
import { MarketMakerData, Status } from '../../../../util/types'
import { Button, ButtonContainer } from '../../../button'
import { ButtonType } from '../../../button/button_styling_types'
import { InlineLoading } from '../../../loading'
import { FullLoading, InlineLoading } from '../../../loading'
import { CurationRow, GenericError } from '../../common/common_styled'

import { DxDaoCuration } from './option/dxdao_curation'
Expand Down Expand Up @@ -40,8 +38,11 @@ interface Props extends RouteComponentProps<any> {
const MarketVerifyWrapper: React.FC<Props> = (props: Props) => {
const { context, marketMakerData } = props || {}
const [selection, setSelection] = useState<number | undefined>()
const [isModalOpen, setIsModalOpen] = useState<boolean>(false)
const { data, error, status } = useKlerosCuration(marketMakerData, context)

const history = useHistory()
const cpk = useCpk()

const selectSource = useCallback(
(value: number) => {
Expand All @@ -53,40 +54,49 @@ const MarketVerifyWrapper: React.FC<Props> = (props: Props) => {
)

const loading = status === Status.Loading && !data
const { ovmAddress } = data || {}
const { marketVerificationData, ovmAddress } = data || {}
const verificationState = marketVerificationData ? marketVerificationData.verificationState : false
const { message: errorMessage } = error || {}
const { address, curatedByDxDao, question } = marketMakerData || {}
const { title } = question || {}

const ovmInstance = useMemo(() => {
if (!context || !context.account || !ovmAddress) return
const signer = context.library.getSigner()
return new ethers.Contract(ovmAddress, _GeneralizedTCR, signer)
}, [context, ovmAddress])

const onSubmitMarket = useCallback(() => {
if (!ovmInstance || !data) return

const columns = [
{
label: 'Question',
type: ItemTypes.TEXT,
},
{
label: 'Market URL',
type: ItemTypes.LINK,
},
]
const values = {
Question: title,
'Market URL': `https://omen.eth.link/#/${address}`,
useEffect(() => {
if (isModalOpen && verificationState) setIsModalOpen(false)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [marketVerificationData])
const onSubmitMarket = useCallback(async () => {
try {
setIsModalOpen(true)

const columns = [
{
label: 'Question',
type: ItemTypes.TEXT,
},
{
label: 'Market URL',
type: ItemTypes.LINK,
},
]
const values = {
Question: title,
'Market URL': `https://omen.eth.link/#/${address}`,
}

const encodedParams = gtcrEncode({ columns, values })
if (!cpk || !marketMakerData || !data || !ovmAddress) {
setIsModalOpen(false)
return
}

await cpk.requestVerification({
params: encodedParams,
submissionDeposit: data.submissionDeposit,
ovmAddress,
})
} catch {
setIsModalOpen(false)
}

const encodedParams = gtcrEncode({ columns, values })
ovmInstance.addItem(encodedParams, {
value: ethers.utils.bigNumberify(data.submissionDeposit),
})
}, [address, data, ovmInstance, title])
}, [address, data, ovmAddress, title, marketMakerData, cpk])

if (!loading && errorMessage) return <GenericError>{errorMessage || 'Failed to fetch curation data'}</GenericError>

Expand All @@ -108,12 +118,13 @@ const MarketVerifyWrapper: React.FC<Props> = (props: Props) => {
</Button>
<Button
buttonType={ButtonType.primaryAlternative}
disabled={loading || typeof selection !== 'number' || !ovmInstance}
disabled={loading || typeof selection !== 'number' || !ovmAddress || verificationState != 1}
kadenzipfel marked this conversation as resolved.
Show resolved Hide resolved
onClick={onSubmitMarket}
>
Request Verification
</Button>
</BottomButtonWrapper>
{isModalOpen && <FullLoading message={`Requesting ${selection === 0 ? `Kleros` : `DxDao`} verification`} />}
</MarketVerification>
)
}
Expand Down
25 changes: 24 additions & 1 deletion app/src/services/cpk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ERC20Service } from './erc20'
import { MarketMakerService } from './market_maker'
import { MarketMakerFactoryService } from './market_maker_factory'
import { OracleService } from './oracle'
import { OvmService } from './ovm'
import { RealitioService } from './realitio'

const logger = getLogger('Services::CPKService')
Expand Down Expand Up @@ -67,6 +68,12 @@ interface CPKRedeemParams {
conditionalTokens: ConditionalTokenService
}

interface CPKRequestVerificationParams {
params: string
ovmAddress: string
submissionDeposit: string
}

class CPKService {
cpk: any
provider: Web3Provider
Expand Down Expand Up @@ -411,6 +418,21 @@ class CPKService {
throw err
}
}
requestVerification = async ({ ovmAddress, params, submissionDeposit }: CPKRequestVerificationParams) => {
try {
const signer = this.provider.getSigner()
const ovm = new OvmService()
const contractInstance = await ovm.createOvmContractInstance(signer, ovmAddress)

const { hash } = await ovm.generateTransaction(params, contractInstance, submissionDeposit)

await this.provider.waitForTransaction(hash)
return true
} catch (err) {
logger.error('Error while requesting market verification via Kleros!', err.message)
throw err
}
}

redeemPositions = async ({
collateralToken,
Expand All @@ -421,7 +443,7 @@ class CPKService {
numOutcomes,
oracle,
question,
}: CPKRedeemParams): Promise<TransactionReceipt> => {
}: CPKRedeemParams) => {
Mi-Lan marked this conversation as resolved.
Show resolved Hide resolved
try {
const signer = this.provider.getSigner()
const account = await signer.getAddress()
Expand Down Expand Up @@ -455,6 +477,7 @@ class CPKService {
} catch (err) {
logger.error(`Error trying to resolve condition or redeem for question id '${question.id}'`, err.message)
throw err
return false
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { OracleService } from './oracle'
export { CPKService } from './cpk'
export { KlerosService } from './kleros'
export { DxTCRService } from './dx_tcr'
export { OvmService } from './ovm'
28 changes: 28 additions & 0 deletions app/src/services/ovm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { abi } from '@kleros/tcr/build/contracts/GeneralizedTCR.json'
import { ethers } from 'ethers'

class OvmService {
abi: any

constructor() {
this.abi = abi
}

createOvmContractInstance = async (signer: ethers.providers.JsonRpcSigner, ovmAddress: string) => {
return new ethers.Contract(ovmAddress, this.abi, signer)
}
generateTransaction = async (params: string, contract: any, deposit: string) => {
try {
if (!params || !contract || !deposit) return false
const transaction = await contract.addItem(params, {
value: ethers.utils.bigNumberify(deposit),
})
return transaction ? transaction : 'invalid'
} catch (e) {
throw new Error('Failed at generating transaction!')
return false
}
}
}

export { OvmService }