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
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.