Send token from owner account from nodejs backend (Custom ERC20 token) - javascript

I made a smart contract for token on the Polygon network. The server part is NodeJS.
Now I'm trying to implement the functionality of sending tokens to the recipient's wallet from the token creator's wallet.
The transfer method in the contract is simply taken from OpenZeppelin's ERC20 contract.
/**
* #dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
Calling the contract method from the server looks like this:
const web3Instance = new web3('providerUrl');
const tokenSmartContract = new this.web3Instance.eth.Contract(TokenABI, 'tokenAddress');
async sendTokenToWallet(amount, wallet) {
await this.tokenSmartContract.methods.transfer(wallet, amount).send();
}
But no transfer happens.
I know I need to use the sender's private key somewhere, but I can't figure out where.

Solved my own problem. Maybe my code example will help someone:
async sendTokenToWallet(amount, wallet) {
const tokenAmount = web3.utils.toWei(amount.toString(), 'ether');
const account = this.web3Instance.eth.accounts.privateKeyToAccount('0x' + 'privateKey');
this.web3Instance.eth.accounts.wallet.add(account);
this.web3Instance.eth.defaultAccount = account.address;
const nonce = await this.web3Instance.eth.getTransactionCount(account.address, 'latest');
const gasPrice = Math.floor(await this.web3Instance.eth.getGasPrice() * 1.10);
const gas = await this.tokenSmartContract.methods
.transfer(wallet, tokenAmount)
.estimateGas({ from: account.address });
await this.tokenSmartContract.methods
.transfer(wallet, tokenAmount)
.send({ from: account.address, gasPrice, nonce, gas });
}

Related

Ethers.js sending a transaction to a smart contract gives 'intrinsic gas too low'

I'm fairly new to blockchain and I'm struggling with sending a transaction to a smart contract - I'm getting intrinsic gas too low error. I've tried searching online for a solution but just couldn't solve it.
From https://stackoverflow.com/a/71261434/20453413 I understand that I need to 'allocate more gas by setting a higher gasLimit', okey, but by how much? Am I calculating gasPrice correctly?
I've deployed my contract to a goerli testnet and I'm using Infura as a provider to the blockchain.
let baseNonce = await provider.getTransactionCount(wallet.getAddress());
const gasPrice = await provider.getGasPrice()
const tx = await someContract.populateTransaction.setFoo('hello');
tx.nonce = baseNonce + 1
tx.value = ethers.utils.parseUnits('0.001', 'ether')
tx.gasPrice = gasPrice
await wallet.signTransaction(tx).then(signedTransaction => {
provider.sendTransaction(signedTransaction).then(transactionResponse => {
console.log("Our transaction response: " + transactionResponse)
})
})
provider.getGasPrice() returns a BigNumber object. Something like this { BigNumber: "21971214174" }
But you need to convert this BigNumber to a string. BigNumber object has a toString() to method. you can check the docs
tx.gasPrice = gasPrice.toString()

Sending or minting tokens to a user wallet using server side code

I have a deployed contract on Goerli and want to let users claim/mint tokens but I'm not sure how to make the user pay the gas fees since the transaction takes place on the server.
This is the working code on the server:
const amountInEther = "15000000.01"
const provider = ethers.getDefaultProvider("goerli", {
etherscan: apiKey,
})
const signer = new ethers.Wallet(privateKey, provider);
const DAI = new ethers.Contract(tokenContractAddress, ABI, signer)
const txObj = await DAI.mintTo(secondWallet, ethers.utils.parseEther(amountInEther))
return response.status(200).send({ txObj })
I'm thinking that this all needs to be on the server with api keys and privateKeys in env variables.
Any other suggestions are welcomed.
Edit - I don't think I explained very well
I am building a game in which players accumulate tokens. At one point the player can claim the tokens. I was thinking of two options:
Mint the tokens and keep them in a wallet that distributes the tokens to the players by calling transferTo in the contract.
Let players mint their earnings directly by calling mintTo in the contract.
I want to keep the claim functionality on the server because I want to query the database using a user id from a token that will be sent to the backend. I don't know how to make sure that in the client the players will mint the correct ammount that they earned.
One of the options would be:
Backend resolves the amount of tokens
Backend sings the message
Client receives the amount and message
Client sends claim transaction with the values passed
Contract validates the signature and the values
Contract mints tokens to the user
Additionally, I would add a nonce value to prevent multiple claims.
Solidity example:
contract ClaimWithSignature {
address public _owner;
mapping(address => uint8) _nonces;
function claim (uint256 amount, uint8 nonce, uint8 v, bytes32 r, bytes32 s) public returns (bool) {
address user = msg.sender;
bytes memory hashStructRaw = abi.encode(
user,
amount,
nonce
);
bytes32 hashStruct = keccak256(hashStructRaw);
address signerRecovered = ecrecover(hashStruct, v, r, s);
require(signerRecovered == _owner, "invalid signature");
require(nonce > _nonces[user], "nonce too low");
// ... mint tokens
_nonces[user] = nonce;
return true;
}
}
and the signature generation would be
import { $abiUtils } from '#dequanto/utils/$abiUtils';
import { $contract } from '#dequanto/utils/$contract';
import { $signRaw } from '#dequanto/utils/$signRaw';
export async function getTxData (userId) {
let { user, amount, nonce } = loadUserData(userId);
let encodedParams = $abiUtils.encode([
'address',
'uint256',
'uint8',
], [
user.address,
amount,
nonce
]);
let hash = $contract.keccak256(encodedParams);
let { v, r, s } = await $signRaw.signEC(hash, owner.key);
return { user, amount, nonce, v, r, s };
}
I've used an example from the test: dequanto/ecrecover.spec.ts
After the user has claimed the tokens, don't forget to update the nonce and amounts on your backend.

How to modify my code to send custom SPL Token instead of regular SOL?

I'm building a website where people log in to their phantom wallet then by clicking on a button they will send a certain amount of our custom token to one wallet.
The code shown below is working with SOL and I would like to make it work with our custom SPL token, I have the mint address of the token but I couldn't find any way to make it work. Could anyone help me?
async function transferSOL(toSend) {
// Detecing and storing the phantom wallet of the user (creator in this case)
var provider = await getProvider();
console.log("Public key of the emitter: ",provider.publicKey.toString());
// Establishing connection
var connection = new web3.Connection(
"https://api.mainnet-beta.solana.com/"
);
// I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
var recieverWallet = new web3.PublicKey("address of the wallet recieving the custom SPL Token");
var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: provider.publicKey,
toPubkey: recieverWallet,
lamports: (web3.LAMPORTS_PER_SOL)*toSend //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
}),
);
// Setting the variables for the transaction
transaction.feePayer = await provider.publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
// Request creator to sign the transaction (allow the transaction)
let signed = await provider.signTransaction(transaction);
// The signature is generated
let signature = await connection.sendRawTransaction(signed.serialize());
// Confirm whether the transaction went through or not
console.log(await connection.confirmTransaction(signature));
//Signature chhap diya idhar
console.log("Signature: ", signature);
}
I'd like to specify that people will use phantom and I cant have access to their private keys (cause it was needed in all the answers I found on internet)
You're very close! You just need to replace the web3.SystemProgram.transfer instruction with an instruction to transfer SPL tokens, referencing the proper accounts. There's an example at the Solana Cookbook covering this exactly situation: https://solanacookbook.com/references/token.html#transfer-token
You can do this with help of anchor and spl-token, which is used for dealing with custom tokens on solana.
This is a custom transfer function. You'll need the mint address of the token, wallet from which the tokens will be taken( which you get in front end when user connects wallet. Can make use of solana-web3), to address and amount.
import * as splToken from "#solana/spl-token";
import { web3, Wallet } from "#project-serum/anchor";
async function transfer(tokenMintAddress: string, wallet: Wallet, to: string, connection: web3.Connection, amount: number) {
const mintPublicKey = new web3.PublicKey(tokenMintAddress);
const {TOKEN_PROGRAM_ID} = splToken
const fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
wallet.publicKey
);
const destPublicKey = new web3.PublicKey(to);
// Get the derived address of the destination wallet which will hold the custom token
const associatedDestinationTokenAddr = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
destPublicKey
);
const receiverAccount = await connection.getAccountInfo(associatedDestinationTokenAddr.address);
const instructions: web3.TransactionInstruction[] = [];
instructions.push(
splToken.createTransferInstruction(
fromTokenAccount.address,
associatedDestinationTokenAddr.address,
wallet.publicKey,
amount,
[],
TOKEN_PROGRAM_ID
)
);
const transaction = new web3.Transaction().add(...instructions);
transaction.feePayer = wallet.publicKey;
transaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
const transactionSignature = await connection.sendRawTransaction(
transaction.serialize(),
{ skipPreflight: true }
);
await connection.confirmTransaction(transactionSignature);
}

Using web3js get coin price on uniswap and sushiswap exchange without using their api

I want to get token prices for uniswap and sushiswap exchange by using web.js
I am new to the crypto dev world, so please elaborate as much you can and if possible an example will be a lifesaver.
This is the best shot I could make... but don't know how to get V3_pool_ABI value
const JSBI = require('jsbi');
const Web3 = require('web3');
(async () => {
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/d5658a9beace4cd98d2c1216cf66b56a'));
var factoryV3 = new web3.eth.Contract(
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint24","name":"fee","type":"uint24"},{"indexed":true,"internalType":"int24","name":"tickSpacing","type":"int24"}],"name":"FeeAmountEnabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":true,"internalType":"uint24","name":"fee","type":"uint24"},{"indexed":false,"internalType":"int24","name":"tickSpacing","type":"int24"},{"indexed":false,"internalType":"address","name":"pool","type":"address"}],"name":"PoolCreated","type":"event"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"}],"name":"createPool","outputs":[{"internalType":"address","name":"pool","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"int24","name":"tickSpacing","type":"int24"}],"name":"enableFeeAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint24","name":"","type":"uint24"}],"name":"feeAmountTickSpacing","outputs":[{"internalType":"int24","name":"","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint24","name":"","type":"uint24"}],"name":"getPool","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"parameters","outputs":[{"internalType":"address","name":"factory","type":"address"},{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"int24","name":"tickSpacing","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"}],"name":"setOwner","outputs":[],"stateMutability":"nonpayable","type":"function"}],
'0x1F98431c8aD98523631AE4a59f267346ea31F984'
);
var pool_address = await factoryV3.methods.getPool('0x6b175474e89094c44da98b954eedeac495271d0f', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 3000).call();
var pool_1 = new web3.eth.Contract(V3_pool_ABI, pool_address);
var pool_balance = await pool_1.methods.slot0.call().call();
var sqrtPriceX96 = pool_balance[0];
var number_1 =JSBI.BigInt(sqrtPriceX96 *sqrtPriceX96* (1e18)/(1e18)/JSBI.BigInt(2) ** (JSBI.BigInt(192)));
})();
The answer would be using oracle contracts, many defi apps depend on there price also.
How you would get it in solidity: (link to blog)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Kovan
* Aggregator: ETH/USD
* Address: 0x9326BFA02ADD2366b30bacB125260Af641031331
*/
constructor() {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return price;
}
}
This can be replicated in js using the web3 library, just get this contract and use its functionality.
Each pool is created from a factory contract, and they all use the same ABI.
You can lookup any of the valid pool addresses, for example the code from your question results in 0xC2e9F25Be6257c210d7Adf0D4Cd6E3E881ba25f8, and its ABI is already published.
var V3_pool_ABI = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount0","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"}],"name":"Collect","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint128","name":"amount0","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"amount1","type":"uint128"}],"name":"CollectProtocol","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paid1","type":"uint256"}],"name":"Flash","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint16","name":"observationCardinalityNextOld","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"observationCardinalityNextNew","type":"uint16"}],"name":"IncreaseObservationCardinalityNext","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"indexed":false,"internalType":"int24","name":"tick","type":"int24"}],"name":"Initialize","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"int24","name":"tickLower","type":"int24"},{"indexed":true,"internalType":"int24","name":"tickUpper","type":"int24"},{"indexed":false,"internalType":"uint128","name":"amount","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint8","name":"feeProtocol0Old","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol1Old","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol0New","type":"uint8"},{"indexed":false,"internalType":"uint8","name":"feeProtocol1New","type":"uint8"}],"name":"SetFeeProtocol","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"int256","name":"amount0","type":"int256"},{"indexed":false,"internalType":"int256","name":"amount1","type":"int256"},{"indexed":false,"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"},{"indexed":false,"internalType":"int24","name":"tick","type":"int24"}],"name":"Swap","type":"event"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount","type":"uint128"}],"name":"burn","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount0Requested","type":"uint128"},{"internalType":"uint128","name":"amount1Requested","type":"uint128"}],"name":"collect","outputs":[{"internalType":"uint128","name":"amount0","type":"uint128"},{"internalType":"uint128","name":"amount1","type":"uint128"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint128","name":"amount0Requested","type":"uint128"},{"internalType":"uint128","name":"amount1Requested","type":"uint128"}],"name":"collectProtocol","outputs":[{"internalType":"uint128","name":"amount0","type":"uint128"},{"internalType":"uint128","name":"amount1","type":"uint128"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeGrowthGlobal0X128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeGrowthGlobal1X128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"flash","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16","name":"observationCardinalityNext","type":"uint16"}],"name":"increaseObservationCardinalityNext","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"liquidity","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxLiquidityPerTick","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint128","name":"amount","type":"uint128"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"mint","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"observations","outputs":[{"internalType":"uint32","name":"blockTimestamp","type":"uint32"},{"internalType":"int56","name":"tickCumulative","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityCumulativeX128","type":"uint160"},{"internalType":"bool","name":"initialized","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32[]","name":"secondsAgos","type":"uint32[]"}],"name":"observe","outputs":[{"internalType":"int56[]","name":"tickCumulatives","type":"int56[]"},{"internalType":"uint160[]","name":"secondsPerLiquidityCumulativeX128s","type":"uint160[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"positions","outputs":[{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256"},{"internalType":"uint128","name":"tokensOwed0","type":"uint128"},{"internalType":"uint128","name":"tokensOwed1","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFees","outputs":[{"internalType":"uint128","name":"token0","type":"uint128"},{"internalType":"uint128","name":"token1","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint8","name":"feeProtocol0","type":"uint8"},{"internalType":"uint8","name":"feeProtocol1","type":"uint8"}],"name":"setFeeProtocol","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"slot0","outputs":[{"internalType":"uint160","name":"sqrtPriceX96","type":"uint160"},{"internalType":"int24","name":"tick","type":"int24"},{"internalType":"uint16","name":"observationIndex","type":"uint16"},{"internalType":"uint16","name":"observationCardinality","type":"uint16"},{"internalType":"uint16","name":"observationCardinalityNext","type":"uint16"},{"internalType":"uint8","name":"feeProtocol","type":"uint8"},{"internalType":"bool","name":"unlocked","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"}],"name":"snapshotCumulativesInside","outputs":[{"internalType":"int56","name":"tickCumulativeInside","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityInsideX128","type":"uint160"},{"internalType":"uint32","name":"secondsInside","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"bool","name":"zeroForOne","type":"bool"},{"internalType":"int256","name":"amountSpecified","type":"int256"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[{"internalType":"int256","name":"amount0","type":"int256"},{"internalType":"int256","name":"amount1","type":"int256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int16","name":"","type":"int16"}],"name":"tickBitmap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tickSpacing","outputs":[{"internalType":"int24","name":"","type":"int24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int24","name":"","type":"int24"}],"name":"ticks","outputs":[{"internalType":"uint128","name":"liquidityGross","type":"uint128"},{"internalType":"int128","name":"liquidityNet","type":"int128"},{"internalType":"uint256","name":"feeGrowthOutside0X128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthOutside1X128","type":"uint256"},{"internalType":"int56","name":"tickCumulativeOutside","type":"int56"},{"internalType":"uint160","name":"secondsPerLiquidityOutsideX128","type":"uint160"},{"internalType":"uint32","name":"secondsOutside","type":"uint32"},{"internalType":"bool","name":"initialized","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}];

Transaction to Uniswap failing on Ropsten TestNet

I am trying to create a transaction on Uniswap programatically, the flow nd code seems to be there, but for whatever reason the transaction fails on Ropsten for "Warning! Error encountered during contract execution [Reverted]". I am using javascript along with Nodejs as my server. Any suggestions on why it is failing? Code below:
const { ethers } = require("ethers");
const walletAddress = "My_own_address";
const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const uniErc20Address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
const uniswapRouterAbi = [
"function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)",
];
async function buyListedTokenWithEth(
amountEthFloat,
uniswapRouterAddress,
provider
) {
const wallet = new ethers.Wallet(Buffer.from(privateKey, "hex"));
const signer = wallet.connect(provider); //provider is Infura https ROPSTEN url
const exchangeContract = new ethers.Contract(
uniswapRouterAddress,
uniswapRouterAbi,
signer
);
const ethAmount = ethers.utils.parseEther("0.1");
const tx = await exchangeContract.swapExactTokensForTokens(
ethAmount,
0,
[wethErc20Address, uniErc20Address],
walletAddress,
createDeadline(), // Math.floor(Date.now() / 1000) + 20
createGasOverrides() // { gasLimit: ethers.utils.hexlify(300000), gasPrice: gasPriceWei }
);
console.log("https://ropsten.etherscan.io/tx/" + tx.hash);
}
The line:
const tx = await exchangeContract.swapExactTokensForTokens(
Should be:
const tx = await exchangeContract.methods.swapExactTokensForTokens(
The error is correct. Your transaction should not be successful.
Go to the uniswap ropsten router address. https://ropsten.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#readContract
There is a function to view WETH address for ropsten.
This is what it returns
0xc778417e063141139fce010982780140aa0cd5ab //Uniswap-router-factory-weth address
instead of
const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
You are asking the uniswap router to use the WETH address you list instead of the actual WETH address that it uses. Think about it this way. Why would an address with 0 liquidity on uniswap work on Uniswap?
Best to write some function to check liquidity before assuming it exist. No, I will not tell you how to do that.

Categories