UNPKG

machinomy

Version:

Micropayments powered by Ethereum

102 lines (101 loc) 4.57 kB
/// <reference types="@machinomy/types-web3" /> /// <reference types="@machinomy/types-truffle-contract" /> import * as Web3 from 'web3'; import { PaymentChannel } from './PaymentChannel'; import * as BigNumber from 'bignumber.js'; import Payment from './payment'; import { TransactionResult } from 'truffle-contract'; import ChannelId from './ChannelId'; import { AcceptPaymentResponse } from './accept_payment_response'; import { AcceptTokenRequest } from './accept_token_request'; import { AcceptTokenResponse } from './accept_token_response'; import Registry from './Registry'; import MachinomyOptions from './MachinomyOptions'; import BuyOptions from './BuyOptions'; import NextPaymentResult from './NextPaymentResult'; import BuyResult from './BuyResult'; import { PaymentRequiredResponse } from './PaymentRequiredResponse'; /** * Machinomy is a library for micropayments in Ether over HTTP. * Machinomy provides API to send and receive a minuscule amount of money instantly. * Core method is [buy]{@link Machinomy.buy}. The method does all the heavy lifting providing an easy interface * for micropayments. * * See [examples](https://github.com/machinomy/machinomy/tree/master/examples) directory for both client and server sides. * * NB. All monetary values below are denominated in Wei, including: [buy]{@link Machinomy.buy} and * [deposit]{@link Machinomy.deposit} methods. */ export default class Machinomy { readonly registry: Registry; /** Ethereum account address that sends the money. */ private readonly account; private migrated; constructor(account: string, web3: Web3, options?: MachinomyOptions); /** * 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 {@link BuyResult}. * * @example * <pre><code>machinomy.buy({ * receiver: receiver, // address * price: 100, * gateway: 'http://localhost:3001/machinomy' * }) * </code></pre> */ buy(options: BuyOptions): Promise<BuyResult>; payment(options: BuyOptions): Promise<NextPaymentResult>; pry(uri: string, datetime?: number): Promise<PaymentRequiredResponse>; buyUrl(uri: string): Promise<BuyResult>; /** * Put more money into the channel. * * @example * <pre><code> * let channelId = '0x0bf080aeb3ed7ea6f9174d804bd242f0b31ff1ea24800344abb580cd87f61ca7' * machinomy.deposit(channelId, web3.toWei(1, "ether").toNumber(())) // Put 1 Ether more * </code></pre> * * @param channelId - Channel id. * @param value - Size of deposit in Wei. */ deposit(channelId: string, value: BigNumber.BigNumber | number): Promise<TransactionResult>; open(receiver: string, value: BigNumber.BigNumber | number, channelId?: ChannelId | string, tokenContract?: string): Promise<PaymentChannel>; /** * Returns the list of opened channels. */ channels(): Promise<PaymentChannel[]>; openChannels(): Promise<PaymentChannel[]>; settlingChannels(): Promise<PaymentChannel[]>; channelById(channelId: string): Promise<PaymentChannel | null>; /** * 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. */ close(channelId: string): Promise<TransactionResult>; /** * Save payment into the storage and return an id of the payment. The id can be used by {@link Machinomy.paymentById}. */ acceptPayment(req: any): Promise<AcceptPaymentResponse>; /** * Return information about the payment by id. */ paymentById(id: string): Promise<Payment | null>; acceptToken(req: AcceptTokenRequest): Promise<AcceptTokenResponse>; shutdown(): Promise<void>; private nextPayment; private checkMigrationsState; }