Can a solidity smart contract execute the approve function in an erc20 contract on behalf of the msg.sender - solidity

Setting up a bank contract to perform deposits and withdraws. I was wondering if its possible for the bank contract can execute the approve function in the erc20 contract on behalf of the msg.sender for the tokens they are wanting to deposit.
Below is my attempt for the bank contract to call the erc20 token contracts approve function. However wouldn't the msg.sender be the bank contract address instead of the original msg.sender (second line of the depositToken function.) This sounds silly but is there a way for the contract to send the request passing in the msg.senders address? If not is there an integrated way for the msg.sender to approve the bank contract address and the amount to enable the bank contract to call the transferFrom function and be provided the allowance.
//best guess on what that would look like inside the function depositTokens
msg.sender = customer;
customer.IER20(usdt).approve.address(this), uint _amount;
address public usdt;
mapping(address => uint) public bankBalance;
constructor() public {
usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
}
usdt = 0x77c24f0Af71257C0ee26e0E0a108F940D1698d53;
function depositTokens(uint _amount) public {
IER20(usdt).approve.address(this), uint _amount;
// Trasnfer usdt tokens to contract
IERC20(usdt).transferFrom(msg.sender, address(this), _amount);
// Update the bank balance in map
bankBalance[msg.sender] = bankBalance[msg.sender] + _amount;
}
//approve function in erc20
function approve(address delegate, uint256 numTokens) public override returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;

The answer is no, the approve function takes the msg.sender as the one that gives allowance. You can see the most used OpenZeppelin implementation here.
However I can see what you are trying to do and I want to add that there is a way to automatically ask an EOA (externally owned account) or a user with a wallet to approve some tokens to the contract before sending a transaction.
I don't know if a wallet does it automatically or you have to code it in the UI but I think that is what you are looking for.
And then after the user already approved your bank, you can remove your first line in the deposit function

Related

How to call a function from smart contract A on a token ID received from smart contract B?

I have two ERC721 smart contracts A and B. I have successfully minted a token ID on contract A and transferred it to contract B address (to transfer it to a contract address and not a wallet address I used IERC721Receiver). From here, is there a way for contract's B functions, which take a token ID as argument, to be called on the token ID received from A that now belongs to B?
For example, if Contract A was:
contract ContractA is ERC721 {
...
function mint(address _to, uint256 _mintAmount) public payable {
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(_to, supply + i);
}
}
}
and contract B was:
contract ContractB is ERC721 {
...
function exampleFunction(uint256 tokenId) public payable {
// Do something with tokenId
}
}
How can I call exampleFunction(6) on ContractB if token ID #6 was transferred from ContractA to ContractB (and not minted on ContractB)?
It just seems to me like there is no way to use methods from ERC721 contracts on token IDs that are not minted from the same contract where the methods are implemented.
Anything helps, so thank you in advance!
I am able to see that ContractB owns the token transferred to it by ContractA, but only on ContractA's ownerOf() method. I cannot do anything with that token on ContractB's methods, even though it now belongs to it.
You can call your exampleFunction() from your onERC721Received() implementation.
However you will not be able to do anything with the token as it will not have been transferred to you yet. The onERC721Received is purely to check that the contract supports receiving ERC721 tokens.

How to use ERC20 token to transfer eth in solidity?

I'm writing a defi project to transfer between an ERC20 token and eth. I want to develop a function that accept the ERC20 token from an address then send eth to that address.
What I deal with that ERC20 token was to generate the token in the smart contract and send the token to the user. The problem is that, if user A send some ERC20 token to user B, what should I do to allow user B use the token to ask for eth in my smart contract?
Another simple question is that, how to ask the user to use their wallet (e.g. metamask) to transfer ERC20 token to my smart contract?
Using the payable you would be able to transfer the native token (eth) from your contract into any user's wallet:
function transferEth(address payable _to, uint _amount) public {
(bool success, ) = _to.call{value: _amount}("");
require(success, "Failed to send Ether");
}
Notice that this function above is not safe as any one can call the transfer to your contract, please consider to use the modifier to prevent it.
In your case I can think of a function like:
function claimEth() public {
if (balanceOf(msg.sender) > 100) {
balances[msg.sender] = balances[msg.sender[.sub(100);
transferEth(msg.sender, 5);
}
}

Solidity: transferFrom can't reach allowance value

Here are the steps I follow on Remix:
Deploy my ERC20Basic token and my DEX contract.
DEX.buy > Buy ERC20Basic tokens in exchange of ETH (works fine).
ERC20Basic.approve(contractAddress, tokenAmount) (works fine).
ERC20Basic.allowance > Works fine, function returns the amount of the allowance.
The problem comes when I try to sell tokens with this function from my DEX contract:
function sell(uint256 amount) public {
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
payable(msg.sender).transfer(amount);
emit Sold(amount);
}
I still get "Check the token allowance".
When I log the allowance I get 0.
When I log msg.sender and address(this), I get the same addresses I used on the Remix interface to get the value of the allowance manually.
It feels like the allowance is reset to 0 when I call the sell function or that the sell function can't reach the value of the allowance. Maybe this is something about memory and storage?
My allowance function inside of ERC20Basic contract is:
function allowance(address owner, address delegate)
public
view
override
returns (uint256)
{
return allowed[owner][delegate];
}
Approval function:
function approve(address delegate, uint256 numTokens)
public
override
returns (bool)
{
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
Allowance structure:
mapping(address => mapping(address => uint256)) allowed;
How are you calling the approve function?
In your code you aren't passing the require statement because your approval wasn't successful. If it was your allowance wouldn't be 0.
You are probably calling the approve function from the contract which won't work because then the msg.sender would be address of the contract not your accounts address.
You have to approve the amount manually outside of the contract for it to work.

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.