@parity/light.js
Version:
A high-level reactive JS library optimized for light clients
131 lines (130 loc) • 5.37 kB
JavaScript
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var abi_1 = require("@parity/abi");
var encode_1 = require("@parity/api/lib/util/encode");
var memoizee = require("memoizee");
var api_1 = require("../../api");
var createRpc_1 = require("../utils/createRpc");
var operators_1 = require("../../utils/operators");
var frequency_1 = require("../../frequency");
var post_1 = require("./post");
/**
* Cache contracts, so that they are:
* - only created after the first call/transaction to a contract has been made
* - further calls/transactions to the same contract doesn't recreate the
* contract
*
* @ignore
* @param address - The contract address.
* @param abiJson - The contract abi.
* @param api - The api Object.
* @return - The contract object as defined in @parity/api.
*/
var getContract = memoizee(function (address, abiJson, api) {
return api.newContract(abiJson, address);
} // use types from @parity/abi
);
/**
* Create a contract, given an api object.
* Pure function version of {@link makeContract}.
*
* @ignore
* @param address - The contract address.
* @param abiJson - The contract abi.
* @param api - The api Object.
* @return - An object whose keys are all the functions of the
* contract, and each function returns an Observable which will fire when the
* function resolves.
*/
var makeContractWithApi = memoizee(function (address, abiJson, api) {
var abi = new abi_1.default(abiJson); // use types from @parity/abi
// Variable result will hold the final object to return
var result = {
abi: abi,
address: address,
get contractObject() {
return getContract(address, abiJson, api);
}
};
// We then copy every key inside contract.instance into our `result` object,
// replacing each the value by an Observable instead of a Promise.
abi.functions.forEach(function (_a) {
var name = _a.name;
// use types from @parity/abi
result[name + "$"] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// We only get the contract when the function is called for the 1st
// time. Note: getContract is memoized, won't create contract on each
// call.
var contract = getContract(address, abiJson, api);
var method = contract.instance[name]; // Hold the method from the Abi
// The last arguments in args can be an options object
var options = args.length === method.inputs.length + 1 ? args.pop() : {};
if (method.constant) {
return createRpc_1.default({
frequency: [frequency_1.default.onEveryBlock$],
name: address + ":" + name,
pipes: function () { return [
operators_1.switchMapPromise(function () {
return contract.instance[name].call(options, args);
})
]; }
})({ provider: api.provider }).apply(void 0, args);
}
else {
var estimate = options.estimate, passphrase = options.passphrase, txFields = __rest(options, ["estimate", "passphrase"]);
return post_1.post$(__assign({ to: address, data: encode_1.abiEncode(method.name, method.inputs.map(function (_a) {
var type = _a.kind.type;
return type;
}), // TODO Use @parity/api types
args) }, txFields), { estimate: estimate, passphrase: passphrase });
}
};
});
return result;
});
/**
* Create a contract.
*
* @param address - The contract address.
* @param abiJson - The contract abi.
* @param options - The options to pass in when creating the contract.
* @return - An object whose keys are all the functions of the
* contract, and each function return an Observable which will fire when the
* function resolves.
*/
exports.makeContract = function (address, abiJson, options) {
if (options === void 0) { options = {}; }
var provider = options.provider;
var api = provider ? api_1.createApiFromProvider(provider) : api_1.getApi();
return makeContractWithApi(address, abiJson, api);
};
;