1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-28 16:47:52 +02:00

token approval & allowance fixes

This commit is contained in:
Matthias Kretschmann 2022-01-26 10:43:32 +00:00
parent 8b31746e57
commit 08a691ceee
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 27 additions and 27 deletions

View File

@ -4,7 +4,7 @@ import { useWeb3 } from '@context/Web3'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { getOceanConfig } from '@utils/ocean' import { getOceanConfig } from '@utils/ocean'
import { ButtonApprove } from './ButtonApprove' import { ButtonApprove } from './ButtonApprove'
import { Datatoken } from '@oceanprotocol/lib' import { allowance, approve, LoggerInstance } from '@oceanprotocol/lib'
export default function TokenApproval({ export default function TokenApproval({
actionButton, actionButton,
@ -28,42 +28,42 @@ export default function TokenApproval({
coin === 'OCEAN' coin === 'OCEAN'
? config.oceanTokenAddress ? config.oceanTokenAddress
: ddo.services[0].datatokenAddress : ddo.services[0].datatokenAddress
const spender = price.address const spender = price?.address
// TODO: how to check if token is approved as .allowance does not exist? const checkTokenApproval = useCallback(async () => {
// const checkTokenApproval = useCallback(async () => { if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
// if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
// const datatokenInstance = new Datatoken(web3) const allowanceValue = await allowance(
// const allowance = await datatokenInstance.allowance( web3,
// tokenAddress, tokenAddress,
// accountId, accountId,
// spender spender
// ) )
// amount &&
// new Decimal(amount).greaterThan(new Decimal('0')) &&
// setTokenApproved(
// new Decimal(allowance).greaterThanOrEqualTo(new Decimal(amount))
// )
// }, [web3, tokenAddress, spender, accountId, amount, isAssetNetwork])
// useEffect(() => { if (!allowanceValue) return
// checkTokenApproval()
// }, [checkTokenApproval]) new Decimal(amount).greaterThan(new Decimal('0')) &&
setTokenApproved(
new Decimal(allowanceValue).greaterThanOrEqualTo(new Decimal(amount))
)
}, [web3, tokenAddress, spender, accountId, amount, isAssetNetwork])
useEffect(() => {
checkTokenApproval()
}, [checkTokenApproval])
async function approveTokens(amount: string) { async function approveTokens(amount: string) {
setLoading(true) setLoading(true)
try { try {
const datatokenInstance = new Datatoken(web3) const tx = await approve(web3, accountId, tokenAddress, spender, amount)
await datatokenInstance.approve(tokenAddress, spender, amount, accountId) LoggerInstance.log(`Approve tokens tx:`, tx)
setTokenApproved(true)
} catch (error) { } catch (error) {
LoggerInstance.error(`Approve tokens tx failed:`, error.message)
} finally {
await checkTokenApproval()
setLoading(false) setLoading(false)
} }
// await checkTokenApproval()
setLoading(false)
} }
return ( return (

View File

@ -72,7 +72,7 @@ export default function Add({
async function getMaximum() { async function getMaximum() {
try { try {
const poolInstance = new Pool(web3, LoggerInstance) const poolInstance = new Pool(web3, LoggerInstance)
const tokenInAddress = await poolInstance.getBasetoken(poolAddress) const tokenInAddress = await poolInstance.getBaseToken(poolAddress)
setTokenInAddress(tokenInAddress) setTokenInAddress(tokenInAddress)
const amountMaxPool = await poolInstance.getReserve( const amountMaxPool = await poolInstance.getReserve(