UNPKG

2.83 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25const semver = __importStar(require("semver"));
26function update(compilerVersion, abi) {
27 let hasConstructor = false;
28 let hasFallback = false;
29 for (let i = 0; i < abi.length; i++) {
30 const item = abi[i];
31 if (item.type === 'constructor') {
32 hasConstructor = true;
33 // <0.4.5 assumed every constructor to be payable
34 if (semver.lt(compilerVersion, '0.4.5')) {
35 item.payable = true;
36 }
37 }
38 else if (item.type === 'fallback') {
39 hasFallback = true;
40 }
41 if (item.type !== 'event') {
42 // add 'payable' to everything, except constant functions
43 if (!item.constant && semver.lt(compilerVersion, '0.4.0')) {
44 item.payable = true;
45 }
46 // add stateMutability field
47 if (semver.lt(compilerVersion, '0.4.16')) {
48 if (item.payable) {
49 item.stateMutability = 'payable';
50 }
51 else if (item.constant) {
52 item.stateMutability = 'view';
53 }
54 else {
55 item.stateMutability = 'nonpayable';
56 }
57 }
58 }
59 }
60 // 0.1.2 from Aug 2015 had it. The code has it since May 2015 (e7931ade)
61 if (!hasConstructor && semver.lt(compilerVersion, '0.1.2')) {
62 abi.push({
63 type: 'constructor',
64 payable: true,
65 stateMutability: 'payable',
66 inputs: []
67 });
68 }
69 if (!hasFallback && semver.lt(compilerVersion, '0.4.0')) {
70 abi.push({
71 type: 'fallback',
72 payable: true,
73 stateMutability: 'payable'
74 });
75 }
76 return abi;
77}
78module.exports = {
79 update
80};