0

My contract was working just fine, but I don't know what happened, now when I try to interact with a new instance of my contract from Remix I get this error 'cannot read property "length" of undefined'. So I've checked everywhere on the forum, and people say it's about gas in deployment, I tried to increase, not working, I tried to reduce the code line in my smart contract, not working. Here is my deployment code:

const Web3 = require('web3');
const HDWalletProvider = require('truffle-hdwallet-provider');
const compiledStoreFactory = require('./Contracts/build/StoreFactory.json');
const fs = require('fs');

const provider = new HDWalletProvider(
  'captain kitten phrase unique return pilot camp fly list wreck rate ill',
  'https://ropsten.infura.io/v3/d4d36e8586154352beaabcb9df651ec1'
);

const web3 = new Web3(provider);

const deploy = async() => {
  const accounts =  await web3.eth.getAccounts();

  console.log('Attempting to deploy from account ', accounts[0])

  const result = await new web3.eth.Contract(JSON.parse(compiledStoreFactory.interface))
    .deploy({data:'0x'+compiledStoreFactory.bytecode})
    .send({from:accounts[0], gas:'6000000'})

    fs.writeFile('address.txt', result.options.address, function(err) {
        if(err) {
            return console.log('pute');
        }
    console.log(result.options.address)
})};

deploy();

Note that if I copy/paste the code in Remix, and deploy the contract from there its working just fine.

Here is the solidity code :

pragma solidity ^0.4.24;

import './Crud.sol';

contract Store{
  address private owner;
  mapping(string=>Product) productDetails;
  mapping(string=>Order) orderDetail;
  struct Product{
      string description;
      uint price;
  }
  struct Order{
      string deliveryAddress;
      uint phone;
  }

  constructor(address creator) public {
    owner = creator;
  }

  modifier restricted(){
      require(msg.sender==owner);
      _;
  }

   using HitchensUnorderedKeySetLib for HitchensUnorderedKeySetLib.Set;
    HitchensUnorderedKeySetLib.Set products;
    HitchensUnorderedKeySetLib.Set orders;

    event StoreUpdate(address sender, string action, bytes32 key);
    event OrderUpdate(address sender, string action, bytes32 key);

    function addProductToStore(bytes32 product) public restricted {
        products.insert(product);
        emit StoreUpdate(msg.sender, "insert", product);
    }

    function addOrders(bytes32 order) public {
        orders.insert(order);
        emit OrderUpdate(msg.sender, "insert", order);
    }

    function removeProductToTheStore(bytes32 product) public restricted {
        products.remove(product);
        emit StoreUpdate(msg.sender, "remove", product);
    }

      function removeOrder(bytes32 order) public restricted {
        orders.remove(order);
        emit OrderUpdate(msg.sender, "remove", order);
    }

    function countProducts() public view returns(uint) {
         return products.count();
    }

    function countOrders() public restricted view returns(uint) {
         return orders.count();
    }


    function getProducts(uint index) public view returns(bytes32 ) {
        return products.keyList[index];
    }

    function getOrders (uint index) public view returns(bytes32 ) {
    return orders.keyList[index];
    }

}



contract StoreFactory {
  address[] private deployedStores;

  function createStore() public {
    deployedStores.push(new Store(msg.sender));
  }

  function getDeployedStores() public view returns (address[]){
    return deployedStores;
  }
}


Here is the address of the deployed contract : 0xA6552d9A49091Aa8D89518b93b6A7B81a93de847

  • did you put the code inside the truffle-config.js ? – strangethingspy May 9 at 11:53
  • no i did not, why should I ? – Le Riche Baron May 9 at 13:02
  • Your deploy code seems correct. Is the address returnd by you script valid? Can you share an deployed contract address and solidity code? Can you try to restart the browser or clear the cache? From your error message it seems to be a bug in Remix. – Ismael May 9 at 21:35
  • yes you can find everything in the edit – Le Riche Baron May 10 at 7:42
  • I tried your contract and it is working. I've created an store from Remix. – Ismael May 10 at 16:31

Your Answer

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

Browse other questions tagged or ask your own question.