@taquito/tzip16 24.2.0 → 24.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.MichelsonStorageView = void 0;
13
4
  const michelson_encoder_1 = require("@taquito/michelson-encoder");
@@ -32,7 +23,7 @@ class MichelsonStorageView {
32
23
  };
33
24
  }
34
25
  /**
35
- * @description According to the tzip-16 standard, the following instructions must not be used in the code of the view:
26
+ * According to the tzip-16 standard, the following instructions must not be used in the code of the view:
36
27
  * 'AMOUNT', 'CREATE_CONTRACT', 'SENDER', 'SET_DELEGATE', 'SOURCE', and 'TRANSFER_TOKENS'
37
28
  * The method throw an error if an illegal instruction is found
38
29
  */
@@ -57,7 +48,7 @@ class MichelsonStorageView {
57
48
  }
58
49
  }
59
50
  /**
60
- * @description According to the tzip-16 standard, in the first version of the specification, the instruction SELF should only be used before ADDRESS
51
+ * According to the tzip-16 standard, in the first version of the specification, the instruction SELF should only be used before ADDRESS
61
52
  * The method throws an error is the instruction SELF is present, but not followed by ADDRESS
62
53
  */
63
54
  illegalUseOfSelfInstruction(code) {
@@ -75,7 +66,7 @@ class MichelsonStorageView {
75
66
  }
76
67
  }
77
68
  /**
78
- * @description Loops through the view's code and replace SELF, BALANCE, NOW, and CHAIN_ID with Michelson expressions that match the current context, if applicable.
69
+ * Loops through the view's code and replace SELF, BALANCE, NOW, and CHAIN_ID with Michelson expressions that match the current context, if applicable.
79
70
  */
80
71
  adaptViewCodeToContext(code, contractBalance, blockTimeStamp, chainId) {
81
72
  const instructionsToReplace = {
@@ -119,59 +110,57 @@ class MichelsonStorageView {
119
110
  const arg = parameterViewSchema.Encode(...args);
120
111
  return { arg, viewParameterType };
121
112
  }
122
- executeView(...args) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- // validate view code against tzip-16 specifications
125
- this.findForbiddenInstructionInViewCodeError(this.code);
126
- this.illegalUseOfSelfInstruction(this.code);
127
- const { arg, viewParameterType } = this.formatArgsAndParameter(args);
128
- const storageType = this.contract.script.code.find((x) => x.prim === 'storage');
129
- const storageArgs = storageType.args[0];
130
- // currentContext
131
- const storageValue = yield this.readProvider.getStorage(this.contract.address, 'head');
132
- const chainId = yield this.readProvider.getChainId();
133
- const contractBalance = (yield this.readProvider.getBalance(this.contract.address, 'head')).toString();
134
- const blockTimestamp = yield this.readProvider.getBlockTimestamp('head');
135
- const code = this.adaptViewCodeToContext(this.code, contractBalance, blockTimestamp, chainId);
136
- if (!this.viewParameterType) {
137
- code.unshift({ prim: 'CDR' });
138
- }
139
- const viewScript = {
140
- script: [
141
- { prim: 'parameter', args: [{ prim: 'pair', args: [viewParameterType, storageArgs] }] },
142
- { prim: 'storage', args: [{ prim: 'option', args: [this.returnType] }] },
143
- {
144
- prim: 'code',
145
- args: [
146
- [
147
- { prim: 'CAR' },
148
- code,
149
- { prim: 'SOME' },
150
- { prim: 'NIL', args: [{ prim: 'operation' }] },
151
- { prim: 'PAIR' },
152
- ],
113
+ async executeView(...args) {
114
+ // validate view code against tzip-16 specifications
115
+ this.findForbiddenInstructionInViewCodeError(this.code);
116
+ this.illegalUseOfSelfInstruction(this.code);
117
+ const { arg, viewParameterType } = this.formatArgsAndParameter(args);
118
+ const storageType = this.contract.script.code.find((x) => x.prim === 'storage');
119
+ const storageArgs = storageType.args[0];
120
+ // currentContext
121
+ const storageValue = await this.readProvider.getStorage(this.contract.address, 'head');
122
+ const chainId = await this.readProvider.getChainId();
123
+ const contractBalance = (await this.readProvider.getBalance(this.contract.address, 'head')).toString();
124
+ const blockTimestamp = await this.readProvider.getBlockTimestamp('head');
125
+ const code = this.adaptViewCodeToContext(this.code, contractBalance, blockTimestamp, chainId);
126
+ if (!this.viewParameterType) {
127
+ code.unshift({ prim: 'CDR' });
128
+ }
129
+ const viewScript = {
130
+ script: [
131
+ { prim: 'parameter', args: [{ prim: 'pair', args: [viewParameterType, storageArgs] }] },
132
+ { prim: 'storage', args: [{ prim: 'option', args: [this.returnType] }] },
133
+ {
134
+ prim: 'code',
135
+ args: [
136
+ [
137
+ { prim: 'CAR' },
138
+ code,
139
+ { prim: 'SOME' },
140
+ { prim: 'NIL', args: [{ prim: 'operation' }] },
141
+ { prim: 'PAIR' },
153
142
  ],
154
- },
155
- ],
156
- storage: { prim: 'None' },
157
- input: { prim: 'Pair', args: [arg, storageValue] },
158
- amount: '0',
159
- chain_id: chainId,
160
- balance: '0',
161
- };
162
- let result;
163
- try {
164
- result = yield this.rpc.runCode(viewScript);
165
- }
166
- catch (error) {
167
- const failWith = (0, taquito_2.validateAndExtractFailwith)(error);
168
- throw failWith
169
- ? new taquito_1.ViewSimulationError(`The simulation of the Michelson view failed with: ${JSON.stringify(failWith)}`, this.viewName, failWith, error)
170
- : error;
171
- }
172
- const viewResultSchema = new michelson_encoder_1.ParameterSchema(this.returnType);
173
- return viewResultSchema.Execute(result.storage.args[0]);
174
- });
143
+ ],
144
+ },
145
+ ],
146
+ storage: { prim: 'None' },
147
+ input: { prim: 'Pair', args: [arg, storageValue] },
148
+ amount: '0',
149
+ chain_id: chainId,
150
+ balance: '0',
151
+ };
152
+ let result;
153
+ try {
154
+ result = await this.rpc.runCode(viewScript);
155
+ }
156
+ catch (error) {
157
+ const failWith = (0, taquito_2.validateAndExtractFailwith)(error);
158
+ throw failWith
159
+ ? new taquito_1.ViewSimulationError(`The simulation of the Michelson view failed with: ${JSON.stringify(failWith)}`, this.viewName, failWith, error)
160
+ : error;
161
+ }
162
+ const viewResultSchema = new michelson_encoder_1.ParameterSchema(this.returnType);
163
+ return viewResultSchema.Execute(result.storage.args[0]);
175
164
  }
176
165
  }
177
166
  exports.MichelsonStorageView = MichelsonStorageView;