Created
June 1, 2018 02:49
-
-
Save nicovalencia/1fbf54bc3951975dfc064b308f5f7fc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
const fetch = require('node-fetch'); | |
const POLLING_INTERVAL = 5000; | |
const EVENTS = { | |
Transfer: {} | |
}; | |
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545")); | |
function loadContractInstance({ abiPath, address }) { | |
return new Promise((resolve, reject) => { | |
fetch(abiPath).then((resp) => { | |
return resp.json(); | |
}).then(data => { | |
let instance = new web3.eth.Contract(data.abi, address); | |
resolve(instance); | |
}); | |
}); | |
} | |
function rebuildTokenIndex(event) { | |
console.log('rebuilding'); | |
// Rebuild teh index here yo: | |
// | |
} | |
function pollForEvent({ instance, eventName, processCb }) { | |
setTimeout(() => pollForEvent(...arguments), POLLING_INTERVAL); | |
// Have to poll in dev until ganache supports WebSockets transport: | |
// https://github.com/trufflesuite/ganache-cli/issues/257 | |
instance.getPastEvents(eventName, { | |
fromBlock: 0, | |
}, (error, events) => { | |
events.forEach(e => { | |
if (!EVENTS[eventName][e.transactionHash]) { | |
EVENTS[eventName][e.transactionHash] = e; | |
processCb(e); | |
} else { | |
console.log('...duplicate event'); | |
} | |
}); | |
}); | |
} | |
loadContractInstance({ | |
abiPath: 'http://localhost:3000/abis/WorldCupPlayerToken.json', | |
address: '0x9a2b051b106f0d3be0a72defcad9799b27a3f46b', | |
}).then((instance) => { | |
pollForEvent({ | |
instance, | |
eventName: 'Transfer', | |
processCb: rebuildTokenIndex, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment