I am trying to create a dividend function for my token. This is in the first part of the contract for my coin:

mapping (address => uint256) private _SavedDividend;

Here is the constructor code:

constructor() public payable ERC20Detailed(tokenName, tokenSymbol, tokenDecimals) {
  _mint(msg.sender, _totalSupply);
  _SavedDividend = 0;

Here is my view and withdraw dividend function:

function ViewDividend(address staker) public view returns (uint256) {
require(msg.sender != (address(0)));

  _SavedDividend = _balances[this].div(_totalSupply).mul(_balances[msg.sender]);
  return _SavedDividend;

function WithdrawDividend() public {

  emit Transfer(address(this), msg.sender, _SavedDividend[msg.sender]);
  _balances[this] = _balances[this].sub(_SavedDividend[msg.sender]);
  _SavedDividend[msg.sender] = 0;

I get a multitude of errors with these including:

TypeError: Mappings cannot be assigned to. _SavedDividend = 0;

TypeError: Type int_const 0 is not implicitly convertible to expected type mapping(address => uint256). _SavedDividend = 0;

TypeError: Mappings cannot be assigned to. _SavedDividend = _balances[this].div(_totalSupply).mul(_balances[msg.sender]);

TypeError: Type contract Miasma is not implicitly convertible to expected type address. _SavedDividend = _balances[this].div(_totalSupply).mul(_balances[msg.sender]);

TypeError: Type uint256 is not implicitly convertible to expected type mapping(address => uint256). _SavedDividend = _balances[this].div(_totalSupply).mul(_balances[msg.sender]);

TypeError: Return argument type mapping(address => uint256) is not implicitly convertible to expected type (type of first return variable) uint256. return _SavedDividend;

I don't understand what these mean. How do I fix this?

  • Maybe you meant to use _SavedDividend[msg.sender] in those places? The code you've written doesn't really make sense, so this is just a guess as to what you're trying to do. – user19510 Jul 18 '19 at 4:07

You assigned a Type of mapping to _SavedDividend here:

mapping (address => uint256) private _SavedDividend;

... then you are trying to assign numbers to the mapping, starting here:

_SavedDividend = 0;

mapping is an indexed type similar to arrays. You need to specify which one. Other parts of the code imply that this is understood. Here, for example, it is the _SavedDividend of the msg.sender, so that is acceptable.

_SavedDividend[msg.sender] = 0;

In the lines causing trouble, it's not clear what you are trying to do. You cannot assign a simple number or return an entire mapping. You have to assign/read/return _SavedDividend[key]. There is a lot going on there. Token contracts and dividends, in particular, are not good practice projects.

Hope it helps.

| improve this answer | |

Your code appears to be implementing ERC20, my recommendation is to inherit from the OpenZeppelin implementation, and use the internal functions to create the functionality that you require.

I suggest you look at the OpenZeppelin documentation on ERC20 tokens https://docs.openzeppelin.org/v2.3.0/tokens#erc20

Your token can inherit from ERC20 and ERC20Detailed.

It could also be worth sharing what you are trying to achieve with the dividend.

As per Rob's answer, the use of mapping is incorrect in places. A good place to start is looking at the Solidity documentation. https://solidity.readthedocs.io/en/latest/types.html#mapping-types

For questions on OpenZeppelin you can also ask in the community forum

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.