All Products
Search
Document Center

Event Interface

Last Updated: May 24, 2019

Subscribe to account events

You can call the event.account operation to subscribe to account events.

Request parameters

The following parameters are encapsulated and passed in as an object.

Name Required Type Description
to true string The target account.

Examples

  1. // Create an event instance
  2. const contractEvent = chain.event.contract({
  3. to: 'first'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Register the event callback
  8. contractEvent.on((err, data) => {
  9. console.log(data)
  10. })
  11. // Update the contract code
  12. chain.ctr.contract('first').update(bytecode, {}, (err, contract, data) => {})

Cancel the subscription of account events

You can call the accountEvent.close operation to cancel the subscription of account events. This operation does not require any request parameters.

Examples

  1. // Create an event instance
  2. const accountEvent = chain.event.account({
  3. to: 'Tester001'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Cancel the event
  8. accountEvent.close((err, data) => {
  9. console.log(data)
  10. })

Subscribe to contract events

You can call the event.contract operation to subscribe to contract events.

Request parameters

The following parameters are encapsulated and passed in as an object.

Name Required Type Description
to true string The hash or name of the target contract.

Examples

  1. // Create an event instance
  2. const contractEvent = chain.event.contract({
  3. to: 'first'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Register the event callback
  8. contractEvent.on((err, data) => {
  9. console.log(data)
  10. })
  11. // Update the contract code
  12. chain.ctr.UpdateContract({
  13. to: 'first',
  14. data: {
  15. code: bytecode
  16. }
  17. }, (err, data, rlpData) => {})

Unsubscribe contract events

You can call the contractEvent.close operation to unsubscribe account events. This operation does not require any request parameters.

Examples

  1. // Create an event instance
  2. const contractEvent = chain.event.contract({
  3. to: 'first'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Unsubscribe the event
  8. contractEvent.close((err, data) => {
  9. console.log(data)
  10. })

Subscribe to topic events

You can call the event.topic operation to subscribe to topic events.

Request parameters

The following parameters are encapsulated and passed in as an object.

Name Required Type Description
to true string The target topic.

Examples

  1. // Create an event instance
  2. const topicEvent = chain.event.topic({
  3. to: 'create_account'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Register the event callback
  8. topicEvent.on((err, data) => {
  9. console.log(data)
  10. })
  11. // Run CreateAccount to trigger the create_account topic event
  12. chain.ctr.CreateAccount({
  13. from: 'Tester001',
  14. to: 'Tester002',
  15. data: {
  16. recover_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  17. auth_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  18. auth_weight: 100
  19. }
  20. }, (err, data) => {})

Unsubscribe topic events

You can call the topicEvent.close operation to unsubscribe topic events. This operation does not require any request parameters.

Examples

  1. // Create an event instance
  2. const topicEvent = chain.event.topic({
  3. to: 'create_account'
  4. }, (err, data) => {
  5. console.log(data)
  6. })
  7. // Unsubscribe the event
  8. topicEvent.close((err, data) => {
  9. console.log(data)
  10. })

Subscribe to block events

You can call the event.block operation to subscribe to contract events. This operation requires no request parameters.

Examples

  1. // Create an event instance
  2. const blockEvent = chain.event.block({}, (err, data) => {
  3. console.log(data)
  4. })
  5. // Register the event callback to trigger the event while generating the block
  6. blockEvent.on((err, data) => {
  7. console.log(data)
  8. })

Unsubscribe block events

You can call the blockEvent.close operation to unsubscribe topic events. This operation does not require any request parameters.

Examples

  1. // Create an event instance
  2. const blockEvent = chain.event.block({}, (err, data) => {
  3. console.log(data)
  4. })
  5. // Unsubscribe the event
  6. blockEvent.close((err, data) => {
  7. console.log(data)
  8. })