-
Notifications
You must be signed in to change notification settings - Fork 110
/
bigmap.ts
168 lines (150 loc) · 4.57 KB
/
bigmap.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { MichelsonMap } from '../michelson-map';
import { BigMapTokenSchema } from '../schema/types';
import {
ComparableToken,
Semantic,
SemanticEncoding,
Token,
TokenFactory,
TokenValidationError,
} from './token';
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing Big Map types
*/
export class BigMapValidationError extends TokenValidationError {
name = 'BigMapValidationError';
constructor(public value: any, public token: BigMapToken, message: string) {
super(value, token, message);
}
}
export class BigMapToken extends Token {
static prim: 'big_map' = 'big_map' as const;
constructor(
protected val: { prim: string; args: any[]; annots?: any[] },
protected idx: number,
protected fac: TokenFactory
) {
super(val, idx, fac);
}
get ValueSchema() {
return this.createToken(this.val.args[1], 0);
}
get KeySchema(): ComparableToken {
return this.createToken(this.val.args[0], 0) as unknown as ComparableToken;
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
public ExtractSchema() {
return {
big_map: {
key: this.KeySchema.ExtractSchema(),
value: this.ValueSchema.ExtractSchema(),
},
};
}
generateSchema(): BigMapTokenSchema {
return {
__michelsonType: BigMapToken.prim,
schema: {
key: this.KeySchema.generateSchema(),
value: this.ValueSchema.generateSchema(),
},
};
}
/**
* @throws {@link BigMapValidationError}
*/
private validate(value: any) {
if (!MichelsonMap.isMichelsonMap(value)) {
throw new BigMapValidationError(
value,
this,
`Value ${JSON.stringify(value)} is not a MichelsonMap`
);
}
}
private objLitToMichelsonMap(val: any): any {
if (val instanceof MichelsonMap) return val;
if (typeof val === 'object') {
if (Object.keys(val).length === 0) {
return new MichelsonMap();
} else {
return MichelsonMap.fromLiteral(val);
}
}
return val;
}
/**
* @throws {@link BigMapValidationError}
*/
public Encode(args: any[]): any {
const val: MichelsonMap<any, any> = this.objLitToMichelsonMap(args.pop());
this.validate(val);
return Array.from(val.keys())
.sort((a: any, b: any) => this.KeySchema.compare(a, b))
.map((key) => {
return {
prim: 'Elt',
args: [this.KeySchema.EncodeObject(key), this.ValueSchema.EncodeObject(val.get(key))],
};
});
}
/**
* @throws {@link BigMapValidationError}
*/
public EncodeObject(args: any, semantic?: SemanticEncoding): any {
const val: MichelsonMap<any, any> = this.objLitToMichelsonMap(args);
this.validate(val);
if (semantic && semantic[BigMapToken.prim]) {
return semantic[BigMapToken.prim](val, this.val);
}
return Array.from(val.keys())
.sort((a: any, b: any) => this.KeySchema.compare(a, b))
.map((key) => {
return {
prim: 'Elt',
args: [this.KeySchema.EncodeObject(key), this.ValueSchema.EncodeObject(val.get(key))],
};
});
}
/**
* @throws {@link InvalidMapTypeError} when the argument passed to val is an array but not a valid map type
* @throws {@link BigMapValidationError} when the value is invalid
*/
public Execute(val: any[] | { int: string }, semantic?: Semantic) {
if (semantic && semantic[BigMapToken.prim]) {
return semantic[BigMapToken.prim](val as any, this.val);
}
if (Array.isArray(val)) {
// Athens is returning an empty array for big map in storage
// Internal: In taquito v5 it is still used to decode big map diff (as if they were a regular map)
const map = new MichelsonMap(this.val);
val.forEach((current) => {
map.set(this.KeySchema.ToKey(current.args[0]), this.ValueSchema.Execute(current.args[1]));
});
return map;
} else if ('int' in val) {
// Babylon is returning an int with the big map id in contract storage
return val.int;
} else {
throw new BigMapValidationError(
val,
this,
`Big map is expecting either an array (Athens) or an object with an int property (Babylon). Got ${JSON.stringify(
val
)}`
);
}
}
findAndReturnTokens(tokenToFind: string, tokens: Token[]) {
if (BigMapToken.prim === tokenToFind) {
tokens.push(this);
}
this.KeySchema.findAndReturnTokens(tokenToFind, tokens);
this.ValueSchema.findAndReturnTokens(tokenToFind, tokens);
return tokens;
}
}