1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const client_common_1 = require("@neo-one/client-common");
|
4 | const errors_1 = require("../errors");
|
5 | const utils_1 = require("../utils");
|
6 | const TransactionBase_1 = require("./TransactionBase");
|
7 | const TransactionType_1 = require("./TransactionType");
|
8 | const MAX_SCRIPT_SIZE = 65536;
|
9 | class InvocationTransaction extends TransactionBase_1.TransactionBase(client_common_1.InvocationTransactionModel) {
|
10 | constructor() {
|
11 | super(...arguments);
|
12 | this.sizeExclusive = utils_1.utils.lazy(() => client_common_1.IOHelper.sizeOfUInt8 + client_common_1.IOHelper.sizeOfVarBytesLE(this.script));
|
13 | }
|
14 | static deserializeWireBase(options) {
|
15 | const { reader } = options;
|
16 | const { type, version } = super.deserializeTransactionBaseStartWireBase(options);
|
17 | if (type !== TransactionType_1.TransactionType.Invocation) {
|
18 | throw new client_common_1.InvalidFormatError(`Expected transaction type to be ${TransactionType_1.TransactionType.Invocation}. Received: ${type}`);
|
19 | }
|
20 | const script = reader.readVarBytesLE(MAX_SCRIPT_SIZE);
|
21 | if (script.length === 0) {
|
22 | throw new client_common_1.InvalidFormatError('Expected invocation script length to not be 0');
|
23 | }
|
24 | let gas = utils_1.utils.ZERO;
|
25 | if (version >= 1) {
|
26 | gas = reader.readFixed8();
|
27 | }
|
28 | const { attributes, inputs, outputs, scripts } = super.deserializeTransactionBaseEndWireBase(options);
|
29 | return new this({
|
30 | version,
|
31 | attributes,
|
32 | inputs,
|
33 | outputs,
|
34 | scripts,
|
35 | script,
|
36 | gas,
|
37 | });
|
38 | }
|
39 | async serializeJSON(context) {
|
40 | const transactionBaseJSON = await super.serializeTransactionBaseJSON(context);
|
41 | const data = await context.tryGetInvocationData(this);
|
42 | let invocationDataJSON;
|
43 | if (data !== undefined) {
|
44 | const { asset, contracts, deletedContractHashes, migratedContractHashes, voteUpdates, actions, result, storageChanges, } = data;
|
45 | invocationDataJSON = {
|
46 | result: result.serializeJSON(context),
|
47 | asset: asset === undefined ? undefined : asset.serializeJSON(context),
|
48 | contracts: contracts.map((contract) => contract.serializeJSON(context)),
|
49 | deletedContractHashes: deletedContractHashes.map((hash) => client_common_1.common.uInt160ToString(hash)),
|
50 | migratedContractHashes: migratedContractHashes.map(([from, to]) => [client_common_1.common.uInt160ToString(from), client_common_1.common.uInt160ToString(to)]),
|
51 | voteUpdates: voteUpdates.map(([address, votes]) => [
|
52 | client_common_1.crypto.scriptHashToAddress({
|
53 | addressVersion: context.addressVersion,
|
54 | scriptHash: address,
|
55 | }),
|
56 | votes.map((vote) => client_common_1.common.ecPointToString(vote)),
|
57 | ]),
|
58 | actions: actions.map((action) => action.serializeJSON(context)),
|
59 | storageChanges: storageChanges.map((storageChange) => storageChange.serializeJSON(context)),
|
60 | };
|
61 | }
|
62 | return Object.assign(Object.assign({}, transactionBaseJSON), { type: 'InvocationTransaction', script: client_common_1.JSONHelper.writeBuffer(this.script), gas: client_common_1.JSONHelper.writeFixed8(this.gas), invocationData: invocationDataJSON });
|
63 | }
|
64 | getSystemFee(_context) {
|
65 | return this.gas;
|
66 | }
|
67 | async verify(options) {
|
68 | if (!this.gas.mod(utils_1.utils.ONE_HUNDRED_MILLION).eq(utils_1.utils.ZERO)) {
|
69 | throw new errors_1.VerifyError('Invalid GAS amount');
|
70 | }
|
71 | return super.verify(options);
|
72 | }
|
73 | }
|
74 | exports.InvocationTransaction = InvocationTransaction;
|
75 |
|
76 |
|