From 97b891925b675f76ab2ed328eff507491eb657ac Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Fri, 31 Aug 2018 16:15:34 +0200 Subject: [PATCH] added contract loader --- dist/contractLoader.js | 22 ++++++++++++++++++++++ dist/ocean-keeper.js | 17 ++++------------- package-lock.json | 13 ++++++++----- src/contractLoader.js | 18 ++++++++++++++++++ src/ocean-keeper.js | 17 ++++------------- 5 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 dist/contractLoader.js create mode 100644 src/contractLoader.js diff --git a/dist/contractLoader.js b/dist/contractLoader.js new file mode 100644 index 0000000..50612fe --- /dev/null +++ b/dist/contractLoader.js @@ -0,0 +1,22 @@ +function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } + +import TruffleContract from 'truffle-contract'; + +const contracts = []; + +export default class ContractLoader { + static _doLoad(what, where, provider) { + return _asyncToGenerator(function* () { + // console.log("Loading", what, "from", where) + const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`); + const contract = TruffleContract(artifact); + contract.setProvider(provider); + contracts[what] = yield contract.at(artifact.address); + return contracts[what]; + })(); + } + + static load(what, where, provider) { + return contracts[what] || ContractLoader._doLoad(what, where, provider); + } +} \ No newline at end of file diff --git a/dist/ocean-keeper.js b/dist/ocean-keeper.js index 55a2af6..d4a5486 100644 --- a/dist/ocean-keeper.js +++ b/dist/ocean-keeper.js @@ -3,7 +3,7 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } import Web3 from 'web3'; -import TruffleContract from 'truffle-contract'; +import ContractLoader from "./contractLoader"; const DEFAULT_GAS = 300000; @@ -19,18 +19,9 @@ export default class OceanKeeper { var _this = this; return _asyncToGenerator(function* () { - const OceanToken = require(`@oceanprotocol/keeper-contracts/artifacts/OceanToken.${_this.network}`); - const oceanToken = TruffleContract(OceanToken); - const OceanMarket = require(`@oceanprotocol/keeper-contracts/artifacts/OceanMarket.${_this.network}`); - const oceanMarket = TruffleContract(OceanMarket); - const OceanAuth = require(`@oceanprotocol/keeper-contracts/artifacts/OceanAuth.${_this.network}`); - const oceanAuth = TruffleContract(OceanAuth); - oceanToken.setProvider(_this.web3.currentProvider); - oceanMarket.setProvider(_this.web3.currentProvider); - oceanAuth.setProvider(_this.web3.currentProvider); - _this.oceanToken = yield oceanToken.at(OceanToken.address); - _this.oceanMarket = yield oceanMarket.at(OceanMarket.address); - _this.oceanAuth = yield oceanAuth.at(OceanAuth.address); + _this.oceanToken = yield ContractLoader.load('OceanToken', _this.network, _this.web3.currentProvider); + _this.oceanMarket = yield ContractLoader.load('OceanMarket', _this.network, _this.web3.currentProvider); + _this.oceanAuth = yield ContractLoader.load('OceanAuth', _this.network, _this.web3.currentProvider); return { oceanToken: _this.oceanToken, diff --git a/package-lock.json b/package-lock.json index dfc3fcc..0594ab6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1725,13 +1725,12 @@ "resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-1.4.2.tgz", "integrity": "sha1-jZWCAsftuq6Dlwf7pvCf8ydgYhA=", "requires": { - "ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git#00ba8463a7f7a67fcad737ff9c2ebd95643427f7", "ethereumjs-util": "^5.1.1" }, "dependencies": { "ethereumjs-abi": { "version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#00ba8463a7f7a67fcad737ff9c2ebd95643427f7", - "from": "git+https://github.com/ethereumjs/ethereumjs-abi.git", + "from": "git+https://github.com/ethereumjs/ethereumjs-abi.git#00ba8463a7f7a67fcad737ff9c2ebd95643427f7", "requires": { "bn.js": "^4.10.0", "ethereumjs-util": "^5.0.0" @@ -5230,11 +5229,16 @@ "resolved": "https://registry.npmjs.org/web3/-/web3-0.20.6.tgz", "integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=", "requires": { - "bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", "crypto-js": "^3.1.4", "utf8": "^2.1.1", "xhr2": "*", "xmlhttprequest": "*" + }, + "dependencies": { + "bignumber.js": { + "version": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", + "from": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934" + } } } } @@ -5365,7 +5369,6 @@ "resolved": "https://registry.npmjs.org/web3/-/web3-0.20.6.tgz", "integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=", "requires": { - "bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", "crypto-js": "^3.1.4", "utf8": "^2.1.1", "xhr2": "*", @@ -5374,7 +5377,7 @@ "dependencies": { "bignumber.js": { "version": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", - "from": "git+https://github.com/frozeman/bignumber.js-nolookahead.git" + "from": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934" } } }, diff --git a/src/contractLoader.js b/src/contractLoader.js new file mode 100644 index 0000000..041c813 --- /dev/null +++ b/src/contractLoader.js @@ -0,0 +1,18 @@ +import TruffleContract from 'truffle-contract' + +const contracts = [] + +export default class ContractLoader { + static async _doLoad(what, where, provider) { + // console.log("Loading", what, "from", where) + const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`) + const contract = TruffleContract(artifact) + contract.setProvider(provider) + contracts[what] = await contract.at(artifact.address); + return contracts[what] + } + + static load(what, where, provider) { + return contracts[what] || ContractLoader._doLoad(what, where, provider) + } +} \ No newline at end of file diff --git a/src/ocean-keeper.js b/src/ocean-keeper.js index 661b598..4ce696a 100644 --- a/src/ocean-keeper.js +++ b/src/ocean-keeper.js @@ -1,5 +1,5 @@ import Web3 from 'web3' -import TruffleContract from 'truffle-contract' +import ContractLoader from "./contractLoader"; const DEFAULT_GAS = 300000 @@ -12,18 +12,9 @@ export default class OceanKeeper { } async initContracts() { - const OceanToken = require(`@oceanprotocol/keeper-contracts/artifacts/OceanToken.${this.network}`) - const oceanToken = TruffleContract(OceanToken) - const OceanMarket = require(`@oceanprotocol/keeper-contracts/artifacts/OceanMarket.${this.network}`) - const oceanMarket = TruffleContract(OceanMarket) - const OceanAuth = require(`@oceanprotocol/keeper-contracts/artifacts/OceanAuth.${this.network}`) - const oceanAuth = TruffleContract(OceanAuth) - oceanToken.setProvider(this.web3.currentProvider) - oceanMarket.setProvider(this.web3.currentProvider) - oceanAuth.setProvider(this.web3.currentProvider) - this.oceanToken = await oceanToken.at(OceanToken.address) - this.oceanMarket = await oceanMarket.at(OceanMarket.address) - this.oceanAuth = await oceanAuth.at(OceanAuth.address) + this.oceanToken = await ContractLoader.load('OceanToken', this.network, this.web3.currentProvider) + this.oceanMarket = await ContractLoader.load('OceanMarket', this.network, this.web3.currentProvider) + this.oceanAuth = await ContractLoader.load('OceanAuth', this.network, this.web3.currentProvider) return { oceanToken: this.oceanToken,