i trying something simple, store and retrieve a data in a smart contract, but when i want to retrieve the data, nothing is returned.
solc version is 0.4.24 web3 version is 1.2
Solidity code:
pragma solidity ^0.4.24;
contract tbdocs {
address owner;
string testData;
constructor() public {
owner = msg.sender;
}
function getOwner() public view returns (address) {
return owner;
}
function setTestData(string data) public returns (string) {
testData = data;
return testData;
}
function getTestData() public view returns (string) {
return testData;
}
}
Node Js code:
const path = require('path');
const fs = require('fs');
const Web3 = require('web3'); // Version 1.2
const solc = require('solc'); // Version 0.4.24
async function compileAndDeplot() {
const simplePath = path.resolve(__dirname, 'contracts', 'simpleStore.sol');
const sourceSolidity = fs.readFileSync(simplePath, 'utf8');
const contract = solc.compile(sourceSolidity, 1).contracts[':tbdocs']
const byteCode = contract.bytecode;
const abi = JSON.parse(contract.interface);
console.log('Compiled.');
const web3 = new Web3('http://localhost:8545');
const accounts = await web3.eth.getAccounts();
const address = accounts[0];
console.log('Using account: ' + address);
var myContract = new web3.eth.Contract(abi)
var contractDeployed = await myContract.deploy({
data: byteCode
}).send({
from: address,
gas: '2900000'
}, function (error, transactionHash) {
console.log('Transaction hash: ' + transactionHash);
});
const contractAddress = contractDeployed.options.address;
console.log('Contract deployed at: ' + contractAddress);
console.log('\nCalling methods: ')
var result = await contractDeployed.methods.getOwner().call();
console.log('\tgetOwner: ' + result); // Works
contractDeployed.methods.setTestData('testing store data').call()
.then(result => {
console.log('\tSet: ' + result);
contractDeployed.methods.getTestData().call()
.then(data => {
console.log('\tGet: ' + data);
});
})
}
compileAndDeplot();
Right now and testing with ganache-cli, but the same result with geth.
ganache-cli -u 0 -l 1000000000
Example output:
Compiled.
Using account: 0xdf6c7a3c1d19DfEF9bae7fC1328f182D781f6631
Transaction hash: 0xad8d9f164d7ec16522eab2a17c8d2c4253f52d0b648a7921b63c6b8bb863f9de
Contract deployed at: 0x0Aa3916BD95B5C02E8ee44c4E9ccf5FAaa328a0a
Calling methods:
getOwner: 0xdf6c7a3c1d19DfEF9bae7fC1328f182D781f6631
Set: testing store data
Get:
As can be seen, the get is empty.
The contract works fine, can be tested here:
Thank for any tips.