Skip to content

Instantly share code, notes, and snippets.

@artiya4u
Last active July 10, 2018 09:43
Show Gist options
  • Save artiya4u/2d63b1043330b023b5f90b6add9adeb7 to your computer and use it in GitHub Desktop.
Save artiya4u/2d63b1043330b023b5f90b6add9adeb7 to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const abi = require('./abi.json'); // ABI of Subscription.sol in "build" directory on truffle project.
// noinspection JSUnresolvedVariable
const web3 = new Web3(
new Web3.providers.WebsocketProvider(config.contract.provider),
);
const subscriptionContract = new web3.eth.Contract(
abi,
'0x000000000000000000000000000000000000000', // Your Subscription.sol contract address.
);
const subscription = subscriptionContract.events
.SubscriptionPurchase({ filter: {} }, (error, event) => {
if (error) return console.error(error);
console.log('Successfully subscribed!', event);
// TODO add user's expiration to database.
})
.on('data', event => {
console.log(event); // same results as the optional callback above
})
.on('changed', event => {
// remove event from local database
})
.on('error', console.error);
// unsubscribes the subscription
subscription.unsubscribe((error) => {
if (error) return console.error(error);
console.log('Successfully unsubscribed!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment