mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
more generic TokenApproval props, more logging
This commit is contained in:
parent
c10ca06dbc
commit
0db0bea3f6
@ -7,19 +7,19 @@ import content from '../../../../content/price.json'
|
|||||||
|
|
||||||
export function ButtonApprove({
|
export function ButtonApprove({
|
||||||
amount,
|
amount,
|
||||||
coin,
|
tokenSymbol,
|
||||||
approveTokens,
|
approveTokens,
|
||||||
isLoading
|
isLoading
|
||||||
}: {
|
}: {
|
||||||
amount: string
|
amount: string
|
||||||
coin: string
|
tokenSymbol: string
|
||||||
approveTokens: (amount: string) => void
|
approveTokens: (amount: string) => void
|
||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { infiniteApproval } = useUserPreferences()
|
const { infiniteApproval } = useUserPreferences()
|
||||||
|
|
||||||
return isLoading ? (
|
return isLoading ? (
|
||||||
<Loader message={`Approving ${coin}...`} />
|
<Loader message={`Approving ${tokenSymbol}...`} />
|
||||||
) : infiniteApproval ? (
|
) : infiniteApproval ? (
|
||||||
<Button
|
<Button
|
||||||
style="primary"
|
style="primary"
|
||||||
@ -27,16 +27,22 @@ export function ButtonApprove({
|
|||||||
disabled={parseInt(amount) < 1}
|
disabled={parseInt(amount) < 1}
|
||||||
onClick={() => approveTokens(`${2 ** 53 - 1}`)}
|
onClick={() => approveTokens(`${2 ** 53 - 1}`)}
|
||||||
>
|
>
|
||||||
Approve {coin}{' '}
|
Approve {tokenSymbol}{' '}
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={content.pool.tooltips.approveInfinite.replace('COIN', coin)}
|
content={content.pool.tooltips.approveInfinite.replace(
|
||||||
|
'COIN',
|
||||||
|
tokenSymbol
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<Button style="primary" size="small" onClick={() => approveTokens(amount)}>
|
<Button style="primary" size="small" onClick={() => approveTokens(amount)}>
|
||||||
Approve {amount} {coin}
|
Approve {amount} {tokenSymbol}
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={content.pool.tooltips.approveSpecific.replace('COIN', coin)}
|
content={content.pool.tooltips.approveSpecific.replace(
|
||||||
|
'COIN',
|
||||||
|
tokenSymbol
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
|
@ -2,7 +2,6 @@ import React, { ReactElement, useCallback, useEffect, useState } from 'react'
|
|||||||
import { useAsset } from '@context/Asset'
|
import { useAsset } from '@context/Asset'
|
||||||
import { useWeb3 } from '@context/Web3'
|
import { useWeb3 } from '@context/Web3'
|
||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import { getOceanConfig } from '@utils/ocean'
|
|
||||||
import { ButtonApprove } from './ButtonApprove'
|
import { ButtonApprove } from './ButtonApprove'
|
||||||
import { allowance, approve, LoggerInstance } from '@oceanprotocol/lib'
|
import { allowance, approve, LoggerInstance } from '@oceanprotocol/lib'
|
||||||
|
|
||||||
@ -10,24 +9,20 @@ export default function TokenApproval({
|
|||||||
actionButton,
|
actionButton,
|
||||||
disabled,
|
disabled,
|
||||||
amount,
|
amount,
|
||||||
coin
|
tokenAddress,
|
||||||
|
tokenSymbol
|
||||||
}: {
|
}: {
|
||||||
actionButton: JSX.Element
|
actionButton: JSX.Element
|
||||||
disabled: boolean
|
disabled: boolean
|
||||||
amount: string
|
amount: string
|
||||||
coin: string
|
tokenAddress: string
|
||||||
|
tokenSymbol: string
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { ddo, price, isAssetNetwork } = useAsset()
|
const { price, isAssetNetwork } = useAsset()
|
||||||
const [tokenApproved, setTokenApproved] = useState(false)
|
const [tokenApproved, setTokenApproved] = useState(false)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const { web3, accountId } = useWeb3()
|
const { web3, accountId } = useWeb3()
|
||||||
|
|
||||||
const config = getOceanConfig(ddo.chainId)
|
|
||||||
|
|
||||||
const tokenAddress =
|
|
||||||
coin === 'OCEAN'
|
|
||||||
? config.oceanTokenAddress
|
|
||||||
: ddo.services[0].datatokenAddress
|
|
||||||
const spender = price?.address
|
const spender = price?.address
|
||||||
|
|
||||||
const checkTokenApproval = useCallback(async () => {
|
const checkTokenApproval = useCallback(async () => {
|
||||||
@ -39,6 +34,7 @@ export default function TokenApproval({
|
|||||||
accountId,
|
accountId,
|
||||||
spender
|
spender
|
||||||
)
|
)
|
||||||
|
LoggerInstance.log(`[token approval] allowanceValue: ${allowanceValue}`)
|
||||||
|
|
||||||
if (!allowanceValue) return
|
if (!allowanceValue) return
|
||||||
|
|
||||||
@ -57,9 +53,12 @@ export default function TokenApproval({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const tx = await approve(web3, accountId, tokenAddress, spender, amount)
|
const tx = await approve(web3, accountId, tokenAddress, spender, amount)
|
||||||
LoggerInstance.log(`Approve tokens tx:`, tx)
|
LoggerInstance.log(`[token approval] Approve tokens tx:`, tx)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
LoggerInstance.error(`Approve tokens tx failed:`, error.message)
|
LoggerInstance.error(
|
||||||
|
`[token approval] Approve tokens tx failed:`,
|
||||||
|
error.message
|
||||||
|
)
|
||||||
} finally {
|
} finally {
|
||||||
await checkTokenApproval()
|
await checkTokenApproval()
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
@ -78,7 +77,7 @@ export default function TokenApproval({
|
|||||||
) : (
|
) : (
|
||||||
<ButtonApprove
|
<ButtonApprove
|
||||||
amount={amount}
|
amount={amount}
|
||||||
coin={coin}
|
tokenSymbol={tokenSymbol}
|
||||||
approveTokens={approveTokens}
|
approveTokens={approveTokens}
|
||||||
isLoading={loading}
|
isLoading={loading}
|
||||||
/>
|
/>
|
||||||
|
@ -6,6 +6,8 @@ import ExplorerLink from '@shared/ExplorerLink'
|
|||||||
import SuccessConfetti from '@shared/SuccessConfetti'
|
import SuccessConfetti from '@shared/SuccessConfetti'
|
||||||
import { useWeb3 } from '@context/Web3'
|
import { useWeb3 } from '@context/Web3'
|
||||||
import TokenApproval from '@shared/TokenApproval'
|
import TokenApproval from '@shared/TokenApproval'
|
||||||
|
import { getOceanConfig } from '@utils/ocean'
|
||||||
|
import { useAsset } from '@context/Asset'
|
||||||
|
|
||||||
export default function Actions({
|
export default function Actions({
|
||||||
isLoading,
|
isLoading,
|
||||||
@ -29,6 +31,7 @@ export default function Actions({
|
|||||||
isDisabled?: boolean
|
isDisabled?: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { networkId } = useWeb3()
|
const { networkId } = useWeb3()
|
||||||
|
const { ddo } = useAsset()
|
||||||
|
|
||||||
const actionButton = (
|
const actionButton = (
|
||||||
<Button
|
<Button
|
||||||
@ -41,6 +44,10 @@ export default function Actions({
|
|||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const config = getOceanConfig(ddo?.chainId)
|
||||||
|
const tokenAddress = config.oceanTokenAddress
|
||||||
|
const tokenSymbol = config.oceanTokenSymbol
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className={styles.actions}>
|
<div className={styles.actions}>
|
||||||
@ -50,7 +57,8 @@ export default function Actions({
|
|||||||
<TokenApproval
|
<TokenApproval
|
||||||
actionButton={actionButton}
|
actionButton={actionButton}
|
||||||
amount={amount}
|
amount={amount}
|
||||||
coin={coin || 'OCEAN'}
|
tokenAddress={tokenAddress}
|
||||||
|
tokenSymbol={tokenSymbol}
|
||||||
disabled={isDisabled}
|
disabled={isDisabled}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user