Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#40] Consistent column order for MBCUSTOMQUERY #50

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2584826
Change defatul number of fileters
daenamkim Nov 26, 2020
dd8442c
Add MBCUSTOMEQUERY test with no filter
daenamkim Nov 26, 2020
6442a65
Add a reference for validators
daenamkim Nov 27, 2020
0116d8b
Update validators
daenamkim Nov 27, 2020
f728067
Change a field name from rule to fieldType
daenamkim Nov 27, 2020
7df1255
Fix regex based on field types
daenamkim Nov 27, 2020
d9176bc
Update MBCUSTOMQUERY tests
daenamkim Nov 27, 2020
5419935
Revert "Add a reference for validators"
daenamkim Nov 27, 2020
478cc01
Add a comment
daenamkim Nov 27, 2020
b7517dd
Change texts
daenamkim Nov 27, 2020
5c1b460
Merge branch 'master' into 30-fix-event-queries-filters
daenamkim Dec 1, 2020
94c243f
Exit when an error happens during tests
daenamkim Dec 4, 2020
773be3a
Add headers preset
daenamkim Dec 11, 2020
15bfb63
Change a test
daenamkim Dec 11, 2020
e29fe41
Merge branch 'master' into 30-fix-event-queries-filters
daenamkim Dec 15, 2020
7cc7360
[#30] Improve event query filter (#35)
daenamkim Dec 25, 2020
e68529a
Remove a wrong valid operand
daenamkim Dec 25, 2020
ddb7a4f
Add formatInts to return number values as integer as is (#55)
daenamkim Dec 25, 2020
99968b9
Merge branch 'master' into 40-consistent-column-order
daenamkim Dec 25, 2020
402f50d
Merge branch '30-fix-event-queries-filters' into 40-consistent-column…
daenamkim Dec 25, 2020
cb1dde6
Merge branch 'master' into 40-consistent-column-order
daenamkim Dec 25, 2020
54088a3
Fix tests
daenamkim Dec 25, 2020
a9d7e17
Update gitignore
daenamkim Jan 12, 2021
ef71cfe
Merge branch 'master' into 40-consistent-column-order
daenamkim Jan 12, 2021
64e3843
Fix headers order
daenamkim Jan 12, 2021
e26397d
Fix variable init
daenamkim Jan 12, 2021
8faf50a
Remove a unused test
daenamkim Jan 12, 2021
821dd6e
Add MBCUSTOMQUERY test with multiple events and filters
daenamkim Jan 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,12 @@ function MBCUSTOMQUERY(events, groupBy, orderBy, limit, offset) {

// turn the array of objects into a flat array
const objArr = results.result.rows;
return objectArrayToArray(objArr);
const headersPreset = Array(payload.events[0].select.length);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could events[0] ever be null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it could be null but throw new Error() will happen before

function buildCustomQuery(events, groupBy, orderBy, limit, offset) {
const query = {
events: [],
};
if (groupBy && groupBy !== '') {
query.groupBy = groupBy;
}
if (orderBy && orderBy !== '') {
query.orderBy = orderBy;
}
// parse and validate header row
if (events.length < 1) {
throw new Error(`Expecting a header row followed by one or more data rows, found ${events.length} rows total`);
}

// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const s of payload.events[0].select) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's not a requirement that each event row is the same event, nor that it has the same aliases. Therefore, I don't think we can just use events[0]:

For example, this is valid:
image

Which generates the following query:

{
    "events": [
        {
            "eventName": "Transfer",
            "select": [
                {
                    "alias": "abc",
                    "inputIndex": 0,
                    "type": "input"
                },
                {
                    "alias": "ghi",
                    "inputIndex": 2,
                    "type": "input"
                }
            ],
            "filter": {
                "rule": "and",
                "children": [
                    {
                        "operator": "equal",
                        "value": "autotoken",
                        "fieldType": "contract_address_label"
                    }
                ]
            }
        },
        {
            "eventName": "Mint",
            "select": [
                {
                    "alias": "def",
                    "inputIndex": 1,
                    "type": "input"
                },
                {
                    "alias": "jkl",
                    "inputIndex": 2,
                    "type": "input"
                }
            ],
            "filter": {
                "rule": "and",
                "children": [
                    {
                        "operator": "equal",
                        "value": "autotoken",
                        "fieldType": "contract_address_label"
                    }
                ]
            }
        }
    ]
}

With the following result:

{
    "status": 200,
    "message": "success",
    "result": {
        "rows": [
            {
                "abc": null,
                "def": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "ghi": null,
                "jkl": "100000000000"
            },
            {
                "abc": "0x0000000000000000000000000000000000000000",
                "def": null,
                "ghi": "100000000000",
                "jkl": null
            },
            {
                "abc": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "def": null,
                "ghi": "10000000000",
                "jkl": null
            },
            {
                "abc": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "def": null,
                "ghi": "1000000000",
                "jkl": null
            },
            {
                "abc": null,
                "def": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "ghi": null,
                "jkl": "100000000000"
            },
            {
                "abc": "0x0000000000000000000000000000000000000000",
                "def": null,
                "ghi": "100000000000",
                "jkl": null
            },
            {
                "abc": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "def": null,
                "ghi": "2000000000",
                "jkl": null
            },
            {
                "abc": null,
                "def": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "ghi": null,
                "jkl": "870000000"
            },
            {
                "abc": "0x0000000000000000000000000000000000000000",
                "def": null,
                "ghi": "870000000",
                "jkl": null
            },
            {
                "abc": "0xf9450d254a66ab06b30cfa9c6e7ae1b7598c7172",
                "def": null,
                "ghi": "100",
                "jkl": null
            }
        ]
    }
}

image

I tested this PR by merging #35 into this branch, and indeed, the output is broken:

image

headersPreset[s.inputIndex] = s.alias;
}
return objectArrayToArray(objArr, headersPreset);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Code.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ function testRunner(testSheetURL) {
0,
],
expected: [
['amount', 'sender'],
[1e+27, '0x89d048be68575f2b56a999ba24faacabd1b919fb'],
[1000000000000000000, '0xa616eed6ad7a0cf5d2388301a710c273ca955e05'],
[1000000000000000000, '0xbac1cd4051c378bf900087ccc445d7e7d02ad745'],
['sender', 'amount'],
['0x89d048be68575f2b56a999ba24faacabd1b919fb', 1e+27],
['0xa616eed6ad7a0cf5d2388301a710c273ca955e05', 1000000000000000000],
['0xbac1cd4051c378bf900087ccc445d7e7d02ad745', 1000000000000000000],
],
},
{
Expand Down
9 changes: 7 additions & 2 deletions src/library/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,16 @@ function eventsToArray(entries) {
return rows;
}

function objectArrayToArray(objArr) {
function objectArrayToArray(objArr, headersPreset) {
const rows = [];

// header row: just take the keys from the first row
const headers = keysFromObj(objArr[0], true);
let headers;
if (!Array.isArray(headersPreset)) {
headers = keysFromObj(objArr[0], false);
} else {
headers = headersPreset;
}
rows.push(headers);

// body rows
Expand Down