Send and receive transactions

Use the PegaSys Orchestrate client library to send transactions from JavaScript applications.

Prerequisites

Import and initialize the broker

const Orchestrate = require('pegasys-orchestrate');

Send transaction to Orchestrate

try {
    const producer = new Producer(['localhost:9092'])
    await producer.connect()
    await producer.sendTransaction({
      chainName: 'besu',
      contractName: 'SimpleToken',
      methodSignature: 'constructor()',
      from: '0x7e654d251da770a068413677967f6d3ea2fea9e4' // Default Orchestrate account in development mode
    })
  } catch (error) {
    console.error(error)
  }

Receive transaction from Orchestrate

// consume receives envelope from
// Orchestrate once transactions have been mined
await consumer.connect()

  // Register the event listener before calling consume
  consumer.on(EventType.Response, async (message: ResponseMessage) => {
    const { offset, topic, value } = message.content()

    console.log('Message received !', { offset, topic })
    if (value.errors && value.errors.length !== 0) {
      console.log('Transaction failed!', { errors: value.errors, txContext: value.txContext })
    } else {
      console.log('Receipt:', value.receipt)
    }

    // We commit every messgage
    await message.commit()
    console.log('Message committed')
  })

  await consumer.consume()
}