UNPKG

2.77 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Message } from 'google-protobuf';
3import EventEmitter from 'events';
4import { Client } from './client';
5import { Address } from './address';
6/**
7 * The Contract class streamlines interaction with a contract that was deployed on a Loom DAppChain.
8 * Each instance of this class is bound to a specific contract, and provides a simple way of calling
9 * into and querying that contract.
10 *
11 * A contract instance can be used to listen to events emitted by the corresponding smart contract,
12 * there is currently only one type of event. The event subscription API matches the NodeJS
13 * EventEmitter API. For example...
14 *
15 * function subscribeToEvents(contract: Contract) {
16 * contract.on(Contract.EVENT, (event: IChainEventArgs) => {
17 * const dataStr = Buffer.from(event.data as Buffer).toString('utf8')
18 * const dataObj = JSON.parse(dataStr)
19 * console.log('Contract Event: ' + dataStr)
20 * })
21 * }
22 */
23export declare class Contract extends EventEmitter {
24 static readonly EVENT: string;
25 private _client;
26 name?: string;
27 address: Address;
28 caller: Address;
29 /**
30 * @param params Parameters.
31 * @param params.contractAddr Address of a contract on the Loom DAppChain.
32 * @param params.contractName Name of the contract.
33 * @param params.callerAddr: Address of the caller, generated from the public key of the tx signer,
34 * e.g. `new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))`
35 * @param params.client: Client to use to communicate with the contract.
36 */
37 constructor(params: {
38 contractAddr: Address;
39 contractName?: string;
40 callerAddr: Address;
41 client: Client;
42 });
43 /**
44 * Calls a contract method that mutates state.
45 * The call into the contract is accomplished by committing a tx to the DAppChain.
46 * @param method Contract method to call.
47 * @param args Arguments to pass to the contract method.
48 * @returns A promise that will be resolved with return value (if any) of the contract method.
49 */
50 callAsync<T extends Message | void>(method: string, args: Message, output?: T): Promise<T | void>;
51 /**
52 * Calls a contract method that doesn't mutate state.
53 * This method is usually used to query the current contract state, it doesn't commit any txs.
54 * @param method Contract method to call.
55 * @param args Arguments to pass to the contract method.
56 * @returns A promise that will be resolved with the return value of the contract method.
57 */
58 staticCallAsync<T extends Message>(method: string, args: Message, output: T): Promise<T>;
59 private _emitContractEvent;
60}