-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtransaction.ts
342 lines (316 loc) · 11.8 KB
/
transaction.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* eslint-disable max-classes-per-file */
import { concat } from '@ethersproject/bytes';
import { Coder, ArrayCoder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder';
import U64Coder from '@fuel-ts/abi-coder/src/coders/u64';
import { ByteArrayCoder } from './byte-array';
import type { Input } from './input';
import { InputCoder } from './input';
import type { Output } from './output';
import { OutputCoder } from './output';
import { StorageSlotCoder } from './storage-slot';
import type { StorageSlot } from './storage-slot';
import type { Witness } from './witness';
import { WitnessCoder } from './witness';
export enum TransactionType /* u8 */ {
Script = 0,
Create = 1,
}
export type TransactionScript = {
type: TransactionType.Script;
/** Gas price for transaction (u64) */
gasPrice: string;
/** Gas limit for transaction (u64) */
gasLimit: string;
/** Price per transaction byte (u64) */
bytePrice: string;
/** Block until which tx cannot be included (u64) */
maturity: string;
/** Script length, in instructions (u16) */
scriptLength: number;
/** Length of script input data, in bytes (u16) */
scriptDataLength: number;
/** Number of inputs (u8) */
inputsCount: number;
/** Number of outputs (u8) */
outputsCount: number;
/** Number of witnesses (u8) */
witnessesCount: number;
/** Merkle root of receipts (b256) */
receiptsRoot: string;
/** Script to execute (byte[]) */
script: string;
/** Script input data (parameters) (byte[]) */
scriptData: string;
/** List of inputs (Input[]) */
inputs: Input[];
/** List of outputs (Output[]) */
outputs: Output[];
/** List of witnesses (Witness[]) */
witnesses: Witness[];
};
export class TransactionScriptCoder extends Coder<TransactionScript, TransactionScript> {
constructor() {
super('TransactionScript', 'struct TransactionScript', 0);
}
encode(value: TransactionScript): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new U64Coder().encode(value.gasPrice));
parts.push(new U64Coder().encode(value.gasLimit));
parts.push(new U64Coder().encode(value.bytePrice));
parts.push(new U64Coder().encode(value.maturity));
parts.push(new NumberCoder('u16').encode(value.scriptLength));
parts.push(new NumberCoder('u16').encode(value.scriptDataLength));
parts.push(new NumberCoder('u8').encode(value.inputsCount));
parts.push(new NumberCoder('u8').encode(value.outputsCount));
parts.push(new NumberCoder('u8').encode(value.witnessesCount));
parts.push(new B256Coder().encode(value.receiptsRoot));
parts.push(new ByteArrayCoder(value.scriptLength).encode(value.script));
parts.push(new ByteArrayCoder(value.scriptDataLength).encode(value.scriptData));
parts.push(new ArrayCoder(new InputCoder(), value.inputsCount).encode(value.inputs));
parts.push(new ArrayCoder(new OutputCoder(), value.outputsCount).encode(value.outputs));
parts.push(new ArrayCoder(new WitnessCoder(), value.witnessesCount).encode(value.witnesses));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [TransactionScript, number] {
let decoded;
let o = offset;
[decoded, o] = new U64Coder().decode(data, o);
const gasPrice = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const gasLimit = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const bytePrice = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const maturity = decoded;
[decoded, o] = new NumberCoder('u16').decode(data, o);
[decoded, o] = new NumberCoder('u16').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
const scriptLength = decoded;
const scriptDataLength = decoded;
const inputsCount = decoded;
const outputsCount = decoded;
const witnessesCount = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const receiptsRoot = decoded;
[decoded, o] = new ByteArrayCoder(scriptLength).decode(data, o);
const script = decoded;
[decoded, o] = new ByteArrayCoder(scriptDataLength).decode(data, o);
const scriptData = decoded;
[decoded, o] = new ArrayCoder(new InputCoder(), inputsCount).decode(data, o);
const inputs = decoded;
[decoded, o] = new ArrayCoder(new OutputCoder(), outputsCount).decode(data, o);
const outputs = decoded;
[decoded, o] = new ArrayCoder(new WitnessCoder(), witnessesCount).decode(data, o);
const witnesses = decoded;
return [
{
type: TransactionType.Script,
gasPrice,
gasLimit,
bytePrice,
maturity,
scriptLength,
scriptDataLength,
inputsCount,
outputsCount,
witnessesCount,
receiptsRoot,
script,
scriptData,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
inputs,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
outputs,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
witnesses,
},
o,
];
}
}
export type TransactionCreate = {
type: TransactionType.Create;
/** Gas price for transaction (u64) */
gasPrice: string;
/** Gas limit for transaction (u64) */
gasLimit: string;
/** Price per transaction byte (u64) */
bytePrice: string;
/** Block until which tx cannot be included (u64) */
maturity: string;
/** Contract bytecode length, in instructions (u32) */
bytecodeLength: number;
/** Witness index of contract bytecode to create (u8) */
bytecodeWitnessIndex: number;
/** Number of static contracts (u8) */
staticContractsCount: number;
/** Number of storage slots to initialize (u16) */
storageSlotsCount: number;
/** Number of inputs (u8) */
inputsCount: number;
/** Number of outputs (u8) */
outputsCount: number;
/** Number of witnesses (u8) */
witnessesCount: number;
/** Salt (b256) */
salt: string;
/** List of static contracts (b256[]) */
staticContracts: string[];
/** List of inputs (StorageSlot[]) */
storageSlots: StorageSlot[];
/** List of inputs (Input[]) */
inputs: Input[];
/** List of outputs (Output[]) */
outputs: Output[];
/** List of witnesses (Witness[]) */
witnesses: Witness[];
};
export class TransactionCreateCoder extends Coder<TransactionCreate, TransactionCreate> {
constructor() {
super('TransactionCreate', 'struct TransactionCreate', 0);
}
encode(value: TransactionCreate): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new U64Coder().encode(value.gasPrice));
parts.push(new U64Coder().encode(value.gasLimit));
parts.push(new U64Coder().encode(value.bytePrice));
parts.push(new U64Coder().encode(value.maturity));
parts.push(new NumberCoder('u32').encode(value.bytecodeLength));
parts.push(new NumberCoder('u8').encode(value.bytecodeWitnessIndex));
parts.push(new NumberCoder('u8').encode(value.staticContractsCount));
parts.push(new NumberCoder('u16').encode(value.storageSlotsCount));
parts.push(new NumberCoder('u8').encode(value.inputsCount));
parts.push(new NumberCoder('u8').encode(value.outputsCount));
parts.push(new NumberCoder('u8').encode(value.witnessesCount));
parts.push(new B256Coder().encode(value.salt));
parts.push(
new ArrayCoder(new B256Coder(), value.staticContractsCount).encode(value.staticContracts)
);
parts.push(
new ArrayCoder(new StorageSlotCoder(), value.storageSlotsCount).encode(value.storageSlots)
);
parts.push(new ArrayCoder(new InputCoder(), value.inputsCount).encode(value.inputs));
parts.push(new ArrayCoder(new OutputCoder(), value.outputsCount).encode(value.outputs));
parts.push(new ArrayCoder(new WitnessCoder(), value.witnessesCount).encode(value.witnesses));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [TransactionCreate, number] {
let decoded;
let o = offset;
[decoded, o] = new U64Coder().decode(data, o);
const gasPrice = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const gasLimit = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const bytePrice = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const maturity = decoded;
[decoded, o] = new NumberCoder('u16').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u16').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
[decoded, o] = new NumberCoder('u8').decode(data, o);
const bytecodeLength = decoded;
const bytecodeWitnessIndex = decoded;
const staticContractsCount = decoded;
const storageSlotsCount = decoded;
const inputsCount = decoded;
const outputsCount = decoded;
const witnessesCount = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const salt = decoded;
[decoded, o] = new ArrayCoder(new B256Coder(), staticContractsCount).decode(data, o);
const staticContracts = decoded;
[decoded, o] = new ArrayCoder(new StorageSlotCoder(), storageSlotsCount).decode(data, o);
const storageSlots = decoded;
[decoded, o] = new ArrayCoder(new InputCoder(), inputsCount).decode(data, o);
const inputs = decoded;
[decoded, o] = new ArrayCoder(new OutputCoder(), outputsCount).decode(data, o);
const outputs = decoded;
[decoded, o] = new ArrayCoder(new WitnessCoder(), witnessesCount).decode(data, o);
const witnesses = decoded;
return [
{
type: TransactionType.Create,
gasPrice,
gasLimit,
bytePrice,
maturity,
bytecodeLength,
bytecodeWitnessIndex,
staticContractsCount,
storageSlotsCount,
inputsCount,
outputsCount,
witnessesCount,
salt,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignores
staticContracts,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
storageSlots,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
inputs,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
outputs,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
witnesses,
},
o,
];
}
}
export type Transaction = TransactionScript | TransactionCreate;
export class TransactionCoder extends Coder<Transaction, Transaction> {
constructor() {
super('Transaction', 'struct Transaction', 0);
}
encode(value: Transaction): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new NumberCoder('u8').encode(value.type));
switch (value.type) {
case TransactionType.Script: {
parts.push(new TransactionScriptCoder().encode(value));
break;
}
case TransactionType.Create: {
parts.push(new TransactionCreateCoder().encode(value));
break;
}
default: {
throw new Error('Invalid Transaction type');
}
}
return concat(parts);
}
decode(data: Uint8Array, offset: number): [Transaction, number] {
let decoded;
let o = offset;
[decoded, o] = new NumberCoder('u8').decode(data, o);
const type = decoded as TransactionType;
switch (type) {
case TransactionType.Script: {
[decoded, o] = new TransactionScriptCoder().decode(data, o);
return [decoded, o];
}
case TransactionType.Create: {
[decoded, o] = new TransactionCreateCoder().decode(data, o);
return [decoded, o];
}
default: {
throw new Error('Invalid Input type');
}
}
}
}