@paradeum/burrow
Version:
TypeScript library that calls a Hyperledger Burrow server over GRPC.
74 lines • 3.22 kB
JavaScript
;
exports.__esModule = true;
exports.Contract = void 0;
var event_1 = require("./event");
var function_1 = require("./function");
var defaultHandlers = {
call: function (result) { return result.raw; },
deploy: function (result) { return result.contractAddress; }
};
var Contract = /** @class */ (function () {
function Contract(abi, code, deployedBytecode, address, burrow, handlers) {
handlers = Object.assign({}, defaultHandlers, handlers);
this.address = address;
this.abi = abi;
this.code = code;
this.deployedBytecode = deployedBytecode;
this.burrow = burrow;
this.handlers = handlers;
addFunctionsToContract(this);
addEventsToContract(this);
}
return Contract;
}());
exports.Contract = Contract;
var addFunctionsToContract = function (contract) {
if (!contract.abi)
return;
contract.abi.filter(function (json) {
return (json.type === 'function' || json.type === 'constructor');
}).forEach(function (json) {
var _a = function_1.SolidityFunction(json, contract), displayName = _a.displayName, typeName = _a.typeName, call = _a.call, encode = _a.encode, decode = _a.decode;
if (json.type === 'constructor') {
contract._constructor = call.bind(contract, false, contract.handlers.deploy, '');
}
else {
// bind the function call to the contract, specify if call or transact is desired
var execute = call.bind(contract, json.constant, contract.handlers.call, null);
execute.sim = call.bind(contract, true, contract.handlers.call, null);
// These allow the interface to be used for a generic contract of this type
execute.at = call.bind(contract, json.constant, contract.handlers.call);
execute.atSim = call.bind(contract, true, contract.handlers.call);
execute.encode = encode.bind(contract);
execute.decode = decode.bind(contract);
// Attach to the contract object
if (!contract[displayName]) {
contract[displayName] = execute;
}
contract[displayName][typeName] = execute;
}
});
// Not every abi has a constructor specification.
// If it doesn't we force a _constructor with null abi
if (!contract._constructor) {
var call = function_1.SolidityFunction(null, contract).call;
contract._constructor = call.bind(contract, false, contract.handlers.deploy, '');
}
};
var addEventsToContract = function (contract) {
if (!contract.abi)
return;
contract.abi.filter(function (json) {
return json.type === 'event';
}).forEach(function (json) {
var _a = event_1.SolidityEvent(json, contract.burrow), displayName = _a.displayName, typeName = _a.typeName, call = _a.call;
var execute = call.bind(contract, null);
execute.once = call.bind(contract, null);
execute.at = call.bind(contract);
if (!contract[displayName]) {
contract[displayName] = execute;
}
contract[displayName][typeName] = call.bind(contract);
});
};
//# sourceMappingURL=contract.js.map