As the error states, .add()
on line 94 is not defined anywhere.
You'll have other errors for .sub()
on lines 106 and 112.
You have included safeAdd()
and safeSub()
, so you will need to use those.
Edit:
Your safe maths functions have no return statements.
Edit#2:
For the sake of time, this compiles:
pragma solidity ^0.5.0;
// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
//
// ----------------------------------------------------------------------------
contract IBEP20 {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
// ----------------------------------------------------------------------------
// Safe Math Library
// ----------------------------------------------------------------------------
contract 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 MONK is IBEP20, SafeMath {
string public name;
string public symbol;
uint8 public decimals; // 18 decimals is the strongly suggested default, avoid changing it
uint256 public _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
/**
* Constrctor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor() public {
name = "MY";
symbol = "My";
decimals = 18;
_totalSupply = 1000000000;
balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
function totalSupply() public view returns (uint) {
return _totalSupply - balances[address(0)];
}
function balanceOf(address tokenOwner) public view returns (uint balance) {
return balances[tokenOwner];
}
function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
return allowed[tokenOwner][spender];
}
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = sub(balances[msg.sender], tokens);
balances[to] = add(balances[to], tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = sub(balances[from], tokens);
allowed[from][msg.sender] = sub(allowed[from][msg.sender], tokens);
balances[to] = add(balances[to], tokens);
emit Transfer(from, to, tokens);
return true;
}
function _mint(address account, uint256 amount) internal {
require(amount != 0);
balances[account] = add(balances[account], amount);
emit Transfer(address(0), account, amount);
}
function burn(uint256 amount) external {
_burn(msg.sender, amount);
}
function _burn(address account, uint256 amount) internal {
require(amount != 0);
require(amount <= balances[account]);
_totalSupply = sub(_totalSupply, amount);
balances[account] = sub(balances[account], amount);
emit Transfer(account, address(0), amount);
}
function burnFrom(address account, uint256 amount) external {
require(amount <= allowed[account][msg.sender]);
allowed[account][msg.sender] = sub(allowed[account][msg.sender], amount);
_burn(account, amount);
}
}
If I just import Foo in another contract, without inheriting from it,
can I read MY_CONSTANT ?
Without forcing a call, no. constant variables are hardcoded into the bytecode, they are not variables, don't take any storage, and don't make it to the ABI unless if you specify it as public
but issuing a call to read a constant variable through the auto generated getter just doesn't make any sense...
If both Foo
and Bar
need that constant, but inheritance doesn't make sense for some reasons, I'd argue that the constant definition doesn't belong in Foo
to begin with. In that case, it has the characteristics of a global / application-wide constant.
Solidity leaves the possibility of declaring constant at a file level for those cases (this issue might also be of interest). Assuming that the following code is placed in a file called constants.sol
which holds various global constants for your application, including MY_CONSTANT
:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
uint256 constant MY_CONSTANT = 6174;
// other global constants...
You can then import it and use it in both Foo
and Bar
:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "./constants.sol";
contract Foo {
// Doesn't need to declare MY_CONSTANT as this is done
// at the file level in constants.sol
// Use the imported definition from ./constants.sol
function myFunction() external pure returns (uint256) {
return MY_CONSTANT;
}
}
contract Bar {
// Use the imported definition from ./constants.sol
function myFunction() external pure returns (uint256) {
return MY_CONSTANT;
}
}
Best Answer
Make sure
contractAddress
is of typeaddress payable
. And yes, you'll need to provide an argument. E.g.: