1

I am trying to set USDT(ERC20) token allowance for SWA (smart wallet account) so that the account can interact with an external contract and send USDT to the contract.

I am using Alchemy SDK to create a SWA and ethers.js (v5) to set allowance.

import {
    LightSmartContractAccount,
    getDefaultLightAccountFactoryAddress,
} from "@alchemy/aa-accounts";
import { AlchemyProvider } from "@alchemy/aa-alchemy";
import { LocalAccountSigner, type Hex } from "@alchemy/aa-core";
import { polygon } from "viem/chains";
import { ethers } from "ethers";
import abi from "./erc20.json";

const chain = polygon;

const PRIVATE_KEY = "PRIVATE_KEY_GOES_HERE" as Hex;
const owner = LocalAccountSigner.privateKeyToAccountSigner(PRIVATE_KEY);

// ERC20 token contract details
const erc20Abi = abi; // ERC20 contract ABI
const erc20Address = "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97";
const allowanceAmount = ethers.utils.parseUnits("1000", "ether"); // Example allowance

// Create a provider to send user operations from your smart account
const provider = new AlchemyProvider({
// get your Alchemy API key at https://dashboard.alchemy.com
apiKey: "ALCHEMY_API_KEY",
chain,
}).connect(
    (rpcClient) =>
        new LightSmartContractAccount({
        rpcClient,
        owner,
        chain,
        factoryAddress: getDefaultLightAccountFactoryAddress(chain),
        })
);

(async () => {
    const scwAddress = await provider.getAddress()
    console.log("Smart Account Address: ", scwAddress); // Log the smart account address

    // Create an instance of the ERC20 contract
    const rpcUrl = 'https://rpc.ankr.com/polygon';
    const etherProvider = new ethers.providers.JsonRpcProvider(rpcUrl);
    const erc20Contract = new ethers.Contract(erc20Address, erc20Abi, etherProvider);
    
    //Prepare the approve transaction
    const approveTx = await erc20Contract.populateTransaction.approve(scwAddress, allowanceAmount);

    // Retrieve allowance amount below
    const allowance = await erc20Contract.allowance(scwAddress, erc20Address);
    console.log("Allowance: ", allowance.toString());
})();

Perhaps, this error occurs because I have a wrong USDT address or abi.json.

USDT_address = "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97";

And here is the abi json that I found in one of public repositories.

[
    {
      "constant": true,
      "inputs": [],
      "name": "name",
      "outputs": [{ "name": "", "type": "string" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        { "name": "_spender", "type": "address" },
        { "name": "_value", "type": "uint256" }
      ],
      "name": "approve",
      "outputs": [{ "name": "", "type": "bool" }],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "totalSupply",
      "outputs": [{ "name": "", "type": "uint256" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        { "name": "_from", "type": "address" },
        { "name": "_to", "type": "address" },
        { "name": "_value", "type": "uint256" }
      ],
      "name": "transferFrom",
      "outputs": [{ "name": "", "type": "bool" }],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "decimals",
      "outputs": [{ "name": "", "type": "uint8" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [{ "name": "_owner", "type": "address" }],
      "name": "balanceOf",
      "outputs": [{ "name": "balance", "type": "uint256" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "symbol",
      "outputs": [{ "name": "", "type": "string" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        { "name": "_to", "type": "address" },
        { "name": "_value", "type": "uint256" }
      ],
      "name": "transfer",
      "outputs": [{ "name": "", "type": "bool" }],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [
        { "name": "_owner", "type": "address" },
        { "name": "_spender", "type": "address" }
      ],
      "name": "allowance",
      "outputs": [{ "name": "", "type": "uint256" }],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    { "payable": true, "stateMutability": "payable", "type": "fallback" },
    {
      "anonymous": false,
      "inputs": [
        { "indexed": true, "name": "owner", "type": "address" },
        { "indexed": true, "name": "spender", "type": "address" },
        { "indexed": false, "name": "value", "type": "uint256" }
      ],
      "name": "Approval",
      "type": "event"
    },
    {
      "anonymous": false,
      "inputs": [
        { "indexed": true, "name": "from", "type": "address" },
        { "indexed": true, "name": "to", "type": "address" },
        { "indexed": false, "name": "value", "type": "uint256" }
      ],
      "name": "Transfer",
      "type": "event"
    }
  ]

1 Answer 1

1

You can call ERC20 approve function can approve sudt cast limit , allowance is query cureent address approve.

//call auth limit
export const callAuthorizedLimit = async (accountAddress: EthereumAddress, erc20Address: EthereumAddress, amount: bigint, authorizeAddress: EthereumAddress) => {
    const client = await walletClient()
    const { request } = await exportPublicClient.simulateContract({
        account: accountAddress,
        address: erc20Address,
        // @ts-ignore 
        abi: erc20Address.toLocaleLowerCase() === contractAddress.aewTokenAddress.toLocaleLowerCase() ? aewTokenAbi : erc20ABI,
        functionName: "approve",
        args: [authorizeAddress, amount]
    })
    return client?.writeContract(request)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.