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:
parent
4c8c0ddd43
commit
97b891925b
22
dist/contractLoader.js
vendored
Normal file
22
dist/contractLoader.js
vendored
Normal 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
17
dist/ocean-keeper.js
vendored
@ -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,
|
||||
|
13
package-lock.json
generated
13
package-lock.json
generated
@ -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"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
18
src/contractLoader.js
Normal file
18
src/contractLoader.js
Normal 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)
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user