@certik/skynet 0.10.6 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/labelling.js CHANGED
@@ -1,90 +1,90 @@
1
- const fetch = require("node-fetch");
2
- const { getRecordByKey, updateRecordByKey } = require("./dynamodb");
3
- const { getEnvironment } = require("./env");
4
-
5
- const ADDRESSES_TABLE_NAME = `skynet-${getEnvironment()}-aml-addresses`;
6
- const SMART_CONTRACTS_TABLE_NAME = `skynet-${getEnvironment()}-aml-smart-contracts`;
7
-
8
- async function addTagsToAddress(address, tags, verbose) {
9
- let tableName = null;
10
- let updateItemTags;
11
-
12
- // Validate tags
13
- tags.forEach((tag) => {
14
- if (!tag.type || !tag.label) {
15
- throw new Error("error: tag must be in format { type, label }");
16
- }
17
- });
18
-
19
- // check if address is a smart contract
20
- let queryResult = await getRecordByKey(SMART_CONTRACTS_TABLE_NAME, { address });
21
-
22
- if (queryResult) {
23
- tableName = SMART_CONTRACTS_TABLE_NAME;
24
- updateItemTags = queryResult.tags ?? [];
25
- } else {
26
- // check if address is an EOA
27
- queryResult = await getRecordByKey(ADDRESSES_TABLE_NAME, { address });
28
-
29
- if (queryResult) {
30
- tableName = ADDRESSES_TABLE_NAME;
31
- updateItemTags = queryResult.tags ?? [];
32
- } else {
33
- // Unknown address type, inquiry endpoint to create a record
34
- const url = `https://aml-inquiry.certik-skynet.com/address?address=${address}`;
35
- const response = await fetch(url);
36
- const json = await response.json();
37
- if (!json.type) {
38
- throw new Error(`Unknown address type in inquiry response: ${address}`);
39
- }
40
- const { type } = json;
41
- tableName = type === "EOA" ? ADDRESSES_TABLE_NAME : SMART_CONTRACTS_TABLE_NAME;
42
- updateItemTags = [];
43
- }
44
- }
45
-
46
- if (tableName) {
47
- // record available, add tag
48
- const newTags = tags.filter((tag) => !updateItemTags.includes(tag));
49
-
50
- if (newTags.length + updateItemTags.length === 0) {
51
- throw new Error("error: no tags to add and none already in database");
52
- }
53
-
54
- if (newTags.length === 0) {
55
- return;
56
- }
57
-
58
- updateItemTags = [...updateItemTags, ...newTags];
59
-
60
- // keep distinct tags
61
- updateItemTags = updateItemTags.filter(
62
- (value, index, self) => self.findIndex((t) => t.type === value.type && t.label === value.label) === index
63
- );
64
-
65
- await updateRecordByKey(tableName, { address: address }, { tags: updateItemTags }, null, verbose);
66
- }
67
-
68
- return { tableName, updateItemTags };
69
- }
70
-
71
- async function queryAddressTags(address) {
72
- let updateItemTags = [];
73
-
74
- let queryResult = await getRecordByKey(SMART_CONTRACTS_TABLE_NAME, { address });
75
-
76
- if (queryResult) {
77
- updateItemTags = queryResult.tags ?? [];
78
- } else {
79
- // check if address is an EOA
80
- queryResult = await getRecordByKey(ADDRESSES_TABLE_NAME, { address });
81
-
82
- if (queryResult) {
83
- updateItemTags = queryResult.tags ?? [];
84
- }
85
- }
86
-
87
- return updateItemTags;
88
- }
89
-
90
- module.exports = { addTagsToAddress, queryAddressTags };
1
+ const fetch = require("node-fetch");
2
+ const { getRecordByKey, updateRecordByKey } = require("./dynamodb");
3
+ const { getEnvironment } = require("./env");
4
+
5
+ const ADDRESSES_TABLE_NAME = `skynet-${getEnvironment()}-aml-addresses`;
6
+ const SMART_CONTRACTS_TABLE_NAME = `skynet-${getEnvironment()}-aml-smart-contracts`;
7
+
8
+ async function addTagsToAddress(address, tags, verbose) {
9
+ let tableName = null;
10
+ let updateItemTags;
11
+
12
+ // Validate tags
13
+ tags.forEach((tag) => {
14
+ if (!tag.type || !tag.label) {
15
+ throw new Error("error: tag must be in format { type, label }");
16
+ }
17
+ });
18
+
19
+ // check if address is a smart contract
20
+ let queryResult = await getRecordByKey(SMART_CONTRACTS_TABLE_NAME, { address });
21
+
22
+ if (queryResult) {
23
+ tableName = SMART_CONTRACTS_TABLE_NAME;
24
+ updateItemTags = queryResult.tags ?? [];
25
+ } else {
26
+ // check if address is an EOA
27
+ queryResult = await getRecordByKey(ADDRESSES_TABLE_NAME, { address });
28
+
29
+ if (queryResult) {
30
+ tableName = ADDRESSES_TABLE_NAME;
31
+ updateItemTags = queryResult.tags ?? [];
32
+ } else {
33
+ // Unknown address type, inquiry endpoint to create a record
34
+ const url = `https://aml-inquiry.certik-skynet.com/address?address=${address}`;
35
+ const response = await fetch(url);
36
+ const json = await response.json();
37
+ if (!json.type) {
38
+ throw new Error(`Unknown address type in inquiry response: ${address}`);
39
+ }
40
+ const { type } = json;
41
+ tableName = type === "EOA" ? ADDRESSES_TABLE_NAME : SMART_CONTRACTS_TABLE_NAME;
42
+ updateItemTags = [];
43
+ }
44
+ }
45
+
46
+ if (tableName) {
47
+ // record available, add tag
48
+ const newTags = tags.filter((tag) => !updateItemTags.includes(tag));
49
+
50
+ if (newTags.length + updateItemTags.length === 0) {
51
+ throw new Error("error: no tags to add and none already in database");
52
+ }
53
+
54
+ if (newTags.length === 0) {
55
+ return;
56
+ }
57
+
58
+ updateItemTags = [...updateItemTags, ...newTags];
59
+
60
+ // keep distinct tags
61
+ updateItemTags = updateItemTags.filter(
62
+ (value, index, self) => self.findIndex((t) => t.type === value.type && t.label === value.label) === index
63
+ );
64
+
65
+ await updateRecordByKey(tableName, { address: address }, { tags: updateItemTags }, null, verbose);
66
+ }
67
+
68
+ return { tableName, updateItemTags };
69
+ }
70
+
71
+ async function queryAddressTags(address) {
72
+ let updateItemTags = [];
73
+
74
+ let queryResult = await getRecordByKey(SMART_CONTRACTS_TABLE_NAME, { address });
75
+
76
+ if (queryResult) {
77
+ updateItemTags = queryResult.tags ?? [];
78
+ } else {
79
+ // check if address is an EOA
80
+ queryResult = await getRecordByKey(ADDRESSES_TABLE_NAME, { address });
81
+
82
+ if (queryResult) {
83
+ updateItemTags = queryResult.tags ?? [];
84
+ }
85
+ }
86
+
87
+ return updateItemTags;
88
+ }
89
+
90
+ module.exports = { addTagsToAddress, queryAddressTags };
package/log.js CHANGED
@@ -1,29 +1,29 @@
1
- function getLine(params) {
2
- let line = "";
3
-
4
- // Convert to string and filter out newline to tabs (AWS Athena)
5
- for (let i = 0, l = params.length; i < l; i++) {
6
- // Certain objects don't get converted
7
- // Note using JSON.stringfy may be too slow for large objects
8
- line += `${params[i]} `.replace(/\n/gm, "\t");
9
- }
10
-
11
- return line.trim();
12
- }
13
-
14
- function timestamp() {
15
- return new Date().toISOString();
16
- }
17
-
18
- const inline = {
19
- log: function (...args) {
20
- console.log(`${timestamp()} ${getLine(args)}`);
21
- },
22
- error: function (...args) {
23
- console.error(`${timestamp()} ${getLine(args)}`);
24
- },
25
- };
26
-
27
- module.exports = {
28
- inline,
29
- };
1
+ function getLine(params) {
2
+ let line = "";
3
+
4
+ // Convert to string and filter out newline to tabs (AWS Athena)
5
+ for (let i = 0, l = params.length; i < l; i++) {
6
+ // Certain objects don't get converted
7
+ // Note using JSON.stringfy may be too slow for large objects
8
+ line += `${params[i]} `.replace(/\n/gm, "\t");
9
+ }
10
+
11
+ return line.trim();
12
+ }
13
+
14
+ function timestamp() {
15
+ return new Date().toISOString();
16
+ }
17
+
18
+ const inline = {
19
+ log: function (...args) {
20
+ console.log(`${timestamp()} ${getLine(args)}`);
21
+ },
22
+ error: function (...args) {
23
+ console.error(`${timestamp()} ${getLine(args)}`);
24
+ },
25
+ };
26
+
27
+ module.exports = {
28
+ inline,
29
+ };
package/metric.js CHANGED
@@ -1,65 +1,65 @@
1
- const { getDocClient } = require("./dynamodb");
2
-
3
- /* Assume table has name/timestamp/value fields */
4
-
5
- async function getMetricAt(tableName, name, timestamp) {
6
- const docClient = getDocClient();
7
-
8
- const query = await docClient
9
- .query({
10
- TableName: tableName,
11
- KeyConditionExpression: "#name = :name and #timestamp <= :timestamp",
12
- ExpressionAttributeNames: {
13
- "#name": "name",
14
- "#timestamp": "timestamp"
15
- },
16
- ExpressionAttributeValues: {
17
- ":name": name,
18
- ":timestamp": timestamp
19
- },
20
- Limit: 1,
21
- ScanIndexForward: false
22
- })
23
- .promise();
24
-
25
- if (query.Count > 0) {
26
- const { value } = query.Items[0];
27
-
28
- return value;
29
- } else {
30
- return null;
31
- }
32
- }
33
-
34
- async function getMetricPreviousValue(tableName, name, timestamp) {
35
- const docClient = getDocClient();
36
-
37
- const query = await docClient
38
- .query({
39
- TableName: tableName,
40
- KeyConditionExpression: "#name = :name and #timestamp < :timestamp",
41
- ExpressionAttributeNames: {
42
- "#timestamp": "timestamp",
43
- "#name": "name"
44
- },
45
- ExpressionAttributeValues: {
46
- ":timestamp": timestamp,
47
- ":name": name
48
- },
49
- Limit: 1,
50
- ScanIndexForward: false
51
- })
52
- .promise();
53
-
54
- if (query.Count === 0) {
55
- return 0;
56
- } else {
57
- const { value } = query.Items[0];
58
- return value;
59
- }
60
- }
61
-
62
- module.exports = {
63
- getMetricAt,
64
- getMetricPreviousValue
65
- }
1
+ const { getDocClient } = require("./dynamodb");
2
+
3
+ /* Assume table has name/timestamp/value fields */
4
+
5
+ async function getMetricAt(tableName, name, timestamp) {
6
+ const docClient = getDocClient();
7
+
8
+ const query = await docClient
9
+ .query({
10
+ TableName: tableName,
11
+ KeyConditionExpression: "#name = :name and #timestamp <= :timestamp",
12
+ ExpressionAttributeNames: {
13
+ "#name": "name",
14
+ "#timestamp": "timestamp"
15
+ },
16
+ ExpressionAttributeValues: {
17
+ ":name": name,
18
+ ":timestamp": timestamp
19
+ },
20
+ Limit: 1,
21
+ ScanIndexForward: false
22
+ })
23
+ .promise();
24
+
25
+ if (query.Count > 0) {
26
+ const { value } = query.Items[0];
27
+
28
+ return value;
29
+ } else {
30
+ return null;
31
+ }
32
+ }
33
+
34
+ async function getMetricPreviousValue(tableName, name, timestamp) {
35
+ const docClient = getDocClient();
36
+
37
+ const query = await docClient
38
+ .query({
39
+ TableName: tableName,
40
+ KeyConditionExpression: "#name = :name and #timestamp < :timestamp",
41
+ ExpressionAttributeNames: {
42
+ "#timestamp": "timestamp",
43
+ "#name": "name"
44
+ },
45
+ ExpressionAttributeValues: {
46
+ ":timestamp": timestamp,
47
+ ":name": name
48
+ },
49
+ Limit: 1,
50
+ ScanIndexForward: false
51
+ })
52
+ .promise();
53
+
54
+ if (query.Count === 0) {
55
+ return 0;
56
+ } else {
57
+ const { value } = query.Items[0];
58
+ return value;
59
+ }
60
+ }
61
+
62
+ module.exports = {
63
+ getMetricAt,
64
+ getMetricPreviousValue
65
+ }