aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src/postman_environment_factory.ts
blob: e899aaa7917ef2789b289a9430339d648a84b625 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { SignedOrder, ZeroEx } from '0x.js';
import { HttpClient } from '@0xproject/connect';
import { Schema, schemas as schemasByName } from '@0xproject/json-schemas';
import { logUtils } from '@0xproject/utils';
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 { addresses as rinkebyAddresses } from './contract_addresses/rinkeby_addresses';
import { addresses as ropstenAddresses } from './contract_addresses/ropsten_addresses';

const ENVIRONMENT_NAME = 'SRA Report';
const networkNameToId: { [networkName: string]: number } = {
    mainnet: 1,
    ropsten: 3,
    rinkeby: 4,
    kovan: 42,
};

export interface EnvironmentValue {
    key: string;
    value: string;
    enabled: true;
    type: 'text';
}

export interface Environment {
    name: string;
    values: EnvironmentValue[];
}

export interface Addresses {
    WETH: string;
    ZRX: string;
    EXCHANGE: 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): Promise<Environment> {
        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(): EnvironmentValue[] {
    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): EnvironmentValue[] {
    const contractAddresses = getContractAddresses(networkId);
    return [
        createEnvironmentValue('tokenContractAddress1', contractAddresses.WETH),
        createEnvironmentValue('tokenContractAddress2', contractAddresses.ZRX),
        createEnvironmentValue('exchangeContractAddress', contractAddresses.EXCHANGE),
    ];
}
async function createOrderEnvironmentValuesAsync(url: string): Promise<EnvironmentValue[]> {
    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 {
        logUtils.log(`${chalk.red(`No orders from /orders found`)}`);
        return [
            createEnvironmentValue('order', ''),
            createEnvironmentValue('orderMaker', ''),
            createEnvironmentValue('orderTaker', ''),
            createEnvironmentValue('orderFeeRecipient', ''),
            createEnvironmentValue('orderHash', ''),
        ];
    }
}
function getContractAddresses(networkId: number): Addresses {
    switch (networkId) {
        case networkNameToId.mainnet:
            return mainnetAddresses;
        case networkNameToId.ropsten:
            return ropstenAddresses;
        case networkNameToId.rinkeby:
            return rinkebyAddresses;
        case networkNameToId.kovan:
            return kovanAddresses;
        default:
            throw new Error('Unsupported network id');
    }
}
function convertSchemaIdToKey(schemaId: string): string {
    let result = schemaId;
    if (_.startsWith(result, '/')) {
        result = result.substr(1);
    }
    result = `${result}Schema`;
    return result;
}

function createEnvironmentValue(key: string, value: string): EnvironmentValue {
    return {
        key,
        value,
        enabled: true,
        type: 'text',
    };
}