Instead of setting value to 1, increment (Also renamed run.js to increment.js).
Added instruction on top (how to run + you should truffle migrate before).
Added link to docs.
Added some helpful logging.
Other small things:
indent = 2
cb => callback
Directly export, no main
Use const
Also currently the try-catch in run.js only catches error if:
Compiled but not deployed (truffle compile && truffle exec scripts/run.js)
It doesn't catch:
Not compiled (Just truffle exec scripts/run.js)
It's rather verbose when I try-catch both cases like below, so I thought it's better to just say at the top of the file: You should run truffle migrate
function getSimpleStorage() {
try {
return artifacts.require("SimpleStorage");
} catch (err) {
throw new Error("Failed to import SimpleStorage contract artifact. You should `truffle migrate` first.");
}
}
async function getDeployed(contract) {
try {
return await contract.deployed();
} catch (err) {
throw new Error("Failed to get deployed SimpleStorage instance. You should `truffle migrate` first.");
}
}
module.exports = async function (callback) {
const SimpleStorage = getSimpleStorage();
const deployed = await getDeployed(SimpleStorage);
const currentValue = (await deployed.read()).toNumber();
console.log(`Current SimpleStorage value: ${currentValue}`);
await deployed.write(value + 1);
const updatedValue = (await deployed.read()).toNumber();
console.log(`Updated SimpleStorage value: ${updatedValue}`);
callback();
};
Building on top of pull#179
run.js
toincrement.js
).truffle migrate
before).Other small things:
cb
=>callback
main
const
Also currently the try-catch in
run.js
only catches error if:truffle compile && truffle exec scripts/run.js
)It doesn't catch:
truffle exec scripts/run.js
)It's rather verbose when I try-catch both cases like below, so I thought it's better to just say at the top of the file: You should run
truffle migrate