Hello I am attempting to add crypto to my online casino but I cant figure out how to auto approve my wallet? I am very new to smart contracts but I would like to expand and set a max allowance on each wallet I am unsure if I am even doing this right I attempted to follow guides online but they go over my head. Please help me make this work.
Contract
this approves the use of USDC WETH and DAI (this is what we accept)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC20 {
function approve(address spender, uint256 amount) external returns (bool);
}
contract BatchApprover {
// Your casino wallet
address public constant CASINOHOT = 0xf8DBf5390518b743deAF7239aa843E602Ff496fb;
address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
function approveAll() external {
IERC20(USDC).approve(CASINOHOT, 1000000000000000000);
IERC20(WETH).approve(CASINOHOT, 1000000000000000000);
IERC20(DAI).approve(CASINOHOT, 1000000000000000000);
}
}
This is my code to link their wallet to our casino.
async function link_wallet_blackjack() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const contract = new ethers.Contract(BATCH_CONTRACT, ["function approveAll()"], signer);
try {
const tx = await contract.approveAll();
await tx.wait();
console.log("Approved wallet maybe???????");
} catch (err) {
console.error("Approval failed:", err);
alert("failed like what.");
}
}
The player wallet would be their address.
const TOKENS = {
usdc: {
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
decimals: 6
},
dai: {
address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
decimals: 18
},
weth: {
address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
decimals: 18
}
};
const tokenContract = new ethers.Contract(token.address, [
"function transferFrom(address from, address to, uint256 amount) external returns (bool)",
"function allowance(address owner, address spender) view returns (uint256)",
"function balanceOf(address account) view returns (uint256)"
], signer);
const allowance = await tokenContract.allowance(player, CASINOHOT);
const balance = await tokenContract.balanceOf(player);
console.log("player allowance:", allowance.toString());
console.log("player balance:", balance.toString());
The allowance is 0 even though the "player's" wallet has 9 dai in it
I can see balance tho which is strange. idk what is going on and transferFrom fails due to not enough dai allowance. Please help me!!!