From 25d5382231a9fcee446e52aeea97c9b532e4d802 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Thu, 4 Jan 2018 18:30:46 -0500 Subject: [PATCH] Remove solcjs upgrade logic and test example --- packages/truffle-contract/index.js | 47 -- .../truffle-contract/test/lib/MetaCoin.json | 4 +- .../truffle-contract/test/lib/MetaCoin.sol.js | 713 ------------------ packages/truffle-contract/test/upgrade.js | 44 -- 4 files changed, 2 insertions(+), 806 deletions(-) delete mode 100644 packages/truffle-contract/test/lib/MetaCoin.sol.js delete mode 100644 packages/truffle-contract/test/upgrade.js diff --git a/packages/truffle-contract/index.js b/packages/truffle-contract/index.js index b194d0bdf01..6ae8c833efe 100644 --- a/packages/truffle-contract/index.js +++ b/packages/truffle-contract/index.js @@ -9,53 +9,6 @@ var contract = function(options) { return Contract.clone(binary); }; -// To be used to upgrade old .sol.js abstractions -contract.fromSolJS = function(soljs_abstraction, ignore_default_network) { - if (ignore_default_network == null) { - ignore_default_network = false; - } - - // Find the latest binary - var latest_network = null; - var latest_network_updated_at = 0; - - var networks = {}; - - Object.keys(soljs_abstraction.all_networks).forEach(function(network_name) { - - if (network_name == "default") { - if (ignore_default_network == true ) { - return; - } else { - throw new Error(soljs_abstraction.contract_name + " has legacy 'default' network artifacts stored within it. Generally these artifacts were a result of running Truffle on a development environment -- in order to store contracts with truffle-contract, all networks must have an identified id. If you're sure this default network represents your development environment, you can ignore processing of the default network by passing `true` as the second argument to this function. However, if you think this network represents artifacts you'd like to keep (i.e., addresses deployed to the main network), you'll need to edit your .sol.js file yourself and change the default network id to be the id of your desired network. For most people, ignoring the default network is the correct option."); - } - } - - if (soljs_abstraction.all_networks[network_name].updated_at > latest_network_updated_at) { - latest_network = network_name; - latest_network_updated_at = soljs_abstraction.all_networks[network_name].updated_at; - } - - networks[network_name] = {}; - - ["address", "events", "links", "updated_at"].forEach(function(key) { - networks[network_name][key] = soljs_abstraction.all_networks[network_name][key]; - }) - }); - - latest_network = soljs_abstraction.all_networks[latest_network] || {}; - - var json = { - contractName: soljs_abstraction.contractName, - unlinked_binary: latest_network.unlinked_binary, - abi: latest_network.abi, - networks: networks, - updated_at: latest_network_updated_at == 0 ? undefined : latest_network_updated_at - }; - - return contract(json); -}; - module.exports = contract; if (typeof window !== "undefined") { diff --git a/packages/truffle-contract/test/lib/MetaCoin.json b/packages/truffle-contract/test/lib/MetaCoin.json index 903353eb36f..53687be8ef4 100644 --- a/packages/truffle-contract/test/lib/MetaCoin.json +++ b/packages/truffle-contract/test/lib/MetaCoin.json @@ -118,9 +118,9 @@ "ConvertLib": "0xfb2844d30ba493d0b3d661811b4200f04b5ce969" }, "address": "0x9b1f922a7d25d237883527c3de6b4765a6d25955", - "updated_at": 1492723344362 + "updatedAt": 1492723344362 } }, "schema_version": "0.0.5", - "updated_at": 1492723344362 + "updatedAt": 1492723344362 } diff --git a/packages/truffle-contract/test/lib/MetaCoin.sol.js b/packages/truffle-contract/test/lib/MetaCoin.sol.js deleted file mode 100644 index 9c40c502c2c..00000000000 --- a/packages/truffle-contract/test/lib/MetaCoin.sol.js +++ /dev/null @@ -1,713 +0,0 @@ -var Web3 = require("web3"); -var SolidityEvent = require("web3/lib/web3/event.js"); - -(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, instance, 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 decodeLogs = function(logs) { - return logs.map(function(log) { - var logABI = C.events[log.topics[0]]; - - if (logABI == null) { - return null; - } - - var decoder = new SolidityEvent(null, logABI, instance.address); - return decoder.decode(log); - }).filter(function(log) { - return log != null; - }); - }; - - 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) { - // If they've opted into next gen, return more information. - if (C.next_gen == true) { - return accept({ - tx: tx, - receipt: receipt, - logs: decodeLogs(receipt.logs) - }); - } else { - return accept(tx); - } - } - - if (timeout > 0 && new Date().getTime() - start > timeout) { - return reject(new Error("Transaction " + tx + " wasn't processed in " + (timeout / 1000) + " seconds!")); - } - - 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], instance, 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; - instance.transactionHash = contract.transactionHash; - }; - - // 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("MetaCoin error: Please call setProvider() first before calling new()."); - } - - var args = Array.prototype.slice.call(arguments); - - if (!this.unlinked_binary) { - throw new Error("MetaCoin 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("MetaCoin contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of MetaCoin: " + 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 MetaCoin.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: MetaCoin 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 = { - "12": { - "abi": [ - { - "constant": false, - "inputs": [ - { - "name": "addr", - "type": "address" - } - ], - "name": "getBalanceInEth", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "receiver", - "type": "address" - }, - { - "name": "amount", - "type": "uint256" - } - ], - "name": "sendCoin", - "outputs": [ - { - "name": "sufficient", - "type": "bool" - } - ], - "payable": false, - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "addr", - "type": "address" - } - ], - "name": "getBalance", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "type": "function" - }, - { - "inputs": [], - "payable": false, - "type": "constructor" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "_from", - "type": "address" - }, - { - "indexed": true, - "name": "_to", - "type": "address" - }, - { - "indexed": false, - "name": "_value", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" - } - ], - "unlinked_binary": "0x606060405234610000575b600160a060020a033216600090815260208190526040902061271090555b5b610223806100386000396000f300606060405263ffffffff60e060020a6000350416637bd703e8811461003a57806390b98a1114610065578063f8b2cb4f14610095575b610000565b3461000057610053600160a060020a03600435166100c0565b60408051918252519081900360200190f35b3461000057610081600160a060020a0360043516602435610140565b604080519115158252519081900360200190f35b3461000057610053600160a060020a03600435166101d8565b60408051918252519081900360200190f35b600073__ConvertLib____________________________6396e4ee3d6100e5846101d8565b60026000604051602001526040518363ffffffff1660e060020a028152600401808381526020018281526020019250505060206040518083038186803b156100005760325a03f415610000575050604051519150505b919050565b600160a060020a03331660009081526020819052604081205482901015610169575060006101d2565b600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35060015b92915050565b600160a060020a0381166000908152602081905260409020545b9190505600a165627a7a72305820cce8b461aef25f4c591a39faf5bda88517b1d4f6030ec6667b0132781f4d55660029", - "events": { - "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "_from", - "type": "address" - }, - { - "indexed": true, - "name": "_to", - "type": "address" - }, - { - "indexed": false, - "name": "_value", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" - } - }, - "updated_at": 1485880529645, - "links": { - "ConvertLib": "0xa8707fb47d661030845ce9e39ec8f3d651fe6edf" - }, - "address": "0xef32aade8dc01b3012b088cab88eac576b550689" - }, - "default": { - "abi": [ - { - "constant": false, - "inputs": [ - { - "name": "addr", - "type": "address" - } - ], - "name": "getBalanceInEth", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "receiver", - "type": "address" - }, - { - "name": "amount", - "type": "uint256" - } - ], - "name": "sendCoin", - "outputs": [ - { - "name": "sufficient", - "type": "bool" - } - ], - "payable": false, - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "addr", - "type": "address" - } - ], - "name": "getBalance", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "type": "function" - }, - { - "inputs": [], - "payable": false, - "type": "constructor" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "_from", - "type": "address" - }, - { - "indexed": true, - "name": "_to", - "type": "address" - }, - { - "indexed": false, - "name": "_value", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" - } - ], - "unlinked_binary": "0x606060405234610000575b600160a060020a033216600090815260208190526040902061271090555b5b610223806100386000396000f300606060405263ffffffff60e060020a6000350416637bd703e8811461003a57806390b98a1114610065578063f8b2cb4f14610095575b610000565b3461000057610053600160a060020a03600435166100c0565b60408051918252519081900360200190f35b3461000057610081600160a060020a0360043516602435610140565b604080519115158252519081900360200190f35b3461000057610053600160a060020a03600435166101d8565b60408051918252519081900360200190f35b600073__ConvertLib____________________________6396e4ee3d6100e5846101d8565b60026000604051602001526040518363ffffffff1660e060020a028152600401808381526020018281526020019250505060206040518083038186803b156100005760325a03f415610000575050604051519150505b919050565b600160a060020a03331660009081526020819052604081205482901015610169575060006101d2565b600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35060015b92915050565b600160a060020a0381166000908152602081905260409020545b9190505600a165627a7a72305820cce8b461aef25f4c591a39faf5bda88517b1d4f6030ec6667b0132781f4d55660029", - "events": { - "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "_from", - "type": "address" - }, - { - "indexed": true, - "name": "_to", - "type": "address" - }, - { - "indexed": false, - "name": "_value", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" - } - }, - "updated_at": 1485880169664, - "links": { - "ConvertLib": "0xe40f28cc7de51f9bdd7b6e6764ca1b358a6cd47f" - }, - "address": "0xb21e3c9aa8ccbbe9d901d8dd9a3269e7741f1bc3" - } -}; - - 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.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; - this.links = this.prototype.links = network.links || {}; - this.events = this.prototype.events = network.events || {}; - - this.network_id = network_id; - }; - - Contract.networks = function() { - return Object.keys(this.all_networks); - }; - - Contract.link = function(name, address) { - if (typeof name == "function") { - var contract = name; - - if (contract.address == null) { - throw new Error("Cannot link contract without an address."); - } - - Contract.link(contract.contract_name, contract.address); - - // Merge events so this contract knows about library's events - Object.keys(contract.events).forEach(function(topic) { - Contract.events[topic] = contract.events[topic]; - }); - - return; - } - - if (typeof name == "object") { - var obj = name; - Object.keys(obj).forEach(function(name) { - var a = obj[name]; - Contract.link(name, a); - }); - return; - } - - Contract.links[name] = address; - }; - - Contract.contract_name = Contract.prototype.contract_name = "MetaCoin"; - Contract.generated_with = Contract.prototype.generated_with = "3.2.0"; - - // Allow people to opt-in to breaking changes now. - Contract.next_gen = false; - - var properties = { - binary: function() { - var binary = Contract.unlinked_binary; - - Object.keys(Contract.links).forEach(function(library_name) { - var library_address = Contract.links[library_name]; - var regex = new RegExp("__" + library_name + "_*", "g"); - - binary = binary.replace(regex, library_address.replace("0x", "")); - }); - - return binary; - } - }; - - Object.keys(properties).forEach(function(key) { - var getter = properties[key]; - - var definition = {}; - definition.enumerable = true; - definition.configurable = false; - definition.get = getter; - - Object.defineProperty(Contract, key, definition); - Object.defineProperty(Contract.prototype, key, definition); - }); - - 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.MetaCoin = Contract; - } -})(); diff --git a/packages/truffle-contract/test/upgrade.js b/packages/truffle-contract/test/upgrade.js deleted file mode 100644 index a42ea1247f8..00000000000 --- a/packages/truffle-contract/test/upgrade.js +++ /dev/null @@ -1,44 +0,0 @@ -var contract = require("../"); -var MetaCoin = require("./lib/MetaCoin.sol.js"); -var assert = require("assert"); -var _ = require("lodash"); - -describe("Upgrading from soljs", function() { - - it("errors if a default network is specified and not ignored", function() { - try { - contract.fromSolJS(MetaCoin); - } catch (e) { - if (e.message.indexOf("MetaCoin has legacy 'default' network artifacts stored within it") != 0) { - throw new Error("Unexpected error:" + e.stack); - } - } - }); - - it("can upgrade and keep all network artifacts intact", function() { - var NewMetaCoin = contract.fromSolJS(MetaCoin, true); - - var abi = MetaCoin.abi; - var binary = MetaCoin.unlinked_binary; - - var all_networks = _.cloneDeep(MetaCoin.all_networks); - - // Remove unused items from all_networks - Object.keys(MetaCoin.all_networks).forEach(function(network_name) { - delete all_networks[network_name].abi; - delete all_networks[network_name].unlinked_binary; - }); - - // Delete the default network, as that'll get ignored. - delete all_networks["default"]; - - // Set the same networks (note: 12 comes from the .sol.js file itself) - NewMetaCoin.setNetwork(12); - MetaCoin.setNetwork(12); - - assert.deepEqual(NewMetaCoin.toJSON().networks, all_networks); - assert.equal(NewMetaCoin.binary, MetaCoin.binary); // This covers links - assert.deepEqual(NewMetaCoin.events, MetaCoin.events); - }); - -});