mirror of
https://github.com/embarklabs/subspace.git
synced 2025-02-22 11:28:15 +00:00
feat: track eth balances
This commit is contained in:
parent
34ca4a7563
commit
908492fe94
@ -19,7 +19,7 @@ class EventSyncer {
|
||||
|
||||
this.newBlocksSubscription = null;
|
||||
this.intervalTracker = null;
|
||||
this.callMethods = [];
|
||||
this.callables = [];
|
||||
}
|
||||
|
||||
init() {
|
||||
@ -96,7 +96,7 @@ class EventSyncer {
|
||||
return;
|
||||
}
|
||||
|
||||
this.callMethods.forEach(fn => {
|
||||
this.callables.forEach(fn => {
|
||||
fn();
|
||||
});
|
||||
});
|
||||
@ -106,7 +106,7 @@ class EventSyncer {
|
||||
if(this.intervalTracker != null || this.options.callInterval === 0) return;
|
||||
|
||||
this.intervalTracker = setInterval(() => {
|
||||
this.callMethods.forEach(fn => {
|
||||
this.callables.forEach(fn => {
|
||||
fn();
|
||||
});
|
||||
}, this.options.callInterval);
|
||||
@ -134,7 +134,37 @@ class EventSyncer {
|
||||
this._initNewBlocksSubscription();
|
||||
this._initCallInterval();
|
||||
|
||||
this.callMethods.push(callContractMethod);
|
||||
this.callables.push(callContractMethod);
|
||||
|
||||
return sub.pipe(distinctUntilChanged((a, b) => equal(a, b)));
|
||||
}
|
||||
|
||||
// TODO: should save value in database (?)
|
||||
trackBalance(address, erc20Address) {
|
||||
const sub = new ReplaySubject();
|
||||
|
||||
let callFn;
|
||||
if(!erc20Address){
|
||||
callFn = () => {
|
||||
const fn = this.web3.getBalance;
|
||||
fn.apply(fn, [address, (err, balance) => {
|
||||
if(err) {
|
||||
sub.error(err);
|
||||
return;
|
||||
}
|
||||
sub.next(balance);
|
||||
}]);
|
||||
};
|
||||
} else {
|
||||
// TODO: track erc20
|
||||
}
|
||||
|
||||
callFn();
|
||||
|
||||
this._initNewBlocksSubscription();
|
||||
this._initCallInterval();
|
||||
|
||||
this.callables.push(callFn);
|
||||
|
||||
return sub.pipe(distinctUntilChanged((a, b) => equal(a, b)));
|
||||
}
|
||||
@ -143,7 +173,7 @@ class EventSyncer {
|
||||
clearInterval(this.intervalTracker);
|
||||
this.newBlocksSubscription.unsubscribe();
|
||||
this.intervalTracker = null;
|
||||
this.callMethods = [];
|
||||
this.callables = [];
|
||||
}
|
||||
|
||||
}
|
||||
|
26
test/test4.js
Normal file
26
test/test4.js
Normal file
@ -0,0 +1,26 @@
|
||||
const Web3Eth = require('web3-eth');
|
||||
|
||||
let eth = new Web3Eth("ws://localhost:8545");
|
||||
|
||||
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"});
|
||||
}, 3000);
|
||||
|
||||
const EventSyncer = require('../src/eventSyncer.js')
|
||||
const eventSyncer = new EventSyncer(eth.currentProvider);
|
||||
|
||||
await eventSyncer.init();
|
||||
|
||||
eventSyncer.trackBalance(accounts[0]).pipe().subscribe((balance) => {
|
||||
console.log("accounts[0] balance is ", balance);
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
run()
|
Loading…
x
Reference in New Issue
Block a user