Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const Ganache = require('ganache-core')
const nock = require('nock')
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-15'
nock.disableNetConnect()
nock.enableNetConnect('localhost')
Enzyme.configure({ adapter: new Adapter() })
// disallow promises from swallowing errors
enableFailureOnUnhandledPromiseRejection()
// ganache server
const server = Ganache.server()
server.listen(8545, () => {
console.log('Ganache Testrpc is running on "http://localhost:8545"')
})
// logging util
const log = require('loglevel')
log.setDefaultLevel(5)
global.log = log
//
// polyfills
//
// fetch
global.fetch = require('isomorphic-fetch')
require('abortcontroller-polyfill/dist/polyfill-patch-fetch')
const Web3Eth = require('web3-eth');
const {deployRatingContract, mine} = require('./utils-web3');
const Subspace = require('../dist/node.js');
const ganache = require("ganache-core");
console.log("The following error is emitted by ganache - https://github.com/trufflesuite/ganache-core/issues/267")
let eth = new Web3Eth(ganache.provider());
async function run() {
let accounts = await eth.getAccounts();
var RatingContract = await deployRatingContract(eth)
// Events are generated in these blocks:
// x x x x x x x x x
// 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
await mine(eth);
await RatingContract.methods.doRating(1, 5).send({from: accounts[0]})
await mine(eth);
await mine(eth);
await RatingContract.methods.doRating(2, 3).send({from: accounts[0]})
await RatingContract.methods.doRating(3, 1).send({from: accounts[0]})
await RatingContract.methods.doRating(4, 5).send({from: accounts[0]})
await RatingContract.methods.doRating(5, 5).send({from: accounts[0]})
async function server(web3, provider) {
try {
// Hack to prevent "connection not open on send" error when using websockets
web3.setProvider(provider.replace(/^ws/, 'http'))
await web3.eth.net.getId()
web3.setProvider(provider)
return
} catch (e) {
/* Ignore */
}
var port = '7545'
if (String(provider).match(/:([0-9]+)$/)) {
port = provider.match(/:([0-9]+)$/)[1]
}
var server = Ganache.server()
await server.listen(port)
return server
}
async function server(web3, provider) {
try {
// Hack to prevent "connection not open on send" error when using websockets
web3.setProvider(provider.replace(/^ws/, 'http'))
await web3.eth.net.getId()
web3.setProvider(provider)
return
} catch (e) {
/* Ignore */
}
let port = '7545'
if (String(provider).match(/:([0-9]+)$/)) {
port = provider.match(/:([0-9]+)$/)[1]
}
const server = Ganache.server()
await server.listen(port)
return server
}
function createTestSetup () {
// raw data source
const dataProvider = GanacheCore.provider()
// create block tracker
const blockTracker = new BlockTracker({
provider: dataProvider,
pollingInterval: 100,
})
// create higher level
const engine = new JsonRpcEngine()
const provider = providerFromEngine(engine)
// add block ref middleware
engine.push(createBlockRefMiddleware({ provider, blockTracker }))
// hit tracker
const hitTracker = createHitTrackerMiddleware()
engine.push(hitTracker)
// add data source
engine.push(providerAsMiddleware(dataProvider))
const query = new EthQuery(provider)
const initProviderWeb3 = async ganacheOptions => {
// Spawn Ganache test blockchain
const provider = Ganache.provider(ganacheOptions || defaultGanacheOptions);
const web3 = new Web3(provider);
const accounts = await web3.eth.getAccounts();
return { provider, web3, accounts };
};
function createTestSetup () {
// raw data source
const ganacheProvider = GanacheCore.provider()
// create higher level
const engine = new JsonRpcEngine()
const provider = providerFromEngine(engine)
const query = new EthQuery(provider)
const ganacheQuery = new EthQuery(ganacheProvider)
return { engine, provider, ganacheProvider, query, ganacheQuery }
}
constructor({ loader, minerAddress, privateKey, provider, realProviderPort = 8545, useJsTree = false, dbPath = "./reputationStates.sqlite" }) {
this.loader = loader;
this.minerAddress = minerAddress;
this.dbPath = dbPath;
this.useJsTree = useJsTree;
if (!this.useJsTree) {
const ganacheProvider = ganache.provider({
network_id: 515,
vmErrorsOnRPCResponse: false,
locked: false,
verbose: true,
accounts: [
{
balance: "0x10000000000000000000000000",
secretKey
}
]
});
this.ganacheProvider = new ethers.providers.Web3Provider(ganacheProvider);
this.ganacheWallet = new ethers.Wallet(secretKey, this.ganacheProvider);
}
// This will have to support provider.getSigner https://docs.ethers.io/ethers.js/html/api-providers.html#jsonrpcprovider
const ganache = require("ganache-core");
const Web3Eth = require('web3-eth');
const Subspace = require('../dist/node.js');
console.log("The following error is emitted by ganache - https://github.com/trufflesuite/ganache-core/issues/267")
let eth = new Web3Eth(ganache.provider());
async function run() {
let accounts = await eth.getAccounts();
setTimeout(async () => {
await eth.sendTransaction({from: accounts[0], to: accounts[1], value: "100000000"});
await eth.sendTransaction({from: accounts[2], to: accounts[0], value: "999999999"});
await eth.sendTransaction({from: accounts[2], to: accounts[0], value: "232433434"});
}, 2000);
const subspace = new Subspace(eth.currentProvider);
await subspace.init();
subspace.trackBalance(accounts[0]).subscribe((balance) => {
console.log("accounts[0] balance is ", balance);
before("set up Ganache server", function(done) {
var logger = {
log: function(message) {
//console.log(message);
}
};
server = TestRPC.server({
logger: logger
});
server.listen(port, function(err) {
provider = new Web3.providers.HttpProvider("http://localhost:" + port);
done();
});
});