Can't transfer ETH from swap smart contract to user - solidity

I try to send back ether when user sell token in swap, and i have that error:
My solidity pragma: pragma solidity >=0.4.22 <0.9.0;
project:/contracts/ANQSwap.sol:33:9: ParserError: Expected primary expression.
payable(address(msg.sender)).transfer(etherToSendBack);
It's my function code:
function sellTokens(uint256 _value) public {
require(anteqToken.balanceOf(msg.sender) >= _value, "You doesn't have enought AnteqToken.");
uint256 etherToSendBack = _value/rate;
require(address(this).balance >= etherToSendBack, "AnteqToken Swap doesn't have enought Ether to buy yours token.");
anteqToken.transferFrom(msg.sender, address(this), _value);
payable(msg.sender).transfer(etherToSendBack);
}
And I too try
payable(address(msg.sender)).transfer(etherToSendBack);

Fixed error
I added one pragma version to all .sol file
pragma solidity ^0.8.0
payable(msg.sender).transfer(etherToSendBack);
Code above works.
I leave this question for other dev if they encountern on similar problem.

Related

Created custom ERC20 contract, balanceOf msg.sender is zero

I've created a ERC20 contract in Remix:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 1002000);
}
}
Then I deployed it:
But the balance of msg.sender is zero:
Does anyone know what's wrong?
I just tried your code and it works perfectly like intended.
It is important, that you compile the correct contract using Remix, see my attached picture. You need to choose the contract "MyToken". I guess you could have deployed the contract "ERC20 - #openzeppelin/contracts/token/ERC20/ERC20.sol" which would lead to the behaviour you mentioned.
compile "MyToken" in Remix

How to withdraw all tokens form my bsc contract

I'm really really inexperienced with contracts and need your help. I created a contract with remix and sent some bnb to it. I want to retrieve it but I can't seem to make it happen.
pragma solidity ^0.8;
interface IERC20 {
function transfer(address _to, uint256 _amount) external returns (bool);
}
contract MyContract {
function withdrawToken(address _tokenContract, uint256 _amount) external {
IERC20 tokenContract = IERC20(_tokenContract);
// transfer the token from address of this contract
// to address of the user (executing the withdrawToken() function)
tokenContract.transfer(msg.sender, _amount);
}
}
This is the code that I'm using from another post but I don't understand it. Do I have to change the "_to" "and "_amount" with the numbers or do I just copy the code and compile it?
I'm really sorry but I have no idea what I did so I just want to take the tokens back.
Thanks
Sorry but you can't withdraw your bnb, bnb isn't a token, bnb is like the ether in ethereum, the native chain currency and the contract doesn't have a function to let you withdraw it, the only way is if you send wbnb, in that case you can look the address of the contract of the wbnb and call the function withdraw of the contract you made
As Jhonny said, BNB is not an actual token, and so your implemented logic won't work to withdraw BNB. But just for you to know, you could create a function which only allows withdrawing BNB (which is the native currency). It would be something like this:
function withdraw(uint _amount) external {
payable(msg.sender).transfer(_amount);
}
Hope you find this useful.

Smart Contract - BEP20: transfer amount exceeds allowance

I'm new in solidity and I'm trying to swap tokens from "Address A" to "Address B".
I used the functions approve and transferFrom, but I'm still getting the error: "Error: VM Exception while processing transaction: reverted with reason string 'BEP20: transfer amount exceeds allowance'"
Could you please help me with this issue?
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.3;
import "./CryptoPlinkoBall.sol";
import "./CryptoPlinko.sol";
import "hardhat/console.sol";
contract TokenSwap {
address admin;
address public owner;
address private _token;
constructor(address token) {
admin = msg.sender;
_token = token;
}
function swapTokens(address recipient, uint256 amount) external {
BEP20(_token).approve(msg.sender, amount);
BEP20(_token).allowance(msg.sender, address(this));
BEP20(_token).transferFrom(msg.sender, recipient, amount);
}
}
When you call BEP20(_token).approve(msg.sender, amount); you are approving the user to move that amount of tokens that the contract owns if you want to transfer the tokens from the user, the user should have called the token contract and approved the amount before calling this function, if you are doing the frontend that will interact with the contract you will need to put the call to the token contract first then the call to this contract
The approve must be mined before the transferFrom gets called.You can't do both on the same call, meanning the approve should occur before going into the swapTokens function.

safeTransfer with token erc1155 in solidity 0.8

I get this error
ERC1155: transfer to non ERC1155Receiver implementer when try to transfer to a smart contract I found this doc https://docs.openzeppelin.com/contracts/4.x/api/token/erc1155 but still don't know how to fix this do I have to abstract IERC1155Receiver interface in my holder token 1155
The receiving contract needs to implement the onERC1155BatchReceived() function based on the ERC-721 definition.
pragma solidity ^0.8;
contract MyContract {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes memory _data) external returns(bytes4) {
// here you can (but don't have to) define your own logic - emit an event, set a storage value, ...
// this is the required return value described in the EIP-721
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
}

Solidity 'out of gas' exception when trying to transfer Ether

I try to transfer Ether from the contract to an address but it gives the error that the transaction is out of gas. I think it's a small problem but I can't find it. I have to specifically use solidity version 0.4.24.
The warning from Remix
The error from MetaMask
I have tried different methods, like:
address.transfer(amount);
address.send(amount);
address.call.value(amount)( );
All methods will give the same out of gas exception. and the send and call method will also give a warning that it's outdated and that I should use the transfer method.
I also tried to adjust the gas and it didn't work, I also tried the needed 2,300 for the transfer listed on the docs.
The code:
pragma solidity ^0.4.24;
contract TestContract {
function payAddress(address _address) external payable {
_address.transfer(msg.value);
}
}
If the problem is that the contract doesn't have any Ether to transfer, can it use the Ether I send with the function call? Or is the problem something else?
Thank you for reading.
edit:
I have tried to send Ether to my Contract and that works, I do have Ether on my contract now, but the function still gives the same error as before. So the problem is something else.
Current code:
pragma solidity ^0.4.24;
contract TestContract {
function() external payable { }
function payContract() public payable {}
function paySomeone(address _address, uint256 _amount) external {
_address.transfer(_amount);
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
The balance of the contract
The parameters I use
Same MetaMask error as before
As you can see here the balance of the contract is 10 wei, but when i try to send 9 wei it still gives the same out of gas error. I also still get the same error from Remix as before.
I also post the issue on the Stack exchange and got an answer there. The issue was my
Ganache version. I switched to the Robsten test network and it worked. I'll link the post here.
Yes. In order to send ether from contract to another address, first you must send some ether to the contract address. Take a look at this and this.