feat: watch erc20 balances
This commit is contained in:
parent
908492fe94
commit
2c7bee47cf
|
@ -6,7 +6,7 @@ const equal = require('fast-deep-equal');
|
|||
const Database = require('./database.js');
|
||||
const Events = require('events');
|
||||
const Web3Eth = require('web3-eth');
|
||||
|
||||
const toBN = require('number-to-bn');
|
||||
class EventSyncer {
|
||||
|
||||
constructor(provider, options = {}) {
|
||||
|
@ -143,6 +143,8 @@ class EventSyncer {
|
|||
trackBalance(address, erc20Address) {
|
||||
const sub = new ReplaySubject();
|
||||
|
||||
// TODO: validate address?
|
||||
|
||||
let callFn;
|
||||
if(!erc20Address){
|
||||
callFn = () => {
|
||||
|
@ -156,7 +158,19 @@ class EventSyncer {
|
|||
}]);
|
||||
};
|
||||
} else {
|
||||
// TODO: track erc20
|
||||
callFn = () => {
|
||||
const fn = this.web3.call;
|
||||
// balanceOf
|
||||
const data = "0x70a08231" + "000000000000000000000000" + erc20Address.substring(2);
|
||||
console.log(data);
|
||||
fn.apply(fn, [{to: erc20Address, data}, (err, result) => {
|
||||
if(err) {
|
||||
sub.error(err);
|
||||
return;
|
||||
}
|
||||
sub.next(toBN(result).toNumber());
|
||||
}]);
|
||||
};
|
||||
}
|
||||
|
||||
callFn();
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
const Web3Eth = require('web3-eth');
|
||||
|
||||
let eth = new Web3Eth("ws://localhost:8545");
|
||||
|
||||
async function deployContract() {
|
||||
let accounts = await eth.getAccounts();
|
||||
|
||||
// pragma solidity ^0.5.0;
|
||||
// contract ERC20Token {
|
||||
// function balanceOf(address _owner)
|
||||
// external
|
||||
// view
|
||||
// returns (uint256 balance)
|
||||
// {
|
||||
// return block.number;
|
||||
// }
|
||||
// }
|
||||
|
||||
let abi = [
|
||||
{
|
||||
"constant": true,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "balanceOf",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "balance",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}
|
||||
];
|
||||
|
||||
var contract = new eth.Contract(abi)
|
||||
let instance = await contract.deploy({
|
||||
data: '0x608060405234801561001057600080fd5b5060d58061001f6000396000f3fe6080604052600436106039576000357c01000000000000000000000000000000000000000000000000000000009004806370a0823114603e575b600080fd5b348015604957600080fd5b50608960048036036020811015605e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050609f565b6040518082815260200191505060405180910390f35b600043905091905056fea165627a7a72305820267fb09927dce2942fe73ac7487c4f1565dae60b08aa5434a22cb1f71366bd090029',
|
||||
arguments: []
|
||||
}).send({
|
||||
from: accounts[0],
|
||||
gas: '4700000'
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let accounts = await eth.getAccounts();
|
||||
var DummyERC20Token = await deployContract()
|
||||
console.dir(DummyERC20Token.options.address)
|
||||
|
||||
|
||||
const EventSyncer = require('../src/eventSyncer.js')
|
||||
const eventSyncer = new EventSyncer(eth.currentProvider);
|
||||
|
||||
await eventSyncer.init();
|
||||
eventSyncer.trackBalance(accounts[0], DummyERC20Token.options.address).pipe().subscribe((balance) => {
|
||||
console.log("balance is ", balance)
|
||||
});
|
||||
}
|
||||
|
||||
run()
|
Loading…
Reference in New Issue