Skip to content
Snippets Groups Projects
Select Git revision
  • b6e361bddf920be06e39ba8ba45899fa059bb83f
  • main default
2 results

operations.go

Blame
  • index.js 2.45 KiB
    // COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2022.
    
    const { Option } = require("commander")
    
    const { createCLIApp, HypercogApiClient } = require("@hypercog/utils")
    const {
      createAsset,
      editComposition,
      sendToCone,
      sendToStock,
      acceptBid
    } = require("@hypercog/sidenor-cli")
    
    const { bidAsset } = require("@hypercog/cement-cli")
    
    const signInAs = async (apiClient, userMail, password) => {
      const [username, organization] = userMail.split("@")
    
      const okLogin = await apiClient.signIn(username, password, organization)
      if (!okLogin) {
        console.error("Unauthorized login")
        return
      }
    }
    
    const createAssetAndSendToCone = async (apiClient, name) => {
      const assetId = await createAsset(apiClient, name)
      await editComposition(apiClient, { assetId })
      return sendToCone(apiClient, { assetId })
    }
    
    const simulateCycle = async (arg1, cmd) => {
      const { api, network, channel, chaincode, password, ...otherArgs } =
        cmd.parent.opts()
    
      const apiClient = new HypercogApiClient(api, network, channel, chaincode)
    
      await signInAs(apiClient, otherArgs.sidenorUser, password)
    
      let batchId = await createAssetAndSendToCone(apiClient, "simulated asset 1")
      batchId = await createAssetAndSendToCone(apiClient, "simulated asset 2")
      batchId = await createAssetAndSendToCone(apiClient, "simulated asset 3")
    
      const selectedStock = await sendToStock(apiClient, { assetId: batchId })
    
      await signInAs(apiClient, otherArgs.cement1User, password)
      await bidAsset(apiClient, {
        assetId: selectedStock.id,
        price: 2
      })
    
      await signInAs(apiClient, otherArgs.cement2User, password)
      await bidAsset(apiClient, {
        assetId: selectedStock.id,
        quantity: selectedStock.quantity - 500,
        price: 3
      })
    
      await signInAs(apiClient, otherArgs.sidenorUser, password)
      await acceptBid(apiClient, { assetId: selectedStock.id })
    }
    
    const program = createCLIApp()
      .addOption(
        new Option(
          "-su --sidenorUser <value>",
          "User who will operate with blockchain as a steel manufacturer"
        ).env("SIDENOR_USER_ID")
      )
      .addOption(
        new Option(
          "-cu1 --cement1User <value>",
          "User who will operate with blockchain as a cement company 1"
        ).env("CEMENT1_USER_ID")
      )
      .addOption(
        new Option(
          "-cu2 --cement2User <value>",
          "User who will operate with blockchain as a cement company 2"
        ).env("CEMENT2_USER_ID")
      )
    
    program.command("simulate").description("Simulate usage").action(simulateCycle)
    
    program.parse(process.argv)