Trying to store a list of struct as a mapping but unable to insert data.

pragma solidity ^0.7.6;

contract PayTree {

address payable owner;
uint public expirationTime;
uint public wordValue;
bytes32 public root;
address[] private allMerchants;
uint public startTime;
bytes32[] usedTokens;

//Stores merchant information
struct merchant {
    address addr;
    uint numOfTokens;
    bytes32[] usedTokens;

//initialise state variables
constructor (uint _timeout, bytes32 _root, uint _wordValue) payable {
    owner = msg.sender;
    expirationTime = block.timestamp + _timeout;
    root = _root;
    startTime = block.timestamp;
    wordValue = _wordValue;

//Maps Merchant ethereum address to merchant information
mapping (address => merchant) public Merchants;

function SetUpdateMerchants (bytes32 _token) private {
    var _merchant = merchant(msg.sender, numOfTokens++, usedTokens.push(_token));
    Merchants[msg.sender] = _merchant;

Getting error:

ParseError: Expected Primary expression var _merchant = merchant(msg.sender, numOfTokens++, usedTokens.push(_token)); ^-^


You can't call usedTokens.push(_token) when creating a new merchat structure


There're so many wrong stuff with this code:

  1. allMerchants is array of addresses and you're trying to push bytes32 variable to that array? Judging by the logic of the contract you want to save the initiator of the transaction here, so change that line to: allMerchants.push(msg.sender);
  2. You're trying to create instance of struct merchant, but the value of numOfTokens is not defined
  3. Why you're creating var _merchant and then saving that variable into Merchants mapping? This way you're creating a variable that will be used only once, you don't really need to create variable in such cases. You can simply do Merchants[msg.sender] = merchant(<STRUCT_PARAMS>)
  4. Since solidity 0.6.0 the .push() method is returning nothing, it used to return the new array length in the past, but not anymore. So at the end you have to edit your method like:
function SetUpdateMerchants (bytes32 _token) private {
    Merchants[msg.sender] = merchant(msg.sender, <SOME_UINT_VALUE>, usedTokens);

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.