Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get transaction approval for Polygon network #47

Open
Jithendra-transfi opened this issue Jun 22, 2023 · 0 comments
Open

How to get transaction approval for Polygon network #47

Jithendra-transfi opened this issue Jun 22, 2023 · 0 comments

Comments

@Jithendra-transfi
Copy link

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'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant