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
Related
I have created and ERC20 token, it works well within Remix, so I wanted to test it on BSCSCAN testnet.
As far as I can tell, it is ERC20 and BEP20 compliant... although I am a little inexperienced, so I may have gone wrong.
It transacts correctly, but on BSCSCAN, it doesn't show any holders. Can anyone help?
Thank you. Chris ... code below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
contract Share {
mapping(address => uint256) public balances;
mapping(address => User) public Users ;
address[] public Userlist;
mapping(address => mapping(address => uint256)) public allowance;
uint256 public totalSupply = 1000000 * 10 **18;
string public _name;
string public _symbol;
uint8 public decimals = 18;
uint public Usercount = 0;
address public creater;
uint256 tax = 10;
uint256 public createdtime;
struct User {
address Useraddress;
uint256 Account;
uint256 Number;
uint256 Purchasetime;
bool Boughtbefore;
}
event Transfer(address indexed from , address indexed to, uint256 amount);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
event reward(address indexed receiver, uint256 amount);
constructor(string memory name_, string memory symbol_) {
Usercount += 1;
_name = name_;
_symbol = symbol_;
User memory newUser = User(msg.sender,totalSupply,Usercount,block.timestamp,true);
Users[msg.sender] = newUser;
creater = Users[msg.sender].Useraddress;
createdtime = block.timestamp;
Userlist.push(msg.sender);
balances[msg.sender]= totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}
function transfer(address _to, uint256 _value) public returns(bool) {
require(balances[msg.sender]>= _value, 'Account too low');
uint256 transferamount = _value - ((_value*tax)/100);
uint256 rewardamount = (_value*tax)/100;
if(Users[_to].Boughtbefore){
Users[_to].Account += transferamount;
Users[msg.sender].Account -= _value;
balances[_to] += transferamount;
balances[msg.sender] -= _value;
}
if(!Users[_to].Boughtbefore){
Usercount += 1;
User memory NewUser = User(_to,transferamount,Usercount,block.timestamp,true);
Users[_to] = NewUser;
Users[msg.sender].Account -= _value;
Userlist.push(_to);
balances[_to] += transferamount;
balances[msg.sender] -= _value;
}
if(Users[msg.sender].Account == 0){
removefromlist(Users[msg.sender].Number);
delete Users[msg.sender];
Usercount -= 1;
}
emit Transfer(msg.sender, _to, transferamount);
distribute(rewardamount);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns(bool) {
require(balances[_from]>= _value, 'Account too low');
require(allowance[_from][msg.sender] >= _value, 'Allowance too low!');
uint256 transferamount = _value - ((_value*tax)/100);
uint256 rewardamount = (_value*tax)/100;
if(Users[_to].Boughtbefore){
Users[_to].Account += transferamount;
Users[_from].Account -= _value;
balances[_to] += transferamount;
balances[msg.sender] -= _value;
}
if(!Users[_to].Boughtbefore){
Usercount += 1;
User memory NewUser = User(_to,transferamount,Usercount,block.timestamp,true);
Users[_to] = NewUser;
Users[_from].Account -= _value;
Userlist.push(_to);
balances[_to] += transferamount;
balances[msg.sender] -= _value;
}
if(Users[_from].Account == 0){
removefromlist(Users[_from].Number);
delete Users[_from];
Usercount -= 1;
}
emit Transfer(_from, _to, transferamount);
distribute(rewardamount);
return true;
}
function distribute(uint256 _charge) internal{
uint _reward = _charge/Usercount;
for(uint256 i=0; i<Usercount; i++){
Users[Userlist[i]].Account += _reward;
balances[Userlist[i]] += _reward;
emit reward(Users[Userlist[i]].Useraddress, _reward);
}
}
function removefromlist (uint256 index) internal {
for (uint256 i = index; i < Userlist.length; i++){
Userlist[i] = Userlist[i +1];
}
Userlist.pop();
}
function totalsupply() public view returns(uint256){
return totalSupply;
}
function name() public view returns(string memory){
return _name;
}
function symbol() public view returns(string memory){
return _symbol;
}
function Accountof(address _owner) public view returns(uint256){
return balances[_owner];
}
function approve(address _spender, uint256 _value) public returns (bool) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function allowances(address owner, address spender) public view returns (uint256 remaining){
return allowance[owner][spender];
}
function burn(uint256 amount) public returns(bool){
require(Users[msg.sender].Account >= amount, 'Account too low');
Users[msg.sender].Account -= amount;
balances[msg.sender] -= amount;
totalSupply -= amount;
if(Users[msg.sender].Account == 0){
removefromlist(Users[msg.sender].Number);
delete Users[msg.sender];
Usercount -= 1;
}
return true;
}
function getOwner()external view returns(address){
return creater;
}
}
The balancesof function needed to be balancesOf.
Just a simple capital letter! It just goes to show how careful you need to be.
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.
enter image description here
pragma solidity ^0.5.0;
contract Tether {
string public name = 'Tether';
string public symbol = 'USDT';
uint256 public totalSupply = 1000000000000000000000000;
uint8 public decimals = 18;
event Transfer(
address indexed _from,
address indexed _to,
uint256 _value
);
event Approval(
address indexed _owner,
address indexed _spender,
uint _value
);
mapping(address=>uint256) public balanceOf;
mapping(address=> mapping(address=>uint265)) public allowance;
constructor() public {
balanceOf[msg.sender] = totalSupply;
}
function transfer(address _to,uint256 _value) public returns (bool success){
require(balanceOf[msg.sender]>= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to]+=_value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function approve(address _spender,uint256 _value) public returns(bool success) {
allowance[msg.sender][_spender] =_value;
emit Approval(msg.sender,_spender, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= balanceOf[_from]);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_to]+=_value;
balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}
i found the error
DeclarationError: Identifier not found or not unique.
mapping(address=> mapping(address=>uint265)) public allowance;
please help me to resolve the error.
It's most likely caused by a typo - there's no such thing as 265 bit unsigned integer (but there is 256 bit).
Replace the uint265 to uint256.
I want to put a transfer tax into my code but not sure how to go about it. I would apply a 2.5% tax on buys and sells. Any help/advice would be greatly appreciated. This is what I have so far:
pragma solidity ^0.8.4;
contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 100000000000 * 10 ** 18;
string public name = "*****";
string public symbol = "****";
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 return(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(true){
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 reutrns(bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
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];
}
}