Options
All
  • Public
  • Public/Protected
  • All
Menu

This class's purpose is to be an abstraction for interacting with the Blockchain, and in particular, the UserDescriptors Smart Contract. This class implements methods that are in the smart contract and executes it on behalf of the invoker of this class. The actual smart contract can be found in ./contracts/UserDescriptors.sol

We want to create a class like this for every smart contract.

Hierarchy

  • UserDescriptorService

Index

Constructors

constructor

  • Initializes the Web3 client that connects to the blockchain and creates the contract object.

    Parameters

    • contractAddress: string

      Address of the contract in the blockchain. If you do not know this address, you can execute the following shell command (AFTER LAUNCHING THE LOCAL BLOCKCHAIN) 'npx ts-node ./bin/deploy-contracts.ts' The script will deploy the contracts to the blockchain and write to the filesystem at ./build/deployed-contracts.json a JSON object that contains all the contracts and their addresses; retrieve the address from there.

    • port: number

      Port in localhost (your personal computer) where the blockchain is running. Typically Ganache runs on port 7545 in the GUI or 8545 on the CLI. You can change the configuration in the truffle-config.js.

    Returns UserDescriptorService

Properties

Private contract

contract: UserDescriptors

Local Smart Contract object that we can invoke methods on which will automatically invoke methods on the actual Smart Contract in the blockchain.

Private providerLink

providerLink: string = "http://localhost:8545"

Local link to our Etherium blockchain (local node)

Private web3Client

web3Client: Web3

Web3 is the main JavaScript client that allows us to interact with nodes in the blockchain. It is what Truffle uses in the backend, but we can invoke it directly to get more control and not rely too much on the Truffle framework. https://web3js.readthedocs.io/en/v1.2.0/getting-started.html

Static Private USER_DESCRIPTOR_ABI_PATH

USER_DESCRIPTOR_ABI_PATH: "build/contracts/UserDescriptors.json" = "build/contracts/UserDescriptors.json"

USER_DESCRIPTOR_ABI_PATH is the path to the JSON that has information about the SmartContract that web3js can read. This allows Web3JS to add methods to a contract object that we can invoke from JavaScript.

Methods

getAccountAtIndex

  • getAccountAtIndex(index: number): Promise<string>
  • Gets all the accounts in the blockchain and returns the ID of the account at a certain index in the returned array.

    Parameters

    • index: number

    Returns Promise<string>

getAllAvailableUnitsForUser

  • getAllAvailableUnitsForUser(accountId: string, gas?: number): Promise<string[]>
  • Abstraction for UserDescriptors (smart contract) method, getAllAvailableUnitForUser(): string[] See ./contracts/UserDescriptors.sol for the actual contract method

    Parameters

    • accountId: string

      ID of the account sending the request (Local blockchain autogenerates 10 accounts to use)

    • Default value gas: number = 5000000

      Optional paramter, defaults to 5,000,000. Need gas to perform any sort of operation.

    Returns Promise<string[]>

getAllValuesRecordedForUnit

  • getAllValuesRecordedForUnit(accountId: string, unit: string, gas?: number): Promise<number[]>
  • Abstraction for UserDescriptors (smart contract) method, getAllValuesRecordedForUnit(unit: string): number[] See ./contracts/UserDescriptors.sol for the actual contract method

    Parameters

    • accountId: string

      ID of the account sending the request (Local blockchain autogenerates 10 accounts to use)

    • unit: string

      A unit such as lb, cm, miles, kilometer, etc

    • Default value gas: number = 5000000

      Optional paramter, defaults to 5,000,000. Need gas to perform any sort of operation.

    Returns Promise<number[]>

Private getContractAbi

  • getContractAbi(): any[]
  • This allows Web3JS to add methods to a contract object that we can invoke for our using the JSON ABI (Application Binary Interface). The JSON ABI has the contract methods that can be invoked with input paramters/types, and output paramters/types.

    Returns any[]

getLatestValueForUnit

  • getLatestValueForUnit(accountId: string, unit: string, gas?: number): Promise<number>
  • Abstraction for UserDescriptors (smart contract) method, getLatestValueForUnit(unit: string): number See ./contracts/UserDescriptors.sol for the actual contract method

    Parameters

    • accountId: string

      ID of the account sending the request (Local blockchain autogenerates 10 accounts to use)

    • unit: string

      A unit such as lb, cm, miles, kilometer, etc

    • Default value gas: number = 5000000

      Optional paramter, defaults to 5,000,000. Need gas to perform any sort of operation.

    Returns Promise<number>

insertValue

  • insertValue(accountId: string, unit: string, value: number, gas?: number): void
  • Abstraction for UserDescriptors (smart contract) method, insertValue(unit: string, value: number) See ./contracts/UserDescriptors.sol for the actual contract method

    Parameters

    • accountId: string

      ID of the account sending the request (Local blockchain autogenerates 10 accounts to use)

    • unit: string

      A unit such as lb, cm, miles, kilometer, etc

    • value: number

      Quantity that of this particular unit

    • Default value gas: number = 5000000

      Optional paramter, defaults to 5,000,000. Need gas to perform any sort of operation.

    Returns void

Generated using TypeDoc