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

wip: support native ether #1262

Merged
merged 66 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
524ec8b
upgrade proxy kit version
hexyls Oct 28, 2020
274fa6d
enable proxy upgrades
hexyls Oct 30, 2020
efda6b0
Merge branch 'master' into feature/support-native-ether
altbee Oct 30, 2020
aca44e0
Merge branch 'master' into feature/support-native-ether
pimato Oct 31, 2020
07c88d0
preserve default token ordering
hexyls Nov 1, 2020
162d156
add ether to funding dropdown
hexyls Nov 2, 2020
0345a9a
add upgrade proxy component
hexyls Nov 2, 2020
116165a
update createMarket method
hexyls Nov 2, 2020
a766f4a
remove weth abi
hexyls Nov 3, 2020
bb3c835
prevent early proxy upgrade message
hexyls Nov 3, 2020
ae21bdb
disable create market button if proxy is not deployed or it needs an …
hexyls Nov 3, 2020
88d1a98
add proxy upgrade button states
hexyls Nov 3, 2020
1f9187f
handle native ether in fetchAccountBalance
hexyls Nov 3, 2020
459735e
Merge branch 'master' into feature/support-native-ether
pimato Nov 3, 2020
5933fdc
monkey patch cpk to fix transaction encoding
hexyls Nov 4, 2020
763ced0
add useCpkProxy and use RemoteData
hexyls Nov 5, 2020
fa821dc
enable native eth funding on existing markets
hexyls Nov 5, 2020
f566ae0
make update proxy message user friendly
hexyls Nov 5, 2020
d63af51
update proxy message and lock file
hexyls Nov 5, 2020
7a97f40
update cpk to v2.2.0-alpha.1
hexyls Nov 6, 2020
d506fd3
Merge branch 'master' into feature/support-native-ether
hexyls Nov 6, 2020
941f05a
enable native ether for buys
hexyls Nov 6, 2020
89f62ad
add extra gas for deploy + upgrade proxy
hexyls Nov 9, 2020
e1c008a
Merge branch 'master' into feature/support-native-ether
hexyls Nov 9, 2020
3b24389
Merge branch 'master' into feature/support-native-ether
pimato Nov 10, 2020
8f56b54
Merge branch 'master' into feature/support-native-ether
hexyls Dec 1, 2020
a61d80b
update lockfile
hexyls Dec 1, 2020
915234b
add sokol contracts
hexyls Dec 8, 2020
eaf03a8
Merge branch 'master' into feature/support-native-ether
hexyls Dec 9, 2020
1435de0
return encodeMultiSendCallData and fix some conditionals
hexyls Dec 9, 2020
9b2606d
rename to pseudoNativeAssetAddress
hexyls Dec 9, 2020
d8c1fee
add getWrapToken
hexyls Dec 9, 2020
2d849e7
add getTargetSafeImplementation
hexyls Dec 9, 2020
e00c7ef
remove value amount if safeApp
hexyls Dec 10, 2020
85b187f
replace getToken with getWrapToken
hexyls Dec 10, 2020
e7b2c28
update useTokens hook
hexyls Dec 10, 2020
360cd83
update gas values for native asset txs
hexyls Dec 10, 2020
0ee4c41
fix missed conditional
hexyls Dec 10, 2020
89c8063
use transaction v2 for single transaction
hexyls Dec 10, 2020
311dfec
generalise UpgradeProxy and fix disabled buttons
hexyls Dec 10, 2020
e261236
Merge branch 'master' into feature/support-native-ether
hexyls Dec 11, 2020
e7ad346
update mainnet contracts
hexyls Dec 11, 2020
dccaba6
moar gas for create tx
hexyls Dec 11, 2020
c1f7c0e
fix approval in addFunding
hexyls Dec 11, 2020
65f8fd8
update contracts
hexyls Dec 14, 2020
09ea8fc
Merge branch 'master' into feature/sokol
hexyls Dec 14, 2020
272831f
update arbitration address
hexyls Dec 15, 2020
3de3e6d
Merge branch 'master' into feature/support-native-ether
hexyls Dec 15, 2020
959eccf
fix bad merge
hexyls Dec 15, 2020
953ab0a
replace UpgradeProxy component
hexyls Dec 15, 2020
4019de1
update contract
hexyls Dec 16, 2020
3762293
update realitio timeout
hexyls Dec 16, 2020
becea7e
update contracts
hexyls Dec 17, 2020
855f58f
Merge branch 'master' into feature/xdai
hexyls Dec 17, 2020
fa2b6aa
Merge branch 'master' into feature/support-native-ether
hexyls Dec 20, 2020
44a7a85
Merge branch 'master' into feature/support-native-ether
hexyls Dec 21, 2020
49e7b44
add sokol details
hexyls Dec 21, 2020
a8d5125
Merge branch 'master' into feature/support-native-ether
hexyls Dec 21, 2020
c2cb0d7
lowercase WSPOA address
hexyls Dec 21, 2020
de33e9e
Merge branch 'master' into feature/xdai
hexyls Dec 21, 2020
3067ff4
Merge branch 'feature/support-native-ether' into feature/xdai
hexyls Dec 22, 2020
b193019
native xdai
hexyls Dec 22, 2020
b871da2
allowance margins
hexyls Dec 22, 2020
40302de
Merge branch 'feature/xdai' into feature/support-native-ether
hexyls Dec 22, 2020
f23cf26
update xdai target safe implementation
hexyls Dec 22, 2020
c94e069
Merge branch 'master' into feature/support-native-ether
pimato Dec 22, 2020
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
5 changes: 3 additions & 2 deletions app/src/components/market/common/currency_selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ interface Props {
balance?: string
placeholder?: Maybe<string>
addAll?: boolean
addNativeAsset?: boolean
}

