3

Is there anyway to deploy a contract permanently on my private chain?

I tried the greeter contract and closed the console. After running the console again and trying to invoke my greet function I received the following error.

Error: 'greeter' is not defined

2
  • Please give us some code to help you debug the case. How exactly do you try to invoke a greeter function? I guess via web3.js? Do you specify the correct address and ABI?
    – SCBuergel
    Commented Feb 22, 2017 at 15:34
  • Like Sebastian said. Unless you started a new chain, the contract is still present. Javascript requires to you initialize the interface each time; the ABI etc. so it can find the contract ... We can't be sure how to guide that process. For example, in truffle, it would be greeter = Greeter.deployed(). In Geth (native) there would be several steps. Commented Feb 22, 2017 at 15:50

2 Answers 2

5

No. A deployed contract on a blockchain does not get deleted when Geth's console is closed.

Restarting the Geth Javascript console requires variables, like greeter, to be re-initialized because the Javascript variables are in memory only and not persisted.

One thing that can help:

Geth has support to load custom JavaScript files into the console through the --preload argument. This can be used to load often used functions, setup web3 contract objects, or ...

geth --preload "/my/scripts/folder/utils.js,/my/scripts/folder/contracts.js" console

For more information, including other ways to use Geth's console, see: https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console

Tools such as Truffle make ongoing contract development easier, for example Truffle saves contract addresses and ABIs, so that as @Rob mentions, re-initializing in Truffle, when needed, would be a simple greeter = Greeter.deployed().

0

It does not get deleted but the variable of that contract is deleted. In order to preserve the variable of the contract before you exit the geth console, you can store the variable with corresponding ABI interface and address of the contract in web3js. Example code in node.js:

var http = require('http');
var Web3js = require('web3');
var web3 = new Web3js("ws://localhost:8546");

var server = http.createServer((req,res)=>{
  res.statusCode = 200;
  res.setHeader('Content-Type','application/json');

  // Contract receives ABI interface and address
  // Then contract is passed to greeter variable
  var greeter = new web3.eth.Contract([
        {
                "constant": false,
                "inputs": [],
                "name": "kill",
                "outputs": [],
                "payable": false,
                "stateMutability": "nonpayable",
                "type": "function"
        },
        {
                "constant": true,
                "inputs": [],
                "name": "greet",
                "outputs": [
                        {
                                "name": "",
                                "type": "string"
                        }
                ],
                "payable": false,
                "stateMutability": "view",
                "type": "function"
        },
        {
                "inputs": [
                        {
                                "name": "_greeting",
                                "type": "string"
                        }
                ],
                "payable": false,
                "stateMutability": "nonpayable",
                "type": "constructor"
        }
], "0x5c6aa1573b32eb75bf516b6d1de1a5a27fadc111"); // <-- this is the address

  greeter.methods.greet().call().then((result) => {
    console.log(result);
    res.end(result);
  });
});

server.listen(8080, () => {
  console.log('alhamdulillah');
});

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.