-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate.ts
309 lines (254 loc) · 6.76 KB
/
generate.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
interface ConstructorMember {
inputs: FunctionMemberInput[]
payable: false
type: 'constructor'
}
interface EventMember {
inputs: EventMemberInput[]
name: string
type: 'event'
}
interface FunctionMember {
inputs: FunctionMemberInput[]
outputs: FunctionMemberInput[]
name: string
constant: boolean
payable: boolean
type: 'function'
}
interface FallbackMember {
type: 'fallback'
payable: boolean
}
interface UnknownMember {
type: string
}
type SolidityType =
| 'address'
| 'address[]'
| 'bool'
| 'bytes'
| 'bytes32'
| 'bytes32[]'
| 'string'
| 'uint8'
| 'uint16'
| 'uint64'
| 'uint256'
| 'uint256[]'
interface FunctionMemberInput {
name: string
type: SolidityType
}
interface EventMemberInput extends FunctionMemberInput {
indexed: boolean
}
type Member = ConstructorMember | EventMember | FunctionMember | FallbackMember
type Abi = (EventMember | FunctionMember | ConstructorMember)[]
interface Definition {
contractName: string
abi: Abi
}
function generateHeader(isTruffle: boolean): string {
let str = `
import * as Web3 from "web3";
import * as BigNumber from "bignumber.js";
`
if (isTruffle) {
str += `
import {SolidityEvent} from 'web3';
`
}
str += `type Address = string;
type TransactionOptions = Partial<Transaction>;
type UInt = number | BigNumber.BigNumber;
interface Transaction {
hash: string;
nonce: number;
blockHash: string | null;
blockNumber: number | null;
transactionIndex: number | null;
from: Address | ContractInstance;
to: string | null;
value: BigNumber.BigNumber
gasPrice: BigNumber.BigNumber
gas: number;
input: string;
}
declare type ContractTest = (accounts: string[]) => void
declare global {
function contract(name: string, test: ContractTest): void
var artifacts: Artifacts
var web3: Web3
var assert: Chai.AssertStatic
}
interface ContractInstance {
address: string;
sendTransaction(options?: TransactionOptions): Promise<void>;
`
if (isTruffle) {
str += `
allEvents(options: {
fromBlock: number
toBlock: number | 'latest'
}): Web3.FilterResult;
`
}
str += `
}
`
return str
}
function generateArtifacts(files: string[]) {
return `
interface Artifacts {
${files.map(file => {
let definition: Definition = require(file)
return `require(name: "${definition.contractName}"): ${
definition.contractName
}Contract`
})}
}
`
}
function buildContract(definition: Definition, isTruffle: boolean) {
return `
export interface ${definition.contractName}Instance extends ContractInstance {
${buildMembers(definition.abi, isTruffle)}
}
export interface ${definition.contractName}Contract {
new: (${buildConstructorArguments(
definition.abi
)}options?: TransactionOptions) => Promise<${definition.contractName}Instance>;
deployed(): Promise<${definition.contractName}Instance>;
at(address: string): ${definition.contractName}Instance;
}
`
}
function buildMembers(abi: Abi, isTruffle: boolean): string {
return abi.map(buildMember).join('\n')
}
function buildMember(member: Member): string {
switch (member.type) {
case 'function':
return buildFunctionMember(member)
case 'event':
return buildEventMember(member)
case 'constructor':
return buildConstructorMember(member)
case 'fallback':
return buildFallbackMember(member)
default:
throw 'Exhaustiveness miss!'
}
}
function isConstructorMember(member: Member): member is ConstructorMember {
return member.type === 'constructor'
}
function buildConstructorArguments(abi: Abi): string {
const constructorMember = abi.find(isConstructorMember)
if (!constructorMember) {
return ''
}
const args = constructorMember.inputs.map(buildFunctionArgument).join(', ')
return args === '' ? args : args + ', '
}
function buildFunctionMember(member: FunctionMember) {
let args = member.inputs.map(buildFunctionArgument).join(', ')
if (args.length > 0) {
args += ', '
}
const functionSignature = `(${args}options?: TransactionOptions)`
return `${member.name}: {
${functionSignature}: Promise<Web3.TransactionReceipt>;
call${functionSignature}: ${translateOutputs(member.outputs)};
sendTransaction${functionSignature}: Promise<string>;
estimateGas${functionSignature}: Promise<number>;
}`
}
function translateOutputs(outputs: FunctionMemberInput[]) {
let valueType
if (outputs.length === 1) {
valueType = translateOutput(outputs[0])
} else if (outputs.length === 0) {
valueType = 'Web3.TransactionReceipt'
} else {
valueType = `[${outputs.map(translateOutput).join(', ')}]`
}
return `Promise<${valueType}>`
}
function translateOutput(output: FunctionMemberInput) {
return translateType(output.type, {UInt: 'BigNumber.BigNumber'})
}
let unnamedArgumentNumber = 0
function unnamedArgumentName(): string {
return `unnamed${unnamedArgumentNumber++}`
}
function buildFunctionArgument(input: FunctionMemberInput): string {
let name = input.name
if (name[0] == '_') {
name = name.slice(1)
}
const type = translateType(input.type)
if (name.length === 0) {
name = unnamedArgumentName()
}
return `${name}: ${type}`
}
function translateType(type: SolidityType, options = {UInt: 'UInt'}): string {
switch (type) {
case 'string':
return 'string'
case 'address':
return 'Address'
case 'address[]':
return 'Address[]'
case 'bool':
return 'boolean'
case 'bytes':
return 'string'
case 'bytes32':
return 'string'
case 'bytes32[]':
return 'string[]'
case 'uint8':
case 'uint16':
case 'uint64':
case 'uint256':
return options.UInt
case 'uint256[]':
return `${options.UInt}[]`
default:
throw `Unexpected case! ${type}`
}
}
function buildEventMember(member: EventMember) {
let args = member.inputs.map(buildFunctionArgument).join(', ')
if (args.length > 0) {
args += ', '
}
const eventType = `{${args}}`
return `${member.name}: Web3.EventFilterCreator<${eventType}>\n`
}
function buildConstructorMember(_member: Member) {
return ''
}
function buildFallbackMember(_member: Member) {
return ''
}
const glob = require('glob')
glob('./build/contracts/*.json', {}, (err: string, files: string[]) => {
let isTruffle = process.argv.indexOf('--truffle') != -1
let buffer = ''
if (err) {
console.log('Error!', err)
return
}
buffer += generateHeader(isTruffle)
buffer += generateArtifacts(files)
files.forEach(file => {
let definition: Definition = require(file)
buffer += buildContract(definition, isTruffle)
})
console.log(buffer)
})