maticnetwork / dagger.js

Simple library to connect with dagger server and manage subscriptions for Ethereum Blockchain.
https://matic.network/dagger
MIT License
218 stars 42 forks source link

Multiple Address in filter #37

Closed dharmveersinh closed 4 years ago

dharmveersinh commented 4 years ago

Hello, Currently, I am using the following code to filter the transaction:

const Web3 = require('web3');
const Dagger = require('@maticnetwork/dagger');
const dagger = new Dagger('wss://kovan.dagger.matic.network');
const abi = require('./abi.json');

var web3 = new Web3('wss://kovan.infura.io/ws/v3/<Infura Key>');
var web3Contract = new web3.eth.Contract(abi,'0x9dd6a7dc18f20a3dcb4d422a7387747072524886');
var contrat = dagger.contract(web3Contract);
var filter = contrat.events.Transfer({filter: {to: '0x4601A10F14C480d22830070aCF6c2c5455DAC27D'}, room: 'latest'});
filter.watch(async function(data, removed){
    console.log('QRXD Recived');
    var a = await web3.eth.getTransaction(data.transactionHash);
    console.log(a);
});

But how can I filter multiple addresses? Thanks in advance.

itzmeanjan commented 4 years ago

Hello @dharmveersinh , can you try passing addresses as an array ?

var filter = contrat.events.Transfer({filter: {to: ['0x4601A10F14C480d22830070aCF6c2c5455DAC27D', '0x4601A10F14C480d22830070aCF6c2c5455DAC27E']}, room: 'latest'});
filter.watch(async function(data, removed){
    console.log('QRXD Recived');
    var a = await web3.eth.getTransaction(data.transactionHash);
    console.log(a);
});
dharmveersinh commented 4 years ago

Hello @itzmeanjan, Appreciate your help but it's not working. Giving me:

.../Test/EthereumTest/node_modules/@maticnetwork/dagger/lib/contract.js:66
        return (t || '+').toLowerCase();
                          ^

TypeError: (t || "+").toLowerCase is not a function

Here is the updated code:

var filter = contrat.events.Transfer({filter: {to: ['0x4601A10F14C480d22830070aCF6c2c5455DAC27D', '0x8b88Ce99Bca33708C9A3414f3DbC34b181A3d6BB']}, room: 'latest'});
filter.watch(async function(data, removed){
    console.log('QRXD Recived');
    var a = await web3.eth.getTransaction(data.transactionHash);
    console.log(a);
});
itzmeanjan commented 4 years ago

Looks like to array is being passed as it is, not iterated over. But you can give it a try, where you get notified for keccak256(Transfer(...)) topic, where you get to set to/ from addresses or leave it by putting */ #.

Let me know how does it behave.

dharmveersinh commented 4 years ago

What I really wanted to do is, just filtering out the transfer events emitted with my multiple addresses on someone's contract. EX: There is a USDT's contract and I have multiple addresses, so whenever any transaction is emitted with those addresses I get notified and I can do further process.

itzmeanjan commented 4 years ago

Then you can create multiple subscriptions.

dharmveersinh commented 4 years ago

But I have more than 1000 addresses. Will it be a memory/CPU issue?

itzmeanjan commented 4 years ago

Yes it'll be. And I guess even if it was allowed to set multiple addresses as an array, it won't let you set that many under a single subscription. Rather you can give it a try and then filter out your address related events. And create same filter for each of ERC20 which you want to track.

dharmveersinh commented 4 years ago

What I am understanding in this code is: dagger.on('latest:log/0xa74476443119a942de498590fe1f2454d7d4ac0d/filter/0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef/0x7da82c7ab4771ff031b66538d2fb9b0b047f6cf9/#', ...) Contract address:0xa74476443119a942de498590fe1f2454d7d4ac0d No idea what this is:0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef Account address: 0x7da82c7ab4771ff031b66538d2fb9b0b047f6cf9 So after the last address and before '#' I can add multiple account addresses which I want to filter, right?

dharmveersinh commented 4 years ago

Well, I tried doing:

dagger.on('latest:log/0x9dd6a7dc18f20a3dcb4d422a7387747072524886/filter/0x4601A10F14C480d22830070aCF6c2c5455DAC27D/0x8b88Ce99Bca33708C9A3414f3DbC34b181A3d6BB/#', result => {
  console.log("New Log : ", result)
})

No Output on any address of these two addresses I tried.

itzmeanjan commented 4 years ago

0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

This is web3.utils.keccak256('Transfer(address,address,uint256)')

itzmeanjan commented 4 years ago

So after the last address and before '#' I can add multiple account addresses which I want to filter, right?

No you can't, you're going to receive all Transfer events where from is some address but to can be anything

itzmeanjan commented 4 years ago

0x4601A10F14C480d22830070aCF6c2c5455DAC27D

I'm not sure which event are you trying to listen

dharmveersinh commented 4 years ago

Thank you for the Information. I guess I need to add multiple subscriptions over my different addresses.