Sending ERC20 tokens using the transfer function - cryptography

I'm pretty new to programing in solidity and I'm currently trying to run a simple smart contract in Remix, seen bellow:
pragma solidity ^0.8.0;
import "github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
contract Swap {
address public owner;
uint256 public balance;
event TransferReceived(address _from, uint _amount);
event TransferSent(address _from, address _destAddr, uint _amount);
constructor() {
owner = msg.sender;
}
receive() payable external {
balance += msg.value;
emit TransferReceived(msg.sender, msg.value);
}
function withdraw(uint amount, address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
destAddr.transfer(amount);
balance -= amount;
emit TransferSent(msg.sender, destAddr, amount);
}
function transferERC20(IERC20 token, address to, uint256 amount) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint256 erc20balance = token.balanceOf(address(this));
require(amount <= erc20balance, "balance is low");
token.transfer(to, amount);
emit TransferSent(msg.sender, to, amount);
}
}
While I can successfully send BNB and call the withdraw function giving the value sent and my wallet address in the BSC testnet, I'm having issues when running the transferERC20 function. The only output that I get when calling this method is the following message:
Gas estimation errored with the following message (see below). The
transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution
reverted" }
I've tried several different addresses that I found in the testnet.bscscan website for BNB while making sure that the contract had enough funds for transfering, but I had no success.
Can someone suggest what might be going wrong in my contract/setup? Am I making this transfer properly?

fix constrcutor
constructor() {
// payable allows payment of ether with a call.
owner = payable(msg.sender);
}
make sure those require statements are satisfied
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
check that you are connected to correct network

Related

Problem sending eth from contract to contract

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT
contract Client {
address payable private hub;
address payable public owner;
uint256 public balance;
constructor(address payable _hub) {
hub = _hub;
owner = payable(msg.sender);
}
receive() payable external {
balance += msg.value;
}
function withdraw(address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint amount = address(this).balance;
destAddr.transfer(amount);
}
function start() public payable {
require(msg.sender == owner, "Only owner can start the process");
uint amount = address(this).balance;
hub.transfer(amount);
balance = 0;
}
function setHub(address payable _new) public {
require(msg.sender == owner, "Only owner can change address");
hub = _new;
}
}
Hi i have a problem, when I deploy this contract and put as input (hub) the other contract, then send eth to this contract, i call the "start" function and throw a gas estimation error.
Someone who can help me pls...
I'm expecting that calling the start function fund will be sent to the other contract that also have a function for receiving eth
receive() payable external {
balance += msg.value;
}
You are getting that gas estimation error because your receive function on the second contract is not empty and the transaction does not have enough gas to execute the code. transfer only sends the amount of gas necessary to execute a transfer of ether (21,000 gas) and nothing more.
You can use call instead of transfer to send ether and set the amount of gas that you want to send. transfer is actually no longer recommended for sending ether.

How to send ETH to smart contract?

