Skip to content

Commit

Permalink
chore: refactor assets and utils package exports
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Feb 8, 2024
1 parent c4abcfb commit 1b27482
Show file tree
Hide file tree
Showing 7 changed files with 881 additions and 295 deletions.
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"exports": {
".": "./src/index.js",
"./assets": "./src/index.js",
"./utils": "./src/utils"
"./utils": "./src/utils/index.js"
},
"files": [
"artifacts/index.js",
"artifacts/contracts",
"artifacts",
"build/contracts",
"contracts",
"migrations/witnet.addresses.json",
Expand Down Expand Up @@ -79,7 +78,6 @@
"truffle": "^5.11.5",
"truffle-assertions": "^0.9.2",
"truffle-flattener": "^1.6.0",
"typescript": "latest",
"web3": "^4.4.0"
"typescript": "latest"
}
}
1 change: 0 additions & 1 deletion scripts/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ if (fs.existsSync("./artifacts")) {
if (os.type() === "Windows_NT") {
exec(`del /s /q artifacts\\*.dbg.json`)
} else {
target = target.replace(/\\/g, "/")
exec(`find ./artifacts -name '*.dbg.json' -exec rm -r {} \;`)
}
}
2 changes: 1 addition & 1 deletion scripts/vanity2gen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assert } = require("chai")
const create2 = require("./eth-create2")
const fs = require("fs")
const utils = require("./utils")
const utils = require("../src/utils")

const addresses = require("../migrations/witnet.addresses")

Expand Down
2 changes: 1 addition & 1 deletion scripts/vanity3gen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assert } = require("chai")
const create3 = require("./eth-create3")
const fs = require("fs")
const utils = require("./utils")
const utils = require("../src/utils")

const addresses = require("../migrations/witnet.addresses")

Expand Down
33 changes: 25 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@ const merge = require("lodash.merge")
const utils = require("./utils")
module.exports = {
getAddresses: (network) => {
const [eco, net] = utils.getRealmNetworkFromArgs(network)
const [eco, net] = utils.getRealmNetworkFromString(network)
if (addresses[net]) {
return merge(
const merged = merge(
addresses.default,
addresses[eco],
addresses[net],
)
return {
WitnetPriceFeeds: merged?.WitnetPriceFeeds,
WitnetRandomness: merged?.WitnetRandomness,
WitnetRequestBoard: merged?.WitnetRequestBoard,
}
} else {
return {}
}
},
listNetworks: () => {
return Object
.entries(addresses)
.filter(value => value[0].indexOf(":") > -1)
.map(value => value[0])
.sort()
supportedEcosystems: () => {
let ecosystems = []
supportedNetworks().forEach(network => {
const [ecosystem,] = utils.getRealmNetworkFromString(network)
if (!ecosystems.includes(ecosystem)) {
ecosystems.push(ecosystem)
}
});
return ecosystems
},
supportedNetworks,
artifacts: {
WitnetBytecodes: require("../artifacts/contracts/WitnetBytecodes.sol/WitnetBytecodes.json"),
WitnetPriceFeeds: require("../artifacts//contracts/apps/WitnetPriceFeeds.sol/WitnetPriceFeeds.json"),
Expand All @@ -34,3 +43,11 @@ module.exports = {
settings: require("../settings"),
utils,
}

function supportedNetworks() {
return Object
.entries(addresses)
.filter(value => value[0].indexOf(":") > -1)
.map(value => value[0])
.sort()
}
36 changes: 28 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require("fs")
require("dotenv").config()
const lockfile = require("proper-lockfile")
const readline = require("readline")
const web3 = require("web3")

const traceHeader = require("./traceHeader")
const traceTx = require("./traceTx")
Expand All @@ -11,8 +10,9 @@ module.exports = {
fromAscii,
getRealmNetworkFromArgs,
getRealmNetworkFromString,
getWitnetArtifactsFromArgs,
getWitnetRequestMethodString,
isDryRun,
isNullAddress,
padLeft,
prompt,
readAddresses,
Expand Down Expand Up @@ -50,14 +50,34 @@ function getRealmNetworkFromString (network) {
}
}

function isDryRun (network) {
return network === "test" || network.split("-")[1] === "fork" || network.split("-")[0] === "develop"
function getWitnetRequestMethodString(method) {
if (!method) {
return "HTTP-GET"
} else {
const strings = {
0: "UNKNOWN",
1: "HTTP-GET",
2: "RNG",
3: "HTTP-POST",
4: "HTTP-HEAD",
}
return strings[method] || method.toString()
}
}

function isNullAddress (addr) {
return !addr ||
addr === "0x0000000000000000000000000000000000000000" ||
!web3.utils.isAddress(addr)
function getWitnetArtifactsFromArgs() {
let selection = []
process.argv.map((argv, index, args) => {
if (argv === "--artifacts") {
selection = args[index + 1].split(",")
}
return argv
})
return selection
};

function isDryRun (network) {
return network === "test" || network.split("-")[1] === "fork" || network.split("-")[0] === "develop"
}

function padLeft (str, char, size) {
Expand Down
Loading

0 comments on commit 1b27482

Please sign in to comment.