0

I'm trying to create an asset in a smart contract using MetaMask and web3.js:

contract AssetTracker {
    struct Asset {
        string name;
        string description;
        string manufacturer;
        uint price;
        uint id;
        address owner;
        bool initialized;
    }
    Asset[] public assets;
    mapping(address => mapping(uint => bool)) public walletStore;

    function createAsset(string _name, string _description, string _manufacture, uint _price, uint id) public {
        Asset memory newAsset = Asset(_name, _description, _manufacture, _price, id, msg.sender, true);
        assets.push(newAsset);
        walletStore[msg.sender][id] = true;
    }
}

I retrieve created assets and render all of them as a table:

const assets = await Promise.all(
    Array(parseInt(assetCount))
        .fill()
        .map((element, index) => {
            return tracker.methods.assets(index).call();
        })
    );

renderRows() {
    return this.props.assets.map((asset, index) => {
        return (
            <AssetRow
                key={index}
                id={index}
                asset={asset}
           />
        );
    });
}

The problem is that addresses are not shown properly. They are like 0x0000000000000000000000000000000000000001 or 0x0000000000000000000000000000000000000007.

The account which creates an asset will become as an owner of that asset. So, in the createAsset function I have msg.sender as the owner and I guess that caused the problem according to this link. However, solutions in the link were not useful for my case. Can somebody help me to solve the problem? To show addresses properly as they are in MetaMask.

enter image description here

2
  • In Asset(_name, _description, _manufacture, _price, id, msg.sender, true);, you are setting onwer to msg.sender, which couldn't possibly be 0x0000000000000000000000000000000000000001 (let alone, all of them getting the same value). Something in your displayed table is all mixed up. Commented Nov 11, 2018 at 19:20
  • When I console log the first element of Assets[], I'll get the same result. It works fine in Remix though.
    – parastoo
    Commented Nov 11, 2018 at 19:42

2 Answers 2

0

There is a chance that this is development issue, you can communicate with old version of smart contract on particular network. Make sure that you use right network, right contract address or you can re-compile, deploy latest version of contract and try new contract.

1
  • You were right. I redeployed the contract and everything is fine now. Thanks!
    – parastoo
    Commented Nov 12, 2018 at 1:51
0

Are you obtaining stored assets with the correct index? because testing on both with remix and truffle work

Using your contract on remix, works as expected Remix results

also, testing with truffle works

const Manager = artifacts.require('AssetTracker');

contract('asset manager', () => {

    it('works', async () => {
        let instance = await Manager.deployed();
        await instance.createAsset("asset1", "house", "bob builder", 123, 0);
        await instance.createAsset("asset2", "house", "bob builder", 123, 1);
        await instance.createAsset("asset3", "house", "bob builder", 123, 2);

        const stored_assets = "3";      

        const assets = await Promise.all(
            Array(parseInt(stored_assets))
                .fill()
                .map((element, index) => {
                    return instance.assets(index);
                })
        );

        console.log(assets);

    })
})

Truffle test results

3
  • You're right, but it does not work with web3. ====> const asset1 = await tracker.methods.assets(0).call(); console.log(asset1); returns the same:===> Result { '0': 'A', '1': 'A', '2': 'A', '3': '1', '4': '1', '5': '0x0000000000000000000000000000000000000001', '6': false, name: 'A', description: 'A', manufacturer: 'A', price: '1', id: '1', owner: '0x0000000000000000000000000000000000000001', initialized: false }
    – parastoo
    Commented Nov 11, 2018 at 19:33
  • weird, because it works for me using just web3. link to a gist with the code i'm using gist.github.com/RobertoC27/4900705b2a01d4253402964d85177a09
    – Roberto C.
    Commented Nov 11, 2018 at 19:55
  • Have you any 0x0...001 address account?
    – Rick Park
    Commented Nov 11, 2018 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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