I want an address to send ETH to the smart contract in the RSVP function.
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract MeetingCreation {
struct Meeting {
address meeting_creator;
uint256 meetingID;
string name;
string description;
uint256 start_date;
uint256 end_date;
uint256 max_tickets;
uint256 ticket_price;
uint256 current_tickets_sold;
address[] attendees;
}
function sendETHtoContract(uint256 j) public payable {
}
function RSVP (uint256 MEETINGID) public payable {
//get the Meeting ID
Meeting storage m = meetings[MEETINGID];
require(m.current_tickets_sold < m.max_tickets,
"No more tickets available"
);
require(msg.sender.balance >= m.ticket_price,
"Not enough funds"
);
console.log("funds before paying", msg.sender.balance);
console.log("funds in smart contract before paying", address(this).balance);
//Attempt #1 To send ETH to contract
sendETHtoContract(m.ticket_price);
//Attempt #2 To send ETH to contract
(bool success, ) = address(this).call{value: m.ticket_price}("");
//Attempt #3 to send ETH to contract
payable(address(this).transfer(msg.value));
console.log("funds AFTEr paying", msg.sender.balance);
console.log("funds in smart contract AFTER paying", address(this).balance);
address[] storage adr;
adr = m.attendees;
adr.push(msg.sender);
m.current_tickets_sold += 1;
}
Both sendETHtoContract and (bool, success) result in not sending any ETH when I tried 3 different addresses.
funds before paying 9999949127225257315696
funds in smart contract before paying 0
funds AFTEr paying 9999949127225257315696
funds in smart contract AFTER paying 0
funds before paying 9999955000801324655336
funds in smart contract before paying 0
funds AFTEr paying 9999955000801324655336
funds in smart contract AFTER paying 0
When I tried using the transfer function, this is the error I get when the contract tries to compile:
"send" and "transfer" are only available for objects of type "address payable", not "address".
I assume that you can delete your attempt for the simple reason that when you add payable modifier, it handle the transfer about ETH from address to smart contract and viceversa.
Said this, I think that you're not enhancing msg.value field. If you using remix IDE, before call RSVP function go into DEPLOY & RUN TRANSACTIONS section and put into VALUE textbox a value that you want sending to smart contract and then you can call RSVP function.
I adjust your smart contract delete your attempts. Try it:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract MeetingCreation {
struct Meeting {
address meeting_creator;
uint256 meetingID;
string name;
string description;
uint256 start_date;
uint256 end_date;
uint256 max_tickets;
uint256 ticket_price;
uint256 current_tickets_sold;
address[] attendees;
}
mapping(uint => Meeting) meetings;
function RSVP (uint256 MEETINGID) public payable {
//get the Meeting ID
Meeting storage m = meetings[MEETINGID];
require(m.current_tickets_sold < m.max_tickets,
"No more tickets available"
);
require(msg.sender.balance >= m.ticket_price,
"Not enough funds"
);
address[] storage adr;
adr = m.attendees;
adr.push(msg.sender);
m.current_tickets_sold += 1;
}
// NOTE: Get smart contract balance
function getSmartContractBalance() external view returns(uint) {
return address(this).balance;
}
}

PancackeswapV2Router swapExactETHForTokensSupportingFeeOnTransferTokens: 'Pancake: TRANSFER_FAILED' when "from" equals to "pancackeSwapV2pair"

PancakeSwap is driving me crazy!
I have this simple contract: when the token are transferred from someone different from the owner, instead of making the transer, swap the reveiced tokens with Bnb.
function _swapAsBnb(address from, address to, uint256 amount) private {
if(from != _owner && !lockSwap){
// I tranfer tokens to the contract address in order to make it able to swap them
_transfer(from, address(this), amount);
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = _uniswapV2Router.WETH();
_approve(address(this), address(_uniswapV2Router), tokenAmount);
// use the lock to avoid cycles during the swap
lockSwap = true;
_uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenAmount, 0, path, address(this), block.timestamp);
lockSwap = false;
}
else{
_transfer(from, to, amount);
emit Transfer(from, to, amount);
}
}
function _transfer(address from, address to, uint256 amount) private {
require(from != address(0), "BEP20: transfer from the zero address");
require(balanceOf(from) >= amount, "BEP20: transfer amount exceeds balance");
unchecked {
_balances[from] -= amount;
}
_balances[to] += amount;
}
receive() external payable {}
The steps I made are:
Deploy the contract on BSC Testnet
Create a test pool at https://pancake.kiemtienonline360.com/ containing the pair WBNB:MyToken
I use another wallet in order to buy the token (don't worry, there is enough liquidity)
What I expect is the new wallet has no token, instead the contract address is supposed to have the just swapped Bnb.
However i receive the following error: 'Pancake: TRANSFER_FAILED'
I noticed this only happen when a wallet buy the token, not when it has been sold.
So the problem occurs when the 'from' argument is the pancakeswap pair it self (in fact, I can avoid the problem by checking that from != uniswapV2Pair address).

Transaction Reverted even though I have a payable fallback function to receive eth, when I deploy without sending eth it works

Here is my code:
pragma solidity 0.8.6;
import "./Allowance.sol";
contract SharedWallet is Allowance {
event MoneySent(address indexed _beneficiary, uint _amount);
event MoneyReceived(address indexed _from, uint _amount);
function withdrawMoney(address payable _to, uint _amount) public ownerOrAllowed(_amount) {
require(_amount <= address(this).balance, "Contract doesn't own enough money");
if(!isOwner()) {
reduceAllowance(msg.sender, _amount);
}
emit MoneySent(_to, _amount);
_to.transfer(_amount);
}
function renounceOwnership() public override onlyOwner {
revert("can't renounceOwnership here"); //not possible with this smart contract
}
fallback () external payable {
emit MoneyReceived(msg.sender, msg.value);
}
}
Are you trying to send Ether to the contract as you deploy it? As I do not think you can do that easily. Well, there is a hacky way. Ethereum contract address is deterministic, i.e. knowing the creator's address and their nonce you can calculate the future address of your smart contract. And then send ETH to that address prior to deploying the contract.

How can I sell caller's tokens via contract function and send them to the caller?

I have a function to claim an accumulated token amount like below. But its throwing TRANSFER_FROM_FAILED. What is the safest way to do this?
function claimAccumulatedAmount() public {
_swapForBNB(_msgSender(), _msgSender(), getAccumulatedAmount(_msgSender()));
}
function _swapForBNB(address spender, address recipient, uint256 amount) internal {
_approve(spender, address(swapRouter), amount);
swapRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
amount,
0, // slippage is unavoidable
sellPath,
recipient,
block.timestamp
);
}
Found solution;
Transfer tokens to address(this) and approve router then make the swap.