I'm practicing with smart contracts and still new to them. I have a instance of a contract that i'm practicing making calls too. I can get the deposit function to work when calling it from my contract but cannot get the withdraw function to work. It keeps reverting.
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "hardhat/console.sol";
interface Matic {
function deposit() external payable;
function withdraw(uint wad) external;
function balanceOf(address _customerAddress) external view returns(uint256);
}
contract Funds {
Matic public depositFunds;
address public owner;
constructor(address _depositFundsAddress) {
owner = msg.sender;
depositFunds = Matic(_depositFundsAddress);
}
// Fallback is called when DepositFunds sends Ether to this contract.
receive() external payable {
payable(owner).transfer(address(this).balance);
}
function deposits() external payable {
depositFunds.deposit{value: msg.value}();
}
function withDras(uint wad) external {
depositFunds.withdraw(wad);
}
function getBalance(address _owner) external view returns (uint256) {
return depositFunds.balanceOf(_owner);
}
}
here is the contract instance
/**
*Submitted for verification at polygonscan.com on 2021-06-09
*/
// https://firebird.finance DeFi multi-chain yield farms deployer & DEXs aggregator.
// Copyright (C) 2015, 2016, 2017 Dapphub
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity ^0.4.18;
contract WMATIC {
string public name = "Wrapped Matic";
string public symbol = "WMATIC";
uint8 public decimals = 18;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
function() public payable {
deposit();
}
function deposit() public payable {
balanceOf[msg.sender] += msg.value;
Deposit(msg.sender, msg.value);
}
function withdraw(uint wad) public {
require(balanceOf[msg.sender] >= wad);
balanceOf[msg.sender] -= wad;
msg.sender.transfer(wad);
Withdrawal(msg.sender, wad);
}
function totalSupply() public view returns (uint) {
return this.balance;
}
function approve(address guy, uint wad) public returns (bool) {
allowance[msg.sender][guy] = wad;
Approval(msg.sender, guy, wad);
return true;
}
function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}
function transferFrom(address src, address dst, uint wad)
public
returns (bool)
{
require(balanceOf[src] >= wad);
if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
require(allowance[src][msg.sender] >= wad);
allowance[src][msg.sender] -= wad;
}
balanceOf[src] -= wad;
balanceOf[dst] += wad;
Transfer(src, dst, wad);
return true;
}
}
I'm not sure exactly what it is i'm doing wrong. on the instance itself the withdraw function works fine but on my contract it keeps reverting
Related
i am trying to develop a nft mint daap. i deploy the erc 721 contract on bsc test net(0x5542bD28FD0D8c22D4AC7D51E62735Ac32d32374) The mint function is working but the metadata is not showing on opensea test net. for metadata and image i am using pinata, here is the link for metadata
https://gateway.pinata.cloud/ipfs/Qmcchbbm2R6CdiwsYbcoLPcY7PSi7yska9HzDCvK977mMU.
the steps i am following: deploy the token, enable public mint , setting the tokenUri.
code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract Minter is ERC721, Ownable{
string private baseTokenURI;
uint256 public mintPrice;
uint256 public totalSupply;
uint256 public maxSupply;
uint256 public maxPerWallet;
bool public isPublicMintEnabled;
address public withdrawWallet;
mapping (address=> uint256) public walletMints;
constructor() payable ERC721("NFTTutorial", "NFT") {
mintPrice= 1 ether;
totalSupply = 0;
maxSupply=14400;
maxPerWallet= 25;
withdrawWallet=0x658385eb2Abf80C76DEeF0d99491d41Fd7B638ff;
}
function setIspublicMintEnable( bool isPublicMintEnabled_) external onlyOwner{
isPublicMintEnabled= isPublicMintEnabled_;
}
function mint(uint256 quantity_) public payable{
require(isPublicMintEnabled,"minting not enabled");
//require(msg.value== quantity_* mintPrice, " wrong mint value");
require(totalSupply+quantity_<= maxSupply,"sold out");
require(walletMints[msg.sender]+ quantity_<= maxPerWallet);
for (uint256 i=0; i <quantity_; i++){
uint256 newTokenId=totalSupply+1;
totalSupply++;
walletMints[msg.sender] = walletMints[msg.sender] + quantity_;
_safeMint(msg.sender, newTokenId);
}
}
/// #dev Returns an URI for a given token ID
function _baseURI() internal view virtual override returns (string memory) {
return baseTokenURI;
}
/// #dev Sets the base token URI prefix.
function setBaseTokenURI(string memory _baseTokenURI) public {
baseTokenURI = _baseTokenURI;
}
function withdraw(address payable _to, uint _amount) public {
_to.transfer(_amount);
}
}
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }
I have implemented the safemoon contract but when I deployed the code in the BSC testnet it's given me an error.
contract SafemoonDemo is Context, IERC20, Ownable {
using SafeMath for uint256;
using Address for address;
mapping (address => uint256) private _rOwned;
mapping (address => uint256) private _tOwned;
mapping (address => mapping (address => uint256)) private _allowances;
mapping (address => bool) private _isExcludedFromFee;
mapping (address => bool) private _isExcluded;
address[] private _excluded;
uint256 private constant MAX = ~uint256(0);
uint256 private _tTotal = 1000 * 10**6 * 10**9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));
uint256 private _tFeeTotal;
string private _name = "SafeMoonDemo";
string private _symbol = "SFD";
uint8 private _decimals = 9;
uint256 public _taxFee = 5;
uint256 private _previousTaxFee = _taxFee;
uint256 public _liquidityFee = 5;
uint256 private _previousLiquidityFee = _liquidityFee;
IUniswapV2Router02 public immutable uniswapV2Router;
address public immutable uniswapV2Pair;
bool inSwapAndLiquify;
bool public swapAndLiquifyEnabled = true;
uint256 public _maxTxAmount = 5000 * 10**6 * 10**9;
uint256 private numTokensSellToAddToLiquidity = 5000 * 10**6 * 10**9;
event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
event SwapAndLiquifyEnabledUpdated(bool enabled);
event SwapAndLiquify(
uint256 tokensSwapped,
uint256 ethReceived,
uint256 tokensIntoLiqudity
);
modifier lockTheSwap {
inSwapAndLiquify = true;
_;
inSwapAndLiquify = false;
}
constructor () public {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
// Create a uniswap pair for this new token
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
// set the rest of the contract variables
uniswapV2Router = _uniswapV2Router;
//exclude owner and this contract from fee
_isExcludedFromFee[owner()] = true;
_isExcludedFromFee[address(this)] = true;
emit Transfer(address(0), _msgSender(), _tTotal);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _tTotal;
}
function balanceOf(address account) public view override returns (uint256) {
if (_isExcluded[account]) return _tOwned[account];
return tokenFromReflection(_rOwned[account]);
}
function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
Your constructor is trying to call the factory() function on the 0x10ED43C718714eb63d5aA57B78B54704E256024E address, expecting a 20-byte response that it would convert to an address type.
Even though there is the PancakeSwap router contract deployed on this address on the BSC mainnet, the BSC testnet doesn't hold any contract on this address.
So your constructor fails as it doesn't get the 20-byte response (from the non-contract address) that it expects.
The Pancakeswap docs page doesn't list any testnet addresses, so it's possible that there are no official Pancakeswap contracts on the testnet.
I'm really new to coding and crypto creation but i'm trying to verify my contract source since 2 hours but i always get the error :
" Error! Unable to generate Contract ByteCode and ABI
Found the following ContractName(s) in source code : Token
But we were unable to locate a matching bytecode (err_code_2)"
Here is my code :
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.2;
contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 100000000000000000 * 10 ** 18;
string public name = "No Marketing Coin";
string public symbol = "NOMK";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor() {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'balance too low');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'balance too low');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint value) public returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
Thanks to everyone if you help me !
I got the same error message when I was verifying my smart contract on the rinkeby testnet, I happened to use Remix for deployment.
The Solution:
When I switched to Hardhat, copied my contract codes from my code editor. Then Boom! It worked, my contract was verified.
Don't know why pasting from remix directly wasn't working.
I'm a bit late but I suggest you to make sure that the compiler version is exactly the same as in Remix and that the license field is set to the same as in the source code.
My problem solved by setting optimization to 200.
I am trying to create my own token using Solidity on Remix Ethereum. I use MetaMask and logged in Binance Smart Chain Testnet . But when I use Solidity Compiler to compile, it keeps showing this error on lines 15, 19, 24, 25, 33, 34:
DeclarationError: Undeclared identifier. Did you mean "balanceOf" or "blances"?
Here the code:
pragma solidity ^0.8.2;
contract MyTestToken {
mapping(address => uint) public blances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 1000000000000 * 10 ** 18;
string public name = "MyToken Test";
string public symbol = "TKNT";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor () {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public view returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'balance too low');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'balance too low');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint value) public returns(bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
Thank you in advance.
It's caused by a typo in your code. Rename the mapping blances to balances.
mapping(address => uint) public blances; // original code
mapping(address => uint) public balances; // corrected
Here is my ERC20 implementation code. When I deploy to Ropsten testing network, I can't add token to my metamask by demonstrating token symbol automatically. I still can't send it to the other account in MEW wallet by error message. All of the function in contract follow the erc20 interface.
contract TWTD {
mapping (address => uint256) public ownerTobalance;
mapping (address => mapping (address => uint256)) public allowance;
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
string TokenName;
string Symbol;
uint8 decimals = 18;
uint256 totalsupply;
using SafeMath for uint256;
function TWTD(string _name, string _symbol, uint256 _initialsupply) public {
ownerTobalance[msg.sender] = _initialsupply;
totalsupply = _initialsupply;
TokenName = _name;
Symbol = _symbol;
}
function totalSupply() public view returns (uint256){
return totalsupply;
}
function balanceOf(address who) public view returns (uint256) {
return ownerTobalance[who];
}
function _transfer(address from, address to, uint256 value) internal {
require ( to != 0x0);
require (ownerTobalance[from] >= value);
uint256 Balance = ownerTobalance[from] + ownerTobalance[to];
ownerTobalance[from].sub(value);
ownerTobalance[to].add(value);
assert(ownerTobalance[from] + ownerTobalance[to] == Balance);
emit Transfer(from,to,value);
}
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender,to,value);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require (allowance[from][msg.sender] >= value);
allowance[from][msg.sender].sub(value);
_transfer(from,to,value);
return true;
}
function approve(address spender, uint256 value) public returns (bool) {
require(value > 0);
allowance[msg.sender][spender] = value;
emit Approval(msg.sender,spender,value);
return true;
}
function allowance(address owner, address spender)
public view returns (uint256) {
return allowance[owner][spender];
}
}