@certik/skynet 0.7.10 → 0.7.14

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/token.js CHANGED
@@ -1,44 +1,44 @@
1
- const { getEnvironment } = require("./env");
2
- const { getPriceClosestTo } = require("./price");
3
- const BigNumber = require("bignumber.js");
4
-
5
- const TOKEN_PRICE_TABLE_NAME = `skynet-${getEnvironment()}-token-prices`;
6
- const TOKEN_PRICE_CACHE = {};
7
-
8
- async function getTokenPriceAt(tokenAddress, timestamp) {
9
- // to avoid huge amount of dynamodb query
10
- // "ceil" timestamp to the closest proximate timestamp
11
- // 100_000 seconds ~= a bit more than 1 day 86_400 seconds
12
- // and use cache
13
- const proximateTimestamp = timestamp + 100_000 - (timestamp % 100_000);
14
-
15
- if (TOKEN_PRICE_CACHE[tokenAddress] && TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp]) {
16
- return TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp];
17
- }
18
-
19
- if (!TOKEN_PRICE_CACHE[tokenAddress]) {
20
- TOKEN_PRICE_CACHE[tokenAddress] = {};
21
- }
22
-
23
- const price = await getPriceClosestTo(TOKEN_PRICE_TABLE_NAME, tokenAddress, proximateTimestamp);
24
-
25
- TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp] = price;
26
-
27
- return price;
28
- }
29
-
30
- function toNativeDecimal(bigNumber, decimals) {
31
- const dividend = new BigNumber(10).exponentiatedBy(new BigNumber(decimals));
32
- return new BigNumber(bigNumber).multipliedBy(dividend);
33
- }
34
-
35
- function toHumanDecimal(bigNumber, decimals) {
36
- const dividend = new BigNumber(10).exponentiatedBy(new BigNumber(decimals));
37
- return new BigNumber(bigNumber).dividedBy(dividend);
38
- }
39
-
40
- module.exports = {
41
- getTokenPriceAt,
42
- toNativeDecimal,
43
- toHumanDecimal,
44
- };
1
+ const { getEnvironment } = require("./env");
2
+ const { getPriceClosestTo } = require("./price");
3
+ const BigNumber = require("bignumber.js");
4
+
5
+ const TOKEN_PRICE_TABLE_NAME = `skynet-${getEnvironment()}-token-prices`;
6
+ const TOKEN_PRICE_CACHE = {};
7
+
8
+ async function getTokenPriceAt(tokenAddress, timestamp) {
9
+ // to avoid huge amount of dynamodb query
10
+ // "ceil" timestamp to the closest proximate timestamp
11
+ // 100_000 seconds ~= a bit more than 1 day 86_400 seconds
12
+ // and use cache
13
+ const proximateTimestamp = timestamp + 100_000 - (timestamp % 100_000);
14
+
15
+ if (TOKEN_PRICE_CACHE[tokenAddress] && TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp]) {
16
+ return TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp];
17
+ }
18
+
19
+ if (!TOKEN_PRICE_CACHE[tokenAddress]) {
20
+ TOKEN_PRICE_CACHE[tokenAddress] = {};
21
+ }
22
+
23
+ const price = await getPriceClosestTo(TOKEN_PRICE_TABLE_NAME, tokenAddress, proximateTimestamp);
24
+
25
+ TOKEN_PRICE_CACHE[tokenAddress][proximateTimestamp] = price;
26
+
27
+ return price;
28
+ }
29
+
30
+ function toNativeDecimal(bigNumber, decimals) {
31
+ const dividend = new BigNumber(10).exponentiatedBy(new BigNumber(decimals));
32
+ return new BigNumber(bigNumber).multipliedBy(dividend);
33
+ }
34
+
35
+ function toHumanDecimal(bigNumber, decimals) {
36
+ const dividend = new BigNumber(10).exponentiatedBy(new BigNumber(decimals));
37
+ return new BigNumber(bigNumber).dividedBy(dividend);
38
+ }
39
+
40
+ module.exports = {
41
+ getTokenPriceAt,
42
+ toNativeDecimal,
43
+ toHumanDecimal,
44
+ };
package/transaction.js CHANGED
@@ -1,47 +1,47 @@
1
- const fetch = require("node-fetch");
2
- const { PROTOCOLS } = require("./const");
3
- const { exponentialRetry } = require("./availability");
4
-
5
- async function getTxReceipt(protocol, txHash, verbose = false) {
6
- let { endpoint } = PROTOCOLS[protocol];
7
-
8
- if (protocol === "bsc") {
9
- // skynet bsc endpoint sometimes does not return tx receipt for some reason
10
- // use public bsc node instead
11
- endpoint = "https://bsc-dataseed.binance.org";
12
- }
13
-
14
- const body = {
15
- jsonrpc: "2.0",
16
- method: "eth_getTransactionReceipt",
17
- params: [txHash],
18
- id: 1
19
- };
20
-
21
- const response = await exponentialRetry(
22
- () => {
23
- return fetch(endpoint, {
24
- method: "POST",
25
- headers: { "Content-Type": "application/json" },
26
- body: JSON.stringify(body)
27
- });
28
- },
29
- {
30
- maxRetry: 6,
31
- test: r => r.ok,
32
- verbose
33
- }
34
- );
35
-
36
- if (!response.ok) {
37
- return null;
38
- }
39
-
40
- const { result } = await response.json();
41
-
42
- return result;
43
- }
44
-
45
- module.exports = {
46
- getTxReceipt
47
- };
1
+ const fetch = require("node-fetch");
2
+ const { PROTOCOLS } = require("./const");
3
+ const { exponentialRetry } = require("./availability");
4
+
5
+ async function getTxReceipt(protocol, txHash, verbose = false) {
6
+ let { endpoint } = PROTOCOLS[protocol];
7
+
8
+ if (protocol === "bsc") {
9
+ // skynet bsc endpoint sometimes does not return tx receipt for some reason
10
+ // use public bsc node instead
11
+ endpoint = "https://bsc-dataseed.binance.org";
12
+ }
13
+
14
+ const body = {
15
+ jsonrpc: "2.0",
16
+ method: "eth_getTransactionReceipt",
17
+ params: [txHash],
18
+ id: 1
19
+ };
20
+
21
+ const response = await exponentialRetry(
22
+ () => {
23
+ return fetch(endpoint, {
24
+ method: "POST",
25
+ headers: { "Content-Type": "application/json" },
26
+ body: JSON.stringify(body)
27
+ });
28
+ },
29
+ {
30
+ maxRetry: 6,
31
+ test: r => r.ok,
32
+ verbose
33
+ }
34
+ );
35
+
36
+ if (!response.ok) {
37
+ return null;
38
+ }
39
+
40
+ const { result } = await response.json();
41
+
42
+ return result;
43
+ }
44
+
45
+ module.exports = {
46
+ getTxReceipt
47
+ };
package/util.js CHANGED
@@ -1,58 +1,58 @@
1
- // Inclusive endAt
2
- function partition(startAt, endAt, numGroups) {
3
- if (numGroups === 0 || startAt > endAt) {
4
- return [];
5
- } else if (startAt === endAt) {
6
- return [[startAt, endAt]];
7
- }
8
-
9
- const span = endAt - startAt + 1;
10
- const step = Math.max(Math.floor(span / numGroups), 1);
11
- const adjustedStep = span % step === 0 ? step : step + 1;
12
-
13
- return [
14
- [startAt, startAt + adjustedStep - 1],
15
- ...partition(startAt + adjustedStep, endAt, numGroups - 1)
16
- ];
17
- }
18
-
19
- // Inclusive range
20
- function range(startAt, endAt, step) {
21
- let arr = [];
22
-
23
- for (let i = startAt; i <= endAt; i += step) {
24
- arr.push([i, Math.min(endAt, i + step - 1)]);
25
- }
26
-
27
- return arr;
28
- }
29
-
30
- function arrayGroup(array, groupSize) {
31
- const groups = [];
32
-
33
- for (let i = 0; i < array.length; i += groupSize) {
34
- groups.push(array.slice(i, i + groupSize));
35
- }
36
-
37
- return groups;
38
- }
39
-
40
- // return an array with numbers of given inclusive range
41
- // given 1, 5
42
- // return [1, 2, 3, 4, 5]
43
- function fillRange(start, end) {
44
- let result = [];
45
-
46
- for (let i = start; i <= end; i++) {
47
- result.push(i);
48
- }
49
-
50
- return result;
51
- }
52
-
53
- module.exports = {
54
- arrayGroup,
55
- partition,
56
- range,
57
- fillRange
58
- };
1
+ // Inclusive endAt
2
+ function partition(startAt, endAt, numGroups) {
3
+ if (numGroups === 0 || startAt > endAt) {
4
+ return [];
5
+ } else if (startAt === endAt) {
6
+ return [[startAt, endAt]];
7
+ }
8
+
9
+ const span = endAt - startAt + 1;
10
+ const step = Math.max(Math.floor(span / numGroups), 1);
11
+ const adjustedStep = span % step === 0 ? step : step + 1;
12
+
13
+ return [
14
+ [startAt, startAt + adjustedStep - 1],
15
+ ...partition(startAt + adjustedStep, endAt, numGroups - 1)
16
+ ];
17
+ }
18
+
19
+ // Inclusive range
20
+ function range(startAt, endAt, step) {
21
+ let arr = [];
22
+
23
+ for (let i = startAt; i <= endAt; i += step) {
24
+ arr.push([i, Math.min(endAt, i + step - 1)]);
25
+ }
26
+
27
+ return arr;
28
+ }
29
+
30
+ function arrayGroup(array, groupSize) {
31
+ const groups = [];
32
+
33
+ for (let i = 0; i < array.length; i += groupSize) {
34
+ groups.push(array.slice(i, i + groupSize));
35
+ }
36
+
37
+ return groups;
38
+ }
39
+
40
+ // return an array with numbers of given inclusive range
41
+ // given 1, 5
42
+ // return [1, 2, 3, 4, 5]
43
+ function fillRange(start, end) {
44
+ let result = [];
45
+
46
+ for (let i = start; i <= end; i++) {
47
+ result.push(i);
48
+ }
49
+
50
+ return result;
51
+ }
52
+
53
+ module.exports = {
54
+ arrayGroup,
55
+ partition,
56
+ range,
57
+ fillRange
58
+ };