export const CurrencySelector: React.FC<Props> = props => {
const {
addAll = false,
addNativeAsset = false,
balance,
context,
currency,
Expand All @@ -50,7 +52,7 @@ export const CurrencySelector: React.FC<Props> = props => {
...restProps
} = props

const tokens = useTokens(context)
const tokens = useTokens(context, addNativeAsset)

const currencyDropdownData: Array<DropdownItemProps> = []

Expand All @@ -73,7 +75,6 @@ export const CurrencySelector: React.FC<Props> = props => {
}
},
})

currentItem = 0
}

Expand Down
8 changes: 7 additions & 1 deletion app/src/components/market/common/outcome_table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { BigNumber } from 'ethers/utils'
import React, { useCallback } from 'react'
import styled, { css } from 'styled-components'

import { useConnectedWeb3Context } from '../../../../hooks'
import { getOutcomeColor } from '../../../../theme/utils'
import { getWrapToken, pseudoNativeAssetAddress } from '../../../../util/networks'
import { formatBigNumber, formatNumber, mulBN } from '../../../../util/tools'
import { BalanceItem, BondItem, OutcomeTableValue, Token, TokenEthereum } from '../../../../util/types'
import { RadioInput, TD, TH, THead, TR, Table } from '../../../common'
Expand Down Expand Up @@ -125,6 +127,10 @@ export const OutcomeTable = (props: Props) => {

const TableCellsAlign = ['left', 'left', 'right', 'right', 'right', 'right', 'right']

const context = useConnectedWeb3Context()
const wrapSymbol = getWrapToken(context.networkId).symbol
const symbol = collateral.address === pseudoNativeAssetAddress ? wrapSymbol : collateral.symbol

const renderTableHeader = () => {
return (
<THead>
Expand All @@ -137,7 +143,7 @@ export const OutcomeTable = (props: Props) => {
style={isBond && index === 1 ? { width: '53%' } : {}}
textAlign={TableCellsAlign[index]}
>
{value} {value === OutcomeTableValue.CurrentPrice && `(${collateral.symbol})`}
{value} {value === OutcomeTableValue.CurrentPrice && `(${symbol})`}
</THStyled>
) : null
})}
Expand Down
48 changes: 44 additions & 4 deletions app/src/components/market/sections/market_buy/market_buy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
useConnectedWeb3Context,
useContracts,
useCpkAllowance,
useCpkProxy,
} from '../../../../hooks'
import { MarketMakerService } from '../../../../services'
import { getLogger } from '../../../../util/logger'
import { getNativeAsset, getWrapToken, pseudoNativeAssetAddress } from '../../../../util/networks'
import { RemoteData } from '../../../../util/remote_data'
import { computeBalanceAfterTrade, formatBigNumber, formatNumber, mulBN } from '../../../../util/tools'
import { MarketDetailsTab, MarketMakerData, OutcomeTableValue, Status, Ternary, Token } from '../../../../util/types'
Expand Down Expand Up @@ -56,7 +58,7 @@ interface Props extends RouteComponentProps<any> {
const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
const context = useConnectedWeb3Context()
const cpk = useConnectedCPKContext()
const { library: provider } = context
const { library: provider, networkId } = context
const signer = useMemo(() => provider.getSigner(), [provider])

const { buildMarketMaker } = useContracts(context)
Expand All @@ -81,6 +83,10 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
const hasEnoughAllowance = RemoteData.mapToTernary(allowance, allowance => allowance.gte(amount || Zero))
const hasZeroAllowance = RemoteData.mapToTernary(allowance, allowance => allowance.isZero())

const [upgradeFinished, setUpgradeFinished] = useState(false)
const { proxyIsUpToDate, updateProxy } = useCpkProxy()
const isUpdated = RemoteData.hasData(proxyIsUpToDate) ? proxyIsUpToDate.data : false

useEffect(() => {
setIsNegativeAmount(formatBigNumber(amount || Zero, collateral.decimals).includes('-'))
}, [amount, collateral.decimals])
Expand Down Expand Up @@ -141,6 +147,19 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
setAllowanceFinished(true)
}

