scientistnik / btsdex

Package for work with BitShares DEX
MIT License
21 stars 27 forks source link

subscribe/unsubscribe/events #6

Closed wmbutler closed 6 years ago

wmbutler commented 6 years ago

Thanks for providing enhancements and adding to your instructions. It's much appreciated.

I am currently using the subscribe method to run a function once 'connected'. I have a few questions about how to handle monitoring activity to perform subsequent operations. Ideally, I'd like to receive events from the particular market(s) my bot is operating on and check to see if my orders need to be updated whenever the market changes.

If that is not possible, I expect I will run a timer that will loop through my startAfterConnected function at a regular interval. I'm hoping you can provide more guidance on observable events.

scientistnik commented 6 years ago

Now you need to use a timer. But in the next version I'll try to add an event to create a new block. Subscription for the account is also in the plans, but it turned out to be more complicated than I expected. I need time to test everything.

scientistnik commented 6 years ago

From btsdex v0.3.1 has three types of events:

For example:

const BitShares = require("btsdex");

BitShares.init("wss://bitshares.openledger.info/ws");

BitShares.subscribe('connected', startAfterConnected);
BitShares.subscribe('block', callEachBlock);
BitShares.subscribe('account', changeAccount, 'trade-bot');

async function startAfterConnected() {/* is called once after connecting to the blockchain */}
async function callEachBlock(obj) {/* is called with each block created */}
async function changeAccount(array) {/* is called when you change the 'trade-bot' account */}
wmbutler commented 6 years ago

This is awesome. Thanks!