/
big-map.ts
66 lines (61 loc) · 2.28 KB
/
big-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Schema, BigMapKeyType } from '@taquito/michelson-encoder';
import BigNumber from 'bignumber.js';
import { ContractProvider } from './interface';
import { HttpResponseError, STATUS_CODE } from '@taquito/http-utils';
export class BigMapAbstraction {
constructor(private id: BigNumber, private schema: Schema, private provider: ContractProvider) {}
/**
*
* @description Fetch one value in a big map
*
* @param keysToEncode Key to query (will be encoded properly according to the schema)
* @param block optional block level to fetch the values from (head will be use by default)
* @returns Return a well formatted json object of a big map value or undefined if the key is not found in the big map
*
*/
async get<T>(keyToEncode: BigMapKeyType, block?: number) {
try {
const id = await this.provider.getBigMapKeyByID<T>(
this.id.toString(),
keyToEncode,
this.schema,
block
);
return id;
} catch (e) {
if (e instanceof HttpResponseError && e.status === STATUS_CODE.NOT_FOUND) {
return undefined;
} else {
throw e;
}
}
}
/**
*
* @description Fetch multiple values in a big map
* All values will be fetched on the same block level. If a block is specified in the request, the values will be fetched at it.
* Otherwise, a first request will be done to the node to fetch the level of the head and all values will be fetched at this level.
* If one of the keys does not exist in the big map, its value will be set to undefined.
*
* @param keysToEncode Array of keys to query (will be encoded properly according to the schema)
* @param block optional block level to fetch the values from
* @param batchSize optional batch size representing the number of requests to execute in parallel
* @returns A MichelsonMap containing the keys queried in the big map and their value in a well-formatted JSON object format
*
*/
async getMultipleValues<T>(keysToEncode: Array<BigMapKeyType>, block?: number, batchSize = 5) {
return this.provider.getBigMapKeysByID<T>(
this.id.toString(),
keysToEncode,
this.schema,
block,
batchSize
);
}
toJSON() {
return this.id.toString();
}
toString() {
return this.id.toString();
}
}