commit
8c86cf7f9c
File diff suppressed because it is too large
Load Diff
|
@ -32,6 +32,6 @@
|
|||
"dependencies": {
|
||||
"lokijs": "^1.5.6",
|
||||
"rxjs": "^6.5.2",
|
||||
"web3": "1.0.0-beta.37"
|
||||
"web3": "^1.0.0-beta.37"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
const { ReplaySubject } = require('rxjs');
|
||||
const { map, scan } = require('rxjs/operators');
|
||||
|
||||
const { exhaustMap, takeLast, take } = require('rxjs/operators');
|
||||
|
||||
let _scan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
acc.shift();
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
|
||||
let _average = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
|
||||
|
||||
let sub = new ReplaySubject();
|
||||
sub.next(1)
|
||||
sub.next(2)
|
||||
sub.next(3)
|
||||
sub.next(4)
|
||||
|
||||
// sub.pipe(_scan, _average).subscribe((v) => {
|
||||
sub.pipe(takeLast(2)).subscribe((v) => {
|
||||
console.log("got value " + v)
|
||||
})
|
||||
|
||||
// result:
|
||||
// got value 1
|
||||
// got value 1.5
|
||||
// got value 2
|
||||
// got value 2.5
|
||||
|
||||
// wanted result:
|
||||
// got value 2.5
|
||||
|
||||
console.dir("---------")
|
||||
|
||||
sub.next(5)
|
||||
sub.next(6)
|
||||
|
||||
// expected afterwards
|
||||
// got value 3.5
|
||||
// got value 4.5
|
|
@ -1,11 +1,14 @@
|
|||
const { fromEvent, interval, ReplaySubject } = require('rxjs');
|
||||
const { throttle, filter } = require('rxjs/operators');
|
||||
const loki = require('lokijs')
|
||||
const Events = require('events')
|
||||
|
||||
class EventSyncer {
|
||||
|
||||
constructor(events) {
|
||||
this.events = events;
|
||||
constructor(web3) {
|
||||
// this.events = events;
|
||||
this.events = new Events;
|
||||
this.web3 = web3;
|
||||
}
|
||||
|
||||
init(cb) {
|
||||
|
@ -41,8 +44,10 @@ class EventSyncer {
|
|||
cb();
|
||||
}
|
||||
|
||||
trackEvent(eventName, filterConditions) {
|
||||
let eventKey = eventName + "-from0x123";
|
||||
// trackEvent(eventName, filterConditions) {
|
||||
trackEvent(contractInstance, eventName, filterConditions) {
|
||||
// let eventKey = eventName + "-from0x123";
|
||||
let eventKey = eventName;
|
||||
|
||||
let tracked = this.db.getCollection('tracked')
|
||||
let lastEvent = tracked.find({ "eventName": eventName })[0]
|
||||
|
@ -61,18 +66,30 @@ class EventSyncer {
|
|||
sub.next(previous)
|
||||
}
|
||||
|
||||
let contractObserver = fromEvent(this.events, eventName)
|
||||
contractObserver.pipe(filter((x) => x.id > lastEvent.id)).pipe(filter(filterConditions)).subscribe((e) => {
|
||||
let contractObserver = fromEvent(this.events, "event-" + eventName)
|
||||
|
||||
// TODO: this should be moved to a 'smart' module
|
||||
// for e.g, it should start fromBlock, from the latest known block (which means it should store block info)
|
||||
// it should be able to do events X at the time to avoid slow downs as well as the 10k limit
|
||||
contractInstance.events[eventName].apply(contractInstance.events[eventName], [(filterConditions || {fromBlock: 0}), (err, event) => {
|
||||
// let eventObject = event.returnValues;
|
||||
// eventObject.id = event.id;
|
||||
this.events.emit("event-" + eventName, event);
|
||||
}])
|
||||
|
||||
// contractObserver.pipe(filter((x) => x.id > lastEvent.id)).pipe(filter(filterConditions)).subscribe((e) => {
|
||||
contractObserver.pipe().subscribe((e) => {
|
||||
console.dir("------- syncing event");
|
||||
e.eventKey = eventKey
|
||||
console.dir(e);
|
||||
// console.dir(e);
|
||||
if (children.find({ 'id': e.id }).length > 0) {
|
||||
console.dir("event already synced: " + e.id)
|
||||
} else {
|
||||
children.insert(e)
|
||||
// TODO: would be nice if trackEvent was smart enough to understand the type of returnValues and do the needed conversions
|
||||
children.insert(e.returnValues)
|
||||
tracked.updateWhere(((x) => x.eventName === eventName), ((x) => x.id = e.id))
|
||||
this.events.emit("updateDB")
|
||||
sub.next(e)
|
||||
sub.next(e.returnValues)
|
||||
}
|
||||
console.dir("-------");
|
||||
})
|
||||
|
|
30
src/index.js
30
src/index.js
|
@ -1,30 +1,12 @@
|
|||
// var Web3 = require('web3')
|
||||
const Events = require('events')
|
||||
const { map, scan } = require('rxjs/operators');
|
||||
|
||||
const Simulator = require('./simulator.js')
|
||||
const EventSyncer = require('./eventSyncer.js')
|
||||
|
||||
const events = new Events()
|
||||
const eventSyncer = new EventSyncer(events);
|
||||
const eventSyncer = new EventSyncer(web3);
|
||||
|
||||
eventSyncer.init(run);
|
||||
eventSyncer.init(() => {});
|
||||
|
||||
function run() {
|
||||
let myscan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
acc.shift();
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
// eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => {
|
||||
// console.dir("current average is " + v)
|
||||
// })
|
||||
|
||||
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
|
||||
|
||||
eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => {
|
||||
console.dir("current average is " + v)
|
||||
})
|
||||
|
||||
const simulator = new Simulator(events);
|
||||
simulator.emitEvents()
|
||||
}
|
||||
// return Event
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
// var Web3 = require('web3')
|
||||
const Events = require('events')
|
||||
const { map, scan } = require('rxjs/operators');
|
||||
|
||||
const Simulator = require('./simulator.js')
|
||||
const EventSyncer = require('./eventSyncer.js')
|
||||
|
||||
const events = new Events()
|
||||
const eventSyncer = new EventSyncer(events);
|
||||
|
||||
eventSyncer.init(run);
|
||||
|
||||
function run() {
|
||||
let myscan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
acc.shift();
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
|
||||
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
|
||||
|
||||
eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => {
|
||||
console.dir("current average is " + v)
|
||||
})
|
||||
|
||||
const simulator = new Simulator(events);
|
||||
simulator.emitEvents()
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
const { map, scan, last, distinctUntilChanged } = require('rxjs/operators');
|
||||
const Web3 = require('web3');
|
||||
|
||||
let web3 = new Web3("ws://localhost:8545");
|
||||
|
||||
let myscan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
acc.shift();
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
|
||||
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
|
||||
|
||||
async function deployContract() {
|
||||
let accounts = await web3.eth.getAccounts();
|
||||
|
||||
// pragma solidity >=0.4.22 <0.6.0;
|
||||
// contract Transaction {
|
||||
// event Rating(uint indexed escrowId, uint rating);
|
||||
// function doRating(uint escrowId, uint rating) external {
|
||||
// emit Rating(escrowId, rating);
|
||||
// }
|
||||
// }
|
||||
|
||||
let abi = [
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "escrowId",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"name": "rating",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "doRating",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "escrowId",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "rating",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Rating",
|
||||
"type": "event"
|
||||
}
|
||||
]
|
||||
|
||||
var contract = new web3.eth.Contract(abi)
|
||||
let instance = await contract.deploy({
|
||||
data: '0x608060405234801561001057600080fd5b5060e78061001f6000396000f3fe6080604052600436106039576000357c010000000000000000000000000000000000000000000000000000000090048063f60781a914603e575b600080fd5b348015604957600080fd5b50607d60048036036040811015605e57600080fd5b810190808035906020019092919080359060200190929190505050607f565b005b817ffdefdf8d82459f7b1eb157e5c44cbe6ee73d8ddd387511fe3622a3ee663b4697826040518082815260200191505060405180910390a2505056fea165627a7a7230582067833697a0e2bccb8bd624c0b06b2183641addb24f7931d8ec3979982bb663790029',
|
||||
arguments: []
|
||||
}).send({
|
||||
from: accounts[0],
|
||||
gas: '4700000'
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let accounts = await web3.eth.getAccounts();
|
||||
var RatingContract = await deployContract()
|
||||
console.dir(RatingContract)
|
||||
|
||||
await RatingContract.methods.doRating(1, 5).send({from: accounts[0]})
|
||||
await RatingContract.methods.doRating(1, 3).send({from: accounts[0]})
|
||||
await RatingContract.methods.doRating(1, 1).send({from: accounts[0]})
|
||||
await RatingContract.methods.doRating(1, 5).send({from: accounts[0]})
|
||||
|
||||
// RatingContract.events.getPastEvents('Rating', {fromBlock: 1})
|
||||
RatingContract.events.Rating({fromBlock: 1}, (err, event) => {
|
||||
// console.dir("new event")
|
||||
// console.dir(event)
|
||||
})
|
||||
|
||||
const EventSyncer = require('../src/eventSyncer.js')
|
||||
const eventSyncer = new EventSyncer(web3);
|
||||
|
||||
eventSyncer.init(() => {
|
||||
|
||||
// TODO: would be nice if trackEvent was smart enough to understand the type of returnValues and do the needed conversions
|
||||
eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), myscan, mymap).subscribe((v) => {
|
||||
// eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => x.rating)).subscribe((v) => {
|
||||
console.dir("value is ")
|
||||
console.dir(v)
|
||||
});
|
||||
|
||||
var max = scan((acc, curr) => {
|
||||
if (curr > acc) return curr;
|
||||
return acc;
|
||||
}, [])
|
||||
|
||||
// eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), last()).subscribe((v) => {
|
||||
eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), max, distinctUntilChanged()).subscribe((v) => {
|
||||
console.dir("max known rating is")
|
||||
console.dir(v)
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// await RatingContract.methods.doRating(1, 5).send({from: accounts[0]})
|
||||
// await RatingContract.methods.doRating(1, 3).send({from: accounts[0]})
|
||||
// await RatingContract.methods.doRating(1, 1).send({from: accounts[0]})
|
||||
// await RatingContract.methods.doRating(1, 5).send({from: accounts[0]})
|
||||
}
|
||||
|
||||
run()
|
|
@ -0,0 +1,118 @@
|
|||
const { map, scan, last, distinctUntilChanged } = require('rxjs/operators');
|
||||
const Web3 = require('web3');
|
||||
|
||||
let web3 = new Web3("ws://localhost:8545");
|
||||
|
||||
let myscan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
acc.shift();
|
||||
}
|
||||
return acc;
|
||||
}, [])
|
||||
|
||||
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
|
||||
|
||||
async function deployContract() {
|
||||
let accounts = await web3.eth.getAccounts();
|
||||
|
||||
// pragma solidity >=0.4.22 <0.6.0;
|
||||
// contract Escrow {
|
||||
// event Created(uint indexed escrowId, address buyer, address seller);
|
||||
// function createEscrow(uint escrowId, address buyer, address seller) external {
|
||||
// emit Created(escrowId, buyer, seller);
|
||||
// }
|
||||
// }
|
||||
|
||||
let abi = [
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "escrowId",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"name": "buyer",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "seller",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "createEscrow",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "escrowId",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "buyer",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "seller",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "Created",
|
||||
"type": "event"
|
||||
}
|
||||
]
|
||||
|
||||
var contract = new web3.eth.Contract(abi)
|
||||
let instance = await contract.deploy({
|
||||
data: '0x608060405234801561001057600080fd5b50610184806100206000396000f3fe60806040526004361061003b576000357c01000000000000000000000000000000000000000000000000000000009004806378015cf414610040575b600080fd5b34801561004c57600080fd5b506100b96004803603606081101561006357600080fd5b8101908080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506100bb565b005b827fcbd6f84bfed2ee8cc01ea152b5d9f7126a72c410dbc5ab04c486a5800627b1908383604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019250505060405180910390a250505056fea165627a7a72305820cc868ec126578f5508ee248fb823cd9f1ac6deb0562091cdf31843840b2a56410029',
|
||||
arguments: []
|
||||
}).send({
|
||||
from: accounts[0],
|
||||
gas: '4700000'
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let accounts = await web3.eth.getAccounts();
|
||||
var EscrowContract = await deployContract()
|
||||
console.dir(EscrowContract)
|
||||
|
||||
await EscrowContract.methods.createEscrow(1, accounts[0], accounts[1]).send({from: accounts[0]})
|
||||
await EscrowContract.methods.createEscrow(1, accounts[1], accounts[2]).send({from: accounts[0]})
|
||||
await EscrowContract.methods.createEscrow(1, accounts[1], accounts[0]).send({from: accounts[0]})
|
||||
await EscrowContract.methods.createEscrow(1, accounts[0], accounts[2]).send({from: accounts[0]})
|
||||
|
||||
// EscrowContract.events.getPastEvents('Rating', {fromBlock: 1})
|
||||
EscrowContract.events.Created({fromBlock: 1}, (err, event) => {
|
||||
// console.dir("new event")
|
||||
// console.dir(event)
|
||||
})
|
||||
|
||||
const EventSyncer = require('../src/eventSyncer.js')
|
||||
const eventSyncer = new EventSyncer(web3);
|
||||
|
||||
eventSyncer.init(() => {
|
||||
console.dir("getting escrows created by " + accounts[0])
|
||||
|
||||
// eventSyncer.trackEvent(EscrowContract, 'Created', ((x) => true)).pipe().subscribe((v) => {
|
||||
eventSyncer.trackEvent(EscrowContract, 'Created', {filter: {buyer: accounts[0]}, fromBlock: 1}).pipe().subscribe((v) => {
|
||||
// eventSyncer.trackEvent(EscrowContract, 'Rating', ((x) => true)).pipe(map(x => x.rating)).subscribe((v) => {
|
||||
console.dir("value is ")
|
||||
console.dir(v)
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
run()
|
Loading…
Reference in New Issue