How to correctly use a BUSD liquidity pool - smartcontracts

We're working on a BEP20 token and given the current market, we'd like to use a BUSD LP.
The only problem is that the reflections and other functions aren't compatible.
Transactions go through successfully, but the reflections and other functions aren't executed / distributed.
Some places I think might help:
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
Might changing WETH to something else help?
Or maybe here:
IUniswapV2Router02 pancakeswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
_pancakeswapV2LiquidityPair = IUniswapV2Factory(pancakeswapV2Router.factory())
.createPair(address(this), pancakeswapV2Router.WETH());
_pancakeswapV2Router = pancakeswapV2Router;
emit Transfer(address(0), _msgSender(), _tTotal);
}
function addLiquidity(uint256 tokenAmount, uint256 bnbAmount) private {
// Approve token transfer to cover all possible scenarios
_approve(address(this), address(_pancakeswapV2Router), tokenAmount);
// Adds the liquidity and gives the LP tokens to the owner of this contract
// The LP tokens need to be manually locked
_pancakeswapV2Router.addLiquidityETH{value: bnbAmount}(
address(this),
tokenAmount,
0, // Take any amount of tokens (ratio varies)
0, // Take any amount of BNB (ratio varies)
owner(),
block.timestamp.add(300)
);
}
Thank you in advance for any help!

Related

Ethereum lottery smart contract reverts due to insufficient funds. Where does my code consumes that much gas?

I am experimenting with solidity and I faced an issue for what I could not find a solution.
The program should let addresses buy ticket at a preset price, and the owner can start the "roll the dice" function which randomly selects the winner and transfer the funds to that address.
I beleive that this program would be easier with mapping instead of array, but getting experience with array was the main purpose of this program.
The error happens when the user calls buyTicket function. Based on the response I beleive the contract comsumes too much gas. Can someone tell me why it doesnt work? I appreciate any other helping comment regarding the rest of the code:)
Thanks in advance!
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Lottery {
constructor () {
owner = msg.sender;
}
address[] public Players;
address private owner;
uint public ticketPrice;
uint public price;
uint public nonce;
uint public variations;
address payable winner;
bool hasTicketAnswer;
event Winner(address _winner);
event PriceSet(uint _setPrice);
event TicketBought();
function setTicketPrice(uint _ticketPrice) public {
require(msg.sender == owner, "Only Owner...");
ticketPrice = _ticketPrice;
emit PriceSet(_ticketPrice);
}
function hasTicket(address _sender) private returns(bool) {
hasTicketAnswer = false;
for (uint i = 0; i < Players.length; i++) {
if (Players[i] == _sender) hasTicketAnswer = true;
}
return hasTicketAnswer;
}
function buyTicket() external payable {
require(ticketPrice > 0, "Price did not set, be patient...");
require(hasTicket(msg.sender) == false, "You cannot have two tickets...");
require(msg.sender.balance <= ticketPrice, "Insufficient funds...");
payable(address(this)).transfer(ticketPrice);
Players.push(address(msg.sender));
price += msg.value;
emit TicketBought();
}
function checkBalance() public view returns(uint) {
return address(this).balance;
}
function rollTheDice() public payable {
variations = Players.length;
winner = payable(Players[uint(keccak256(abi.encodePacked(msg.sender, nonce, block.timestamp))) % variations]);
winner.transfer(price);
emit Winner(winner);
}
receive () external payable {
}
}
Besides probably finding the problem, I've read some things that I'd like to comment on.
Your problem
The reason why you're getting the "Insufficient funds" error is because the condition is returning false. You're asking the msg.sender balance to be less than or equal (<=) to ticketPrice, when it should be more than or equal (>=).
Let's say Alice has a balance of 0.05 ETH and interacts with the contract whose ticket price is 0.001 ETH...
require(0.05 ETH <= 0.001 ETH) // Reverting...
Observations
I'm curious if you're intentionally coding the buyTicket function in that way. What it actually does is checking if the msg.sender has enough ETH to buy a ticket in its wallet, which doesn't mean that the user is effectively sending ETH in the transaction (the amount of wei sent in the transaction can be checked with msg.value, you can read more about it here).
My last observation is the payable(address(this)).transfer(ticketPrice) line of code, because it's not necessary to do so, once a payable function receives ETH, it is saved into the contract... In that line you're just making the Bob's contract to send ETH to the Bob's contract, which just wastes gas without reason
I hope I've helped with you and if I wasn't completely clear in any thing I've said don't doubt in asking me

There is a way to transfer / mint tokens without ERC20 owner approval method?

