-
Notifications
You must be signed in to change notification settings - Fork 110
/
mutez.ts
113 lines (95 loc) · 2.43 KB
/
mutez.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
import {
Token,
TokenFactory,
ComparableToken,
TokenValidationError,
SemanticEncoding,
} from '../token';
import BigNumber from 'bignumber.js';
import { BaseTokenSchema } from '../../schema/types';
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing Mutez
*/
export class MutezValidationError extends TokenValidationError {
name = 'MutezValidationError';
constructor(public value: any, public token: MutezToken, message: string) {
super(value, token, message);
}
}
export class MutezToken extends ComparableToken {
static prim: 'mutez' = 'mutez' as const;
constructor(
protected val: { prim: string; args: any[]; annots: any[] },
protected idx: number,
protected fac: TokenFactory
) {
super(val, idx, fac);
}
public Execute(val: any) {
return new BigNumber(val[Object.keys(val)[0]]);
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
public ExtractSchema() {
return MutezToken.prim;
}
generateSchema(): BaseTokenSchema {
return {
__michelsonType: MutezToken.prim,
schema: MutezToken.prim,
};
}
/**
* @throws {@link MutezValidationError}
*/
private validate(val: any) {
const bigNumber = new BigNumber(val);
if (bigNumber.isNaN()) {
throw new MutezValidationError(val, this, `Value is not a number: ${val}`);
}
}
/**
* @throws {@link MutezValidationError}
*/
public Encode(args: any[]): any {
const val = args.pop();
this.validate(val);
return { int: String(val).toString() };
}
/**
* @throws {@link MutezValidationError}
*/
public EncodeObject(val: any, semantic?: SemanticEncoding): any {
this.validate(val);
if (semantic && semantic[MutezToken.prim]) {
return semantic[MutezToken.prim](val);
}
return { int: String(val).toString() };
}
public ToBigMapKey(val: string | number) {
return {
key: { int: String(val) },
type: { prim: MutezToken.prim },
};
}
public ToKey({ int }: any) {
return int;
}
compare(mutez1: string | number, mutez2: string | number) {
const o1 = Number(mutez1);
const o2 = Number(mutez2);
if (o1 === o2) {
return 0;
}
return o1 < o2 ? -1 : 1;
}
findAndReturnTokens(tokenToFind: string, tokens: Token[]) {
if (MutezToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}