add test for max value

This commit is contained in:
Iuri Matias 2019-08-12 11:10:02 -04:00
parent 3e8a6a244d
commit 1f11062a80
3 changed files with 135 additions and 5 deletions

View File

@ -71,7 +71,7 @@ class EventSyncer {
// 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], [{fromBlock: 0}, (err, event) => {
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);

View File

@ -1,4 +1,4 @@
const { map, scan } = require('rxjs/operators');
const { map, scan, last, distinctUntilChanged } = require('rxjs/operators');
const Web3 = require('web3');
let web3 = new Web3("ws://localhost:8545");
@ -94,9 +94,21 @@ async function run() {
eventSyncer.init(() => {
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 ")
// 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)
});

118
test/test2.js Normal file
View File

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