Skip to content

Commit

Permalink
[JS->TS] Migrate invokeViewFunction.js (#815)
Browse files Browse the repository at this point in the history
* Migrate appropriate utilities and invokeViewFunction.js
* cast info to unknown and log the output to console
  • Loading branch information
bh2smith authored May 29, 2020
1 parent 9c9e57c commit 9b52240
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
6 changes: 0 additions & 6 deletions scripts/invokeViewFunction.js

This file was deleted.

25 changes: 25 additions & 0 deletions scripts/invoke_view_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { parseArgs, getBatchExchange } from "./util";

module.exports = async (callback: Truffle.ScriptCallback) => {
try {
const args = parseArgs();
if (args.length < 1) {
callback(
"Error: This script requires arguments - <functionName> [..args]",
);
}
const [functionName, ...arg] = args;
const exchange = await getBatchExchange(artifacts);
const info: unknown = await exchange.contract.methods[functionName](
...arg,
).call();
// Note that this script is intended to be used for bash scripting and
// one would usually grep for the results of this execution. Thus,
// the raw output is printed rather than include irrelevant log details.
// eslint-disable-next-line no-console
console.log(info);
callback();
} catch (error) {
callback(error);
}
};
9 changes: 9 additions & 0 deletions scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ export async function addTokens(
// Return token information
return tokens;
}

export function parseArgs(): string[] {
const args = process.argv.slice(4);
const index = args.indexOf("--network");
if (index > -1) {
args.splice(index, 2);
}
return args;
}
29 changes: 0 additions & 29 deletions scripts/utilities.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { setAllowance } from "./util"

const getArgumentsHelper = function () {
const args = process.argv.slice(4)
const index = args.indexOf("--network")
if (index > -1) {
args.splice(index, 2)
}
return args
}

const getOrderData = async function (instance, callback, web3, argv) {
const minBuy = web3.utils.toWei(String(argv.minBuy))
const maxSell = web3.utils.toWei(String(argv.maxSell))
Expand All @@ -31,24 +22,6 @@ const getOrderData = async function (instance, callback, web3, argv) {
return [argv.buyToken, argv.sellToken, minBuy, maxSell, sender]
}

const invokeViewFunction = async function (contract, callback) {
try {
const args = getArgumentsHelper()
if (args.length < 1) {
callback("Error: This script requires arguments - <functionName> [..args]")
}
const [functionName, ...arg] = args

const instance = await contract.deployed()
const info = await instance[functionName].call(...arg)

console.log(info)
callback()
} catch (error) {
callback(error)
}
}

async function createMintableToken(artifacts) {
const ERC20Mintable = artifacts.require("ERC20Mintable")
return ERC20Mintable.new()
Expand Down Expand Up @@ -128,9 +101,7 @@ async function mintOwl({ users, minter, amount, owl }) {
}

module.exports = {
getArgumentsHelper,
getOrderData,
invokeViewFunction,
mintOwl,
deleteOrders,
setAllowances,
Expand Down
4 changes: 2 additions & 2 deletions scripts/wait_seconds.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { waitForNSeconds } = require("../build/common/test/utilities")
const { getArgumentsHelper } = require("./script_utilities.js")
const { parseArgs } = require("../build/common/scripts/util")

module.exports = async (callback) => {
try {
const args = getArgumentsHelper()
const args = parseArgs()
if (args.length != 1) {
callback("Error: This script requires arguments - <seconds>")
}
Expand Down

0 comments on commit 9b52240

Please sign in to comment.