market/src/components/@shared/TokenApproval/index.tsx

97 lines
2.5 KiB
TypeScript
Raw Normal View History

Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
import React, { ReactElement, useCallback, useEffect, useState } from 'react'
2021-10-13 18:48:59 +02:00
import { useAsset } from '@context/Asset'
import { useWeb3 } from '@context/Web3'
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
import Decimal from 'decimal.js'
2021-10-18 20:44:33 +02:00
import { ButtonApprove } from './ButtonApprove'
2022-01-26 11:43:32 +01:00
import { allowance, approve, LoggerInstance } from '@oceanprotocol/lib'
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
export default function TokenApproval({
actionButton,
disabled,
amount,
tokenAddress,
tokenSymbol,
setSubmitting,
setIsTokenApproved
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
}: {
actionButton: JSX.Element
disabled: boolean
amount: string
tokenAddress: string
tokenSymbol: string
setSubmitting?: (isSubmitting: boolean) => void
setIsTokenApproved: (isApproved: boolean) => void
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
}): ReactElement {
const { asset, isAssetNetwork } = useAsset()
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
const [tokenApproved, setTokenApproved] = useState(false)
const [loading, setLoading] = useState(false)
const { web3, accountId } = useWeb3()
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
const spender = asset?.accessDetails?.addressOrId
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
2022-01-26 11:43:32 +01:00
const checkTokenApproval = useCallback(async () => {
if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
2022-01-26 11:43:32 +01:00
const allowanceValue = await allowance(
web3,
tokenAddress,
accountId,
spender
)
LoggerInstance.log(`[token approval] allowanceValue: ${allowanceValue}`)
2022-01-26 11:43:32 +01:00
if (!allowanceValue) return
new Decimal(amount).greaterThan(new Decimal('0')) &&
setTokenApproved(
new Decimal(allowanceValue).greaterThanOrEqualTo(new Decimal(amount))
)
setIsTokenApproved(
new Decimal(allowanceValue).greaterThanOrEqualTo(new Decimal(amount))
)
2022-01-26 11:43:32 +01:00
}, [web3, tokenAddress, spender, accountId, amount, isAssetNetwork])
useEffect(() => {
checkTokenApproval()
}, [checkTokenApproval])
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
async function approveTokens(amount: string) {
setLoading(true)
setSubmitting(true)
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
try {
2022-01-26 11:43:32 +01:00
const tx = await approve(web3, accountId, tokenAddress, spender, amount)
LoggerInstance.log(`[token approval] Approve tokens tx:`, tx)
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
} catch (error) {
LoggerInstance.error(
`[token approval] Approve tokens tx failed:`,
error.message
)
2022-01-26 11:43:32 +01:00
} finally {
await checkTokenApproval()
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
setLoading(false)
setSubmitting(false)
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
}
}
return (
<>
{tokenApproved ||
disabled ||
amount === '0' ||
amount === '' ||
!amount ||
typeof amount === 'undefined' ? (
actionButton
) : (
<ButtonApprove
amount={amount}
tokenSymbol={tokenSymbol}
Token approval split-up (#640) * Token approval component * check if datatoken approved * display token amount on button, add approve function * approve token based on token type * approve token for trade, remove for pool lequidity remove action * verify approval on amount change * show action button only if amount is approved * catch approve error and stop loadin * display token amount with 2 decimals on trade token approval * infinite approval and UI fixes * fixed alert warning not showing, account id for approve * wip * fixed displayed token amount to approve for swap * token amount text fix * lint error fix * package version update * version fix * downgrade version * fixed error for no wallet connected * update package-lock * display token name, and changed amount precision * removed empty file, fixed token switch error * refactor for better user experience * move content * ExplorerLink console error fixes * UI tweaks * slightly changed button logic * fix Trade form approvals * cleanup * don't block add liquidity button * merge fixes * hook dependency cleanup * dtItem fix, error fixes based on asset network match * disable action button if field is not valid, undefined trade tokens * fix infiniteApproval user preference saving * remove unneccessary string conversion * used Decimal for dtAmount and oceanAmount * changed token spender address * bump ocean.js to vo.17.5 * fix lint * replace Number with Decimal * fix getting to add liquidity screen without wallet connected * fix crash when switching coins after value input Co-authored-by: Norbi <katunanorbert@gmai.com> Co-authored-by: Matthias Kretschmann <m@kretschmann.io> Co-authored-by: mihaisc <mihai@oceanprotocol.com>
2021-09-30 12:54:44 +02:00
approveTokens={approveTokens}
isLoading={loading}
/>
)}
</>
)
}