1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01: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 { getOceanConfig } from '@utils/ocean'
import { ButtonApprove } from './ButtonApprove'
import { Datatoken } from '@oceanprotocol/lib'
import { allowance, approve, LoggerInstance } from '@oceanprotocol/lib'
export default function TokenApproval({
actionButton,
@ -28,42 +28,42 @@ export default function TokenApproval({
coin === 'OCEAN'
? config.oceanTokenAddress
: 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 () => {
// if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
const checkTokenApproval = useCallback(async () => {
if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
// const datatokenInstance = new Datatoken(web3)
// const allowance = await datatokenInstance.allowance(
// tokenAddress,
// accountId,
// spender
// )
// amount &&
// new Decimal(amount).greaterThan(new Decimal('0')) &&
// setTokenApproved(
// new Decimal(allowance).greaterThanOrEqualTo(new Decimal(amount))
// )
// }, [web3, tokenAddress, spender, accountId, amount, isAssetNetwork])
const allowanceValue = await allowance(
web3,
tokenAddress,
accountId,
spender
)
// useEffect(() => {
// checkTokenApproval()
// }, [checkTokenApproval])
if (!allowanceValue) return
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) {
setLoading(true)
try {
const datatokenInstance = new Datatoken(web3)
await datatokenInstance.approve(tokenAddress, spender, amount, accountId)
setTokenApproved(true)
const tx = await approve(web3, accountId, tokenAddress, spender, amount)
LoggerInstance.log(`Approve tokens tx:`, tx)
} catch (error) {
LoggerInstance.error(`Approve tokens tx failed:`, error.message)
} finally {
await checkTokenApproval()
setLoading(false)
}
// await checkTokenApproval()
setLoading(false)
}
return (

View File

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