1

I have a smart contract. I want to update the contents (list of names) of that contract let's say after every 5 minutes. The names change after this set duration. My approach is to create an instance of that contract(new address) and then write the new set of names. I however feel this is not efficient. Is it possible to update stuff without having to change the address or creating a new instance? Is there a better way to do this?

1 Answer 1

1

in your contract define a setter :

pragma solidity ^0.4.24;

contract Names {

 string[] names;

 function setname(uint i, string s)public returns(bool){

     names[i]=s;
     return true;
 }
}

and in your front end let's say using javascript after each 5 minutes you call setname function as follow :

elapsed5minutes();
var abi=[//your abi array];
    var contractAddress = "//your contract address";
    var contract = web3.eth.contract(abi).at(contractAddress);
     contract.setname.sendTransaction(3,"Brian",{{from:web3.eth.accounts[0]}},function (error, result){   if(!error){
                            console.log(result);
                        } else{
                            console.log(error);
                        }
                });

so you keep the interacting with same contract instance, avoiding you paying more gas for deploying new instance.

1
  • thanks. this was really helpful. I am fairly new to solidity. Jul 11, 2018 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.