@azuro-org/toolkit 0.1.4 → 0.1.6

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 CHANGED
@@ -10,107 +10,120 @@ npm i --save @azuro-org/toolkit
10
10
  ```
11
11
 
12
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
13
  ## Helpers
37
14
 
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)
15
+ ### `aggregateOutcomesByMarkets.ts`
16
+
17
+ This helper function allows you to aggregate outcomes that are obtained from conditions by markets. This function takes
18
+ an array of conditions and groups outcomes by their market key. It then sorts the outcomes within each market group
19
+ and returns an array of market objects, each containing an array of outcomes.
20
+
21
+ Here is an example of how you can use it:
22
+
23
+ ```graphql
24
+ query Game($id: String!) {
25
+ game(id: $id) {
26
+ liquidityPool {
27
+ address
28
+ }
29
+ conditions {
30
+ conditionId
31
+ coreAddress
32
+ outcomes {
33
+ outcomeId
34
+ }
35
+ }
36
+ }
37
+ }
50
38
  ```
51
39
 
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'
40
+ ```ts
41
+ type Conditions = {
42
+ conditionId: string
43
+ coreAddress: string
44
+ outcomes: {
45
+ outcomeId: string
46
+ }
47
+ }
62
48
 
63
- dictionaries.marketNames['1-1-1'] // "Full Time Result"
64
- dictionaries.marketDescriptions['1-1-1'] // "You predict the result..."
49
+ aggregateOutcomesByMarkets({
50
+ lpAddress: game.liquidityPool.lpAddress,
51
+ conditions: game.conditions,
52
+ dictionaries, // ** check the note below
53
+ })
65
54
  ```
66
55
 
67
- **!!! Note that there are no texts for each `outcomeId` !!!**
56
+ The result will be of type
68
57
 
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`.
58
+ ```ts
59
+ type Outcome = {
60
+ selectionName: string
61
+ conditionId: string
62
+ outcomeId: string
63
+ lpAddress: string
64
+ coreAddress: string
65
+ }
72
66
 
67
+ type OutcomesByMarkets = {
68
+ marketName: string
69
+ outcomes: Outcome[][]
70
+ }[]
73
71
  ```
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
72
 
