Skip to content

Commit

Permalink
feat: create constructorArgs json file upon migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Feb 13, 2024
1 parent 3b0fbcb commit 94c51c2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 39 deletions.
5 changes: 5 additions & 0 deletions migrations/constructorArgs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ten:testnet": {
"WitnetRequestBoardTrustableObscuro": "0000000000000000000000000000f7eb1d08e68c361b8a0c4a36f442c58f10000000000000000000000000000000b677d4a6d20c3b087c52a36e4bed558de0000000000000000000000000000000000000000000000000000000000000000001322e302e312d6466623331353500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e3aa000000000000000000000000000000000000000000000000000000000000fef90000000000000000000000000000000000000000000000000000000000010faa0000000000000000000000000000000000000000000000000000000000004e20"
}
}
4 changes: 2 additions & 2 deletions migrations/scripts/1_deployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const utils = require("../../src/utils")
const WitnetDeployer = artifacts.require("WitnetDeployer")

module.exports = async function (deployer, network, [,,, master]) {
const addresses = await utils.readAddresses()
const addresses = await utils.readJsonFromFile("./migrations/addresses.json")
if (!addresses[network]) addresses[network] = {};

const factoryAddr = addresses[network]?.WitnetDeployer || addresses?.default?.WitnetDeployer || ""
Expand All @@ -15,7 +15,7 @@ module.exports = async function (deployer, network, [,,, master]) {
const factory = await WitnetDeployer.deployed()
addresses[network].WitnetDeployer = factory.address
if (!utils.isDryRun(network)) {
await utils.saveAddresses(addresses)
await utils.overwriteJsonFile("./migrations/addresses.json", addresses)
}
} else {
const factory = await WitnetDeployer.at(factoryAddr)
Expand Down
4 changes: 2 additions & 2 deletions migrations/scripts/2_libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const utils = require("../../src/utils")
const WitnetDeployer = artifacts.require("WitnetDeployer")

module.exports = async function (_, network, [, from]) {
const addresses = await utils.readAddresses()
const addresses = await utils.readJsonFromFile("./migrations/addresses.json")
if (!addresses[network]) addresses[network] = {};

const targets = settings.getArtifacts(network)
Expand Down Expand Up @@ -39,7 +39,7 @@ module.exports = async function (_, network, [, from]) {
process.exit(1)
}
if (!utils.isDryRun(network)) {
await utils.saveAddresses(addresses)
await utils.overwriteJsonFile("./migrations/addresses.json", addresses)
}
} else {
utils.traceHeader(`Skipped '${key}'`)
Expand Down
6 changes: 4 additions & 2 deletions migrations/scripts/3_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = async function (_, network, [, from]) {
async function deploy (specs) {
const { from, key, libs, intrinsics, immutables, network, targets } = specs

const addresses = await utils.readAddresses()
const addresses = await utils.readJsonFromFile("./migrations/addresses.json")
if (!addresses[network]) addresses[network] = {};

const selection = utils.getWitnetArtifactsFromArgs()
Expand Down Expand Up @@ -107,7 +107,9 @@ async function deploy (specs) {
}
// save addresses file if required
if (!utils.isDryRun(network)) {
await utils.saveAddresses(addresses)
await utils.overwriteJsonFile("./migrations/addresses.json", addresses)
const args = {}; args[network] = {}; args[network][key] = constructorArgs.slice(2);
await utils.overwriteJsonFile("./migrations/constructorArgs.json", args)
}
} else {
utils.traceHeader(`Skipped '${key}'`)
Expand Down
4 changes: 2 additions & 2 deletions migrations/scripts/4_proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = async function (_, network, [, from, reporter]) {
async function deploy (target) {
const { from, key, network, specs, targets } = target

const addresses = await utils.readAddresses(network)
const addresses = await utils.readJsonFromFile("./migrations/addresses.json")
if (!addresses[network]) addresses[network] = {};

const mutables = specs[key].mutables
Expand Down Expand Up @@ -89,7 +89,7 @@ async function deploy (target) {
process.exit(1)
}
if (!utils.isDryRun(network)) {
await utils.saveAddresses(addresses)
await utils.overwriteJsonFile("./migrations/addresses.json", addresses)
}
} else {
const oldAddr = await getProxyImplementation(from, proxyAddr)
Expand Down
4 changes: 2 additions & 2 deletions migrations/scripts/5_apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = async function (_, network, [, from,, master]) {
async function deploy (specs) {
const { from, gas, key, libs, intrinsics, immutables, network, targets, vanity } = specs

const addresses = await utils.readAddresses()
const addresses = await utils.readJsonFromFile("./migrations/addresses.json")
if (!addresses[network]) addresses[network] = {};

const artifact = artifacts.require(key)
Expand Down Expand Up @@ -69,7 +69,7 @@ async function deploy (specs) {
}
// save addresses file if required
if (!utils.isDryRun(network)) {
await utils.saveAddresses(addresses)
await utils.overwriteJsonFile("./migrations/addresses.json", addresses)
}
} else {
utils.traceHeader(`Skipped '${key}'`)
Expand Down
37 changes: 25 additions & 12 deletions src/utils/index.js → src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const fs = require("fs")
require("dotenv").config()
const lockfile = require("proper-lockfile")
const readline = require("readline")

const traceHeader = require("./traceHeader")
const traceTx = require("./traceTx")
const web3 = require("web3")

module.exports = {
fromAscii,
Expand All @@ -16,8 +14,8 @@ module.exports = {
isNullAddress,
padLeft,
prompt,
readAddresses,
saveAddresses,
readJsonFromFile,
overwriteJsonFile,
traceHeader,
traceTx,
}
Expand Down Expand Up @@ -115,18 +113,33 @@ async function prompt (text) {
return answer
}

async function readAddresses () {
const filename = "./migrations/witnet.addresses.json"
async function readJsonFromFile (filename) {
lockfile.lockSync(filename)
const addrs = JSON.parse(await fs.readFileSync(filename))
const json = JSON.parse(await fs.readFileSync(filename))
lockfile.unlockSync(filename)
return addrs || {}
return json|| {}
}

async function saveAddresses (addrs) {
const filename = "./migrations/witnet.addresses.json"
async function overwriteJsonFile (filename, extra) {
lockfile.lockSync(filename)
const json = { ...JSON.parse(fs.readFileSync(filename)), ...addrs };
const json = { ...JSON.parse(fs.readFileSync(filename)), ...extra };
fs.writeFileSync(filename, JSON.stringify(json, null, 4), { flag: "w+" })
lockfile.unlockSync(filename)
}

function traceHeader (header) {
console.log("")
console.log(" ", header)
console.log(" ", `${"-".repeat(header.length)}`)
}

function traceTx (tx) {
console.info(" ", "> transaction hash: ", tx.receipt.transactionHash)
console.info(" ", "> gas used: ", tx.receipt.gasUsed.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","))
console.info(" ", "> gas price: ", tx.receipt.effectiveGasPrice / 10 ** 9, "gwei")
console.info(" ", "> total cost: ", web3.utils.fromWei(
BigInt(tx.receipt.gasUsed * tx.receipt.effectiveGasPrice).toString(),
"ether"
), "ETH"
)
}
5 changes: 0 additions & 5 deletions src/utils/traceHeader.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/utils/traceTx.js

This file was deleted.

0 comments on commit 94c51c2

Please sign in to comment.