Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Machinomy

Machinomy is a library for micropayments in Ether (and ERC20 tokens) over HTTP. Machinomy provides API to send and receive a minuscule amount of money instantly. Core method is buy. The method does all the heavy lifting providing an easy interface for micropayments.

NB. All monetary values below are denominated in Wei, including: buy and deposit methods.

You can find ES5 example in this folder of the project.

example

Buying with Machinomy (TypeScript)

import Machinomy from 'machinomy'
import Web3 = require('web3')

const sender = '0x5bf66080c92b81173f470e25f9a12fc146278429'
const provider = new Web3.providers.HttpProvider("http://localhost:8545")
let web3 = new Web3(provider)

let machinomy = new Machinomy(sender, web3, { engine: 'nedb' })

const price = Number(web3.toWei(1, 'ether'))
const receiver = '0xebeab176c2ca2ae72f11abb1cecad5df6ccb8dfe'
const result = await machinomy.buy({
  receiver: receiver,
  price: price,
  gateway: 'http://localhost:3001/machinomy'
 })
let channelId = result.channelId
await machinomy.close(channelId)
// wait till the receiver claims the money during settling period
await machinomy.close(channelId) // and get the change if he/she does not

Hierarchy

  • Machinomy

Index

Constructors

constructor

  • Create an instance of Machinomy.

    example

    Instantiating Machinomy.

    const sender = '0x5bf66080c92b81173f470e25f9a12fc146278429'
    const provider = new Web3.providers.HttpProvider("http://localhost:8545")
    let web3 = new Web3(provider)
    
    let machinomy = new Machinomy(sender, web3, { engine: 'nedb' })

    Parameters

    • account: string

      Ethereum account address that sends the money. Make sure it is managed by Web3 instance passed as web3 param.

    • web3: Web3

      Prebuilt web3 instance that manages the account and signs payments.

    • options: MachinomyOptions

      Options object

    Returns Machinomy

Properties

Private account

account: string

Ethereum account address that sends the money.

Private channelContract

channelContract: ChannelContract

Private channelManager

channelManager: ChannelManager

Private channelsDao

channelsDao: ChannelsDatabase

Private client

client: Client

Private databaseFile

databaseFile: string

Private engine

engine: string | Engine

Private Optional minimumChannelAmount

minimumChannelAmount: BigNumber.BigNumber

Private serviceContainer

serviceContainer: Container

Private Optional settlementPeriod

settlementPeriod: undefined | number

Private storage

storage: Storage

Private transport

transport: Transport

Private web3

web3: Web3

Web3 instance that manages Machinomy.account's private key

Methods

acceptPayment

acceptToken

buy

  • Entrypoint for a purchasing.

    Wnen you buy for the first time from the same receiver, the method opens a channel with a deposit equal to price✕10. Next method call forms a payment and sends it via http to gateway url.

    The method then returns a token and channel id, in form of BuyResult.

    example
    machinomy.buy({
      receiver: receiver,
      price: 100,
      gateway: 'http://localhost:3001/machinomy'
     })
    

    Parameters

    Returns Promise<BuyResult>

buyUrl

channels

close

  • close(channelId: string): Promise<TransactionResult>
  • Share the money between sender and reciver according to payments made.

    For example a channel was opened with 10 Ether. Sender makes 6 purchases, 1 Ether each. Total value transferred is 6 Ether. If a party closes the channel, the money deposited to the channel are split. The receiver gets 6 Ether. 4 unspent Ethers return to the sender.

    A channel can be closed in two ways, according to what party initiates that. The method nicely abstracts over that, so you do not need to know what is really going on under the hood. For more details on how payment channels work refer to a website.

    Parameters

    • channelId: string

    Returns Promise<TransactionResult>

deposit

  • deposit(channelId: string, value: BigNumber.BigNumber | number): Promise<TransactionResult>
  • Put more money into the channel.

    example
    
    let channelId = '0x0bf080aeb3ed7ea6f9174d804bd242f0b31ff1ea24800344abb580cd87f61ca7'
    machinomy.deposit(channelId, web3.toWei(1, "ether").toNumber(())) // Put 1 Ether more
    

    Parameters

    • channelId: string

      Channel id.

    • value: BigNumber.BigNumber | number

      Size of deposit in Wei.

    Returns Promise<TransactionResult>

paymentById

  • paymentById(id: string): Promise<Payment | null>
  • Return information about the payment by id.

    Parameters

    • id: string

    Returns Promise<Payment | null>

shutdown

  • shutdown(): Promise<void>

Generated using TypeDoc