82
- if (!marketName) {
83
- marketName = assembleMarketName(outcomeId, dictionaries) // "Whole game - Winner of match Goal"
73
+ `*` returned `outcomes` are wrapped with additional array `Outcome[][]`
74
+
75
+ `**` `dictionaries` contain a set of texts for each outcome ID used in Azuro. These texts can be used to display outcomes
76
+ in a user-friendly way and provide more context about what the outcome represents. Dictionaries are an important
77
+ component of Azuro's infrastructure as they allow for standardized and consistent outcomes across all markets and events.
78
+ [Read more about dictionaries](https://azuro-v2-docs.surge.sh/build-own-app/dive-deeper/dictionaries).
79
+
80
+ You can pass additional data in outcomes if required, the helper will add it to outcomes in the result:
81
+
82
+ ```graphql {11}
83
+ query Game($id: String!) {
84
+ game(id: $id) {
85
+ liquidityPool {
86
+ address
87
+ }
88
+ conditions {
89
+ conditionId
90
+ coreAddress
91
+ outcomes {
92
+ outcomeId
93
+ odds
94
+ }
95
+ }
96
+ }
84
97
  }
85
98
  ```
86
99
 
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
+ ```ts
101
+ type MyOutcome = {
102
+ outcomeId: string
103
+ odds: string
104
+ }
100
105
 
101
- getMarketDescription(1, dictionaries) // "You predict the result..."
102
- getMarketDescription(42, dictionaries) // undefined. Note that there is no `assemblyMarketDescription` helper.
106
+ aggregateOutcomesByMarkets<MyOutcome>({
107
+ lpAddress: game.liquidityPool.lpAddress,
108
+ conditions: game.conditions,
109
+ dictionaries, // ** check the note below
110
+ })
103
111
  ```
104
112
 
105
- ### Get selection (outcome) name
106
-
107
- ```js
108
- import { assembleSelectionName } from '@azuro-org/dictionaries'
109
- import dictionaries from './dist'
113
+ The result will be of type
110
114
 
111
- const outcomeId = 1
112
- const selectionName = assembleSelectionName(outcomeId, dictionaries) // "Yes"
115
+ ```ts {7}
116
+ type Outcome = {
117
+ selectionName: string
118
+ conditionId: string
119
+ outcomeId: string
120
+ lpAddress: string
121
+ coreAddress: string
122
+ odds: string
123
+ }
113
124
 
114
- const outcomeId = 4
115
- const selectionName = assembleSelectionName(outcomeId, dictionaries) // "Team 2 (4.5)"
125
+ type OutcomesByMarkets = {
126
+ marketName: string
127
+ outcomes: Outcome[][]
128
+ }[]
116
129
  ```
@@ -6,25 +6,22 @@ type Outcome<T> = T & {
6
6
  lpAddress: string;
7
7
  coreAddress: string;
8
8
  };
9
- type Markets<T> = {
9
+ type FinalMarket<T> = {
10
10
  marketName: string;
11
11
  outcomes: Outcome<T>[][];
12
- }[];
12
+ };
13
13
  type Props = {
14
14
  lpAddress: string;
15
15
  conditions: {
16
16
  [key: string]: any;
17
17
  conditionId: string;
18
+ coreAddress: string;
18
19
  outcomes: {
19
20
  [key: string]: any;
20
21
  outcomeId: string;
21
22
  }[];
22
- core: {
23
- [key: string]: any;
24
- address: string;
25
- };
26
23
  }[];
27
24
  dictionaries: Dictionaries;
28
25
  };
29
- export default function aggregateOutcomesByMarkets<T extends {}>(props: Props): Markets<T>;
26
+ export default function aggregateOutcomesByMarkets<T extends {}>(props: Props): FinalMarket<T>[];
30
27
  export {};
package/dist/index.es.js CHANGED
@@ -36,52 +36,53 @@ function __rest(s, e) {
36
36
  return t;
37
37
  }function aggregateOutcomesByMarkets(props) {
38
38
  var lpAddress = props.lpAddress, conditions = props.conditions, dictionaries = props.dictionaries;
39
- // group conditions by marketId
40
- var outcomesByMarketKey = {};
41
- var result = {};
39
+ var marketsMap = {};
42
40
  conditions.forEach(function (_a) {
43
- var conditionId = _a.conditionId, outcomes = _a.outcomes, core = _a.core;
41
+ var conditionId = _a.conditionId, outcomes = _a.outcomes, coreAddress = _a.coreAddress;
44
42
  outcomes.forEach(function (_a) {
45
43
  var outcomeId = _a.outcomeId, rest = __rest(_a, ["outcomeId"]);
44
+ // we are using the same key format that was discussed earlier
46
45
  var marketKey = getMarketKey(outcomeId, dictionaries);
46
+ // we are obtaining the human-readable names of each market and the corresponding outcome selections
47
47
  var marketName = getMarketName(outcomeId, dictionaries);
48
48
  var selectionName = assembleSelectionName(outcomeId, dictionaries);
49
- var outcome = __assign({ conditionId: conditionId, outcomeId: outcomeId, selectionName: selectionName, lpAddress: lpAddress, coreAddress: core.address }, rest);
50
- // it's important to use "marketKey" because it's unique
51
- // on other hand "marketId" can be same for different groups of conditions
52
- // "marketKey" is a string template "marketId-gamePeriodId-gameTypeId[-teamPlayerId]"
53
- if (!outcomesByMarketKey[marketKey]) {
54
- outcomesByMarketKey[marketKey] = [];
55
- result[marketKey] = {
49
+ var outcome = __assign({ conditionId: conditionId, outcomeId: outcomeId, lpAddress: lpAddress, coreAddress: coreAddress, selectionName: selectionName }, rest);
50
+ if (!marketsMap[marketKey]) {
51
+ marketsMap[marketKey] = {
56
52
  marketName: marketName,
57
53
  outcomes: [],
58
54
  };
59
55
  }
60
- outcomesByMarketKey[marketKey].push(outcome);
56
+ marketsMap[marketKey].outcomes.push(outcome);
61
57
  });
62
58
  });
59
+ var finalMarketsMap = {};
63
60
  // sort by outcomeId and group by conditionId
64
- Object.keys(outcomesByMarketKey).forEach(function (marketKey) {
65
- var marketId = +marketKey.split('-')[0];
66
- // get outcomes related to the market
67
- var outcomes = outcomesByMarketKey[marketKey];
68
- // sort the conditions by selectionId (outcome)
61
+ Object.keys(marketsMap).forEach(function (marketKey) {
62
+ var _a = marketsMap[marketKey], marketName = _a.marketName, outcomes = _a.outcomes;
63
+ finalMarketsMap[marketKey] = {
64
+ marketName: marketName,
65
+ outcomes: null,
66
+ };
67
+ // sort the outcomes by `selectionId` (outcome's selection reference)
69
68
  outcomes.sort(function (a, b) {
70
69
  var left = dictionaries.outcomes[a.outcomeId].selectionId;
71
70
  var right = dictionaries.outcomes[b.outcomeId].selectionId;
72
71
  return left - right;
73
72
  });
74
- // markets with different conditionIds and not require additional grouping or sorting
75
- var marketsWithDifferentConditionIds = [1, 2];
76
- if (marketsWithDifferentConditionIds.includes(marketId)) {
77
- result[marketKey].outcomes = [outcomes];
73
+ // "Full Time Result" and "Double Chance" are the markets whose outcomes don't require sorting
74
+ var MARKETS_THAT_DONT_NEED_GROUPING = [1, 2];
75
+ var marketId = marketKey.split('-')[0];
76
+ if (MARKETS_THAT_DONT_NEED_GROUPING.includes(+marketId)) {
77
+ // it's worth noting that the outcomes are wrapped within an array here due to the "rows" that are presented below
78
+ finalMarketsMap[marketKey].outcomes = [outcomes];
78
79
  }
79
- // group by conditionId to allow draw outcomes by rows in UI, e.g.
80
- //
81
- // Team 1 - Total Goals:
82
- // Over (1.5) Under (1.5)
83
- // Over (0.5) Under (0.5)
84
80
  else {
81
+ // group the outcomes by condition ID, which will allow us to display the draw outcomes in separate rows
82
+ //
83
+ // Handicap:
84
+ // H1 (-0.5) H2 (0.5)
85
+ // H1 (0.5) H2 (-0.5)
85
86
  var outcomesByConditionId_1 = {};
86
87
  outcomes.forEach(function (outcome) {
87
88
  var key = outcome.conditionId;
@@ -90,8 +91,8 @@ function __rest(s, e) {
90
91
  }
91
92
  outcomesByConditionId_1[key].push(outcome);
92
93
  });
93
- var outcomesArr = Object.values(outcomesByConditionId_1);
94
- result[marketKey].outcomes = outcomesArr.sort(function (a, b) {
94
+ var rows = Object.values(outcomesByConditionId_1);
95
+ finalMarketsMap[marketKey].outcomes = rows.sort(function (a, b) {
95
96
  var aSum = a.reduce(function (acc, _a) {
96
97
  var outcomeId = _a.outcomeId;
97
98
  return acc + +outcomeId;
@@ -104,5 +105,5 @@ function __rest(s, e) {
104
105
  });
105
106
  }
106
107
  });
107
- return Object.values(result);
108
+ return Object.values(finalMarketsMap);
108
109
  }export{aggregateOutcomesByMarkets};
@@ -6,25 +6,22 @@ type Outcome<T> = T & {
6
6
  lpAddress: string;
7
7
  coreAddress: string;
8
8
  };
9
- type Markets<T> = {
9
+ type FinalMarket<T> = {
10
10
  marketName: string;
11
11
  outcomes: Outcome<T>[][];
12
- }[];
12
+ };
13
13
  type Props = {
14
14
  lpAddress: string;
15
15
  conditions: {
16
16
  [key: string]: any;
17
17
  conditionId: string;
18
+ coreAddress: string;
18
19
  outcomes: {
19
20
  [key: string]: any;
20
21
  outcomeId: string;
21
22
  }[];
22
- core: {
23
- [key: string]: any;
24
- address: string;
25
- };
26
23
  }[];
27
24
  dictionaries: Dictionaries;
28
25
  };
29
- export default function aggregateOutcomesByMarkets<T extends {}>(props: Props): Markets<T>;
26
+ export default function aggregateOutcomesByMarkets<T extends {}>(props: Props): FinalMarket<T>[];
30
27
  export {};
package/lib/index.js CHANGED
@@ -36,52 +36,53 @@ function __rest(s, e) {
36
36
  return t;
37
37
  }function aggregateOutcomesByMarkets(props) {
38
38
  var lpAddress = props.lpAddress, conditions = props.conditions, dictionaries$1 = props.dictionaries;
39
- // group conditions by marketId
40
- var outcomesByMarketKey = {};
41
- var result = {};
39
+ var marketsMap = {};
42
40
  conditions.forEach(function (_a) {
43
- var conditionId = _a.conditionId, outcomes = _a.outcomes, core = _a.core;
41
+ var conditionId = _a.conditionId, outcomes = _a.outcomes, coreAddress = _a.coreAddress;
44
42
  outcomes.forEach(function (_a) {
45
43
  var outcomeId = _a.outcomeId, rest = __rest(_a, ["outcomeId"]);
44
+ // we are using the same key format that was discussed earlier
46
45
  var marketKey = dictionaries.getMarketKey(outcomeId, dictionaries$1);
46
+ // we are obtaining the human-readable names of each market and the corresponding outcome selections
47
47
  var marketName = dictionaries.getMarketName(outcomeId, dictionaries$1);
48
48
  var selectionName = dictionaries.assembleSelectionName(outcomeId, dictionaries$1);
49
- var outcome = __assign({ conditionId: conditionId, outcomeId: outcomeId, selectionName: selectionName, lpAddress: lpAddress, coreAddress: core.address }, rest);
50
- // it's important to use "marketKey" because it's unique
51
- // on other hand "marketId" can be same for different groups of conditions
52
- // "marketKey" is a string template "marketId-gamePeriodId-gameTypeId[-teamPlayerId]"
53
- if (!outcomesByMarketKey[marketKey]) {
54
- outcomesByMarketKey[marketKey] = [];
55
- result[marketKey] = {
49
+ var outcome = __assign({ conditionId: conditionId, outcomeId: outcomeId, lpAddress: lpAddress, coreAddress: coreAddress, selectionName: selectionName }, rest);
50
+ if (!marketsMap[marketKey]) {
51
+ marketsMap[marketKey] = {
56
52
  marketName: marketName,
57
53
  outcomes: [],
58
54
  };
59
55
  }
60
- outcomesByMarketKey[marketKey].push(outcome);
56
+ marketsMap[marketKey].outcomes.push(outcome);
61
57
  });
62
58
  });
59
+ var finalMarketsMap = {};
63
60
  // sort by outcomeId and group by conditionId
64
- Object.keys(outcomesByMarketKey).forEach(function (marketKey) {
65
- var marketId = +marketKey.split('-')[0];
66
- // get outcomes related to the market
67
- var outcomes = outcomesByMarketKey[marketKey];
68
- // sort the conditions by selectionId (outcome)
61
+ Object.keys(marketsMap).forEach(function (marketKey) {
62
+ var _a = marketsMap[marketKey], marketName = _a.marketName, outcomes = _a.outcomes;
63
+ finalMarketsMap[marketKey] = {
64
+ marketName: marketName,
65
+ outcomes: null,
66
+ };
67
+ // sort the outcomes by `selectionId` (outcome's selection reference)
69
68
  outcomes.sort(function (a, b) {
70
69
  var left = dictionaries$1.outcomes[a.outcomeId].selectionId;
71
70
  var right = dictionaries$1.outcomes[b.outcomeId].selectionId;
72
71
  return left - right;
73
72
  });
74
- // markets with different conditionIds and not require additional grouping or sorting
75
- var marketsWithDifferentConditionIds = [1, 2];
76
- if (marketsWithDifferentConditionIds.includes(marketId)) {
77
- result[marketKey].outcomes = [outcomes];
73
+ // "Full Time Result" and "Double Chance" are the markets whose outcomes don't require sorting
74
+ var MARKETS_THAT_DONT_NEED_GROUPING = [1, 2];
75
+ var marketId = marketKey.split('-')[0];
76
+ if (MARKETS_THAT_DONT_NEED_GROUPING.includes(+marketId)) {
77
+ // it's worth noting that the outcomes are wrapped within an array here due to the "rows" that are presented below
78
+ finalMarketsMap[marketKey].outcomes = [outcomes];
78
79
  }
79
- // group by conditionId to allow draw outcomes by rows in UI, e.g.
80
- //
81
- // Team 1 - Total Goals:
82
- // Over (1.5) Under (1.5)
83
- // Over (0.5) Under (0.5)
84
80
  else {
81
+ // group the outcomes by condition ID, which will allow us to display the draw outcomes in separate rows
82
+ //
83
+ // Handicap:
84
+ // H1 (-0.5) H2 (0.5)
85
+ // H1 (0.5) H2 (-0.5)
85
86
  var outcomesByConditionId_1 = {};
86
87
  outcomes.forEach(function (outcome) {
87
88
  var key = outcome.conditionId;
@@ -90,8 +91,8 @@ function __rest(s, e) {
90
91
  }
91
92
  outcomesByConditionId_1[key].push(outcome);
92
93
  });
93
- var outcomesArr = Object.values(outcomesByConditionId_1);
94
- result[marketKey].outcomes = outcomesArr.sort(function (a, b) {
94
+ var rows = Object.values(outcomesByConditionId_1);
95
+ finalMarketsMap[marketKey].outcomes = rows.sort(function (a, b) {
95
96
  var aSum = a.reduce(function (acc, _a) {
96
97
  var outcomeId = _a.outcomeId;
97
98
  return acc + +outcomeId;
@@ -104,5 +105,5 @@ function __rest(s, e) {
104
105
  });
105
106
  }
106
107
  });
107
- return Object.values(result);
108
+ return Object.values(finalMarketsMap);
108
109
  }exports.aggregateOutcomesByMarkets=aggregateOutcomesByMarkets;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azuro-org/toolkit",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Set of helpers to work with Azuro protocol",
5
5
  "module": "dist/index.es.js",
6
6
  "main": "lib/index.js",
@@ -25,7 +25,7 @@
25
25
  "bugs": {
26
26
  "url": "https://github.com/Azuro-protocol/toolkit/issues"
27
27
  },
28
- "homepage": "https://github.com/Azuro-protocol/toolkit/packages/toolkit#readme",
28
+ "homepage": "https://github.com/Azuro-protocol/toolkit#readme",
29
29
  "peerDependencies": {
30
30
  "@azuro-org/dictionaries": "^1.0.5"
31
31
  },