Skip to content
Snippets Groups Projects
Commit 08fb3cc3 authored by Gomez Goiri, Aitor's avatar Gomez Goiri, Aitor
Browse files

Implementing bid related functions in cement and sidenor CLIs

parent 9e8ad4e5
No related branches found
No related tags found
No related merge requests found
// COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021. // COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021.
const faker = require("faker")
const inquirer = require("inquirer") const inquirer = require("inquirer")
const { const {
createCLIApp, createCLIApp,
loginAndCallAfterwards, loginAndCallAfterwards,
promptAssetSelection, promptAssetSelection
STATUSES
} = require("@hypercog/utils") } = require("@hypercog/utils")
const bidAsset = async (apiClient, { assetId: paramAssetId, quantity }) => { const bidAsset = async (
const assetId = apiClient,
paramAssetId || { assetId: paramAssetId, quantity, price }
(await promptAssetSelection( ) => {
await apiClient.richQuery({ const biddableAssets = await apiClient.getBiddableAssets()
fields: {
status: STATUSES.STOCK
}
})
))
const assetToBid = await apiClient.getAsset(assetId) const assetId = paramAssetId || (await promptAssetSelection(biddableAssets))
if (assetToBid.fields.status !== STATUSES.STOCK) {
console.error("You can only bid for a slag stock")
return
}
let chosenQuantity = quantity let chosenQuantity = quantity
if (!quantity) { if (!quantity) {
...@@ -33,22 +22,25 @@ const bidAsset = async (apiClient, { assetId: paramAssetId, quantity }) => { ...@@ -33,22 +22,25 @@ const bidAsset = async (apiClient, { assetId: paramAssetId, quantity }) => {
{ {
type: "number", type: "number",
name: "inputQuantity", name: "inputQuantity",
message: "Escoge una cantidad" message: "Choose an ammount of slag"
} }
]) ])
chosenQuantity = inputQuantity chosenQuantity = inputQuantity
} }
await apiClient.modifyAsset({ let chosenPrice = price
id: assetId, if (!quantity) {
...assetToBid, const { inputPrice } = await inquirer.prompt([
fields: { {
...assetToBid.fields, type: "number",
// TODO add bid function to automatically add me!!! name: "inputPrice",
bids: [...assetToBid.bids, { who: "me", quantity: chosenQuantity }] message: "Provide the offered price"
}
])
chosenPrice = inputPrice
} }
})
await apiClient.bidForAsset(assetId, chosenQuantity, chosenPrice)
console.log("Bid for slag stock", assetId) console.log("Bid for slag stock", assetId)
} }
...@@ -56,12 +48,10 @@ const program = createCLIApp() ...@@ -56,12 +48,10 @@ const program = createCLIApp()
program program
.command("bid") .command("bid")
.description("Pujar por partida de escoria") .description("Bid for slag")
.option( .option("--asset-id [value]", "Identifier of the slag stock to bid for")
"--asset-id [value]", .option("--quantity [value]", "Amount of slag you are interested in")
"Identificador del montón de escoria por el que pujar" .option("--price [value]", "Price offered for the slag")
)
.option("--quantity [value]", "Cantidad de escoria a obtener")
.action(loginAndCallAfterwards(bidAsset)) .action(loginAndCallAfterwards(bidAsset))
program.parse(process.argv) program.parse(process.argv)
// COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021. // COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021.
const faker = require("faker") const faker = require("faker")
const inquirer = require("inquirer")
const { const {
createCLIApp, createCLIApp,
...@@ -114,10 +115,10 @@ const sendToStock = async (apiClient, { assetId: paramAssetId }) => { ...@@ -114,10 +115,10 @@ const sendToStock = async (apiClient, { assetId: paramAssetId }) => {
{ {
id: "newStock", id: "newStock",
type: TYPES.RESIDUO_HIERRO_ACERO, type: TYPES.RESIDUO_HIERRO_ACERO,
fields: { name: "Nuevo montón", status: STATUSES.STOCK } fields: { name: "New stock", status: STATUSES.STOCK }
} }
], ],
"Seleccione el montón al que enviar el cono" "Select the stock where the cone will be merged"
) )
if (!selectedStockId === "newStock") { if (!selectedStockId === "newStock") {
...@@ -150,36 +151,95 @@ const sendToStock = async (apiClient, { assetId: paramAssetId }) => { ...@@ -150,36 +151,95 @@ const sendToStock = async (apiClient, { assetId: paramAssetId }) => {
console.log("Moved to stock", newStockAsset.id) console.log("Moved to stock", newStockAsset.id)
} }
const bidResponse =
action =>
async (apiClient, { assetId: paramAssetId, bidder: paramBidder }) => {
const assetId =
paramAssetId ||
(await promptAssetSelection(
await apiClient.richQuery({
fields: {
status: STATUSES.STOCK
}
})
))
const assetSelected = await apiClient.getAsset(assetId)
if (!assetSelected.fields.bids || assetSelected.fields.bids.length === 0) {
console.error(`There are no active bids to be ${action}ed`)
return
}
let bidder = paramBidder
if (!bidder) {
const { inputBidder } = await inquirer.prompt([
{
type: "list",
name: "inputBidder",
message: `Select the bidder whose bid will be ${action}ed`,
choices: assetSelected.fields.bids.map(b => ({
name: `${b.bidder} (quantity: ${b.quantity}, price: ${b.price})`,
value: b.bidder
}))
}
])
bidder = inputBidder
}
await apiClient[`${action}Bid`](assetId, bidder)
console.log(`Bid from ${bidder} ${action}ed for asset ${assetId}`)
}
const program = createCLIApp() const program = createCLIApp()
program program
.command("register") .command("register")
.description("Registro inicial de residuo (escoria)") .description("Initial registration of the slag")
.option("--asset-name <value>", "Nombre del residuo") .option("--asset-name <value>", "Slag name")
.action(loginAndCallAfterwards(createAsset)) .action(loginAndCallAfterwards(createAsset))
program program
.command("composition") .command("composition")
.description("Medición de composición de la escoria") .description("Provide a slag composition measure")
.option( .option(
"--asset-id [value]", "--asset-id [value]",
"Identificador de la escoria a la que se ha medido la composición" "Identifier of the slag whose composition has been measured"
) )
.action(loginAndCallAfterwards(editComposition)) .action(loginAndCallAfterwards(editComposition))
program program
.command("to-cone") .command("to-cone")
.description("Enviar escoria al cono") .description("Send slag to cone")
.option("--asset-id [value]", "Identificador de la escoria a llevar a cono") .option("--asset-id [value]", "Identifier of the slag to be sent to the cone")
.action(loginAndCallAfterwards(sendToCone)) .action(loginAndCallAfterwards(sendToCone))
program program
.command("to-stock") .command("to-stock")
.description("Enviar escoria del cono al montón") .description("Send slag from cone to stock")
.option( .option(
"--asset-id [value]", "--asset-id [value]",
"Identificador de la escoria a llevar al montón" "Identifier of the slag batch to be merged in the stock"
) )
.action(loginAndCallAfterwards(sendToStock)) .action(loginAndCallAfterwards(sendToStock))
program
.command("bid-accept")
.description("Accept bid for slag stock")
.option("--asset-id [value]", "Identifier of the slag stock")
.option(
"--bidder [value]",
"Identifier of the bidder for the provided slag stock"
)
.action(loginAndCallAfterwards(bidResponse("accept")))
program
.command("bid-reject")
.description("Reject bid for slag stock")
.option("--asset-id [value]", "Identifier of the slag stock")
.option(
"--bidder [value]",
"Identifier of the bidder for the provided slag stock"
)
.action(loginAndCallAfterwards(bidResponse("reject")))
program.parse(process.argv) program.parse(process.argv)
// COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021.
const TraceblockApiClient = require("@traceblock/api-client")
class HypercogApiClient extends TraceblockApiClient {
constructor(endpoint, network, channel, chaincode) {
super(endpoint, network, channel, chaincode)
}
getBiddableAssets() {
return this._queryExtractPayload("hypercog-list-bid-assets")
}
bidForAsset(id, quantity, price) {
return this._invoke("hypercog-bid-asset", {
body: {
id,
bid: {
quantity,
price
}
}
})
}
acceptBid(id, bidder) {
return this._invoke("hypercog-accept-asset", {
body: { id, bidder }
})
}
rejectBid(id, bidder) {
return this._invoke("hypercog-reject-asset", {
body: { id, bidder }
})
}
}
module.exports = HypercogApiClient
// COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021.
const utils = require("./utils")
const HypercogApiClient = require("./utils")
module.exports = {
...utils,
HypercogApiClient
}
{ {
"name": "@hypercog/utils", "name": "@hypercog/utils",
"version": "0.0.1", "version": "0.0.1",
"main": "utils.js", "main": "index.js",
"dependencies": { "dependencies": {
"@traceblock/api-client": "^0.2.5", "@traceblock/api-client": "^0.2.5",
"commander": "^8.2.0", "commander": "^8.2.0",
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
require("dotenv").config() require("dotenv").config()
const { Command, Option } = require("commander") const { Command, Option } = require("commander")
const ApiClient = require("@traceblock/api-client")
const inquirer = require("inquirer") const inquirer = require("inquirer")
const faker = require("faker") const faker = require("faker")
faker.locale = "es" faker.locale = "es"
const HypercogApiClient = require("./client")
const { version } = require("./package.json") const { version } = require("./package.json")
/* /*
...@@ -87,7 +87,7 @@ const createCLIApp = () => { ...@@ -87,7 +87,7 @@ const createCLIApp = () => {
const loginAndCallAfterwards = fn => async (arg1, cmd) => { const loginAndCallAfterwards = fn => async (arg1, cmd) => {
const { api, network, channel, chaincode, user, password } = cmd.parent.opts() const { api, network, channel, chaincode, user, password } = cmd.parent.opts()
const apiClient = new ApiClient(api, network, channel, chaincode) const apiClient = new HypercogApiClient(api, network, channel, chaincode)
const [username, organization] = user.split("@") const [username, organization] = user.split("@")
...@@ -117,12 +117,9 @@ const createRandomLocation = () => ({ ...@@ -117,12 +117,9 @@ const createRandomLocation = () => ({
desc: faker.address.streetAddress() desc: faker.address.streetAddress()
}) })
const promptAssetSelection = async ( const promptAssetSelection = async (assets = [], message = "Select asset") => {
assets = [],
message = "Seleccione el activo"
) => {
const choices = assets.map(a => ({ const choices = assets.map(a => ({
name: `${a.id} (${a.fields.name}, tipo: ${a.type}, estado: ${a.fields.status})`, name: `${a.id} (${a.fields.name}, type: ${a.type}, status: ${a.fields.status})`,
value: a value: a
})) }))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment