change mapping function method to function() method solidity - solidity

can anyone help to change this code:
mapping(address => uint) public balances;
into function
Full code:
pragma solidity ^0.8.0;
contract SmartSHop{
mapping(address => uint) public balances;
function deposit() public payable{
balances[msg.sender] += msg.value;
}
function withdraw(uint _amount) public{
require(balances[msg.sender]>= _amount, "Not enough ether");
balances[msg.sender] -= _amount;
(bool sent,) = msg.sender.call{value: _amount}("Sent");
require(sent, "failed to send ETH");
}
function getBalance(address _owner) public view returns (uint){
return address(_owner).balance;
}
}

Related

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?

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 getting an error, a declaration error but its pointing to something in quotes which i don't understand

the error I 'm getting reads
DeclarationError: Undeclared identifier.
--> SharedWallet.sol:17:17:
|
17 | require(isOwner() || allowance[msg.sender] >= _amount, "You are not allowed!");
| ^^^^^^^
pragma solidity ^0.8.6;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Allowance is Ownable {
mapping(address => uint256) public allowance;
function addAllowance(address _who, uint256 _amount) public onlyOwner {
allowance[_who] = _amount;
}
modifier ownerOrAllowed(uint256 _amount) {
require(
isOwner() || allowance[msg.sender] >= _amount,
"You are not allowed!"
);
_;
}
function reduceAllowance(address _who, uint256 _amount)
internal
ownerOrAllowed(_amount)
{
allowance[_who] -= _amount;
}
}
contract SharedWallet is Allowance {
uint256 public balanceReceived;
function sendEther() public payable {
balanceReceived += msg.value;
}
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function withdrawMoney(address payable _to, uint256 _amount)
public
ownerOrAllowed(_amount)
{
require(
_amount <= address(this).balance,
"not enough funds in contract"
);
if (!isOwner()) {
reduceAllowance(msg.sender, _amount);
}
_to.transfer(_amount);
}
fallback() external payable {}
}
pragma solidity ^0.8.6;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Allowance is Ownable {
mapping(address => uint256) public allowance;
// This will fix compile error, or, need use inheritance to prevent duplicate code.
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function addAllowance(address _who, uint256 _amount) public onlyOwner {
allowance[_who] = _amount;
}
modifier ownerOrAllowed(uint256 _amount) {
require(
isOwner() || allowance[msg.sender] >= _amount,
"You are not allowed!"
);
_;
}
function reduceAllowance(address _who, uint256 _amount)
internal
ownerOrAllowed(_amount)
{
allowance[_who] -= _amount;
}
}
contract SharedWallet is Allowance {
uint256 public balanceReceived;
function sendEther() public payable {
balanceReceived += msg.value;
}
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function withdrawMoney(address payable _to, uint256 _amount)
public
ownerOrAllowed(_amount)
{
require(
_amount <= address(this).balance,
"not enough funds in contract"
);
if (!isOwner()) {
reduceAllowance(msg.sender, _amount);
}
_to.transfer(_amount);
}
fallback() external payable {}
}

I have an ico code deployed but whenever i transfer ethereum to the address i get no tokens in return

So this is my code which compiles correctly in remix.ethereum and i deployed it on ropsten test chain successfully i validated the code on etherscan(https://ropsten.etherscan.io/address/0x22f27d3812a4d8bd2b6d870c300ea741906a7875) and i can start the ICO but i dont recieve tokens when i transfer eth to the creator address
pragma solidity ^0.4.23;
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender) public view returns (uint256);
function transferFrom(address from, address to, uint256 value) public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) balances;
uint256 totalSupply_;
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
emit Transfer(msg.sender, _to, _value);
return true;
}
function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner];
}
}
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
emit Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256) {
return allowed[_owner][_spender];
}
function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
uint oldValue = allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
}
contract Configurable {
uint256 public constant cap = 1000000*10**18;
uint256 public constant basePrice = 100*10**18; // tokens per 1 ether
uint256 public tokensSold = 0;
uint256 public constant tokenReserve = 1000000*10**18;
uint256 public remainingTokens = 0;
}
contract CrowdsaleToken is StandardToken, Configurable, Ownable {
enum Stages {
none,
icoStart,
icoEnd
}
Stages currentStage;
constructor() public {
currentStage = Stages.none;
balances[owner] = balances[owner].add(tokenReserve);
totalSupply_ = totalSupply_.add(tokenReserve);
remainingTokens = cap;
emit Transfer(address(this), owner, tokenReserve);
}
function () public payable {
require(currentStage == Stages.icoStart);
require(msg.value > 0);
require(remainingTokens > 0);
uint256 weiAmount = msg.value;
uint256 tokens = weiAmount.mul(basePrice).div(1 ether);
uint256 returnWei = 0;
if(tokensSold.add(tokens) > cap){
uint256 newTokens = cap.sub(tokensSold);
uint256 newWei = newTokens.div(basePrice).mul(1 ether);
returnWei = weiAmount.sub(newWei);
weiAmount = newWei;
tokens = newTokens;
}
tokensSold = tokensSold.add(tokens);
remainingTokens = cap.sub(tokensSold);
if(returnWei > 0){
msg.sender.transfer(returnWei);
emit Transfer(address(this), msg.sender, returnWei);
}
balances[msg.sender] = balances[msg.sender].add(tokens);
emit Transfer(address(this), msg.sender, tokens);
totalSupply_ = totalSupply_.add(tokens);
owner.transfer(weiAmount);
}
function startIco() public onlyOwner {
require(currentStage != Stages.icoEnd);
currentStage = Stages.icoStart;
}
function endIco() internal {
currentStage = Stages.icoEnd;
if(remainingTokens > 0)
balances[owner] = balances[owner].add(remainingTokens);
owner.transfer(address(this).balance);
}
function finalizeIco() public onlyOwner {
require(currentStage != Stages.icoEnd);
endIco();
}
}
contract PkpICOTest is CrowdsaleToken {
string public constant name = "PkpICOTest";
string public constant symbol = "PKPICT";
uint32 public constant decimals = 18;
}

DeclarationError: Undeclared identifier. Did you mean "balanceOf" or "blances"?

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

Whant's wrong is my erc20 token deploy in solidity remix ide

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];
}
}