Skip to content

Instantly share code, notes, and snippets.

@daragao
Created June 12, 2016 14:23
Show Gist options
  • Save daragao/42a6982a5ffbc1ac5190f4a2622dd479 to your computer and use it in GitHub Desktop.
Save daragao/42a6982a5ffbc1ac5190f4a2622dd479 to your computer and use it in GitHub Desktop.
Greeter.sol compiled
var Web3 = require("web3");
(function() {
// Planned for future features, logging, etc.
function Provider(provider) {
this.provider = provider;
}
Provider.prototype.send = function() {
this.provider.send.apply(this.provider, arguments);
};
Provider.prototype.sendAsync = function() {
this.provider.sendAsync.apply(this.provider, arguments);
};
var BigNumber = (new Web3()).toBigNumber(0).constructor;
var Utils = {
is_object: function(val) {
return typeof val == "object" && !Array.isArray(val);
},
is_big_number: function(val) {
if (typeof val != "object") return false;
// Instanceof won't work because we have multiple versions of Web3.
try {
new BigNumber(val);
return true;
} catch (e) {
return false;
}
},
merge: function() {
var merged = {};
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
var object = args[i];
var keys = Object.keys(object);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var value = object[key];
merged[key] = value;
}
}
return merged;
},
promisifyFunction: function(fn, C) {
var self = this;
return function() {
var instance = this;
var args = Array.prototype.slice.call(arguments);
var tx_params = {};
var last_arg = args[args.length - 1];
// It's only tx_params if it's an object and not a BigNumber.
if (Utils.is_object(last_arg) && !Utils.is_big_number(last_arg)) {
tx_params = args.pop();
}
tx_params = Utils.merge(C.class_defaults, tx_params);
return new Promise(function(accept, reject) {
var callback = function(error, result) {
if (error != null) {
reject(error);
} else {
accept(result);
}
};
args.push(tx_params, callback);
fn.apply(instance.contract, args);
});
};
},
synchronizeFunction: function(fn, C) {
var self = this;
return function() {
var args = Array.prototype.slice.call(arguments);
var tx_params = {};
var last_arg = args[args.length - 1];
// It's only tx_params if it's an object and not a BigNumber.
if (Utils.is_object(last_arg) && !Utils.is_big_number(last_arg)) {
tx_params = args.pop();
}
tx_params = Utils.merge(C.class_defaults, tx_params);
return new Promise(function(accept, reject) {
var callback = function(error, tx) {
if (error != null) {
reject(error);
return;
}
var timeout = C.synchronization_timeout || 240000;
var start = new Date().getTime();
var make_attempt = function() {
C.web3.eth.getTransactionReceipt(tx, function(err, receipt) {
if (err) return reject(err);
if (receipt != null) {
return accept(tx, receipt);
}
if (timeout > 0 && new Date().getTime() - start > timeout) {
return reject(new Error("Transaction " + tx + " wasn't processed in " + (timeout / 1000) + " seconds!"));
}
attempts += 1;
setTimeout(make_attempt, 1000);
});
};
make_attempt();
};
args.push(tx_params, callback);
fn.apply(self, args);
});
};
}
};
function instantiate(instance, contract) {
instance.contract = contract;
var constructor = instance.constructor;
// Provision our functions.
for (var i = 0; i < instance.abi.length; i++) {
var item = instance.abi[i];
if (item.type == "function") {
if (item.constant == true) {
instance[item.name] = Utils.promisifyFunction(contract[item.name], constructor);
} else {
instance[item.name] = Utils.synchronizeFunction(contract[item.name], constructor);
}
instance[item.name].call = Utils.promisifyFunction(contract[item.name].call, constructor);
instance[item.name].sendTransaction = Utils.promisifyFunction(contract[item.name].sendTransaction, constructor);
instance[item.name].request = contract[item.name].request;
instance[item.name].estimateGas = Utils.promisifyFunction(contract[item.name].estimateGas, constructor);
}
if (item.type == "event") {
instance[item.name] = contract[item.name];
}
}
instance.allEvents = contract.allEvents;
instance.address = contract.address;
};
// Use inheritance to create a clone of this contract,
// and copy over contract's static functions.
function mutate(fn) {
var temp = function Clone() { return fn.apply(this, arguments); };
Object.keys(fn).forEach(function(key) {
temp[key] = fn[key];
});
temp.prototype = Object.create(fn.prototype);
bootstrap(temp);
return temp;
};
function bootstrap(fn) {
fn.web3 = new Web3();
fn.class_defaults = fn.prototype.defaults || {};
// Set the network iniitally to make default data available and re-use code.
// Then remove the saved network id so the network will be auto-detected on first use.
fn.setNetwork("default");
fn.network_id = null;
return fn;
};
// Accepts a contract object created with web3.eth.contract.
// Optionally, if called without `new`, accepts a network_id and will
// create a new version of the contract abstraction with that network_id set.
function Contract() {
if (this instanceof Contract) {
instantiate(this, arguments[0]);
} else {
var C = mutate(Contract);
var network_id = arguments.length > 0 ? arguments[0] : "default";
C.setNetwork(network_id);
return C;
}
};
Contract.currentProvider = null;
Contract.setProvider = function(provider) {
var wrapped = new Provider(provider);
this.web3.setProvider(wrapped);
this.currentProvider = provider;
};
Contract.new = function() {
if (this.currentProvider == null) {
throw new Error("Contract error: Please call setProvider() first before calling new().");
}
var args = Array.prototype.slice.call(arguments);
if (!this.binary) {
throw new Error("Contract error: contract binary not set. Can't deploy new instance.");
}
var regex = /__[^_]+_+/g;
var unlinked_libraries = this.binary.match(regex);
if (unlinked_libraries != null) {
unlinked_libraries = unlinked_libraries.map(function(name) {
// Remove underscores
return name.replace(/_/g, "");
}).sort().filter(function(name, index, arr) {
// Remove duplicates
if (index + 1 >= arr.length) {
return true;
}
return name != arr[index + 1];
}).join(", ");
throw new Error("Contract contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of Contract: " + unlinked_libraries);
}
var self = this;
return new Promise(function(accept, reject) {
var contract_class = self.web3.eth.contract(self.abi);
var tx_params = {};
var last_arg = args[args.length - 1];
// It's only tx_params if it's an object and not a BigNumber.
if (Utils.is_object(last_arg) && !Utils.is_big_number(last_arg)) {
tx_params = args.pop();
}
tx_params = Utils.merge(self.class_defaults, tx_params);
if (tx_params.data == null) {
tx_params.data = self.binary;
}
// web3 0.9.0 and above calls new twice this callback twice.
// Why, I have no idea...
var intermediary = function(err, web3_instance) {
if (err != null) {
reject(err);
return;
}
if (err == null && web3_instance != null && web3_instance.address != null) {
accept(new self(web3_instance));
}
};
args.push(tx_params, intermediary);
contract_class.new.apply(contract_class, args);
});
};
Contract.at = function(address) {
if (address == null || typeof address != "string" || address.length != 42) {
throw new Error("Invalid address passed to Contract.at(): " + address);
}
var contract_class = this.web3.eth.contract(this.abi);
var contract = contract_class.at(address);
return new this(contract);
};
Contract.deployed = function() {
if (!this.address) {
throw new Error("Cannot find deployed address: Contract not deployed or address not set.");
}
return this.at(this.address);
};
Contract.defaults = function(class_defaults) {
if (this.class_defaults == null) {
this.class_defaults = {};
}
if (class_defaults == null) {
class_defaults = {};
}
var self = this;
Object.keys(class_defaults).forEach(function(key) {
var value = class_defaults[key];
self.class_defaults[key] = value;
});
return this.class_defaults;
};
Contract.extend = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < arguments.length; i++) {
var object = arguments[i];
var keys = Object.keys(object);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var value = object[key];
this.prototype[key] = value;
}
}
};
Contract.all_networks = {
"default": {
"abi": [
{
"constant": false,
"inputs": [],
"name": "kill",
"outputs": [],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "greet",
"outputs": [
{
"name": "",
"type": "string"
}
],
"type": "function"
},
{
"inputs": [
{
"name": "_greeting",
"type": "string"
}
],
"type": "constructor"
}
],
"binary": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
"unlinked_binary": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
"updated_at": 1465741285681
}
};
Contract.checkNetwork = function(callback) {
var self = this;
if (this.network_id != null) {
return callback();
}
this.web3.version.network(function(err, result) {
if (err) return callback(err);
var network_id = result.toString();
// If we have the main network,
if (network_id == "1") {
var possible_ids = ["1", "live", "default"];
for (var i = 0; i < possible_ids.length; i++) {
var id = possible_ids[i];
if (Contract.all_networks[id] != null) {
network_id = id;
break;
}
}
}
if (self.all_networks[network_id] == null) {
return callback(new Error(self.name + " error: Can't find artifacts for network id '" + network_id + "'"));
}
self.setNetwork(network_id);
callback();
})
};
Contract.setNetwork = function(network_id) {
var network = this.all_networks[network_id] || {};
this.abi = this.prototype.abi = network.abi;
this.binary = this.prototype.binary = network.binary;
this.unlinked_binary = this.prototype.unlinked_binary = network.unlinked_binary;
this.address = this.prototype.address = network.address;
this.updated_at = this.prototype.updated_at = network.updated_at;
if (this.unlinked_binary == null || this.unlinked_binary == "") {
this.unlinked_binary = this.prototype.unlinked_binary = this.binary;
}
this.network_id = network_id;
};
Contract.networks = function() {
return Object.keys(this.all_networks);
};
Contract.contract_name = Contract.prototype.contract_name = "Contract";
Contract.generated_with = Contract.prototype.generated_with = "3.0.3";
bootstrap(Contract);
if (typeof module != "undefined" && typeof module.exports != "undefined") {
module.exports = Contract;
} else {
// There will only be one version of this contract in the browser,
// and we can use that.
window.Contract = Contract;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment