0

I am encountering an error message SPENDER_DOES_NOT_HAVE_ALLOWANCE while attempting to transfer HTS tokens from my wallet using a smart contract. Although I deployed my smart contract some time ago on the network, it appears to be malfunctioning as I am now receiving a CONTRACT_REVERT_EXECUTED message each time I call my function.

I am sharing the code for my Solidity contract below and would appreciate your help in identifying the issue:

function myTransferToken(address token, address to, int64 amount) external {
        int responseCode = HederaTokenService.transferToken(
            token,
            msg.sender,
            to,
            int64(amount)
        );
        require(
            responseCode == HederaResponseCodes.SUCCESS,
            "Failed to transfer token"
        );
    }

Thank you for your assistance.

Pathorn Teng
  • 411
  • 6

1 Answers1

0

Due to the security model change in version 0.35.2 (https://docs.hedera.com/hedera/networks/release-notes/services#security-updates-hedera-smart-contract-service-security-model-changes) , users are now required to explicitly allow smart contracts to transfer tokens out of their wallets. Before a smart contract can initiate a transfer from the user's wallet, the user must first call AccountAllowanceApproveTransaction and specify the smart contract address as the spender. This will allow the smart contract to execute the transfer without encountering any errors. The example of how you can call AccountAllowanceApproveTransaction is below.

export async function ftAllowanceFcn(
 tId,
 owner,
 spender,
 allowBal,
 pvKey,
 client
) {
 const allowanceTx = new AccountAllowanceApproveTransaction()
   .approveTokenAllowance(tokenId, owner, spender, allowBal)
   .freezeWith(client);
 const allowanceSign = await allowanceTx.sign(pvKey);
 const allowanceSubmit = await allowanceSign.execute(client);
 const allowanceRx = await allowanceSubmit.getReceipt(client);
 return allowanceRx;
}
Pathorn Teng
  • 411
  • 6