All files ForeignGivethBridge.js

94.12% Statements 16/17
75% Branches 3/4
100% Functions 9/9
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40          2x 2x       2x       24x 1x 1x     23x   23x 23x 23x       3x   3x   3x 3x              
import logger from 'winston';
import { GivethBridge, ForeignGivethBridge } from './contracts';
 
export default class {
  constructor(web3, address) {
    this.web3 = web3;
    this.bridge = new ForeignGivethBridge(web3, address);
    // passing wrong web3 instance here b/c it doesn't matter
    // only using this object to generate the call data to the
    // homeBridge
    this.homeBridge = new GivethBridge(web3).$contract;
  }
 
  getRelayTransactions(fromBlock, toBlock) {
    if (toBlock < fromBlock) {
      logger.debug(`ForeignGivethBridge  -> toBlock: ${toBlock} < fromBlock: ${fromBlock} ... ignoring fetch getRelayTransactions request`);
      return Promise.resolve([]);
    }
 
    return this.bridge.$contract
      .getPastEvents('Withdraw', { fromBlock, toBlock })
      .then((events) => events.map(e => this.eventToTx(e)))
      .then(promises => Promise.all(promises))
      .then((results) => results.filter(r => r !== undefined));
  }
 
  eventToTx(event) {
    logger.info('handling ForeignGivethBridge event: ', event);
 
    switch (event.event) {
      case 'Withdraw':
        return this.web3.eth.getTransaction(event.transactionHash)
          .then(tx => Object.assign({}, event.returnValues, {
            foreignTx: event.transactionHash
          }));
      default:
        return Promise.resolve(undefined);
    }
  }
}