tkh
March 11, 2022, 10:52am
1
I change DaiToken address to an polygon testnet usdc token address (0xe11A86849d99F524cAC3E7A0Ec1241828e332C62) but the function is not working.
End goal
deposit USDC to contract
withdraw USDC after I deposit it on step 1
pragma solidity ^0.5.0;
interface DaiToken {
function transfer(address dst, uint wad) external returns (bool);
function transferFrom(address src, address dst, uint wad) external returns (bool);
function balanceOf(address guy) external view returns (uint);
}
contract TokenFarm {
DaiToken public daiToken;
address owner;
mapping(address => uint) public stakingBalance;
/*
Kovan DAI: 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa
*/
constructor() public {
daiToken = DaiToken(0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa);
owner = msg.sender;
}
function stakeTokens(uint _amount) public {
// amount should be > 0
require(_amount > 0, "amount should be > 0");
// transfer Dai to this contract for staking
daiToken.transferFrom(msg.sender, address(this), _amount);
// update staking balance
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
}
// Unstaking Tokens (Withdraw)
function unstakeTokens() public {
uint balance = stakingBalance[msg.sender];
// balance should be > 0
require (balance > 0, "staking balance cannot be 0");
// Transfer Mock Dai tokens to this contract for staking
daiToken.transfer(msg.sender, balance);
// reset staking balance to 0
stakingBalance[msg.sender] = 0;
}
}
You will need to let the USDC/DAI token contract approve this
contract, because this contract will move funds on the msg.sender's behalf either from the wallet to this contract or from this contract to the wallet.
tkh
March 11, 2022, 11:25am
3
Thanks I am a newbie to this how could it perform the approve on usdc contract?
The USDC
or DAI
contract is also an ERC20 contract. You can call the approve
function on those contracts.
tkh
March 11, 2022, 1:19pm
5
I face an error when I use stakeTokens function. error: Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }
I changed my code as below:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract TokenFarm {
IERC20 public daiToken;
address owner;
mapping(address => uint) public stakingBalance;
/*
Kovan DAI: 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa
*/
constructor() {
daiToken = IERC20(0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa);
owner = msg.sender;
}
function stakeTokens(uint _amount) public {
// amount should be > 0
require(_amount > 0, "amount should be > 0");
daiToken.approve(0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa, _amount);
// transfer Dai to this contract for staking
daiToken.transferFrom(msg.sender, address(this), _amount);
// update staking balance
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
}
// Unstaking Tokens (Withdraw)
function unstakeTokens() public {
uint balance = stakingBalance[msg.sender];
// balance should be > 0
require (balance > 0, "staking balance cannot be 0");
// Transfer Mock Dai tokens to this contract for staking
daiToken.transfer(msg.sender, balance);
// reset staking balance to 0
stakingBalance[msg.sender] = 0;
}
}
In this code, it is this
contract approving 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa
, but I think in your original logic, it should be the msg.sender
approving this
contract on USDC/DAI contract.
The approval by msg.sender
on this
contract happens on USDC/DAI contracts. They do not need to be written in this
contract. It's a separate manual operation.
asato
March 16, 2022, 1:57am
7
You need to approve dai for this contract to transfer it outside of contract before calling stakeTokens function.