Quick Start SDK

Goal

SDKs allow us to:

  • Send transactions via Orchestrate;
  • Consume mined transactions in a decoded format from Orchestrate;
  • Generate private keys stored in Orchestrate.

In charge of interacting with Orchestrate SDK will:

  • Marshal message in protobuf format and send it to a Kafka queue;
  • Listen a Kafka queue of decoded transactions, and unmarshall messages.

Quick-Start

To quickly start implementing Orchestrate

.1. Install

npm install pegasys-orchestrate --save

.2. Import and initialize the broker

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

// produce allows to send an envelope to Orchestrate
// requesting a transaction execution
const produce = async broker => {
  try {
    const producer = await broker.producer();

    try {
      const msg = await producer.send({
        // Indicate a chain ID, Orchestrate is connected to (here assuming Ropsten)
        chainId: '3',
        // Indicate a name of a Smart Contract that has been previously registered
        // on Orchestrate (using the CLI for example)
        call: {
          contract: {
            name: 'Counter',
            tag: 'v0.1.0',
          },
          method: 'constructor()',
        },
        gas: 2000000,
        // Indicate sender address, it should refer to an account 
        // previously generated in Orchestrate (using the CLI for example) prefunded with Ether
        from: '0x7e654d251da770a068413677967f6d3ea2fea9e4',
      });
      console.log('Message sent: ', msg);
    } catch (e) {
      console.error(e);
    }
  } catch (e) {
    console.error(e);
  }
};

// consume allows to receive envelope from
// Orchestrate once transactions have been mined 
const consume = async broker => {
  try {
    const consumer = await broker.consumer();

    const consume = consumer.consume();

    consume.on('message', message => {
      console.log('Message consumed: ', message);
    });

    consume.on('error', error => {
      console.error(error);
    });

    consume.on('offsetOutOfRange', error => {
      console.error(error);
    });
  } catch (e) {
    console.error(e);
  }
};

(async () => {
  try {
    // Initialize Orchestrate instance 
    const orch = new Orchestrate();
    const broker = orch.broker('localhost:9092');

    // Start consuming
    consume(broker);

    // Produce envelope
    produce(broker);
  } catch (e) {
    console.error(e);
  }
})();

// Message sent:  { offset: 9, id: '5b479d04-8dcc-498d-af51-c3e456a070f4' }

// Message consumed:  {
//     topic: 'topic-tx-decoded',
//     value: {
//         chain: {
//             id: '888',
//             iseip155: false
//         },
//         sender: {
//             id: '',
//             addr: '0x7e654d251da770a068413677967f6d3ea2fea9e4'
//         },
//         receiver: undefined,
//         call: {
//             contract: {
//                 registry: '',
//                 name: 'SimpleToken',
//                 tag: '',
//                 abi: '',
//                 bytecode: '',
//                 deployedbytecode: ''
//             },
//             method: {
//                 signature: 'constructor()',
//                 abi: ''
//             },
//             argsList: [],
//             quorum: undefined
//         },
//         tx: {
//             txData: {
//                 nonce: 1,
//                 to: '',
//                 value: '',
//                 gas: 2000000,
//                 gasPrice: '0x4a817c800',
//                 data: '0x60806040523480156100......7ddfc7f0d879a47389c19964d60029'
//             },
//             raw: '0xf90c65808504........714f64e08440f53c8b70'
//             ',
//             hash: '0x71c6cd4fe0d6c17dbae9dde7deb38cb14aafc2fed5eb41b0c7a3b596501da41c'
//         },
//         receipt: {
//             txHash: '0x71c6cd4fe0d6c17dbae9dde7deb38cb14aafc2fed5eb41b0c7a3b596501da41c',
//             blockHash: '0xbaca9e2e5843704a0f6bf42557bba155cc07885726f9a0faafda27e4803ed1c5',
//             blockNumber: 35,
//             txIndex: 0,
//             contractAddress: '0xe5ce65038f9d1c841a33CC816eE674F8a0E31E74',
//             postState: '',
//             status: 1,
//             bloom: 'AAIAAAAAAAAAA........AAAAAAAAAAAAAAA==',
//             logsList: [{
//                 address: '0x71B7d704598945e72E7581BAC3b070D300dc6Eb3',
//                 topicsList: [
//                     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//                     '0x0000000000000000000000000000000000000000000000000000000000000000',
//                     '0x0000000000000000000000007e654d251da770a068413677967f6d3ea2fea9e4'
//                 ],
//                 data: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIeGeDJurJAAAA=',
//                 event: 'Transfer(address,address,uint256)',
//                 decodedDataMap: {
//                     from: '0x0000000000000000000000000000000000000000',
//                     to: '0x7E654d251Da770A068413677967F6d3Ea2FeA9E4',
//                     value: '10000000000000000000000'
//                 },
//                 blockNumber: 128,
//                 txHash: '0xcbb8c17e59156500a668954353120b948d9397c8ba4d74935ebd32b702ca7b7a',
//                 txIndex: 0,
//                 blockHash: '0x942f9916e22c011d424a2ac9c65fbaaa529b52952d5b800fb71e1f754f2b838c',
//                 index: 0,
//                 removed: false
//             }],
//             gasUsed: 815261,
//             cumulativeGasUsed: 815261
//         },
//         errorsList: [],
//         metadata: {
//             id: '471b3dec-6f8c-4394-8fa8-ef7d3e8c59e1',
//             extraMap: {}
//         }
//     },
//     offset: 0,
//     partition: 0,
//     highWaterOffset: 1,
//     key: < Buffer 03 78 >
// }

Generate wallets stored in a vault

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

(async () => {
  try {
    // Init broker to connect to a kafka borker
    const orch = new Orchestrate()
    const broker = orch.broker('localhost:9092');

    // Init class to generate wallets in Orchestrate
    const walletGenerator = await broker.walletGenerator();

    // Generate multiple wallets asynchronously and get addresses
    const wallets = await Promise.all([
      walletGenerator.generate(),
      walletGenerator.generate(),
      walletGenerator.generate(),
      walletGenerator.generate(),
      walletGenerator.generate(),
      walletGenerator.generate(),
      walletGenerator.generate()
    ]);
    console.log('Wallets generated: ', wallets);
  } catch (e) {
    console.error(e);
  }
})();
// Wallets generated: [
//   '0x171cfe7D47f3F6316A5128215dc960a2812D557D',
//   '0x9cD7eE2014a2dA810AB51166835c9cc3601D9516',
//   '0xaAdCe2f8764c155e01711dD1bC6aE340ae4D5735',
//   '0x28ea3Cec494F86B71b9B62482963f36f82F0b5Fd',
//   '0xD0062dFB39180C2Ef8FCbcFEb75944e3059895ce',
//   '0xE2d3e35c0e0731208eC09A21955E557b59aa08d6',
//   '0x499d0E4990c8ce51cA189fb2960064442E39b98e'
// ]