lib/contract_client/client.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ContractClient = void 0;
var _assembled_transaction = require("./assembled_transaction");
class ContractClient {
  /**
   * Generate a class from the contract spec that where each contract method
   * gets included with an identical name.
   *
   * Each method returns an {@link AssembledTransaction} that can be used to
   * modify, simulate, decode results, and possibly sign, & submit the
   * transaction.
   */
  constructor(spec, options) {
    this.spec = spec;
    this.options = options;
    this.spec.funcs().forEach(xdrFn => {
      const method = xdrFn.name().toString();
      const assembleTransaction = (args, methodOptions) => _assembled_transaction.AssembledTransaction.build({
        method,
        args: args && spec.funcArgsToScVals(method, args),
        ...options,
        ...methodOptions,
        errorTypes: spec.errorCases().reduce((acc, curr) => ({
          ...acc,
          [curr.value()]: {
            message: curr.doc().toString()
          }
        }), {}),
        parseResultXdr: result => spec.funcResToNative(method, result)
      });

      // @ts-ignore error TS7053: Element implicitly has an 'any' type
      this[method] = spec.getFunc(method).inputs().length === 0 ? opts => assembleTransaction(undefined, opts) : assembleTransaction;
    });
  }
  txFromJSON = json => {
    const {
      method,
      ...tx
    } = JSON.parse(json);
    return _assembled_transaction.AssembledTransaction.fromJSON({
      ...this.options,
      method,
      parseResultXdr: result => this.spec.funcResToNative(method, result)
    }, tx);
  };
}
exports.ContractClient = ContractClient;