1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

added contract loader

This commit is contained in:
Sebastian Gerske 2018-08-31 16:15:34 +02:00
parent 4c8c0ddd43
commit 97b891925b
5 changed files with 56 additions and 31 deletions

22
dist/contractLoader.js vendored Normal file
View File

@ -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);
}
}

17
dist/ocean-keeper.js vendored
View File

@ -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"); }); }; } 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 Web3 from 'web3';
import TruffleContract from 'truffle-contract'; import ContractLoader from "./contractLoader";
const DEFAULT_GAS = 300000; const DEFAULT_GAS = 300000;
@ -19,18 +19,9 @@ export default class OceanKeeper {
var _this = this; var _this = this;
return _asyncToGenerator(function* () { return _asyncToGenerator(function* () {
const OceanToken = require(`@oceanprotocol/keeper-contracts/artifacts/OceanToken.${_this.network}`); _this.oceanToken = yield ContractLoader.load('OceanToken', _this.network, _this.web3.currentProvider);
const oceanToken = TruffleContract(OceanToken); _this.oceanMarket = yield ContractLoader.load('OceanMarket', _this.network, _this.web3.currentProvider);
const OceanMarket = require(`@oceanprotocol/keeper-contracts/artifacts/OceanMarket.${_this.network}`); _this.oceanAuth = yield ContractLoader.load('OceanAuth', _this.network, _this.web3.currentProvider);
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);
return { return {
oceanToken: _this.oceanToken, oceanToken: _this.oceanToken,

13
package-lock.json generated
View File

@ -1725,13 +1725,12 @@
"resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-1.4.2.tgz", "resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-1.4.2.tgz",
"integrity": "sha1-jZWCAsftuq6Dlwf7pvCf8ydgYhA=", "integrity": "sha1-jZWCAsftuq6Dlwf7pvCf8ydgYhA=",
"requires": { "requires": {
"ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git#00ba8463a7f7a67fcad737ff9c2ebd95643427f7",
"ethereumjs-util": "^5.1.1" "ethereumjs-util": "^5.1.1"
}, },
"dependencies": { "dependencies": {
"ethereumjs-abi": { "ethereumjs-abi": {
"version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#00ba8463a7f7a67fcad737ff9c2ebd95643427f7", "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": { "requires": {
"bn.js": "^4.10.0", "bn.js": "^4.10.0",
"ethereumjs-util": "^5.0.0" "ethereumjs-util": "^5.0.0"
@ -5230,11 +5229,16 @@
"resolved": "https://registry.npmjs.org/web3/-/web3-0.20.6.tgz", "resolved": "https://registry.npmjs.org/web3/-/web3-0.20.6.tgz",
"integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=", "integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=",
"requires": { "requires": {
"bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934",
"crypto-js": "^3.1.4", "crypto-js": "^3.1.4",
"utf8": "^2.1.1", "utf8": "^2.1.1",
"xhr2": "*", "xhr2": "*",
"xmlhttprequest": "*" "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", "resolved": "https://registry.npmjs.org/web3/-/web3-0.20.6.tgz",
"integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=", "integrity": "sha1-PpcwauAk+yThCj11yIQwJWIhUSA=",
"requires": { "requires": {
"bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934",
"crypto-js": "^3.1.4", "crypto-js": "^3.1.4",
"utf8": "^2.1.1", "utf8": "^2.1.1",
"xhr2": "*", "xhr2": "*",
@ -5374,7 +5377,7 @@
"dependencies": { "dependencies": {
"bignumber.js": { "bignumber.js": {
"version": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", "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"
} }
} }
}, },

18
src/contractLoader.js Normal file
View File

@ -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)
}
}

View File

@ -1,5 +1,5 @@
import Web3 from 'web3' import Web3 from 'web3'
import TruffleContract from 'truffle-contract' import ContractLoader from "./contractLoader";
const DEFAULT_GAS = 300000 const DEFAULT_GAS = 300000
@ -12,18 +12,9 @@ export default class OceanKeeper {
} }
async initContracts() { async initContracts() {
const OceanToken = require(`@oceanprotocol/keeper-contracts/artifacts/OceanToken.${this.network}`) this.oceanToken = await ContractLoader.load('OceanToken', this.network, this.web3.currentProvider)
const oceanToken = TruffleContract(OceanToken) this.oceanMarket = await ContractLoader.load('OceanMarket', this.network, this.web3.currentProvider)
const OceanMarket = require(`@oceanprotocol/keeper-contracts/artifacts/OceanMarket.${this.network}`) this.oceanAuth = await ContractLoader.load('OceanAuth', this.network, this.web3.currentProvider)
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)
return { return {
oceanToken: this.oceanToken, oceanToken: this.oceanToken,