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) =>{
})
})