susyweb
Version:
Sophon JavaScript API, middleware to talk to a sophon node over RPC
355 lines (303 loc) • 10.2 kB
JavaScript
/*
This file is part of susyweb.js.
susyweb.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
susyweb.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MSRCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with susyweb.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file sof.js
* @author Marek Kotewicz <marek@sofdev.com>
* @author Fabian Vogelsteller <fabian@sofdev.com>
* @date 2015
*/
;
var formatters = require('../formatters');
var utils = require('../../utils/utils');
var Method = require('../method');
var Property = require('../property');
var c = require('../../utils/config');
var Contract = require('../contract');
var watches = require('./watches');
var Filter = require('../filter');
var IsSyncing = require('../syncing');
var namereg = require('../namereg');
var Iban = require('../iban');
var transfer = require('../transfer');
var blockCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "sof_getBlockByHash" : "sof_getBlockByNumber";
};
var transactionFromBlockCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'sof_getTransactionByBlockHashAndIndex' : 'sof_getTransactionByBlockNumberAndIndex';
};
var uncleCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'sof_getUncleByBlockHashAndIndex' : 'sof_getUncleByBlockNumberAndIndex';
};
var getBlockTransactionCountCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'sof_getBlockTransactionCountByHash' : 'sof_getBlockTransactionCountByNumber';
};
var uncleCountCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'sof_getUncleCountByBlockHash' : 'sof_getUncleCountByBlockNumber';
};
function Sof(susyweb) {
this._requestManager = susyweb._requestManager;
var self = this;
methods().forEach(function(method) {
method.attachToObject(self);
method.setRequestManager(self._requestManager);
});
properties().forEach(function(p) {
p.attachToObject(self);
p.setRequestManager(self._requestManager);
});
this.iban = Iban;
this.sendIBANTransaction = transfer.bind(null, this);
}
Object.defineProperty(Sof.prototype, 'defaultBlock', {
get: function () {
return c.defaultBlock;
},
set: function (val) {
c.defaultBlock = val;
return val;
}
});
Object.defineProperty(Sof.prototype, 'defaultAccount', {
get: function () {
return c.defaultAccount;
},
set: function (val) {
c.defaultAccount = val;
return val;
}
});
var methods = function () {
var getBalance = new Method({
name: 'getBalance',
call: 'sof_getBalance',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
var getStorageAt = new Method({
name: 'getStorageAt',
call: 'sof_getStorageAt',
params: 3,
inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter]
});
var getCode = new Method({
name: 'getCode',
call: 'sof_getCode',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatters.outputBlockFormatter
});
var getUncle = new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex],
outputFormatter: formatters.outputBlockFormatter,
});
var getCompilers = new Method({
name: 'getCompilers',
call: 'sof_getCompilers',
params: 0
});
var getBlockTransactionCount = new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.toDecimal
});
var getBlockUncleCount = new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.toDecimal
});
var getTransaction = new Method({
name: 'getTransaction',
call: 'sof_getTransactionByHash',
params: 1,
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionFromBlock = new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex],
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionReceipt = new Method({
name: 'getTransactionReceipt',
call: 'sof_getTransactionReceipt',
params: 1,
outputFormatter: formatters.outputTransactionReceiptFormatter
});
var getTransactionCount = new Method({
name: 'getTransactionCount',
call: 'sof_getTransactionCount',
params: 2,
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: utils.toDecimal
});
var sendRawTransaction = new Method({
name: 'sendRawTransaction',
call: 'sof_sendRawTransaction',
params: 1,
inputFormatter: [null]
});
var sendTransaction = new Method({
name: 'sendTransaction',
call: 'sof_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var signTransaction = new Method({
name: 'signTransaction',
call: 'sof_signTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var sign = new Method({
name: 'sign',
call: 'sof_sign',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, null]
});
var call = new Method({
name: 'call',
call: 'sof_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var estimateGas = new Method({
name: 'estimateGas',
call: 'sof_estimateGas',
params: 1,
inputFormatter: [formatters.inputCallFormatter],
outputFormatter: utils.toDecimal
});
var compilePolynomial = new Method({
name: 'compile.polynomial',
call: 'sof_compilePolynomial',
params: 1
});
var compileLLL = new Method({
name: 'compile.lll',
call: 'sof_compileLLL',
params: 1
});
var compileSerpent = new Method({
name: 'compile.serpent',
call: 'sof_compileSerpent',
params: 1
});
var submitWork = new Method({
name: 'submitWork',
call: 'sof_submitWork',
params: 3
});
var getWork = new Method({
name: 'getWork',
call: 'sof_getWork',
params: 0
});
return [
getBalance,
getStorageAt,
getCode,
getBlock,
getUncle,
getCompilers,
getBlockTransactionCount,
getBlockUncleCount,
getTransaction,
getTransactionFromBlock,
getTransactionReceipt,
getTransactionCount,
call,
estimateGas,
sendRawTransaction,
signTransaction,
sendTransaction,
sign,
compilePolynomial,
compileLLL,
compileSerpent,
submitWork,
getWork
];
};
var properties = function () {
return [
new Property({
name: 'coinbase',
getter: 'sof_coinbase'
}),
new Property({
name: 'mining',
getter: 'sof_mining'
}),
new Property({
name: 'hashrate',
getter: 'sof_hashrate',
outputFormatter: utils.toDecimal
}),
new Property({
name: 'syncing',
getter: 'sof_syncing',
outputFormatter: formatters.outputSyncingFormatter
}),
new Property({
name: 'gasPrice',
getter: 'sof_gasPrice',
outputFormatter: formatters.outputBigNumberFormatter
}),
new Property({
name: 'accounts',
getter: 'sof_accounts'
}),
new Property({
name: 'blockNumber',
getter: 'sof_blockNumber',
outputFormatter: utils.toDecimal
}),
new Property({
name: 'protocolVersion',
getter: 'sof_protocolVersion'
})
];
};
Sof.prototype.contract = function (abi) {
var factory = new Contract(this, abi);
return factory;
};
Sof.prototype.filter = function (options, callback, filterCreationErrorCallback) {
return new Filter(options, 'sof', this._requestManager, watches.sof(), formatters.outputLogFormatter, callback, filterCreationErrorCallback);
};
Sof.prototype.namereg = function () {
return this.contract(namereg.global.abi).at(namereg.global.address);
};
Sof.prototype.icapNamereg = function () {
return this.contract(namereg.icap.abi).at(namereg.icap.address);
};
Sof.prototype.isSyncing = function (callback) {
return new IsSyncing(this._requestManager, callback);
};
module.exports = Sof;