All files GivethBridge.js

92.59% Statements 25/27
77.78% Branches 7/9
100% Functions 11/11
92% Lines 23/25

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84            1x 1x 1x 1x       24x 6x     6x   18x   18x 18x 18x       15x       17x   17x   7x 7x       7x   7x                             8x 8x       8x   8x                             2x        
import logger from 'winston';
import { GivethBridge, ForeignGivethBridge } from './contracts';
import { LiquidPledging } from 'giveth-liquidpledging';
 
export default class {
    constructor(homeWeb3, foreignWeb3, address, foreignAddress) {
        this.web3 = homeWeb3;
        this.bridge = new GivethBridge(homeWeb3, address);
        this.foreignBridge = new ForeignGivethBridge(foreignWeb3, foreignAddress);
        this.lp = new LiquidPledging(foreignWeb3).$contract;
    }
 
    getRelayTransactions(fromBlock, toBlock) {
        if (toBlock < fromBlock) {
            logger.debug(
                `GivethBridge -> toBlock: ${toBlock} < fromBlock: ${fromBlock} ... ignoring fetch getRelayTransactions request`,
            );
            return Promise.resolve([]);
        }
        return this.bridge.$contract
            .getPastEvents('allEvents', { fromBlock, toBlock })
            .then(events => events.map(e => this.eventToTx(e)))
            .then(promises => Promise.all(promises))
            .then(results => results.filter(r => r !== undefined));
    }
 
    getToken(mainToken) {
        return this.foreignBridge.tokenMapping(mainToken);
    }
 
    eventToTx(event) {
        logger.info('handling GivethBridge event: ', event);
 
        switch (event.event) {
            case 'Donate': {
                const { giverId, receiverId, token, amount } = event.returnValues;
                return Promise.all([
                    this.web3.eth.getTransaction(event.transactionHash),
                    this.getToken(token),
                ]).then(([tx, sideToken]) => {
                    Iif (!tx)
                        throw new Error(`Failed to fetch transaction ${event.transactionHash}`);
                    return {
                        homeTx: event.transactionHash,
                        giverId,
                        receiverId,
                        mainToken: token,
                        sideToken,
                        amount,
                        sender: tx.from,
                        data: this.lp.methods
                            .donate(giverId, receiverId, sideToken, amount)
                            .encodeABI(),
                    };
                });
            }
            case 'DonateAndCreateGiver': {
                const { giver, receiverId, token, amount } = event.returnValues;
                return Promise.all([
                    this.web3.eth.getTransaction(event.transactionHash),
                    this.getToken(token),
                ]).then(([tx, sideToken]) => {
                    Iif (!tx)
                        throw new Error(`Failed to fetch transaction ${event.transactionHash}`);
                    return {
                        homeTx: event.transactionHash,
                        giver,
                        receiverId,
                        mainToken: token,
                        sideToken,
                        amount,
                        sender: tx.from,
                        data: this.lp.methods
                            .addGiverAndDonate(receiverId, giver, sideToken, amount)
                            .encodeABI(),
                    };
                });
            }
            default:
                return Promise.resolve(undefined);
        }
    }
}