web3 subscribe topics

CrethialCrethial GermanyMember Posts: 6
edited December 2017 in web3-js
Hi,

I'm trying to write a little script using web3 1.0.0-beta.26 to watch things on the blockchain related to CryptoKitties.

Somehow my first two attempts at posting some code got cut off.

Here is some code:
    var Web3 = require('web3');
    var web3 = new Web3(Web3.givenProvider || "ws://localhost:8546");
    
    var subscription = web3.eth.subscribe('logs', {
    	address: '0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C' // sales auction contract
    }, function(error, result) {
    	if (!error) {
    		console.log(result);
    	}
    });

It works, I'm getting events but I don't know how to interpret the "topics" array.
From what I've read it's supposed to be the sha3() hash of the fingerprint of the event.
I've tried fingerprinting lots of strings and none of them match.

According to the latest ERC721 draft, the event should be Transfer(address,address,uint256).

Yet console.log(web3.utils.sha3("Transfer(address,address,uint256)"));
yields 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef which is a value that never appears in the list of topics I am receiving.


Here are some results:
{ address: '0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C',
  topics:
   [ '0x4fcc30d90a842164dd58501ab874a101a3749c3d4747139cefe7c876f4ccebd2' ],
  data: '0x00000000000000000000000000000000000000000000000000000000000409020000000000000000000000000000000000000000000000000018de76816d8000
0000000000000000000000005f8119e5cab14c2f4ecc81cef51789f1a3bc611a',
  blockNumber: 0,
  transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  transactionIndex: 0,
  blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  logIndex: 0,
  removed: false,
  id: 'log_247b1779',
}
{ address: '0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C',
  topics:
   [ '0xa9c8dfcda5664a5a124c713e386da27de87432d5b668e79458501eb296389ba7' ],
  data: '0x000000000000000000000000000000000000000000000000000000000003ad5e000000000000000000000000000000000000000000000000008e1bc9bf040000000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000002a300',
  blockNumber: 0,
  transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  transactionIndex: 0,
  blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  logIndex: 0,
  removed: false,
  id: 'log_247b1779',
}
{ address: '0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C',
  topics: 
   [ '0x2809c7e17bf978fbc7194c0a694b638c4215e9140cacc6c38ca36010b45697df' ],
  data: '0x00000000000000000000000000000000000000000000000000000000000270d0',
  blockNumber: 0,
  transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  transactionIndex: 0,
  blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  logIndex: 0,
  removed: false,
  id: 'log_247b1779',
 }
Could someone explain to me the values for the topics array I am seeing here? I tried to hash various Event signatures with sha3() but it came up with something else.
Post edited by Crethial on

Comments

  • CrethialCrethial GermanyMember Posts: 6
    I figured it out by looking at the ABI.

    One of the events is "AuctionSuccessful" with the signature
    AuctionSuccessful(uint256,uint256,address)

    sha3() yields 0x4fcc30d90a842164dd58501ab874a101a3749c3d4747139cefe7c876f4ccebd2

    Another one is

    AuctionCreated(uint256,uint256,uint256,uint256)

    yielding

    0xa9c8dfcda5664a5a124c713e386da27de87432d5b668e79458501eb296389ba7

    Here is my new working code:
    #!/usr/bin/env node
    var Web3 = require('web3');
    var web3 = new Web3(Web3.givenProvider || "ws://localhost:8546");
    var sprintf = require('sprintf-js').sprintf;
    var abi = [...] // abi from etherscan
    
    var contractAddress = '0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C';
    var KittyAuction = new web3.eth.Contract(abi, contractAddress);
    
    KittyAuction.events.AuctionSuccessful(null, function(error, result) {
            if (error) return
            //console.log(result)
            var tokenId = result.returnValues.tokenId;
            var price = result.returnValues.totalPrice;
            var winner = result.returnValues.winner;
            console.log(sprintf("Kittie: %6s price: %2.4f winner: %s", tokenId, price/1000000000000000000, winner));
    });
Sign In or Register to comment.