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

Commit

Permalink
Meta Tx (#2366)
Browse files Browse the repository at this point in the history
* Meta-tx wip

* Localhost proxy flag

* Support for both proxy and wallet

* Proxy convenience functions

* Fix tests for latest master

* Relayer progress

* Docs

* Puppeteer tests for relayer

* Fix contract test
  • Loading branch information
nick authored Jun 3, 2019
1 parent 1e4d461 commit 42aa0be
Show file tree
Hide file tree
Showing 51 changed files with 1,053 additions and 405 deletions.
3 changes: 1 addition & 2 deletions dapps/marketplace/src/components/WithPrices.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import withCurrencyBalances from 'hoc/withCurrencyBalances'
import withWallet from 'hoc/withWallet'
import get from 'lodash/get'
import floor from 'lodash/floor'

Expand Down Expand Up @@ -91,4 +90,4 @@ const WithPrices = ({
return children({ prices: results, tokenStatus })
}

export default withWallet(withCurrencyBalances(WithPrices))
export default withCurrencyBalances(WithPrices)
88 changes: 43 additions & 45 deletions dapps/marketplace/src/hoc/withCanTransact.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,57 @@ import CanBuyQuery from 'queries/CanBuy'

import withConfig from './withConfig'

function cannotTransact({ data, error, loading, canTransactConfig }) {
if (error) {
return { cannotTransact: 'load-error' }
}
if (loading) {
return { cannotTransact: 'loading' }
}
const walletType = get(data, 'web3.walletType')
const metaMaskId = get(data, 'web3.metaMaskAccount.id')
if (!walletType) {
return { cannotTransact: 'no-wallet' }
}
if (walletType === 'Mobile' && !metaMaskId) {
return {}
}
if (walletType === 'Web3 Wallet') {
return {}
}
const balance = get(data, 'web3.metaMaskAccount.balance.eth')
if (balance === '0' && !canTransactConfig.relayer) {
return { cannotTransact: 'no-balance' }
}
const desiredNetwork = get(data, 'web3.networkId'),
selectedNetwork = get(data, 'web3.metaMaskNetworkId')

if (desiredNetwork !== selectedNetwork) {
return {
cannotTransact: 'wrong-network',
cannotTransactData: get(data, 'web3.networkName')
}
}

return {}
}

function withCanTransact(WrappedComponent) {
const WithCanTransact = ({ config, ...props }) => {
const WithCanTransact = ({ canTransactConfig, ...props }) => {
return (
<Query query={CanBuyQuery}>
{({ data, error, loading }) => {
if (error) {
return <WrappedComponent {...props} cannotTransact="load-error" />
}
if (loading) {
return <WrappedComponent {...props} cannotTransact="loading" />
}

const walletType = get(data, 'web3.walletType')
const metaMaskId = get(data, 'web3.metaMaskAccount.id')
if (!walletType) {
return <WrappedComponent {...props} cannotTransact="no-wallet" />
}
// Use mobile wallet if it's available and MetaMask isn't enabled.
if (walletType == 'Mobile' && !metaMaskId) {
return <WrappedComponent {...props} />
}
if (walletType === 'Web3 Wallet') {
return <WrappedComponent {...props} />
}

if (!metaMaskId) {
return <WrappedComponent {...props} cannotTransact="no-wallet" />
}

const balance = get(data, 'web3.metaMaskAccount.balance.eth')
if (balance === '0' && !config.relayer) {
return <WrappedComponent {...props} cannotTransact="no-balance" />
}

const desiredNetwork = get(data, 'web3.networkId'),
selectedNetwork = get(data, 'web3.metaMaskNetworkId')

if (desiredNetwork !== selectedNetwork) {
return (
<WrappedComponent
{...props}
cannotTransact="wrong-network"
cannotTransactData={get(data, 'web3.networkName')}
/>
)
}

return <WrappedComponent {...props} />
return (
<WrappedComponent
{...props}
{...cannotTransact({ data, error, loading, canTransactConfig })}
/>
)
}}
</Query>
)
}
return withConfig(WithCanTransact)
return withConfig(WithCanTransact, 'canTransactConfig')
}

export default withCanTransact
27 changes: 10 additions & 17 deletions dapps/marketplace/src/hoc/withConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ import get from 'lodash/get'

import ConfigQuery from 'queries/Config'

function withConfig(WrappedComponent) {
const WithConfig = props => {
return (
<Query query={ConfigQuery}>
{({ data, networkStatus }) => {
const config = get(data, 'configObj', {})
return (
<WrappedComponent
{...props}
config={config}
configLoading={networkStatus === 1}
/>
)
}}
</Query>
)
}
function withConfig(WrappedComponent, prop = 'config') {
const WithConfig = ({ ...props }) => (
<Query query={ConfigQuery}>
{({ data, networkStatus }) => {
props[prop] = get(data, 'configObj') || {}
props[`${prop}Loading`] = networkStatus === 1
return <WrappedComponent {...props} />
}}
</Query>
)
return WithConfig
}

Expand Down
8 changes: 5 additions & 3 deletions dapps/marketplace/src/hoc/withCurrencyBalances.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Query } from 'react-apollo'
import gql from 'graphql-tag'
import get from 'lodash/get'

import withWallet from './withWallet'

const query = gql`
query GetCurrencyBalances(
$tokens: [String]
Expand Down Expand Up @@ -50,9 +52,9 @@ function withCurrencyBalances(WrappedComponent) {
skip={!props.wallet}
variables={{
account: props.wallet,
proxy: props.walletProxy,
proxy: props.walletPredictedProxy,
tokens: props.targets,
useProxy: props.walletProxy !== props.wallet
useProxy: props.walletPredictedProxy !== props.wallet
}}
fetchPolicy="network-only"
>
Expand All @@ -65,7 +67,7 @@ function withCurrencyBalances(WrappedComponent) {
)}
</Query>
)
return WithCurrencyBalances
return withWallet(WithCurrencyBalances)
}

export default withCurrencyBalances
60 changes: 21 additions & 39 deletions dapps/marketplace/src/hoc/withGrowthCampaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,47 @@ import QueryError from 'components/QueryError'
import get from 'lodash/get'

import enrollmentStatusQuery from 'queries/EnrollmentStatus'
import profileQuery from 'queries/Profile'
import allCampaignsQuery from 'queries/AllGrowthCampaigns'
import withWallet from './withWallet'

function withGrowthCampaign(
WrappedComponent,
{ fetchPolicy = 'network-only', queryEvenIfNotEnrolled, suppressErrors } = {}
) {
const WithGrowthCampaign = props => {
return (
<Query query={profileQuery} notifyOnNetworkStatusChange={true}>
<Query
query={enrollmentStatusQuery}
variables={{ walletAddress: props.wallet }}
skip={!props.wallet}
fetchPolicy={fetchPolicy}
>
{({ data, error }) => {
if (error && !suppressErrors) {
return <QueryError error={error} query={profileQuery} />
return <QueryError error={error} query={enrollmentStatusQuery} />
}

const walletAddress = get(data, 'web3.primaryAccount.id')

const enrollmentStatus = get(data, 'enrollmentStatus')
return (
<Query
query={enrollmentStatusQuery}
variables={{ walletAddress }}
skip={!walletAddress}
query={allCampaignsQuery}
notifyOnNetworkStatusChange={true}
skip={
queryEvenIfNotEnrolled ? false : enrollmentStatus !== 'Enrolled'
}
fetchPolicy={fetchPolicy}
>
{({ data, error }) => {
if (error && !suppressErrors) {
return (
<QueryError error={error} query={enrollmentStatusQuery} />
)
return <QueryError error={error} query={allCampaignsQuery} />
}

const enrollmentStatus = get(data, 'enrollmentStatus')
return (
<Query
query={allCampaignsQuery}
notifyOnNetworkStatusChange={true}
skip={
queryEvenIfNotEnrolled
? false
: enrollmentStatus !== 'Enrolled'
}
fetchPolicy={fetchPolicy}
>
{({ data, error }) => {
if (error && !suppressErrors) {
return (
<QueryError error={error} query={allCampaignsQuery} />
)
}

return (
<WrappedComponent
{...props}
growthEnrollmentStatus={enrollmentStatus}
growthCampaigns={get(data, 'campaigns.nodes') || []}
/>
)
}}
</Query>
<WrappedComponent
{...props}
growthEnrollmentStatus={enrollmentStatus}
growthCampaigns={get(data, 'campaigns.nodes') || []}
/>
)
}}
</Query>
Expand All @@ -71,7 +53,7 @@ function withGrowthCampaign(
</Query>
)
}
return WithGrowthCampaign
return withWallet(WithGrowthCampaign)
}

export default withGrowthCampaign
17 changes: 11 additions & 6 deletions dapps/marketplace/src/hoc/withGrowthRewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ export default function withGrowthRewards(WrappedComponent) {

activeCampaign.actions.forEach(action => {
if (action.type === 'ListingIdPurchased') {
const normalisedReward = parseInt(
web3.utils
.toBN(action.reward ? action.reward.amount : 0)
.div(decimalDivision)
.toString()
)
let normalisedReward = 0
try {
normalisedReward = parseInt(
web3.utils
.toBN(action.reward ? action.reward.amount : 0)
.div(decimalDivision)
.toString()
)
} catch (e) {
/* Ignore */
}
ognListingRewards[action.listingId] = normalisedReward
}
})
Expand Down
3 changes: 2 additions & 1 deletion dapps/marketplace/src/hoc/withWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ function withWallet(WrappedComponent) {
{/* TODO: see if there's a way to avoid polling */}
{({ data, error, networkStatus }) => {
if (error) console.error(error)

const predicted = get(data, 'web3.primaryAccount.predictedProxy.id')
return (
<WrappedComponent
{...props}
wallet={get(data, 'web3.primaryAccount.id')}
walletType={get(data, 'web3.walletType')}
walletLoading={networkStatus === 1}
walletProxy={get(data, 'web3.primaryAccount.proxy.id')}
walletPredictedProxy={predicted}
/>
)
}}
Expand Down
9 changes: 8 additions & 1 deletion dapps/marketplace/src/mutations/AllowToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ export default gql`
$from: String!
$to: String!
$value: String!
$forceProxy: Boolean
) {
updateTokenAllowance(token: $token, from: $from, to: $to, value: $value) {
updateTokenAllowance(
token: $token
from: $from
to: $to
value: $value
forceProxy: $forceProxy
) {
id
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ class CreateListing extends Component {

this.setState({ waitFor: 'pending' })

const { listing, tokenBalance, wallet } = this.props
const { listing, tokenBalance, walletProxy } = this.props

const variables = applyListingData(this.props, {
deposit: tokenBalance >= Number(listing.boost) ? listing.boost : '0',
depositManager: wallet,
from: wallet
depositManager: walletProxy,
from: walletProxy
})

createListing({ variables })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ class UpdateListing extends Component {

this.setState({ waitFor: 'pending' })

const { listing, tokenBalance, wallet } = this.props
const { listing, tokenBalance } = this.props

updateListing({
variables: applyListingData(this.props, {
listingID: this.props.listing.id,
additionalDeposit:
tokenBalance >= Number(listing.boost) ? listing.boost : '0',
from: wallet
from: this.props.listing.seller.id
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DeployIdentity extends Component {
this.setState({ waitFor: 'pending' })
const profile = this.props.profile
const variables = {
from: this.props.wallet,
from: this.props.walletProxy,
attestations: this.props.attestations,
profile
}
Expand Down
11 changes: 9 additions & 2 deletions dapps/marketplace/src/pages/listing/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class EditListing extends Component {
'title',
'description',
'category',
'subCategory'
'subCategory',
'seller'
]),
acceptedTokens: tokens.length ? tokens : ['token-ETH'],
quantity: String(props.listing.unitsTotal),
Expand Down Expand Up @@ -74,7 +75,13 @@ class EditListing extends Component {
}
}

return <CreateListing listing={listing} refetch={this.props.refetch} />
return (
<CreateListing
seller={get(this.props, 'listing.seller.id')}
listing={listing}
refetch={this.props.refetch}
/>
)
}
}

Expand Down
Loading

0 comments on commit 42aa0be

Please sign in to comment.