const showUpgrade =
(!isUpdated && collateral.address === pseudoNativeAssetAddress) ||
(upgradeFinished && collateral.address === pseudoNativeAssetAddress)

const upgradeProxy = async () => {
if (!cpk) {
return
}

await updateProxy()
setUpgradeFinished(true)
}

const finish = async () => {
try {
if (!cpk) {
Expand All @@ -154,6 +173,7 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {

await cpk.buyOutcomes({
amount: amount || Zero,
collateral,
outcomeIndex,
marketMaker,
})
Expand Down Expand Up @@ -181,6 +201,7 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
}

const showSetAllowance =
collateral.address !== pseudoNativeAssetAddress &&
!cpk?.cpk.isSafeApp() &&
(allowanceFinished || hasZeroAllowance === Ternary.True || hasEnoughAllowance === Ternary.False)

Expand Down Expand Up @@ -214,14 +235,23 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
!amount ||
(status !== Status.Ready && status !== Status.Error) ||
amount?.isZero() ||
(!cpk?.cpk.isSafeApp() && hasEnoughAllowance !== Ternary.True) ||
(!cpk?.cpk.isSafeApp() && collateral.address !== pseudoNativeAssetAddress && hasEnoughAllowance !== Ternary.True) ||
amountError !== null ||
isNegativeAmount
isNegativeAmount ||
(!isUpdated && collateral.address === pseudoNativeAssetAddress)

const wrapAddress = getWrapToken(networkId).address

const currencyFilters =
collateral.address === wrapAddress || collateral.address === pseudoNativeAssetAddress
? [wrapAddress, pseudoNativeAssetAddress.toLowerCase()]
: []

const switchOutcome = (value: number) => {
setNewShares(balances.map((balance, i) => (i === outcomeIndex ? balance.shares.add(tradedShares) : balance.shares)))
setOutcomeIndex(value)
}

return (
<>
<OutcomeTable
Expand Down Expand Up @@ -252,10 +282,12 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
<div>
<CurrenciesWrapper>
<CurrencySelector
addNativeAsset
balance={formatBigNumber(maybeCollateralBalance || Zero, collateral.decimals, 5)}
context={context}
currency={collateral.address}
disabled
disabled={currencyFilters.length ? false : true}
filters={currencyFilters}
onSelect={(token: Token | null) => {
if (token) {
setCollateral(token)
Expand Down Expand Up @@ -330,6 +362,14 @@ const MarketBuyWrapper: React.FC<Props> = (props: Props) => {
onUnlock={unlockCollateral}
/>
)}
{showUpgrade && (
<SetAllowance
collateral={getNativeAsset(context.networkId)}
finished={upgradeFinished && RemoteData.is.success(proxyIsUpToDate)}
loading={RemoteData.is.asking(proxyIsUpToDate)}
onUnlock={upgradeProxy}
/>
)}
<StyledButtonContainer>
<Button buttonType={ButtonType.secondaryLine} onClick={() => switchMarketTab(MarketDetailsTab.swap)}>
Cancel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useConnectedWeb3Context } from '../../../../hooks/connectedWeb3'
import { ERC20Service } from '../../../../services'
import { getLogger } from '../../../../util/logger'
import { MarketCreationStatus } from '../../../../util/market_creation_status_data'
import { pseudoNativeAssetAddress } from '../../../../util/networks'
import { MarketData } from '../../../../util/types'
import { ModalConnectWallet } from '../../../modal'

Expand Down Expand Up @@ -40,7 +41,7 @@ const MarketWizardCreatorContainer: FC = () => {

setMarketCreationStatus(MarketCreationStatus.creatingAMarket())

if (!cpk.cpk.isSafeApp()) {
if (!cpk.cpk.isSafeApp() && marketData.collateral.address !== pseudoNativeAssetAddress) {
// Approve collateral to the proxy contract
const collateralService = new ERC20Service(provider, account, marketData.collateral.address)
const hasEnoughAlowance = await collateralService.hasEnoughAllowance(account, cpk.address, marketData.funding)
Expand All @@ -49,6 +50,7 @@ const MarketWizardCreatorContainer: FC = () => {
await collateralService.approveUnlimited(cpk.address)
}
}

const { marketMakerAddress, transaction } = await cpk.createMarket({
marketData,
conditionalTokens,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
useConnectedCPKContext,
useConnectedWeb3Context,
useCpkAllowance,
useCpkProxy,
useTokens,
} from '../../../../../../hooks'
import { useGraphMarketsFromQuestion } from '../../../../../../hooks/useGraphMarketsFromQuestion'
import { BalanceState, fetchAccountBalance } from '../../../../../../store/reducer'
import { MarketCreationStatus } from '../../../../../../util/market_creation_status_data'
import { getNativeAsset, pseudoNativeAssetAddress } from '../../../../../../util/networks'
import { RemoteData } from '../../../../../../util/remote_data'
import { formatBigNumber, formatDate, formatNumber } from '../../../../../../util/tools'
import { Arbitrator, Ternary, Token } from '../../../../../../util/types'
Expand Down Expand Up @@ -225,10 +227,13 @@ const FundingAndFeeStep: React.FC<Props> = (props: Props) => {

const [amount, setAmount] = useState<BigNumber>(funding)
const [amountToDispaly, setAmountToDisplay] = useState<string>('')

const hasEnoughAllowance = RemoteData.mapToTernary(allowance, allowance => allowance.gte(funding))
const hasZeroAllowance = RemoteData.mapToTernary(allowance, allowance => allowance.isZero())

const [upgradeFinished, setUpgradeFinished] = useState(false)
const { proxyIsUpToDate, updateProxy } = useCpkProxy()
const isUpdated = RemoteData.hasData(proxyIsUpToDate) ? proxyIsUpToDate.data : false

useEffect(() => {
dispatch(fetchAccountBalance(account, provider, collateral))
}, [dispatch, account, provider, collateral])
Expand Down Expand Up @@ -256,7 +261,7 @@ const FundingAndFeeStep: React.FC<Props> = (props: Props) => {
const [customFee, setCustomFee] = useState(false)
const [exceedsMaxFee, setExceedsMaxFee] = useState<boolean>(false)

const tokensAmount = useTokens(context).length
const tokensAmount = useTokens(context, true).length

const amountError =
maybeCollateralBalance === null
Expand All @@ -276,12 +281,27 @@ const FundingAndFeeStep: React.FC<Props> = (props: Props) => {
!account ||
amountError !== null ||
exceedsMaxFee ||
isNegativeDepositAmount
isNegativeDepositAmount ||
(!isUpdated && collateral.address === pseudoNativeAssetAddress)

const showSetAllowance =
collateral.address !== pseudoNativeAssetAddress &&
!cpk?.cpk.isSafeApp() &&
(allowanceFinished || hasZeroAllowance === Ternary.True || hasEnoughAllowance === Ternary.False)

const showUpgrade =
(!isUpdated && collateral.address === pseudoNativeAssetAddress) ||
(upgradeFinished && collateral.address === pseudoNativeAssetAddress)

const upgradeProxy = async () => {
if (!cpk) {
return
}

await updateProxy()
setUpgradeFinished(true)
}

const unlockCollateral = async () => {
if (!cpk) {
return
Expand Down Expand Up @@ -390,6 +410,7 @@ const FundingAndFeeStep: React.FC<Props> = (props: Props) => {
{tokensAmount > 1 && (
<CurrenciesWrapper>
<CurrencySelector
addNativeAsset
balance={formatNumber(collateralBalanceFormatted, 5)}
context={context}
currency={collateral.address}
Expand Down Expand Up @@ -465,6 +486,14 @@ const FundingAndFeeStep: React.FC<Props> = (props: Props) => {
style={{ marginBottom: 20 }}
/>
)}
{showUpgrade && (
<SetAllowance
collateral={getNativeAsset(context.networkId)}
finished={upgradeFinished && RemoteData.is.success(proxyIsUpToDate)}
loading={RemoteData.is.asking(proxyIsUpToDate)}
onUnlock={upgradeProxy}
/>
)}
<WarningMessage
additionalDescription={''}
description={
Expand Down
Loading