Error: unknown account when executing a contract function

When I call this in my localhost I get “Uncaught (in promise) Error: Returned error: unknown account”

async function allow() {

        setLoading(true)

        try {

            const approveBusdContract = busdContract.methods.approve(appEnv.contractAddress, `0x${ALLOWANCE_ASKED.toString(16)}`)

            await approveBusdContract.send({ from: userAddress })

            const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call({ from: userAddress })

            setAllowance(newAllowance)

        } finally {

            setLoading(false)

        }

    }

you could use some debugging to see if you used the values that you expect.

You could also try to use Moralis.executeFunction: https://docs.moralis.io/moralis-server/web3/web3#executefunction

I already checked and all the info is ok.
I used that info directly in busd testnet contract and work well.
I’ll see this Moralis.executeFunction, but I really don’t understand why my code is not working.
Just to add, this is the error:image

on what line you get that error?
await approveBusdContract.send({ from: userAddress })
or
const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call({ from: userAddress })

I think it is in
const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call({ from: userAddress })

you could try: const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call()

I tried and get the same error

can you add a console.log before that line to be sure that the error is there?

You mean like this?
If yes, the console.log don’t show up

console.log("error in call")
const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call()

I mean that line, yes
Ok, if that console.log didn’t show up, you can do something similar to previous lines to see what is the line that gives that error

console.log("1")
await approveBusdContract.send({ from: userAddress })
console.log("2")
const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call()

image

it looks like the error is on that line with approve, do you get a metamask pop up there?

No, and I’m searching what can cause that but don’t find anything conclusive.

what is the value of the second parameter, that should be a value in WEI?

const ALLOWANCE_ASKED = 10e30

I tried a console.log(0x${ALLOWANCE_ASKED.toString(16)})

And get 0x7e37be2022c090000000000000
When I go to the busd testnet contract and put that, works normaly.

image

bscscan may do a conversion, what it happens if you send it as number, without converting it to string?

you can also use Moralis.Units: https://docs.moralis.io/moralis-server/tools/moralis-units#converting-native-asset-eth-bnb-matic-etc-to-wei

also, I don’t know how you did this part:

    window.web3 = await Moralis.enableWeb3()
    let contract_instance = new web3.eth.Contract(abi, CONTRACT_ADDRESS);

I tried insert a direct valeu instead of a string and get the same error, I don’t think this is the problem.
This is the initial part and final part of the code, I think this is the last thing I have to solve to complete this game.

const ALLOWANCE_ASKED = 10e30

export const BUSDAllowanceWrapper: FC = ({ children }) => {

    const { web3 } = useMoralis()

    const userAddress = useUserAddress()

    const [ loading, setLoading ] = useState(false)

    const [ allowance, setAllowance ] = useState<number>()

    const busdContract = useMemo(() => new web3.eth.Contract(busdAbi as AbiItem[], appEnv.busdAddress), [ web3 ])

   

    async function allow() {

        setLoading(true)

        try {

            const approveBusdContract = busdContract.methods.approve(appEnv.contractAddress, `0x${ALLOWANCE_ASKED.toString(16)}`)

            await approveBusdContract.send({ from: userAddress })

            const newAllowance = await busdContract.methods.allowance(userAddress, appEnv.contractAddress).call({ from: userAddress })

            setAllowance(newAllowance)

        } finally {

            setLoading(false)

        }

    }

    useEffect(() => {

        if (userAddress) {

            setLoading(true)

            busdContract.methods.allowance(userAddress, appEnv.contractAddress).call({ from: userAddress }).then((allowed: number) => {

                setAllowance(allowed)

                console.log(allowed)

            }).finally(() => setLoading(false))

        }

    }, [ busdContract, userAddress ])

if you copy this line and put it at the beginning of that try block, does it work?

if it doesn’t work, then maybe you have to initialise web3