aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src/postman_environment_factory.ts
blob: f4094315ea82a5447619b5b1c2975c51e367aefb (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { SignedOrder, ZeroEx } from '0x.js';
import { HttpClient } from '@0xproject/connect';
import { Schema, schemas as schemasByName } from '@0xproject/json-schemas';
import chalk from 'chalk';
import * as _ from 'lodash';

import { addresses as kovanAddresses } from './contract_addresses/kovan_addresses';
import { addresses as mainnetAddresses } from './contract_addresses/mainnet_addresses';
import { utils } from './utils';

const ENVIRONMENT_NAME = 'SRA Report';

interface EnvironmentValue {
    key: string;
}

export const postmanEnvironmentFactory = {
    /**
     * Dynamically generates a postman environment (https://www.getpostman.com/docs/v6/postman/environments_and_globals/manage_environments)
     * When running the postman collection via newman, we provide it a set of environment variables
     * These variables include:
     *  - 0x JSON schemas for response body validation
     *  - Contract addresses based on the network id for making specific queries (ex. baseTokenAddress=ZRX_address)
     *  - Order properties for making specific queries (ex. maker=orderMaker)
     */
    async createPostmanEnvironmentAsync(url: string, networkId: number) {
        const orderEnvironmentValues = await createOrderEnvironmentValuesAsync(url);
        const allEnvironmentValues = _.concat(
            createSchemaEnvironmentValues(),
            createContractAddressEnvironmentValues(networkId),
            orderEnvironmentValues,
            createEnvironmentValue('url', url),
        );
        const environment = {
            name: ENVIRONMENT_NAME,
            values: allEnvironmentValues,
        };
        return environment;
    },
};
function createSchemaEnvironmentValues() {
    const schemas: Schema[] = _.values(schemasByName);
    const schemaEnvironmentValues = _.compact(
        _.map(schemas, (schema: Schema) => {
            if (_.isUndefined(schema.id)) {
                return undefined;
            } else {
                const schemaKey = convertSchemaIdToKey(schema.id);
                const stringifiedSchema = JSON.stringify(schema);
                const schemaEnvironmentValue = createEnvironmentValue(schemaKey, stringifiedSchema);
                return schemaEnvironmentValue;
            }
        }),
    );
    const schemaKeys = _.map(schemaEnvironmentValues, (environmentValue: EnvironmentValue) => {
        return environmentValue.key;
    });
    const result = _.concat(schemaEnvironmentValues, createEnvironmentValue('schemaKeys', JSON.stringify(schemaKeys)));
    return result;
}
function createContractAddressEnvironmentValues(networkId: number) {
    const contractAddresses = getContractAddresses(networkId);
    return [
        createEnvironmentValue('tokenContractAddress1', contractAddresses.WETH),
        createEnvironmentValue('tokenContractAddress2', contractAddresses.ZRX),
        createEnvironmentValue('exchangeContractAddress', contractAddresses.EXCHANGE),
    ];
}
async function createOrderEnvironmentValuesAsync(url: string) {
    const httpClient = new HttpClient(url);
    const orders = await httpClient.getOrdersAsync();
    const orderIfExists = _.head(orders);
    if (!_.isUndefined(orderIfExists)) {
        return [
            createEnvironmentValue('order', JSON.stringify(orderIfExists)),
            createEnvironmentValue('orderMaker', orderIfExists.maker),
            createEnvironmentValue('orderTaker', orderIfExists.taker),
            createEnvironmentValue('orderFeeRecipient', orderIfExists.feeRecipient),
            createEnvironmentValue('orderHash', ZeroEx.getOrderHashHex(orderIfExists)),
        ];
    } else {
        utils.log(`${chalk.red(`No orders from /orders found`)}`);
        return [
            createEnvironmentValue('order', ''),
            createEnvironmentValue('orderMaker', ''),
            createEnvironmentValue('orderTaker', ''),
            createEnvironmentValue('orderFeeRecipient', ''),
            createEnvironmentValue('orderHash', ''),
        ];
    }
}
function getContractAddresses(networkId: number) {
    switch (networkId) {
        case 1:
            return mainnetAddresses;
        case 42:
            return kovanAddresses;
        default:
            throw new Error('Unsupported network id');
    }
}
function convertSchemaIdToKey(schemaId: string) {
    let result = schemaId;
    if (_.startsWith(result, '/')) {
        result = result.substr(1);
    }
    result = `${result}Schema`;
    return result;
}
function createEnvironmentValue(key: string, value: string) {
    return {
        key,
        value,
        enabled: true,
        type: 'text',
    };
}