You will first need to establish connect your wallet to the webpage (and the library). I like to use Ethers as the web3 client library.
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
// Prompt user for account connections
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
console.log("Account:", await signer.getAddress());
After this, you need to create the interface for your contract. For this, you'll need two things, the address and the ABI
The address is the deployed address of the ERC20 token. Using the USDC token address here, and specifying the amount of decimals in the token.
const CONTRACT_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const DECIMALS = 6
The full ABI of a contract is created when building from Solidity. If you are lucky, you can get it from Etherscan.
Luckily, ERC20 contracts are standardized, so you can use a general ERC20 abi, or just provide the ABI of the single method you are using like this:
const abi = ["function transfer(address to, uint amount)"];
Now you can initiate the contract and call the Transfer
method.
const erc20 = new ethers.Contract(CONTRACT_ADDRESS, abi, provider);
const transferToAddress = "0x..."
const amount = ethers.utils.parseUnits("1", DECIMALS);
await erc20.transfer(transferToAddress, amount)
Here is a working codesandbox
Yes, I think it's possible.
You can store deposited values in a mapping like
mapping(address => uint)
Withdraw requests also can be deposited in a mapping like this with a bool default value false
mapping(address => mapping(value, bool)
Only the contract owner/manager address is allowed to change that bool value. When it's set to true, the requested erc20 token amount is transferred to the address that deposited it. And the amount is deducted from the deposit value.
With this implemantation, you will also have a history of all withdrawals in a mapping style.
And, there should be the main difference while working with ERC20 instead of ETH, the user needs to approve the contract for the erc20 token.
Edit after comments: While sending erc20 tokens from the contract to another address you should use transfer function instead of transferfrom so you don't need any approve for that. You can use transfer because your contract is calling the token contract and msg.sender value while token contract code running is equal to your contract's address.
Best Answer
You can start with checking the
allowance
for your spender by the owner.If the allowance is below your requirement, you'll need to first perform a
approve
transaction. And then do thetransferFrom
. If you do this as two separate transactions, it'll open up the MetaMask twice and the users will have to authorise 2 transactions separately.We could have used multi-call contract to combine these calls inside a single transaction but most
approve
function implementations works based onmsg.sender
.ERC-777 tries to fix this by requiring a single transaction to effectively perform the equivalent of
approve
andtransferFrom
. You can check out more about ERC-777 here.