I trying to understand how crowdsale work in this way of buying tokens.
The part of send ether to a contract is ok, but the part of token transfer is still dark for me.
I have a ERC20Mintable token, in the latest version of openzeppelin.
My crowdsale contract will have thousands and thousands of buyers. In most tutorials, they teach transfering tokens with transferFrom, but that requires the approval of ERC20 owner correct ? Is what most of tutorials show. I can mint either, probably because only owner can mint tokens.
My question is: there is a method that users can buy tokens without any action of the ERC20 owner?
Thanks!
Owner approval is needed for transferFrom function. Because with this function you are allowing third-party account transfer from your account to someone.
Let's say I want to transfer token from your account to my brother's account. To be able to do this, you have to give permission first and this permission is stored in a mapping. If you allow me to transfer a specific amount from your account, you first add my account into this mapping
// my address is allowed to transfer token to other address
mapping(address=>mapping(address=>uint256)) allowed;
with approve function.
function approve(address _spender, uint256 _value) public override returns (bool success){
// you are calling this. so you are the msg.sender
// first we are checking if you have enough token to be transferred
require(tokenBalances[msg.sender]>=_value,"insufficient token");
// then you register my account with the _value
allowed[msg.sender][_spender]=_value;
// if in the future there is a dispute, we can check those events for verification
emit Approval(msg.sender,_spender,_value);
return true;
}
This where owner approval used. If you want to transfer money from your account to another account, you use the transfer function:
function transfer(address _to, uint256 _value) public override returns (bool success){
require(tokenBalances[msg.sender]>=_value,"you do not have enough tokens");
tokenBalances[msg.sender]-=_value;
tokenBalances[_to]+=_value;
emit Transfer(msg.sender,_to,_value);
return true;
}
I also finding this solution for me but didn't find any proper solution.
however find a solution for now.
create a function that is payable and pass amount(how much buyer buy) and make a keccak hash with (sender , value and amount) and store in a map and send transfer receive eth to _admin address.
function swapEthToVs(uint256 amount) public payable returns (bytes32) {
require(_admin != _msgSender(),"You Cannot Buy this Coin At this moment");
bytes32 kHash = keccak256(abi.encodePacked(msg.value,amount,_msgSender()));
swapHash[_origin()] = kHash;
payable(address(_admin)).transfer(msg.value);
return kHash;
}
then create api that take (sender , amount ,contractOwner) and call another function with contractOwnerSigner
function verifySwapHash(uint256 eth,address to,uint256 amount) public returns (bool) {
require(swapHash[to] == keccak256(abi.encodePacked(eth, amount, to)),"Invalid hash no trace found");
transfer(to, amount);
delete swapHash[to];
return true;
}
I know this is risky but i didn't found any solution. if you found any solution please describe solution.

Token balance check is not working in Solidity smart contract

here is my code snippet and i have no idea why its not working
function Testdeposit(address _assetAddress) public returns (uint256 status)
{
//IERC20 erc20 = IERC20(_assetAddress);
//erc20.transferFrom(senderAddress, address(this), _amount));
//uint256 amount = erc20.allowance(senderAddress, address(this));
uint256 amount = IERC20(_assetAddress).balanceOf(address(this));
return amount;
}
i am using a standard IERC20 interface. just it dsnt have an event to emit. i assume it to return me 0 if there is no balance but its gvng me erro. Transcation not going through.
Any suggestions??
It is not working because you may have not overridden the IERC20 functions. At least as they should. I would highly recommend using the ERC20 standard instead, just for how easy it is to implement.

Burning Deployed ERC Tokens In an NFT Mint Function - Compiles, but Transaction Fails

