-
Notifications
You must be signed in to change notification settings - Fork 475
/
abi.ts
63 lines (53 loc) · 1.46 KB
/
abi.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
import * as semver from 'semver';
function update (compilerVersion, abi) {
let hasConstructor = false;
let hasFallback = false;
for (let i = 0; i < abi.length; i++) {
const item = abi[i];
if (item.type === 'constructor') {
hasConstructor = true;
// <0.4.5 assumed every constructor to be payable
if (semver.lt(compilerVersion, '0.4.5')) {
item.payable = true;
}
} else if (item.type === 'fallback') {
hasFallback = true;
}
if (item.type !== 'event') {
// add 'payable' to everything, except constant functions
if (!item.constant && semver.lt(compilerVersion, '0.4.0')) {
item.payable = true;
}
// add stateMutability field
if (semver.lt(compilerVersion, '0.4.16')) {
if (item.payable) {
item.stateMutability = 'payable';
} else if (item.constant) {
item.stateMutability = 'view';
} else {
item.stateMutability = 'nonpayable';
}
}
}
}
// 0.1.2 from Aug 2015 had it. The code has it since May 2015 (e7931ade)
if (!hasConstructor && semver.lt(compilerVersion, '0.1.2')) {
abi.push({
type: 'constructor',
payable: true,
stateMutability: 'payable',
inputs: []
});
}
if (!hasFallback && semver.lt(compilerVersion, '0.4.0')) {
abi.push({
type: 'fallback',
payable: true,
stateMutability: 'payable'
});
}
return abi;
}
export = {
update
};