@azuro-org/toolkit 0.1.0

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/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Toolkit Package
2
+
3
+ This package provides helpers to develop an app using Azuro Protocol.
4
+
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ npm i --save @azuro-org/toolkit
10
+ ```
11
+
12
+
13
+ ## CLI
14
+
15
+ All dictionaries stored in [public repository](https://github.com/Azuro-protocol/public-config/tree/main/dictionaries).
16
+ For ease of use, the dictionaries have a version and file format.
17
+
18
+ It's easy to download dictionary files with CLI. In your package.json add script:
19
+
20
+ ```json
21
+ "scripts": {
22
+ "get-dicts": "dictionaries -o {OUTPUT_DIR} -v {VERSION} -t {FILES_TYPE}"
23
+ }
24
+ ```
25
+
26
+ - `VERSION` is the version of downloaded dictionaries. [Find the version you need here](https://github.com/Azuro-protocol/public-config/tree/main/dictionaries).
27
+ - `OUTPUT_DIR` is the directory where to put downloaded files.
28
+ - `FILES_TYPE` is the extension of downloaded files. Accepts `ts`, `js`, `maps`, `arrays`. `maps` and `arrays` are
29
+ json files with different output format.
30
+
31
+ ```bash
32
+ dictionaries -o ./dist -v 2.0.0 -t ts # will download v2.0.0 typescript files to ./dist directory
33
+ ```
34
+
35
+
36
+ ## Helpers
37
+
38
+ ```js
39
+ import { getMarketKey, getMarketName, getMarketDescription, assembleMarketName, assembleSelectionName } from '@azuro-org/dictionaries'
40
+ ```
41
+
42
+ ### Get market name and description
43
+
44
+ ```js
45
+ import { getMarketKey } from '@azuro-org/dictionaries'
46
+ import dictionaries from './path-to-downloaded-dictionaries'
47
+
48
+ const outcomeId = 1
49
+ const marketKey = getMarketKey(outcomeId, dictionaries)
50
+ ```
51
+
52
+ `getMarketKey(outcomeId, dictionaries)` returns the string key `marketId-gamePeriodId-gameTypeId[-teamPlayerId]`
53
+ built from the dictionaries related to passed `outcomeId`.
54
+
55
+ In the example above the result is `1-1-1`.
56
+
57
+ There are two dictionary files `marketNames.js` and `marketDescriptions.js`. `marketKey` is used to receive market name
58
+ and description for specific outcome ID.
59
+
60
+ ```
61
+ import dictionaries from './path-to-downloaded-dictionaries'
62
+
63
+ dictionaries.marketNames['1-1-1'] // "Full Time Result"
64
+ dictionaries.marketDescriptions['1-1-1'] // "You predict the result..."
65
+ ```
66
+
67
+ **!!! Note that there are no texts for each `outcomeId` !!!**
68
+
69
+ `marketNames[marketKey]` and `marketDescriptions[marketKey]` may return `undefined`. For `marketName` generation there
70
+ is other helper `assembleMarketName`. It generates human readable market name based on outcome `marketId`, `gamePeriodId`,
71
+ `gameTypeId`, `teamPlayerId`.
72
+
73
+ ```
74
+ import { getMarketKey, assembleMarketName } from '@azuro-org/dictionaries'
75
+ import dictionaries from './path-to-downloaded-dictionaries'
76
+
77
+ const outcomeId = 42
78
+ const marketKey = getMarketKey(outcomeId, dictionaries)
79
+
80
+ let marketName = dictionaries[marketKey] // undefined
81
+
82
+ if (!marketName) {
83
+ marketName = assembleMarketName(outcomeId, dictionaries) // "Whole game - Winner of match Goal"
84
+ }
85
+ ```
86
+
87
+ There are additional 2 sugar helpers:
88
+
89
+ ```js
90
+ import { getMarketName } from '@azuro-org/dictionaries'
91
+ import dictionaries from './path-to-downloaded-dictionaries'
92
+
93
+ getMarketName(1, dictionaries) // "Full Time Result"
94
+ getMarketName(42, dictionaries) // "Whole game - Winner of match Goal"
95
+ ```
96
+
97
+ ```js
98
+ import { getMarketDescription } from '@azuro-org/dictionaries'
99
+ import dictionaries from './path-to-downloaded-dictionaries'
100
+
101
+ getMarketDescription(1, dictionaries) // "You predict the result..."
102
+ getMarketDescription(42, dictionaries) // undefined. Note that there is no `assemblyMarketDescription` helper.
103
+ ```
104
+
105
+ ### Get selection (outcome) name
106
+
107
+ ```js
108
+ import { assembleSelectionName } from '@azuro-org/dictionaries'
109
+ import dictionaries from './dist'
110
+
111
+ const outcomeId = 1
112
+ const selectionName = assembleSelectionName(outcomeId, dictionaries) // "Yes"
113
+
114
+ const outcomeId = 4
115
+ const selectionName = assembleSelectionName(outcomeId, dictionaries) // "Team 2 (4.5)"
116
+ ```
@@ -0,0 +1,26 @@
1
+ import { type Dictionaries } from '@azuro-org/dictionaries';
2
+ type Outcome = {
3
+ conditionId: string;
4
+ outcomeId: string;
5
+ selectionName: string;
6
+ lpAddress: string;
7
+ coreAddress: string;
8
+ };
9
+ type Markets = {
10
+ marketName: string;
11
+ outcomes: Outcome[][];
12
+ }[];
13
+ type Props = {
14
+ lpAddress: string;
15
+ coreAddress: string;
16
+ conditions: {
17
+ [key: string]: any;
18
+ outcomes: {
19
+ [key: string]: any;
20
+ outcomeId: string;
21
+ }[];
22
+ }[];
23
+ dictionaries: Dictionaries;
24
+ };
25
+ export default function aggregateOutcomesByMarkets(props: Props): Markets;
26
+ export {};
@@ -0,0 +1 @@
1
+ export { default as aggregateOutcomesByMarkets } from './aggregateOutcomesByMarkets';
@@ -0,0 +1,78 @@
1
+ import {getMarketKey,getMarketName,assembleSelectionName}from'@azuro-org/dictionaries';function aggregateOutcomesByMarkets(props) {
2
+ var lpAddress = props.lpAddress, coreAddress = props.coreAddress, conditions = props.conditions, dictionaries = props.dictionaries;
3
+ // group conditions by marketId
4
+ var outcomesByMarketKey = {};
5
+ var result = {};
6
+ conditions.forEach(function (_a) {
7
+ var conditionId = _a.conditionId, outcomes = _a.outcomes;
8
+ outcomes.forEach(function (_a) {
9
+ var outcomeId = _a.outcomeId;
10
+ var marketKey = getMarketKey(outcomeId, dictionaries);
11
+ var marketName = getMarketName(outcomeId, dictionaries);
12
+ var selectionName = assembleSelectionName(outcomeId, dictionaries);
13
+ var outcome = {
14
+ conditionId: conditionId,
15
+ outcomeId: outcomeId,
16
+ selectionName: selectionName,
17
+ lpAddress: lpAddress,
18
+ coreAddress: coreAddress,
19
+ };
20
+ // it's important to use "marketKey" because it's unique
21
+ // on other hand "marketId" can be same for different groups of conditions
22
+ // "marketKey" is a string template "marketId-gamePeriodId-gameTypeId[-teamPlayerId]"
23
+ if (!outcomesByMarketKey[marketKey]) {
24
+ outcomesByMarketKey[marketKey] = [];
25
+ result[marketKey] = {
26
+ marketName: marketName,
27
+ outcomes: [],
28
+ };
29
+ }
30
+ outcomesByMarketKey[marketKey].push(outcome);
31
+ });
32
+ });
33
+ // sort by outcomeId and group by conditionId
34
+ Object.keys(outcomesByMarketKey).forEach(function (marketKey) {
35
+ var marketId = +marketKey.split('-')[0];
36
+ // get outcomes related to the market
37
+ var outcomes = outcomesByMarketKey[marketKey];
38
+ // sort the conditions by selectionId (outcome)
39
+ outcomes.sort(function (a, b) {
40
+ var left = dictionaries.outcomes[a.outcomeId].selectionId;
41
+ var right = dictionaries.outcomes[b.outcomeId].selectionId;
42
+ return left - right;
43
+ });
44
+ // markets with different conditionIds and not require additional grouping or sorting
45
+ var marketsWithDifferentConditionIds = [1, 2];
46
+ if (marketsWithDifferentConditionIds.includes(marketId)) {
47
+ result[marketKey].outcomes = [outcomes];
48
+ }
49
+ // group by conditionId to allow draw outcomes by rows in UI, e.g.
50
+ //
51
+ // Team 1 - Total Goals:
52
+ // Over (1.5) Under (1.5)
53
+ // Over (0.5) Under (0.5)
54
+ else {
55
+ var outcomesByConditionId_1 = {};
56
+ outcomes.forEach(function (outcome) {
57
+ var key = outcome.conditionId;
58
+ if (!outcomesByConditionId_1[key]) {
59
+ outcomesByConditionId_1[key] = [];
60
+ }
61
+ outcomesByConditionId_1[key].push(outcome);
62
+ });
63
+ var outcomesArr = Object.values(outcomesByConditionId_1);
64
+ result[marketKey].outcomes = outcomesArr.sort(function (a, b) {
65
+ var aSum = a.reduce(function (acc, _a) {
66
+ var outcomeId = _a.outcomeId;
67
+ return acc + +outcomeId;
68
+ }, 0);
69
+ var bSum = b.reduce(function (acc, _a) {
70
+ var outcomeId = _a.outcomeId;
71
+ return acc + +outcomeId;
72
+ }, 0);
73
+ return aSum - bSum;
74
+ });
75
+ }
76
+ });
77
+ return Object.values(result);
78
+ }export{aggregateOutcomesByMarkets};
@@ -0,0 +1,26 @@
1
+ import { type Dictionaries } from '@azuro-org/dictionaries';
2
+ type Outcome = {
3
+ conditionId: string;
4
+ outcomeId: string;
5
+ selectionName: string;
6
+ lpAddress: string;
7
+ coreAddress: string;
8
+ };
9
+ type Markets = {
10
+ marketName: string;
11
+ outcomes: Outcome[][];
12
+ }[];
13
+ type Props = {
14
+ lpAddress: string;
15
+ coreAddress: string;
16
+ conditions: {
17
+ [key: string]: any;
18
+ outcomes: {
19
+ [key: string]: any;
20
+ outcomeId: string;
21
+ }[];
22
+ }[];
23
+ dictionaries: Dictionaries;
24
+ };
25
+ export default function aggregateOutcomesByMarkets(props: Props): Markets;
26
+ export {};
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as aggregateOutcomesByMarkets } from './aggregateOutcomesByMarkets';
package/lib/index.js ADDED
@@ -0,0 +1,78 @@
1
+ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var dictionaries=require('@azuro-org/dictionaries');function aggregateOutcomesByMarkets(props) {
2
+ var lpAddress = props.lpAddress, coreAddress = props.coreAddress, conditions = props.conditions, dictionaries$1 = props.dictionaries;
3
+ // group conditions by marketId
4
+ var outcomesByMarketKey = {};
5
+ var result = {};
6
+ conditions.forEach(function (_a) {
7
+ var conditionId = _a.conditionId, outcomes = _a.outcomes;
8
+ outcomes.forEach(function (_a) {
9
+ var outcomeId = _a.outcomeId;
10
+ var marketKey = dictionaries.getMarketKey(outcomeId, dictionaries$1);
11
+ var marketName = dictionaries.getMarketName(outcomeId, dictionaries$1);
12
+ var selectionName = dictionaries.assembleSelectionName(outcomeId, dictionaries$1);
13
+ var outcome = {
14
+ conditionId: conditionId,
15
+ outcomeId: outcomeId,
16
+ selectionName: selectionName,
17
+ lpAddress: lpAddress,
18
+ coreAddress: coreAddress,
19
+ };
20
+ // it's important to use "marketKey" because it's unique
21
+ // on other hand "marketId" can be same for different groups of conditions
22
+ // "marketKey" is a string template "marketId-gamePeriodId-gameTypeId[-teamPlayerId]"
23
+ if (!outcomesByMarketKey[marketKey]) {
24
+ outcomesByMarketKey[marketKey] = [];
25
+ result[marketKey] = {
26
+ marketName: marketName,
27
+ outcomes: [],
28
+ };
29
+ }
30
+ outcomesByMarketKey[marketKey].push(outcome);
31
+ });
32
+ });
33
+ // sort by outcomeId and group by conditionId
34
+ Object.keys(outcomesByMarketKey).forEach(function (marketKey) {
35
+ var marketId = +marketKey.split('-')[0];
36
+ // get outcomes related to the market
37
+ var outcomes = outcomesByMarketKey[marketKey];
38
+ // sort the conditions by selectionId (outcome)
39
+ outcomes.sort(function (a, b) {
40
+ var left = dictionaries$1.outcomes[a.outcomeId].selectionId;
41
+ var right = dictionaries$1.outcomes[b.outcomeId].selectionId;
42
+ return left - right;
43
+ });
44
+ // markets with different conditionIds and not require additional grouping or sorting
45
+ var marketsWithDifferentConditionIds = [1, 2];
46
+ if (marketsWithDifferentConditionIds.includes(marketId)) {
47
+ result[marketKey].outcomes = [outcomes];
48
+ }
49
+ // group by conditionId to allow draw outcomes by rows in UI, e.g.
50
+ //
51
+ // Team 1 - Total Goals:
52
+ // Over (1.5) Under (1.5)
53
+ // Over (0.5) Under (0.5)
54
+ else {
55
+ var outcomesByConditionId_1 = {};
56
+ outcomes.forEach(function (outcome) {
57
+ var key = outcome.conditionId;
58
+ if (!outcomesByConditionId_1[key]) {
59
+ outcomesByConditionId_1[key] = [];
60
+ }
61
+ outcomesByConditionId_1[key].push(outcome);
62
+ });
63
+ var outcomesArr = Object.values(outcomesByConditionId_1);
64
+ result[marketKey].outcomes = outcomesArr.sort(function (a, b) {
65
+ var aSum = a.reduce(function (acc, _a) {
66
+ var outcomeId = _a.outcomeId;
67
+ return acc + +outcomeId;
68
+ }, 0);
69
+ var bSum = b.reduce(function (acc, _a) {
70
+ var outcomeId = _a.outcomeId;
71
+ return acc + +outcomeId;
72
+ }, 0);
73
+ return aSum - bSum;
74
+ });
75
+ }
76
+ });
77
+ return Object.values(result);
78
+ }exports.aggregateOutcomesByMarkets=aggregateOutcomesByMarkets;
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@azuro-org/toolkit",
3
+ "version": "0.1.0",
4
+ "description": "Set of helpers to work with Azuro protocol",
5
+ "module": "dist/index.es.js",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "scripts": {
9
+ "dev": "rollup -cw",
10
+ "build": "rimraf ./dist && rimraf ./lib && rollup -c --compact",
11
+ "prepublishOnly": "npm run build"
12
+ },
13
+ "keywords": [
14
+ "azuro",
15
+ "azuro-protocol",
16
+ "toolkit",
17
+ "helpers"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/Azuro-protocol/toolkit.git"
22
+ },
23
+ "author": "",
24
+ "license": "ISC",
25
+ "bugs": {
26
+ "url": "https://github.com/Azuro-protocol/toolkit/issues"
27
+ },
28
+ "homepage": "https://github.com/Azuro-protocol/toolkit/packages/toolkit#readme",
29
+ "peerDependencies": {
30
+ "@azuro-org/dictionaries": "^1.0.5"
31
+ },
32
+ "devDependencies": {
33
+ "@babel/core": "^7.17.0",
34
+ "@rollup/plugin-babel": "^6.0.3",
35
+ "@rollup/plugin-commonjs": "^23.0.3",
36
+ "@rollup/plugin-json": "^4.1.0",
37
+ "@types/node": "^17.0.21",
38
+ "builtin-modules": "^3.3.0",
39
+ "minimist": "^1.2.7",
40
+ "rimraf": "^3.0.2",
41
+ "rollup": "^2.67.0",
42
+ "rollup-plugin-babel": "^4.4.0",
43
+ "rollup-plugin-typescript2": "^0.34.1",
44
+ "tslib": "^2.4.1",
45
+ "typescript": "^4.6.2"
46
+ }
47
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "module": "esnext",
5
+ "moduleResolution": "node",
6
+ "lib": [ "es2017", "es7", "es6", "dom" ],
7
+ "outDir": "lib",
8
+ "jsx": "react",
9
+ "strict": true,
10
+ "declaration": true,
11
+ "declarationDir": ".",
12
+ "esModuleInterop": true
13
+ },
14
+ "exclude": [
15
+ "node_modules",
16
+ "dist",
17
+ "lib"
18
+ ]
19
+ }