truffle-box / react-box

Truffle, Webpack and React boilerplate.
https://truffle-box.github.io/
MIT License
736 stars 251 forks source link

Update script #180

Closed cliffoo closed 2 years ago

cliffoo commented 2 years ago

Building on top of pull#179

Other small things:

Also currently the try-catch in run.js only catches error if:

It doesn't catch:

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();
};