My question is when a DApp calles a ERC20 smart contract to get approved for certain amount, wouldn't it always be the address of calling smart contract approving an address for certain amount? It doesn't serve the purpose for the smart contract to get approved for it.
Apr 14, 2021, 12:55 PM
contract Token {
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
}
contract DApp {
Token token;
constructor (address addr){
token = Token(addr);
token.approve(address(this), {A large number});
}
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
}
contract DApp {
Token token;
constructor (address addr){
token = Token(addr);
token.approve(address(this), {A large number});
}
}
Apr 14, 2021, 12:57 PM
Yes, that concept is not correct. User has to call approve(DApp, ) to Token
You can test yourself. Go to any dex and try to swap any token vs token. You'll see that the first request will be an approve request and then the trade request (except if you approved before).
Apr 14, 2021, 1:09 PM
That's awesome! This's been puzzling me for a while! Thanks
Apr 14, 2021, 1:11 PM
You're welcome :)
Apr 14, 2021, 1:12 PM
I'm trying on SushiSwap and this is my first time on it. Is this page actually asking for my approval for token transfers?
"View the addresses of your permitted accounts (required)" has more going on behind the scene than the literal meaning. Is that true?
Apr 14, 2021, 1:26 PM
No :) Back in the days when MetaMask was younger, visitors of a website exposed their addresses to website owners automatically. Basically they could see the addresses of their visitors and theoretically could track them. To prevent that, MetaMask implemented the request that users need to confirm the exposing of their address to the website owner, which is the selection you see in the screenshot. This just allows the website to see your address, but nothing is done yet
Apr 14, 2021, 1:28 PM
I'm asking this because our DApp also interacts with ERC20 tokens. Users will pay or get paid for ERC20 tokens. The DApp must call functions in token contracts. Right now, I have not figured out how to get approvals from users.
Apr 14, 2021, 1:29 PM
You'll have to google a few things to get to that point, best is to google how to build a dApp with MetaMask (make sure to use as recent tutorials as possible, those from 2017 most likely are outdated already).
Apr 14, 2021, 1:30 PM
Sure, I will. A quick question before that might be if this approval is implemented in the front end or backend? In smart contracts or in JS scripts?
Apr 14, 2021, 1:32 PM
It's in the frontend, via i.e. web3 other ethersjs and MetaMask
Apr 14, 2021, 1:34 PM
👍cool
Apr 14, 2021, 1:35 PM