1

I try to deploy a smart contract on a private block chain (geth started with --dev). However, even if my contract is as simple as possible, I cannot get the stored value.

pragma solidity ^0.8.8;

contract test
{
  string public str="test";
}

I have compiled it with solc, grouped the commands in a script...

eth.defaultAccount=eth.accounts[0]; 
var bin = '0x...'
var abi = [{"inputs":[],"name":"str","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}];
var contract = eth.contract(abi);
var gas = eth.estimateGas({ data: bin });
var tx = { data: bin, gas: gas };
var deployed_contract = contract.new(tx);

... and loaded it to my block chain using geth:

> loadScript("./smartcontract/test.js");
INFO [01-04|10:41:39.804] Setting new local account                address=0x327F27e8b4CbdC9eCfE1a449899A2d252E36f3C3
INFO [01-04|10:41:39.804] Submitted contract creation              hash=0xe29d49ef6b0e84142636f3e163bedbd41ce6ce1f962275849abd4abb2388a0fd from=0x327F27e8b4CbdC9eCfE1a449899A2d252E36f3C3 nonce=0 contract=0xb3D9cB50DDE3fcF4052b365Ba953Fa25FA2415c1 value=0
INFO [01-04|10:41:39.805] Commit new sealing work                  number=1 sealhash=3333d1..cf0a0f uncles=0 txs=1 gas=206,578 fees=2.06578e-13 elapsed="939.258µs"
INFO [01-04|10:41:39.806] Successfully sealed new block            number=1 sealhash=3333d1..cf0a0f hash=104fe0..830fd7 elapsed=1.224ms
INFO [01-04|10:41:39.806] 🔨 mined potential block                  number=1 hash=104fe0..830fd7
"0x327f27e8b4cbdc9ecfe1a449899a2d252e36f3c3"
> INFO [01-04|10:41:39.808] Commit new sealing work                  number=2 sealhash=8c7b71..922b0f uncles=0 txs=0 gas=0       fees=0           elapsed="965.341µs"
WARN [01-04|10:41:39.808] Block sealing failed                     err="sealing paused while waiting for transactions"
INFO [01-04|10:41:39.808] Commit new sealing work                  number=2 sealhash=8c7b71..922b0f uncles=0 txs=0 gas=0       fees=0           elapsed=1.406ms

Then I defined an instance variable...

> var instance = contract.at("0x327F27e8b4CbdC9eCfE1a449899A2d252E36f3C3");
undefined

... and tried to get the value of str:

> instance.
instance._eth            instance.address         instance.constructor     instance.transactionHash 
instance.abi             instance.allEvents       instance.str             
> instance.str();
INFO [01-04|10:43:00.747] Submitted transaction                    hash=0x821b11c0a6fdef92aeba4ee97a11b6652db1316295722f03912f7a8dddf9b3fc from=0x327F27e8b4CbdC9eCfE1a449899A2d252E36f3C3 nonce=1 recipient=0x327F27e8b4CbdC9eCfE1a449899A2d252E36f3C3 value=0
INFO [01-04|10:43:00.747] Commit new sealing work                  number=2 sealhash=cd8275..62fb36 uncles=0 txs=1 gas=21064   fees=2.1064e-14  elapsed="359.047µs"
"0x821b11c0a6fdef92aeba4ee97a11b6652db1316295722f03912f7a8dddf9b3fc"
> INFO [01-04|10:43:00.747] Successfully sealed new block            number=2 sealhash=cd8275..62fb36 hash=ddf6cb..1659ff elapsed="452.117µs"
INFO [01-04|10:43:00.747] 🔨 mined potential block                  number=2 hash=ddf6cb..1659ff
INFO [01-04|10:43:00.748] Commit new sealing work                  number=3 sealhash=2ca181..59631e uncles=0 txs=0 gas=0       fees=0           elapsed="225.336µs"
WARN [01-04|10:43:00.748] Block sealing failed                     err="sealing paused while waiting for transactions"
INFO [01-04|10:43:00.748] Commit new sealing work                  number=3 sealhash=2ca181..59631e uncles=0 txs=0 gas=0       fees=0           elapsed="437.142µs"

I would expect to have "test" written. Could someone tell me how I could get the value of "str"?

Many thanks,

eric

1
  • The problem is that you're issuing a transaction. A transaction can't return data from the contract. You should issue a static read-only call to the contract (don't ask me how with geth) Commented Jan 4, 2023 at 11:15

2 Answers 2

0

you can define a view function in your contract that returns the value of the str variable.

function getStr() public view returns (string memory) {
  return str;
}

You can then call this function using:

instance.getStr()
0

First of all, thank you for your answers.

Four minutes (!) after my question, Lauri Peltonen put me on the right path. Here is the full answer.

Taking the above example, the commands script should be:

eth.defaultAccount=eth.accounts[0]; 
var bin = '0x...'
var abi = [{"inputs":[],"name":"str","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}];
eth.estimateGas({ data: bin });

var _contract = eth.contract(abi);
var test = _contract.new("test", { data: bin, gas: gas }, function(e, contract){
...... if(!e) {
.........  if(!contract.address) { 
............  console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); 
............  } else { 
............  console.log("Contract mined! Address: " + contract.address); 
............  console.log(contract); 
............ }
......... }
...... })

Then, once loaded, it returns the following message:

INFO [01-07|16:10:37.001] Setting new local account                address=0xac6F16B1F6D964Ded3b99a47686954AdfD92c76E
INFO [01-07|16:10:37.020] Submitted contract creation              hash=0xbfff9cf902f31c1dec70d475d71bc2b5649742136fcd88c6a0888056cf5f5f17 from=0xac6F16B1F6D964Ded3b99a47686954AdfD92c76E nonce=0 contract=0xD9d3Aba65A8fc0Fe8AD6E6c6111b48A7D84f6d8F value=0
Contract transaction send: TransactionHash: 0xbfff9cf902f31c1dec70d475d71bc2b5649742136fcd88c6a0888056cf5f5f17 waiting to be mined...
undefined
> INFO [01-07|16:10:37.042] Commit new sealing work                  number=1 sealhash=9ed182..ad3fbe uncles=0 txs=1 gas=206,578 fees=2.06578e-13 elapsed=21.334ms
INFO [01-07|16:10:37.059] Successfully sealed new block            number=1 sealhash=9ed182..ad3fbe hash=7ecea9..8a19ae elapsed=17.354ms
INFO [01-07|16:10:37.060] 🔨 mined potential block                  number=1 hash=7ecea9..8a19ae
INFO [01-07|16:10:37.061] Commit new sealing work                  number=2 sealhash=51ce33..3f3ce9 uncles=0 txs=0 gas=0       fees=0           elapsed="427.816µs"
WARN [01-07|16:10:37.061] Block sealing failed                     err="sealing paused while waiting for transactions"
INFO [01-07|16:10:37.090] Commit new sealing work                  number=2 sealhash=51ce33..3f3ce9 uncles=0 txs=0 gas=0       fees=0           elapsed=29.457ms
Contract mined! Address: 0xd9d3aba65a8fc0fe8ad6e6c6111b48a7d84f6d8f

From there, as mentioned by Lauri Peltonen, using the right function (call()), it becomes possible to get the value.

> test.address
"0xd9d3aba65a8fc0fe8ad6e6c6111b48a7d84f6d8f"
> test.str.call();
"test"

I have later added a function to set a new value which can be called using .sendTransaction("other value");

Thanks for your help.

eric

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.