简体   繁体   中英

.allowance method for importing erc20 token to metamask

I was trying out the.allowance method to import erc20 token in my case PKS to metamask. The issue is I don't get the metamask popup for importing the erc20 token but I get the balances PKS token for the particular account without importing.

need this output before fetching balances: 在此处输入图像描述 Is there any way to adjust the code it should first ask for import and then fetch the balance.

here's my code so far and I am calling getErcBal in useEffect:

async function getErcBal(){
        var WEB3 = await connection();
        var currAddr = WEB3.address;
        var web3 = WEB3.web3Conn;
      
        if(WEB3.isMeta){
          setuseraddress(currAddr);
          setContractAdr(dethContractAddr);
            if(dethContractAddr){
                var dethContract = new web3.eth.Contract(DETH, dethContractAddr);
                var getAllowance = await dethContract.methods
                                    .allowance(currAddr, dethContractAddr)
                                    .call();
                }
                var tokenDethBln = await dethContract.methods.balanceOf(currAddr).call();
            }
            var PksVal = 0;
              if (tokenDethBln && tokenDethBln > 0) {
                PksVal = tokenDethBln / 1000000000000000000;
              }
            setPksbalance(PksVal.toFixed(4))
         
          }
      
      }

Allowance method is not for importing funds. Allowance function uses two addresses, first one is owner address, and second one is spender address. If you previously approved someone to use your funds then it will return some value, otherwise it will give zero.

If you want to import token to metamask, choose the account and you will see the import token option:

在此处输入图像描述

Get the ERC20 token address and paste it to the field. It will automatically populate the "symbol" field:

在此处输入图像描述

Click on "Add Custom Token" then you will get next screen:

在此处输入图像描述

click on "Import Tokens"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM