0

First of all i use Truffle and Ganache.

I made a smart contract with solidity defining the following structures:

struct TimestampproTeil {
    uint Fahrwerk;
    uint Chaussy;
    uint Kabine;
    uint Auspuff;
}

struct Traktor {
    uint produktionsnummer;
    Kabine kab;
    Reifen reif;
    Fahrwerk fahrw;
    Chaussy chau;
    Auspuff ausp;
    TraktorTyp typ;
    uint timestamp;
    TimestampproTeil teile;
}

and in order to interact with the blockchain i use ganache-cli.

My API looks like this

const fs = require('fs');
const solc = require('solc');
const path = require('path');
const Web3 = require('web3');
const web3 = new Web3("http://127.0.0.1:8545");

//Contract
const input = fs.readFileSync(__dirname + '/contracts/ProductionSimple.sol', 'UTF-8');
//Amount of contracts
const output = solc.compile(input, 1);

const abi = JSON.parse(output.contracts[':ProductionSimple'].interface);
const bytecode = output.contracts[':ProductionSimple'].bytecode;

//contract 
const myContract = new web3.eth.Contract(abi);

const instance = myContract.deploy({
    data: bytecode
})
.send({
    from: '0x52750243c1d99839e2267c2805fe0c81261e2325', 
    gas:6721975
})
.then((newContractInstance) => {
    return newContractInstance; // instance with the new contract address
});

instance.then((legocontract) =>{
    legocontract.methods.getTimeAuspuff(3).call((err, result) => { console.log(result)})
})

instance.then((legocontract) =>{
    legocontract.methods.setTimeAuspuff(1).send({from: '0x52750243c1d99839e2267c2805fe0c81261e2325',gas:6721975});
})

instance.then((legocontract) =>{
    legocontract.methods.getTimeAuspuff(1).call((err, result) => { console.log(result)})
})

The functions i used for the transaction

function getTimeAuspuff(uint prodNr)view public returns(uint){
        return(traktoren[prodNr].teile.Auspuff);
    }

    function setTimeAuspuff(uint prodNr) public{
        uint day = now;
        traktoren[prodNr].teile.Auspuff = day;
    }

SetTimeAusPuff() changes the Auspuff value in TimestampproTeil and getTimeAuspuff() returns this value. When i interact with the node console everthing works fine for me and i can call sender functions adding new or changing values. When i just run the whole file a new block gets generated but it does not create new values or change already existing values. Also there is no error message that occurs. So what can i do in order to fix this problem?

I use web3 version 1.0.0-beta.37 because with higher versions ganache-cli ends up in an endless loop.

Thanks for your help guys!

Updated Code:

const instance = async () => {
    const legocontract = await myContract.deploy({
       data: bytecode
    }).send({
    from: '0x51668bbc9278666a06d6731fe081f179e3ec934c', 
    gas:6721975
    })
    .then((newContractInstance) => {
        return newContractInstance; // instance with the new contract address
    })
    return legocontract;
};



instance().then((contract) =>{
    const getTime = async () => {
      await contract.methods.getTimeAuspuff(1).call().then((result) => { console.log(result)})
      await contract.methods.setTimeAuspuff(1).send({from: '0x51668bbc9278666a06d6731fe081f179e3ec934c',gas:6721975})
      await contract.methods.getTimeAuspuff(1).call().then((result) => { console.log(result)})
    };
    getTime().then((time) =>{
    })
})
  • Please get rid of all those screenshots, and post your code in plain text (indented 4 spaces to the right). – goodvibration Jul 21 at 12:53
  • Sry, this is my first time asking a question.. hope i got i right – Eiden Jul 21 at 13:10
0

I think your problem is caused by the promises you use them but do not await/chain for the result. From the commandline they will finish before you type/past the next command, but from a script it will run immediately before it finishes.

async function main() {
  const legocontract = await myContract.deploy({
    data: bytecode
  })
  .send({
    from: '0x52750243c1d99839e2267c2805fe0c81261e2325',
    gas:6721975
  })

  const before = await legocontract.methods.getTimeAuspuff(3).call()
  console.log(before);

  await legocontract.methods.setTimeAuspuff(1).send({
    from: '0x52750243c1d99839e2267c2805fe0c81261e2325',
    gas:6721975
  })

  const after = await legocontract.methods.getTimeAuspuff(1).call()
  console.log(after);
}

main()
  • Yes, that was the problem! I found out that the contract address was null for this transaction. But for await i need async functions. Is there a way to write the code without all the nesting? I will add the updated code to the question. – Eiden Jul 22 at 19:49
  • You can wrap the code inside an async function and only use await so you don't have to pass callbacks to then(). If you want to manage error you have to enclose in a try/catch but otherwise IMHO it is more readable. – Ismael Jul 22 at 20:37
  • Thank you! works fine – Eiden Jul 23 at 9:11

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.