I am very new to Solidity, and have recently been working on trying to learn the ropes. For reference, I have been using code from this video (https://www.youtube.com/watch?v=tBMk1iZa85Y) as a primer after having gone through the basic crypto zombies tutorial series.
I have been attempting to adapt the Solidity contract code presented in this video (which I had functioning just fine!) to require a Burn of a specified amount of an ERC-20 token before minting an NFT as an exercise for myself. I thought I had what should be a valid implementation which compiled in Remix, and then deployed to Rinkeby. I call the allowAccess function in Remix after deploying to Rinkeby, and that succeeds. But, when I call the mint function with the two parameters, I get: "gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted."
If I still send the transaction, metamask yields "Transaction xx failed! Transaction encountered an error.".
I'm positive it has to do with "require(paymentToken.transfer(burnwallet, amounttopay),"transfer Failed");", though I'm not sure what's wrong. Below is my entire contract code. I'm currently just interacting with the Chainlink contract on Rinkeby as my example, since they have a convenient token faucet.
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
contract myNFTwithBurn is ERC721, Ownable {
address externalTokenAddress = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709; //Token Type to burn on minting
uint256 amounttopay = 5; //number of these tokens to burn
IERC20 paymentToken = IERC20(externalTokenAddress); //my code: create an interface of the external token
address burnwallet = 0x000000000000000000000000000000000000dEaD; //burn wallet
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
using Strings for uint256;
// Optional mapping for token URIs
mapping (uint256 => string) private _tokenURIs;
// Base URI
string private _baseURIextended;
constructor() ERC721("NFTsWithBurn","NWB") {
}
function setBaseURI(string memory baseURI_) external onlyOwner() {
_baseURIextended = baseURI_;
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
}
function _baseURI() internal view virtual override returns (string memory) {
return _baseURIextended;
}
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
// If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
return string(abi.encodePacked(base, tokenId.toString()));
}
function allowAccess() public
{
paymentToken.approve(address(this), 5000000); //This is my attempt to allow the contract access to the user's external tokens, in this case Chainlink (paymentToken)
}
function mintItem(address to, string memory tokenURI)
public
onlyOwner
returns (uint256)
{
require(paymentToken.transfer(burnwallet, amounttopay),"transfer Failed"); //Try to transfer 5 chainlink to the burn wallet
_tokenIds.increment();
uint256 id = _tokenIds.current();
_mint(to, id);
_setTokenURI(id, tokenURI);
return id;
}
}
If anybody can at least point me to what I'm doing completely wrong in the code that I've added, please do! TIA!
I'm not sure why are you trying to burn link in order to mint and nft but first check if the link code does not have a require that check if the destination address is the burn address if it has then burn the link is not possible and you should use any other erc20 maybe your own erc20, also your contract probably does not have any link and if you want to transfer the link from the user you should do this in the contract paymentToken.transferFrom(msg.sender,destinationAddress,amount) and if the user previously approve your contract you will able to send the tokens, and i suppose that the purpose of the allowAccess function is to make the user approve the contract to move the tokens that will never work, the approve function let's anyone that call it approve any address to move an amount of tokens, the thing is that to know who is approving to let other to move the tokens the function use msg.sender to explain how this work take a look at this example
let's say that your contract is the contract A and the link contract is the contract B
now a user call allowAccess in the contract A, so here the msg.sender is the user because they call the function
now internally this function call approve on contract B, here the contract A is the msg.sender, because the contract is who call the function
so what allowAccess is really doing is making the contract approving itself to move their own tokens that I assume it doesn't have

BEP20 Tokens are not being swapped for BNB and sent to charity wallet

I'm trying to create a bep20-token where a percentage of the transaction volume is converted into BNB, and subsequently sent to a designated charity wallet to store the BNB for future donations. Another feature of this token is that a another part of the transaction is also swapped for BNB and locked together with the token in the liquidity pool. This last feature is working, however, the wallet I set up as the charity wallet is not receiving BNB. I have already set up a liquidity pool with the test tokens and minor amount of BNB thats how I learned its not doing what it was designed for. This token is a fork of YUMMY and the complete contract can be viewed here: https://bscscan.com/address/0x05f2df7b3d612a23fe12162a6c996447dce728a5#code
function sendBNBToCharity(uint256 amount) private {
swapTokensForEth(amount);
_charityWalletAddress.transfer(address(this).balance);
}
function _setCharityWallet(address payable charityWalletAddress) external onlyOwner() {
_charityWalletAddress = charityWalletAddress;
}
function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
// split the contract balance into thirds
uint256 halfOfLiquify = contractTokenBalance.div(4);
uint256 otherHalfOfLiquify = contractTokenBalance.div(4);
uint256 portionForFees = contractTokenBalance.sub(halfOfLiquify).sub(otherHalfOfLiquify);
// capture the contract's current ETH balance.
// this is so that we can capture exactly the amount of ETH that the
// swap creates, and not make the liquidity event include any ETH that
// has been manually sent to the contract
uint256 initialBalance = address(this).balance;
// swap tokens for ETH
swapTokensForEth(halfOfLiquify); // <- this breaks the ETH -> HATE swap when swap+liquify is triggered
swapTokensForEth(portionForFees);
// how much ETH did we just swap into?
uint256 newBalance = address(this).balance.sub(initialBalance);
// add liquidity to uniswap
addLiquidity(otherHalfOfLiquify, newBalance);
sendBNBToCharity(portionForFees);
emit SwapAndLiquify(halfOfLiquify, newBalance, otherHalfOfLiquify);
}
function swapTokensForEth(uint256 tokenAmount) private {
// generate the uniswap pair path of token -> weth
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
// make the swap
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0, // accept any amount of ETH
path,
address(this),
block.timestamp
);
}
Your swapTokensForEth function is swapping to WETH, not to ETH. Wrapped ETH is a coin that holds ETH and makes it act like an ERC20 token, so that it can be used on Uniswap and other place.
To get ETH out of your WETH, you'll need call WETH.withdraw(amount), which will convert your WETH back to ETH.