update tests; use web3-eth

This commit is contained in:
Iuri Matias 2019-08-21 14:51:13 -04:00
parent 0ae7023c6b
commit 7fe20a8e9f
8 changed files with 9557 additions and 3981 deletions

2609
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,6 @@
"dependencies": { "dependencies": {
"lokijs": "^1.5.6", "lokijs": "^1.5.6",
"rxjs": "^6.5.2", "rxjs": "^6.5.2",
"web3": "^1.0.0-beta.37" "web3-eth": "^1.2.1"
} }
} }

View File

@ -4,13 +4,13 @@ const { randomString } = require('./utils.js');
const Database = require('./database.js'); const Database = require('./database.js');
const Events = require('events'); const Events = require('events');
const Web3 = require('web3'); const Web3Eth = require('web3-eth');
class EventSyncer { class EventSyncer {
constructor(provider, dbFilename) { constructor(provider, dbFilename) {
this.events = new Events(); this.events = new Events();
this.web3 = new Web3(provider); this.web3 = new Web3Eth(provider);
this.dbFilename = dbFilename || 'phoenix.db'; this.dbFilename = dbFilename || 'phoenix.db';
} }

View File

@ -10,14 +10,13 @@ import withObservables from '@nozbe/with-observables'
const { scan, map } = require('rxjs/operators'); const { scan, map } = require('rxjs/operators');
import { of } from 'rxjs'; import { of } from 'rxjs';
const phoenix = new Phoenix(web3.currentProvider);
const phoenix = new Phoenix(web3);
const RankItem = ({items, onUpvote, onDownvote}) => { const RankItem = ({items, onUpvote, onDownvote}) => {
return items.map((item, i) => <div key={i} className="item"> return items.map((item, i) => <div key={i} className="item">
<b>{i+1}</b> - {item.addr}<br /> <b>{i+1}</b> - {item.addr}<br />
Upvotes: {item.upvotes} - Downvotes: {item.downvotes}<br /> Upvotes: {item.upvotes} - Downvotes: {item.downvotes}<br />
<button onClick={onUpvote(item.addr)}>Upvote</button> | <button onClick={onDownvote(item.addr)}>Downvote</button> <button onClick={onUpvote(item.addr)}>Upvote</button> | <button onClick={onDownvote(item.addr)}>Downvote</button>
</div>); </div>);
}; };
@ -33,58 +32,54 @@ class App extends React.Component {
} }
componentDidMount(){ componentDidMount(){
EmbarkJS.onReady((err) => { //(async () => {
if(err){ EmbarkJS.onReady(async (err) => {
console.error(err); if(err){
return; console.error(err);
} return;
}
phoenix.init(() => {
observables.items = phoenix await phoenix.init()
.trackEvent(Ranking, 'Rating', {filter: {}, fromBlock: 1})
.pipe( observables.items = phoenix
scan((acc, curr) => { .trackEvent(Ranking, 'Rating', {filter: {}, fromBlock: 1})
const votes = {...acc[curr.addr]} || {}; .pipe(
return { scan((acc, curr) => {
...acc, const votes = {...acc[curr.addr]} || {};
[curr.addr]: { return {
upvotes: (votes.upvotes || 0) + (curr.rating >= 3 ? 1 : 0), ...acc,
downvotes: (votes.downvotes || 0) + (curr.rating < 3 ? 1 : 0) [curr.addr]: {
} upvotes: (votes.upvotes || 0) + (curr.rating >= 3 ? 1 : 0),
} downvotes: (votes.downvotes || 0) + (curr.rating < 3 ? 1 : 0)
}, {}), }
map(summary => { }
return Object.keys(summary).map(k => { }, {}),
return {addr: k, ...summary[k]} map(summary => {
}) return Object.keys(summary).map(k => {
}) return {addr: k, ...summary[k]}
); })
})
);
this.setState({ready: true}); this.setState({ready: true});
}); });
}); //})();
} }
upvote = address => () => { upvote = address => () => {
Ranking.methods.rate(address, 5).send(); Ranking.methods.rate(address, 5).send();
} }
downvote = address => () => { downvote = address => () => {
Ranking.methods.rate(address, 1).send(); Ranking.methods.rate(address, 1).send();
} }
render() { render() {
const {ready} = this.state; const {ready} = this.state;
if(!ready) return <span>Loading...</span>; if(!ready) return <span>Loading...</span>;
return <EnhancedRankItem items={observables.items} onUpvote={this.upvote} onDownvote={this.downvote} />; return <EnhancedRankItem items={observables.items} onUpvote={this.upvote} onDownvote={this.downvote} />;
} }
} }
ReactDOM.render(<App />, document.getElementById('content')); ReactDOM.render(<App />, document.getElementById('content'));

View File

@ -11,6 +11,7 @@
"devDependencies": {}, "devDependencies": {},
"dependencies": { "dependencies": {
"@nozbe/with-observables": "^1.0.5", "@nozbe/with-observables": "^1.0.5",
"embark": "^4.1.0",
"phoenix": "../../", "phoenix": "../../",
"react": "^16.9.0", "react": "^16.9.0",
"react-dom": "^16.9.0", "react-dom": "^16.9.0",

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,7 @@ import { map, scan} from 'rxjs/operators';
import Web3 from 'web3'; import Web3 from 'web3';
import withObservables from '@nozbe/with-observables' import withObservables from '@nozbe/with-observables'
const web3 = new Web3("ws://localhost:8545"); const web3 = new Web3("ws://localhost:8545");
const Escrow = ({escrow}) => { const Escrow = ({escrow}) => {
console.log("Received new escrow property", escrow); console.log("Received new escrow property", escrow);
@ -17,7 +16,6 @@ const enhance = withObservables(['escrow'], ({ escrow }) => ({ escrow }));
const EnhancedEscrow = enhance(Escrow) const EnhancedEscrow = enhance(Escrow)
class App extends React.Component { class App extends React.Component {
state = { state = {
@ -39,18 +37,14 @@ class App extends React.Component {
await this.EscrowContract.methods.createEscrow(1, accounts[1], accounts[0]).send({from: accounts[0]}) await this.EscrowContract.methods.createEscrow(1, accounts[1], accounts[0]).send({from: accounts[0]})
await this.EscrowContract.methods.createEscrow(1, accounts[0], accounts[2]).send({from: accounts[0]}) await this.EscrowContract.methods.createEscrow(1, accounts[0], accounts[2]).send({from: accounts[0]})
const eventSyncer = new Phoenix(web3); const eventSyncer = new Phoenix(web3.currentProvider);
eventSyncer.init(() => { await eventSyncer.init();
const replayObj = eventSyncer.trackEvent(this.EscrowContract, 'Created', {filter: {buyer: accounts[0]}, fromBlock: 1}); const replayObj = eventSyncer.trackEvent(this.EscrowContract, 'Created', {filter: {buyer: accounts[0]}, fromBlock: 1});
this.setState({ this.setState({
escrow: replayObj.asObservable() escrow: replayObj.asObservable()
});
}); });
})(); })();
} }
createTrx = async () => { createTrx = async () => {

1228
yarn.lock

File diff suppressed because it is too large Load Diff