Uniswap / examples

120 stars 147 forks source link

How to get transaction approval for Polygon network #47

Open Jithendra-transfi opened 1 year ago

Jithendra-transfi commented 1 year ago

const WMATIC_TOKEN = new Token( ChainId.POLYGON, "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270", 18, "WMATIC", "Wrapped Matic" );

const USDC_TOKEN = new Token( ChainId.POLYGON, "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", 6, "USDC", "USD Coin" );

const CurrentConfig = { rpc: { mainnet: "https://polygon-mainnet.infura.io/v3/2945612d95e74e4f82e05d4fbf00db6c", }, tokens: { in: USDC_TOKEN, amountIn: 25, out: WMATIC_TOKEN, poolFee: FeeAmount.MEDIUM, }, };

const sendTransaction = async (transaction) => { if(transaction.value){ transaction.value = BigNumber.from(transaction.value) }

const txRes = await wallet.sendTransaction(transaction);  // getting error over here
if(txRes){
    console.log('Hurrey')
}else{
    console.log('Hell')
}
let receipt = null
const provider = getProvider();
while(receipt === null){
 try {
     receipt = await provider.getTransactionReceipt(txRes.hash)
     if(receipt === null){
         continue
     }
 } catch (error) {
     console.log(`Receipt error:`, error)
   break
 }
}
if(receipt){
 console.log('Transaction Sent')
 return true;
}else{
 console.log('Transaction Failed')
 return null;
}

}

const getTokenTransferApproval = async (token) => { const provider = getProvider(); const address = getWalletAddress();

if (!provider || !address) { console.log("please check provider or walletAddress"); } try { const tokenContract = new ethers.Contract( token.address, ERC20_ABI, provider );

const transaction = await tokenContract.populateTransaction.approve(
    SWAP_ROUTER_ADDRESS,
    fromReadableAmount(
      TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER,
      token.decimals
    ).toString()
  )

const resp = await sendTransaction({
  ...transaction,
  from: address,
});

return resp;

} catch (e) { console.log(e); console.log('approval failed') } };

const tokenApproval = await getTokenTransferApproval(CurrentConfig.tokens.in);

getting error as follow:

Error: transaction underpriced at /Users/jithendrakumar/Desktop/uniswap/new-uniswap1/node_modules/@ethersproject/providers/lib/json-rpc-batch-provider.js:72:41 at Array.forEach () at /Users/jithendrakumar/Desktop/uniswap/new-uniswap1/node_modules/@ethersproject/providers/lib/json-rpc-batch-provider.js:69:27 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: -32000, data: undefined, transaction: { type: 2, chainId: 137, nonce: 21, maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true }, maxFeePerGas: BigNumber { _hex: '0x6a0d032ab4', _isBigNumber: true }, gasPrice: null, gasLimit: BigNumber { _hex: '0xb41d', _isBigNumber: true }, to: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', value: BigNumber { _hex: '0x00', _isBigNumber: true }, data: '0x095ea7b3000000000000000000000000e592427a0aece92de3edee1f18e0157c0586156400000000000000000000000000000000000000000001a784379d99db42000000', accessList: [], hash: '0x8a40ff615225b916935ceb17e2098b7ff178ef1d24b5ff1ad0bb10d7dd1c693f', v: 0, r: '0x0ddf0e88ae53876d4d2d70a556bc25bffab46533ac19ebf75b8150c349c74ebd', s: '0x6c2ec585a1c12c094bfc7c75b735749cbae97e3056e70de0f1837ae0aba8ee3b', from: '0xaDe56237C3b7d4E000641eA0484dC9B44B9d366f', confirmations: 0 }, transactionHash: '0x8a40ff615225b916935ceb17e2098b7ff178ef1d24b5ff1ad0bb10d7dd1c693f' }