0

I have a cotract that used to work with web3 and solidity 0.4.21 now I'haven't been able to get a god answer from web3 although my tests work perfectly. this is my contract:

pragma solidity >=0.4.21 <0.6.0;

import "./MyContract.sol";

contract ContractsManager {

    address public owner;
    MyContract[] public contracts;

    constructor() public{
        owner = msg.sender;
    }

    function getContracts() public view returns (MyContract[] memory) {
        return contratos;
    }
}    

This is my js:

if (typeof web3 !== 'undefined') {
    this.web3Provider = web3.currentProvider;
}
else {
    this.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
}
web3 = new Web3(this.web3Provider);

var ContractsManagerArtifact = await $.getJSON('ContractsManager.json');
var MyContractArtifact = await $.getJSON('MyContract.json');

this.solContracts.ContractsManager = TruffleContract(ContractsManagerArtifact);
this.solContracts.MyContract = TruffleContract(MyContractArtifact);

this.solContracts.ContractsManager.setProvider(this.web3Provider);
this.solContracts.MyContract.setProvider(this.web3Provider);

this.manager = await this.solContracts.ContractsManager.deployed();
var addresses = await this.manager.getContracts();

for (let i = 0; i < addresses.length; i++) {
    var address = addresses[i];
    var contratoSol = await this.solContracts.MyContract.at(address);
}

When I test my getContracts() function using truffle test, it returns an empty array [] but in my javascript the getContracts() function its returning undefined.

I tried changing my web3.js version with the same result, am I missing something for solidity 5.0 to work well with web3.js?

Thanks

  • It's solidity 0.5, not 5.0. – goodvibration Mar 7 at 13:58
  • And you need to explain what exactly you have upgraded, since there are 3 components here - Solc, Web3 and Truffle. If you've upgraded Truffle from v4.x to v5.x, then you've implicitly upgraded Solc from v0.4.x to v0.5.x and web3 from v0.x to v1.x. – goodvibration Mar 7 at 14:00
  • I completely installed the application in a new computer installing Truffle v5.0.7 (core: 5.0.7), Solidity v0.5.0 (solc-js) and Node v10.15.2. I tried also updating web3.js and truffle-contract.js using the latest CDNs but TruffleContract stoped working so I left my old files that sadly were pasted in the project and I don't know their versions. – Ana Franco Mar 7 at 14:26
  • Comparing it to some CDN I think I have web3.js version 0.19.0 – Ana Franco Mar 7 at 14:38
1

Solved it, I had to update my web3 code too, started using webpack and migrate using truffle migrate --reset to add my contracts again to ganache after a restart I made.

This is my js:

const Web3 = require('web3')
const contract = require("truffle-contract");
this.web3Provider = Web3.givenProvider || 'ws://localhost:8545';
this.web3 = new Web3(this.web3Provider);

var ContractsManagerArtifact = await $.getJSON('ContractsManager.json');
var MyContractArtifact = await $.getJSON('MyContract.json');

this.solContracts.ContractsManager = contract(ContractsManagerArtifact);
this.solContracts.MyContract = contract(MyContractArtifact);

this.solContracts.ContractsManager.setProvider(this.web3Provider);
this.solContracts.MyContract.setProvider(this.web3Provider);

this.manager = await this.solContracts.ContractsManager.deployed();
var addresses = await this.manager.getContracts();

for (let i = 0; i < addresses.length; i++) {
    var address = addresses[i];
    var contratoSol = await this.solContracts.MyContract.at(address);
}

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.