Hello everyone, i need help, im trying to do what look like a simple task, trasfer a token from addressA to addressB through mycontract, but im wasting hours on this problem, so hopefully someone can help me.
i need a function that people can use to swap na old version of a token with a new one, i found how to get his balance and send him the same amount of the new token.
but now i need that when this is done the oldtoken get transfered from him to the contract or the burn address but when i try to do a trasferfrom i got execution reverted: ERC20: transfer amount exceeds allowance even if i put the allowance to a much bigger number of what im trying to transfer, then i tried with a normal transfer execution reverted: Transfer amount exceeds the maxTxAmount. same here i tried to set the maxtxamount to a much bigger number than the token im trying to transfer but still same error... can anyone help me to understand how to trasfer a token from addressA to addressB (that will be the contract or the burn) through a function in mycontract? ty😊
Jul 23, 2021, 10:29 PM
can't see clearly without the code
Jul 23, 2021, 10:36 PM
im still not sure how to do this transfer so i tried some stuff but i have nothing that look like it can work at the moment i have this:
function query_oldBalance(address _adrs) view public returns (uint) {
address _tokenAddress = "old token address"
return IERC20(_tokenAddress).balanceOf(_adrs);}
to get his old_token balace, then
function claimToken() public {
require(query_oldBalance(msg.sender) >= 10000);
_tokenTransfer(address(this), msg.sender, query_oldBalance(msg.sender) , false);
to send him the same amount of new_token,
but now the user has both old and new token, so i need to remove his old token to prevent him swapping more than one time, i thought of transfer his old token to the burn but cant figure out how to do this transfer brecousse i got those error
function query_oldBalance(address _adrs) view public returns (uint) {
address _tokenAddress = "old token address"
return IERC20(_tokenAddress).balanceOf(_adrs);}
to get his old_token balace, then
function claimToken() public {
require(query_oldBalance(msg.sender) >= 10000);
_tokenTransfer(address(this), msg.sender, query_oldBalance(msg.sender) , false);
to send him the same amount of new_token,
but now the user has both old and new token, so i need to remove his old token to prevent him swapping more than one time, i thought of transfer his old token to the burn but cant figure out how to do this transfer brecousse i got those error
Jul 23, 2021, 10:48 PM
the user needs to approve token transfer to the new token on behalf of the user, that is approve(new_token_address, amount)
then you can write something like
function swap(amount) public {
IERC20(old_token_address).transferFrom(msg.sender, address(this), amount);
this.transfer(msg.sender, amount);
}
That's more or less. I wrote it on TG, you should doublecheck
then you can write something like
function swap(amount) public {
IERC20(old_token_address).transferFrom(msg.sender, address(this), amount);
this.transfer(msg.sender, amount);
}
That's more or less. I wrote it on TG, you should doublecheck
Jul 23, 2021, 10:56 PM
i'll try in a min thankyou ❤️, approve(new_token_address, amount) so approve(address(this), amount) right? because this function is on the new_token contract
Jul 23, 2021, 11:05 PM
no, this approve must be done on the old token
Jul 23, 2021, 11:08 PM
oh ok so like IERC20(oldtokenaddress).approve(new_token_address, amount) yeah i know i dont really understand this part ahaha btw ty for wasting your time answering me 😊
Jul 23, 2021, 11:14 PM
no, this cannot be inside the new contract because it needs to be sent by EOA that owns the tokens. the approve works by checking the msg.sender if you see the spec.
that's why you see many DEXes have two separate buttons (approve and buy)
Jul 23, 2021, 11:22 PM
oh ok so if i wanted to do it in a dapps i need one button to call the approve of the old contract and one to call this function on this one, xfect thankyou very much ❤️
Jul 23, 2021, 11:25 PM
yep, pretty much
Jul 23, 2021, 11:26 PM
👏 you saved me lot of time, i thought it was possibile to call the approve and the after the transfer from the msg.sender directly inside this function so that the user could approve and 'swap' in the same 'click' 😁
Jul 23, 2021, 11:29 PM
u need to call approve function. with the deed wallet as recipient and the amount of tokens to be sent before colling the transfer function
Jul 26, 2021, 10:09 AM