@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
81 lines (79 loc) • 3.13 kB
JavaScript
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
import * as chainMethods from './chain.js';
import { sendTransaction } from './send-transaction.js';
import * as spendMethods from './spend.js';
import * as contractGaMethods from './contract/ga.js';
import { buildTxAsync } from './tx/builder/index.js';
import { mapObject } from './utils/other.js';
import { wrapWithProxy } from './utils/wrap-proxy.js';
const methods = {
...chainMethods,
sendTransaction,
...spendMethods,
...contractGaMethods
};
var _wrappedOptions = /*#__PURE__*/new WeakMap();
/**
* AeSdkMethods is the composition of:
* - chain methods
* - tx methods
* - aens methods
* - spend methods
* - oracle methods
* - contract methods
* - contract ga methods
*
* While these methods can be used separately, this class provides a handy way to store
* their context (current account, network, and compiler to use).
*/
class AeSdkMethods {
/**
* @param options - Options
*/
constructor(options = {}) {
_defineProperty(this, "_options", {});
_classPrivateFieldInitSpec(this, _wrappedOptions, void 0);
Object.assign(this._options, options);
_classPrivateFieldSet(_wrappedOptions, this, {
onAccount: wrapWithProxy(() => this._options.onAccount),
onNode: wrapWithProxy(() => this._options.onNode),
onCompiler: wrapWithProxy(() => this._options.onCompiler)
});
}
/**
* Returns sdk instance options with references to current account, node, compiler.
* Used to create an instance (Contract, Oracle) bound to AeSdk state.
* @param mergeWith - Merge context with these extra options
* @returns Context object
*/
getContext(mergeWith = {}) {
return {
...this._options,
..._classPrivateFieldGet(_wrappedOptions, this),
...mergeWith
};
}
// TODO: omit onNode from options, because it is already in context
async buildTx(options) {
// TODO: remove `any` at the same time as AeSdk class
return buildTxAsync({
...this.getContext(),
...options
});
}
}
Object.assign(AeSdkMethods.prototype, mapObject(methods, ([name, handler]) => [name, function methodWrapper(...args) {
args.length = handler.length;
const options = args[args.length - 1];
args[args.length - 1] = this.getContext(options);
return handler(...args);
}]));
// eslint-disable-next-line @typescript-eslint/no-redeclare
const AeSdkMethodsTyped = AeSdkMethods;
export default AeSdkMethodsTyped;
//# sourceMappingURL=AeSdkMethods.js.map