@aeternity/aepp-sdk 8.2.0 → 10.0.0-beta.1

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.
Files changed (145) hide show
  1. package/README.md +16 -9
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/account/base.js +33 -119
  10. package/es/account/base.js.map +1 -1
  11. package/es/account/memory.js +17 -19
  12. package/es/account/memory.js.map +1 -1
  13. package/es/account/multiple.js +75 -154
  14. package/es/account/multiple.js.map +1 -1
  15. package/es/ae/aens.js +164 -599
  16. package/es/ae/aens.js.map +1 -1
  17. package/es/ae/contract.js +108 -916
  18. package/es/ae/contract.js.map +1 -1
  19. package/es/ae/index.js +89 -250
  20. package/es/ae/index.js.map +1 -1
  21. package/es/ae/oracle.js +132 -500
  22. package/es/ae/oracle.js.map +1 -1
  23. package/es/chain/index.js +4 -23
  24. package/es/chain/index.js.map +1 -1
  25. package/es/chain/node.js +292 -1018
  26. package/es/chain/node.js.map +1 -1
  27. package/es/channel/handlers.js +597 -1086
  28. package/es/channel/handlers.js.map +1 -1
  29. package/es/channel/index.js +203 -451
  30. package/es/channel/index.js.map +1 -1
  31. package/es/channel/internal.js +129 -301
  32. package/es/channel/internal.js.map +1 -1
  33. package/es/contract/aci/index.js +319 -491
  34. package/es/contract/aci/index.js.map +1 -1
  35. package/es/contract/aci/transformation.js +35 -711
  36. package/es/contract/aci/transformation.js.map +1 -1
  37. package/es/contract/compiler.js +129 -203
  38. package/es/contract/compiler.js.map +1 -1
  39. package/es/contract/ga/helpers.js +17 -122
  40. package/es/contract/ga/helpers.js.map +1 -1
  41. package/es/contract/ga/index.js +102 -247
  42. package/es/contract/ga/index.js.map +1 -1
  43. package/es/contract/index.js +1 -1
  44. package/es/contract/index.js.map +1 -1
  45. package/es/index.js +2 -3
  46. package/es/index.js.map +1 -1
  47. package/es/node-pool/index.js +54 -73
  48. package/es/node-pool/index.js.map +1 -1
  49. package/es/node.js +56 -157
  50. package/es/node.js.map +1 -1
  51. package/es/other.d.ts +1 -1
  52. package/es/tx/builder/helpers.js +49 -89
  53. package/es/tx/builder/helpers.js.map +1 -1
  54. package/es/tx/builder/index.js +205 -303
  55. package/es/tx/builder/index.js.map +1 -1
  56. package/es/tx/builder/schema.js +585 -573
  57. package/es/tx/builder/schema.js.map +1 -1
  58. package/es/tx/index.js +1 -1
  59. package/es/tx/index.js.map +1 -1
  60. package/es/tx/tx-object.js +105 -135
  61. package/es/tx/tx-object.js.map +1 -1
  62. package/es/tx/tx.js +810 -1586
  63. package/es/tx/tx.js.map +1 -1
  64. package/es/tx/validator.js +191 -426
  65. package/es/tx/validator.js.map +1 -1
  66. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +17 -21
  67. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
  68. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +38 -74
  69. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
  70. package/es/utils/aepp-wallet-communication/content-script-bridge.js +14 -15
  71. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
  72. package/es/utils/aepp-wallet-communication/helpers.js +60 -105
  73. package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
  74. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +179 -510
  75. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
  76. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +70 -89
  77. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
  78. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +354 -603
  79. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
  80. package/es/utils/aepp-wallet-communication/schema.js +43 -39
  81. package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
  82. package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
  83. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
  84. package/es/utils/amount-formatter.js +41 -61
  85. package/es/utils/amount-formatter.js.map +1 -1
  86. package/es/utils/async-init.js +28 -64
  87. package/es/utils/async-init.js.map +1 -1
  88. package/es/utils/bignumber.js +3 -11
  89. package/es/utils/bignumber.js.map +1 -1
  90. package/es/utils/bytes.js +5 -9
  91. package/es/utils/bytes.js.map +1 -1
  92. package/es/utils/crypto.js +49 -75
  93. package/es/utils/crypto.js.map +1 -1
  94. package/es/utils/hd-wallet.js +45 -81
  95. package/es/utils/hd-wallet.js.map +1 -1
  96. package/es/utils/json-big.js +16 -49
  97. package/es/utils/json-big.js.map +1 -1
  98. package/es/utils/keystore.js +81 -246
  99. package/es/utils/keystore.js.map +1 -1
  100. package/es/utils/mptree.js +88 -134
  101. package/es/utils/mptree.js.map +1 -1
  102. package/es/utils/other.js +10 -60
  103. package/es/utils/other.js.map +1 -1
  104. package/es/utils/semver-satisfies.js +9 -28
  105. package/es/utils/semver-satisfies.js.map +1 -1
  106. package/es/utils/string.js +4 -13
  107. package/es/utils/string.js.map +1 -1
  108. package/es/utils/swagger.js +85 -218
  109. package/es/utils/swagger.js.map +1 -1
  110. package/package.json +52 -43
  111. package/src/account/base.js +8 -9
  112. package/src/account/multiple.js +20 -16
  113. package/src/ae/aens.js +25 -21
  114. package/src/ae/contract.js +65 -246
  115. package/src/ae/index.js +31 -7
  116. package/src/ae/oracle.js +21 -17
  117. package/src/chain/index.js +2 -12
  118. package/src/chain/node.js +26 -29
  119. package/src/channel/handlers.js +0 -1
  120. package/src/channel/index.js +4 -5
  121. package/src/contract/aci/index.js +221 -106
  122. package/src/contract/aci/transformation.js +30 -422
  123. package/src/contract/compiler.js +45 -4
  124. package/src/contract/ga/helpers.js +2 -12
  125. package/src/contract/ga/index.js +22 -26
  126. package/src/index.js +2 -4
  127. package/src/node-pool/index.js +21 -12
  128. package/src/node.js +22 -26
  129. package/src/tx/builder/helpers.js +12 -24
  130. package/src/tx/builder/index.js +18 -15
  131. package/src/tx/builder/schema.js +43 -108
  132. package/src/tx/tx-object.js +0 -2
  133. package/src/tx/tx.js +157 -144
  134. package/src/tx/validator.js +134 -202
  135. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +1 -3
  136. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +9 -10
  137. package/src/utils/aepp-wallet-communication/schema.js +1 -3
  138. package/src/utils/amount-formatter.ts +1 -1
  139. package/src/utils/bignumber.ts +2 -1
  140. package/src/utils/crypto.js +4 -17
  141. package/src/utils/other.ts +1 -1
  142. package/src/utils/semver-satisfies.ts +1 -1
  143. package/src/utils/swagger.js +24 -7
  144. package/CHANGELOG.md +0 -1716
  145. package/src/contract/aci/helpers.js +0 -140
@@ -1,41 +1,46 @@
1
- import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
2
- import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
3
- import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
4
- import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
5
- import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
6
- import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
7
- import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
8
- import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
9
- import _merge from "ramda/src/merge";
10
1
  import _path from "ramda/src/path";
11
2
  import _prop from "ramda/src/prop";
12
3
  import _defaultTo from "ramda/src/defaultTo";
13
- import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
14
- import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
15
- import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
16
- import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
17
- import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
18
-
19
- function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
20
-
21
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context7; _forEachInstanceProperty(_context7 = ownKeys(Object(source), true)).call(_context7, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context8; _forEachInstanceProperty(_context8 = ownKeys(Object(source))).call(_context8, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
22
-
4
+ import _pick from "ramda/src/pick";
23
5
  import BigNumber from 'bignumber.js';
24
- import { buildContractMethods, decodeCallResult, decodeEvents, getFunctionACI, prepareArgsForEncode as prepareArgs } from './helpers';
25
- import { isAddressValid } from '../../utils/crypto';
6
+ import { Encoder as Calldata } from '@aeternity/aepp-calldata';
7
+ import { decodeEvents } from './transformation';
8
+ import { DRY_RUN_ACCOUNT, DEPOSIT } from '../../tx/builder/schema';
9
+ import TxObject from '../../tx/tx-object';
10
+ import { decode } from '../../tx/builder/helpers';
11
+ /**
12
+ * Get function schema from contract ACI object
13
+ * @param {Object} aci Contract ACI object
14
+ * @param {String} name Function name
15
+ * @param external
16
+ * @return {Object} function ACI
17
+ */
18
+
19
+ function getFunctionACI(aci, name, external) {
20
+ const fn = aci.functions.find(f => f.name === name);
21
+ if (!fn && name !== 'init') throw new Error(`Function ${name} doesn't exist in contract`);
22
+ return { ...fn,
23
+ bindings: [{
24
+ state: aci.state,
25
+ type_defs: aci.type_defs,
26
+ name: aci.name
27
+ }, ...external.map(_pick(['state', 'type_defs', 'name']))],
28
+ event: aci.event ? aci.event.variant : []
29
+ };
30
+ }
26
31
  /**
27
32
  * Generate contract ACI object with predefined js methods for contract usage - can be used for creating a reference to already deployed contracts
28
33
  * @alias module:@aeternity/aepp-sdk/es/contract/aci
29
- * @param {String} source Contract source code
30
34
  * @param {Object} [options={}] Options object
31
- * @param {String} [options.aci] Contract ACI
35
+ * @param {String} [options.source] Contract source code
36
+ * @param {String} [options.bytecode] Contract bytecode
37
+ * @param {Object} [options.aci] Contract ACI
32
38
  * @param {String} [options.contractAddress] Contract address
33
39
  * @param {Object} [options.filesystem] Contact source external namespaces map
34
- * @param {Object} [options.forceCodeCheck=true] Don't check contract code
35
- * @param {Object} [options.opt] Contract options
40
+ * @param {Boolean} [options.validateBytecode] Compare source code with on-chain version
36
41
  * @return {ContractInstance} JS Contract API
37
42
  * @example
38
- * const contractIns = await client.getContractInstance(sourceCode)
43
+ * const contractIns = await client.getContractInstance({ source })
39
44
  * await contractIns.deploy([321]) or await contractIns.methods.init(321)
40
45
  * const callResult = await contractIns.call('setState', [123]) or await contractIns.methods.setState.send(123, options)
41
46
  * const staticCallResult = await contractIns.call('setState', [123], { callStatic: true }) or await contractIns.methods.setState.get(123, options)
@@ -43,486 +48,309 @@ import { isAddressValid } from '../../utils/crypto';
43
48
  * Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is stateful or not
44
49
  */
45
50
 
46
- export default function getContractInstance(_x) {
47
- return _getContractInstance.apply(this, arguments);
48
- }
49
-
50
- function _getContractInstance() {
51
- _getContractInstance = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(source) {
52
- var _context5;
53
-
54
- var _ref,
55
- aci,
56
- contractAddress,
57
- _ref$filesystem,
58
- filesystem,
59
- _ref$forceCodeCheck,
60
- forceCodeCheck,
61
- opt,
62
- defaultOptions,
63
- instance,
64
- contract,
65
- onChanByteCode,
66
- isCorrespondingBytecode,
67
- _args4 = arguments;
68
-
69
- return _regeneratorRuntime.wrap(function _callee4$(_context6) {
70
- while (1) {
71
- switch (_context6.prev = _context6.next) {
72
- case 0:
73
- _ref = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {}, aci = _ref.aci, contractAddress = _ref.contractAddress, _ref$filesystem = _ref.filesystem, filesystem = _ref$filesystem === void 0 ? {} : _ref$filesystem, _ref$forceCodeCheck = _ref.forceCodeCheck, forceCodeCheck = _ref$forceCodeCheck === void 0 ? true : _ref$forceCodeCheck, opt = _ref.opt;
74
- _context6.t0 = aci;
75
-
76
- if (_context6.t0) {
77
- _context6.next = 6;
78
- break;
79
- }
80
-
81
- _context6.next = 5;
82
- return this.contractGetACI(source, {
83
- filesystem: filesystem
84
- });
85
-
86
- case 5:
87
- _context6.t0 = _context6.sent;
88
-
89
- case 6:
90
- aci = _context6.t0;
91
-
92
- if (!contractAddress) {
93
- _context6.next = 11;
94
- break;
95
- }
96
-
97
- _context6.next = 10;
98
- return this.resolveName(contractAddress, 'ct', {
99
- resolveByNode: true
100
- });
101
-
102
- case 10:
103
- contractAddress = _context6.sent;
104
-
105
- case 11:
106
- defaultOptions = _objectSpread(_objectSpread({}, this.Ae.defaults), {}, {
107
- skipArgsConvert: false,
108
- skipTransformDecoded: false,
109
- callStatic: false,
110
- waitMined: true,
111
- verify: false,
112
- filesystem: filesystem
113
- });
114
- instance = {
115
- "interface": _defaultTo(null, _prop('interface', aci)),
116
- aci: _defaultTo(null, _path(['encoded_aci', 'contract'], aci)),
117
- externalAci: aci.external_encoded_aci ? _mapInstanceProperty(_context5 = aci.external_encoded_aci).call(_context5, function (a) {
118
- return a.contract || a.namespace;
119
- }) : [],
120
- source: source,
121
- compiled: null,
122
- deployInfo: {
123
- address: contractAddress
124
- },
125
- options: _merge(defaultOptions, opt),
126
- compilerVersion: this.compilerVersion,
127
- setOptions: function setOptions(opt) {
128
- this.options = _merge(this.options, opt);
129
- }
130
- }; // Check for valid contract address and contract code
131
-
132
- if (!contractAddress) {
133
- _context6.next = 30;
134
- break;
135
- }
136
-
137
- if (isAddressValid(contractAddress, 'ct')) {
138
- _context6.next = 16;
139
- break;
140
- }
141
-
142
- throw new Error('Invalid contract address');
143
51
 
144
- case 16:
145
- _context6.next = 18;
146
- return this.getContract(contractAddress)["catch"](function (e) {
147
- return null;
148
- });
149
-
150
- case 18:
151
- contract = _context6.sent;
152
-
153
- if (!(!contract || !contract.active)) {
154
- _context6.next = 21;
155
- break;
156
- }
157
-
158
- throw new Error("Contract with address ".concat(contractAddress, " not found on-chain or not active"));
159
-
160
- case 21:
161
- if (forceCodeCheck) {
162
- _context6.next = 30;
163
- break;
164
- }
165
-
166
- _context6.next = 24;
167
- return this.getContractByteCode(contractAddress);
168
-
169
- case 24:
170
- onChanByteCode = _context6.sent.bytecode;
171
- _context6.next = 27;
172
- return this.validateByteCodeAPI(onChanByteCode, instance.source, instance.options)["catch"](function (e) {
173
- return false;
174
- });
175
-
176
- case 27:
177
- isCorrespondingBytecode = _context6.sent;
178
-
179
- if (isCorrespondingBytecode) {
180
- _context6.next = 30;
181
- break;
182
- }
183
-
184
- throw new Error('Contract source do not correspond to the contract bytecode deployed on the chain');
185
-
186
- case 30:
187
- /**
188
- * Compile contract
189
- * @alias module:@aeternity/aepp-sdk/es/contract/aci
190
- * @rtype () => ContractInstance: Object
191
- * @return {ContractInstance} Contract ACI object with predefined js methods for contract usage
192
- */
193
- instance.compile = compile({
194
- client: this,
195
- instance: instance
196
- });
197
- /**
198
- * Deploy contract
199
- * @alias module:@aeternity/aepp-sdk/es/contract/aci
200
- * @rtype (init: Array, options: Object = { skipArgsConvert: false }) => ContractInstance: Object
201
- * @param {Array} init Contract init function arguments array
202
- * @param {Object} [options={}] options Options object
203
- * @param {Boolean} [options.skipArgsConvert=false] Skip Validation and Transforming arguments before prepare call-data
204
- * @return {ContractInstance} Contract ACI object with predefined js methods for contract usage
205
- */
206
-
207
- instance.deploy = deploy({
208
- client: this,
209
- instance: instance
210
- });
211
- /**
212
- * Call contract function
213
- * @alias module:@aeternity/aepp-sdk/es/contract/aci
214
- * @rtype (init: Array, options: Object = { skipArgsConvert: false, skipTransformDecoded: false, callStatic: false }) => CallResult: Object
215
- * @param {String} fn Function name
216
- * @param {Array} params Array of function arguments
217
- * @param {Object} [options={}] Array of function arguments
218
- * @param {Boolean} [options.skipArgsConvert=false] Skip Validation and Transforming arguments before prepare call-data
219
- * @param {Boolean} [options.skipTransformDecoded=false] Skip Transform decoded data to JS type
220
- * @param {Boolean} [options.callStatic=false] Static function call
221
- * @return {Object} CallResult
222
- */
223
-
224
- instance.call = call({
225
- client: this,
226
- instance: instance
227
- });
228
- /**
229
- * Decode Events
230
- * @alias module:@aeternity/aepp-sdk/es/contract/aci
231
- * @rtype (fn: String, events: Array) => DecodedEvents: Array
232
- * @param {String} fn Function name
233
- * @param {Array} events Array of encoded events(callRes.result.log)
234
- * @return {Object} DecodedEvents
235
- */
236
-
237
- instance.decodeEvents = eventDecode({
238
- instance: instance
239
- });
240
- /**
241
- * Generate proto function based on contract function using Contract ACI schema
242
- * All function can be called like:
243
- * 'await contract.methods.testFunction()' -> then sdk will decide to use dry-run or send tx on-chain base on if function stateful or not.
244
- * Also you can manually do that:
245
- * `await contract.methods.testFunction.get()` -> use call-static(dry-run)
246
- * `await contract.methods.testFunction.send()` -> send tx on-chain
247
- */
248
-
249
- instance.methods = buildContractMethods(instance)();
250
- return _context6.abrupt("return", instance);
251
-
252
- case 36:
253
- case "end":
254
- return _context6.stop();
255
- }
256
- }
257
- }, _callee4, this);
52
+ export default async function getContractInstance() {
53
+ var _this = this;
54
+
55
+ let {
56
+ source,
57
+ bytecode,
58
+ aci,
59
+ contractAddress,
60
+ filesystem = {},
61
+ validateBytecode,
62
+ ...otherOptions
63
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
64
+ aci = aci || source && (await this.contractGetACI(source, {
65
+ filesystem
258
66
  }));
259
- return _getContractInstance.apply(this, arguments);
260
- }
261
-
262
- var eventDecode = function eventDecode(_ref2) {
263
- var instance = _ref2.instance;
264
- return function (fn, events) {
265
- return decodeEvents(events, getFunctionACI(instance.aci, fn, {
266
- external: instance.externalAci
267
- }));
67
+ if (!aci) throw new Error('Either ACI or source code is required');
68
+ contractAddress = contractAddress && (await this.resolveName(contractAddress, 'ct', {
69
+ resolveByNode: true
70
+ }));
71
+ if (!contractAddress && !source && !bytecode) throw new Error('Can\'t create instance by ACI without address');
72
+
73
+ if (contractAddress) {
74
+ const contract = await this.getContract(contractAddress).catch(() => null);
75
+ if (!contract) throw new Error(`Contract with address ${contractAddress} not found on-chain`);
76
+ if (!contract.active) throw new Error(`Contract with address ${contractAddress} not active`);
77
+ }
78
+
79
+ const instance = {
80
+ interface: _defaultTo(null, _prop('interface', aci)),
81
+ aci: _defaultTo(null, _path(['encoded_aci', 'contract'], aci)),
82
+ calldata: new Calldata([aci.encoded_aci, ...aci.external_encoded_aci]),
83
+ externalAci: aci.external_encoded_aci ? aci.external_encoded_aci.map(a => a.contract || a.namespace) : [],
84
+ source,
85
+ bytecode,
86
+ deployInfo: {
87
+ address: contractAddress
88
+ },
89
+ options: { ...this.Ae.defaults,
90
+ callStatic: false,
91
+ filesystem,
92
+ ...otherOptions
93
+ },
94
+ compilerVersion: this.compilerVersion
268
95
  };
269
- };
270
-
271
- var call = function call(_ref3) {
272
- var client = _ref3.client,
273
- instance = _ref3.instance;
274
- return /*#__PURE__*/function () {
275
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(fn) {
276
- var _context;
277
-
278
- var params,
279
- options,
280
- opt,
281
- fnACI,
282
- source,
283
- result,
284
- _args = arguments;
285
- return _regeneratorRuntime.wrap(function _callee$(_context2) {
286
- while (1) {
287
- switch (_context2.prev = _context2.next) {
288
- case 0:
289
- params = _args.length > 1 && _args[1] !== undefined ? _args[1] : [];
290
- options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
291
- opt = _objectSpread(_objectSpread({}, instance.options), options);
292
- fnACI = getFunctionACI(instance.aci, fn, {
293
- external: instance.externalAci
294
- });
295
- source = opt.source || instance.source;
296
-
297
- if (fn) {
298
- _context2.next = 7;
299
- break;
300
- }
301
96
 
302
- throw new Error('Function name is required');
303
-
304
- case 7:
305
- if (instance.deployInfo.address) {
306
- _context2.next = 9;
307
- break;
308
- }
309
-
310
- throw new Error('You need to deploy contract before calling!');
311
-
312
- case 9:
313
- if (!(BigNumber(opt.amount).gt(0) && Object.prototype.hasOwnProperty.call(fnACI, 'payable') && !fnACI.payable)) {
314
- _context2.next = 11;
315
- break;
316
- }
317
-
318
- throw new Error(_concatInstanceProperty(_context = "You try to pay \"".concat(opt.amount, "\" to function \"")).call(_context, fn, "\" which is not payable. Only payable function can accept tokens"));
319
-
320
- case 11:
321
- if (opt.skipArgsConvert) {
322
- _context2.next = 17;
323
- break;
324
- }
325
-
326
- _context2.next = 14;
327
- return prepareArgs(fnACI, params);
328
-
329
- case 14:
330
- _context2.t0 = _context2.sent;
331
- _context2.next = 18;
332
- break;
333
-
334
- case 17:
335
- _context2.t0 = params;
336
-
337
- case 18:
338
- params = _context2.t0;
339
-
340
- if (!opt.callStatic) {
341
- _context2.next = 25;
342
- break;
343
- }
344
-
345
- _context2.next = 22;
346
- return client.contractCallStatic(source, instance.deployInfo.address, fn, params, opt);
347
-
348
- case 22:
349
- _context2.t1 = _context2.sent;
350
- _context2.next = 28;
351
- break;
352
-
353
- case 25:
354
- _context2.next = 27;
355
- return client.contractCall(source, instance.deployInfo.address, fn, params, opt);
356
-
357
- case 27:
358
- _context2.t1 = _context2.sent;
359
-
360
- case 28:
361
- result = _context2.t1;
362
- _context2.t2 = _objectSpread;
363
- _context2.t3 = _objectSpread({}, result);
364
-
365
- if (!opt.waitMined) {
366
- _context2.next = 37;
367
- break;
368
- }
369
-
370
- _context2.next = 34;
371
- return decodeCallResult(result, fnACI, opt);
372
-
373
- case 34:
374
- _context2.t4 = _context2.sent;
375
- _context2.next = 38;
376
- break;
377
-
378
- case 37:
379
- _context2.t4 = {};
380
-
381
- case 38:
382
- _context2.t5 = _context2.t4;
383
- return _context2.abrupt("return", (0, _context2.t2)(_context2.t3, _context2.t5));
384
-
385
- case 40:
386
- case "end":
387
- return _context2.stop();
388
- }
389
- }
390
- }, _callee);
391
- }));
97
+ if (validateBytecode) {
98
+ if (!contractAddress) throw new Error('Can\'t validate bytecode without contract address');
99
+ const onChanBytecode = (await this.getContractByteCode(contractAddress)).bytecode;
100
+ const isValid = source && (await this.validateByteCodeAPI(onChanBytecode, source, instance.options).catch(() => false)) || bytecode === onChanBytecode;
101
+ if (!isValid) throw new Error(`Contract ${source ? 'source' : 'bytecode'} do not correspond to the bytecode deployed on the chain`);
102
+ }
103
+ /**
104
+ * Compile contract
105
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
106
+ * @rtype () => ContractInstance: Object
107
+ * @return {String} bytecode bytecode
108
+ */
109
+
110
+
111
+ instance.compile = async function () {
112
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
113
+ if (instance.bytecode) throw new Error('Contract already compiled');
114
+ if (!instance.source) throw new Error('Can\'t compile without source code');
115
+ const {
116
+ bytecode
117
+ } = await _this.contractCompile(instance.source, { ...instance.options,
118
+ ...options
119
+ });
120
+ instance.bytecode = bytecode;
121
+ return instance.bytecode;
122
+ };
392
123
 
393
- return function (_x2) {
394
- return _ref4.apply(this, arguments);
124
+ const sendAndProcess = async (tx, options) => {
125
+ const txData = await this.send(tx, options);
126
+ const result = {
127
+ hash: txData.hash,
128
+ tx: TxObject({
129
+ tx: txData.rawTx
130
+ }),
131
+ txData,
132
+ rawTx: txData.rawTx
395
133
  };
396
- }();
397
- };
398
-
399
- var deploy = function deploy(_ref5) {
400
- var client = _ref5.client,
401
- instance = _ref5.instance;
402
- return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
403
- var init,
404
- options,
405
- opt,
406
- fnACI,
407
- source,
408
- _yield$client$contrac,
409
- owner,
410
- transaction,
411
- address,
412
- createdAt,
413
- result,
414
- rawTx,
415
- _args2 = arguments;
416
-
417
- return _regeneratorRuntime.wrap(function _callee2$(_context3) {
418
- while (1) {
419
- switch (_context3.prev = _context3.next) {
420
- case 0:
421
- init = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : [];
422
- options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
423
- opt = _objectSpread(_objectSpread({}, instance.options), options);
424
- fnACI = getFunctionACI(instance.aci, 'init', {
425
- external: instance.externalAci
426
- });
427
- source = opt.source || instance.source;
428
-
429
- if (instance.compiled) {
430
- _context3.next = 8;
431
- break;
432
- }
433
-
434
- _context3.next = 8;
435
- return instance.compile(opt);
436
-
437
- case 8:
438
- if (opt.skipArgsConvert) {
439
- _context3.next = 14;
440
- break;
441
- }
442
-
443
- _context3.next = 11;
444
- return prepareArgs(fnACI, init);
445
-
446
- case 11:
447
- _context3.t0 = _context3.sent;
448
- _context3.next = 15;
449
- break;
450
-
451
- case 14:
452
- _context3.t0 = init;
134
+ if (options.waitMined === false) return result;
135
+ const txInfo = await this.getTxInfo(txData.hash);
136
+ await handleCallError(txInfo);
137
+ return { ...result,
138
+ result: txInfo
139
+ };
140
+ };
453
141
 
454
- case 15:
455
- init = _context3.t0;
142
+ const handleCallError = _ref => {
143
+ let {
144
+ returnType,
145
+ returnValue
146
+ } = _ref;
147
+ let message; // TODO: ensure that it works correctly https://github.com/aeternity/aepp-calldata-js/issues/88
456
148
 
457
- if (!opt.callStatic) {
458
- _context3.next = 20;
459
- break;
460
- }
149
+ switch (returnType) {
150
+ case 'ok':
151
+ return;
461
152
 
462
- return _context3.abrupt("return", client.contractCallStatic(source, null, 'init', init, _objectSpread(_objectSpread({}, opt), {}, {
463
- bytecode: instance.compiled
464
- })));
153
+ case 'revert':
154
+ message = instance.calldata.decodeFateString(returnValue);
155
+ break;
465
156
 
466
- case 20:
467
- _context3.next = 22;
468
- return client.contractDeploy(instance.compiled, opt.source || instance.source, init, opt);
157
+ case 'error':
158
+ message = decode(returnValue).toString();
159
+ break;
469
160
 
470
- case 22:
471
- _yield$client$contrac = _context3.sent;
472
- owner = _yield$client$contrac.owner;
473
- transaction = _yield$client$contrac.transaction;
474
- address = _yield$client$contrac.address;
475
- createdAt = _yield$client$contrac.createdAt;
476
- result = _yield$client$contrac.result;
477
- rawTx = _yield$client$contrac.rawTx;
478
- instance.deployInfo = {
479
- owner: owner,
480
- transaction: transaction,
481
- address: address,
482
- createdAt: createdAt,
483
- result: result,
484
- rawTx: rawTx
485
- };
486
- return _context3.abrupt("return", instance.deployInfo);
161
+ default:
162
+ throw new Error(`Unknown returnType: ${returnType}`);
163
+ }
487
164
 
488
- case 31:
489
- case "end":
490
- return _context3.stop();
491
- }
165
+ throw new Error(`Invocation failed${message ? `: "${message}"` : ''}`);
166
+ };
167
+ /**
168
+ * Deploy contract
169
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
170
+ * @rtype (init: Array, options: Object) => ContractInstance: Object
171
+ * @param {Array} params Contract init function arguments array
172
+ * @param {Object} [options] options
173
+ * @return {Object} deploy info
174
+ */
175
+
176
+
177
+ instance.deploy = async function () {
178
+ let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
179
+ let options = arguments.length > 1 ? arguments[1] : undefined;
180
+ const opt = { ...instance.options,
181
+ ...options,
182
+ deposit: DEPOSIT
183
+ };
184
+ if (!instance.bytecode) await instance.compile(opt);
185
+ if (opt.callStatic) return instance.call('init', params, opt);
186
+ if (instance.deployInfo.address) throw new Error('Contract already deployed');
187
+ const ownerId = await _this.address(opt);
188
+ const {
189
+ tx,
190
+ contractId
191
+ } = await _this.contractCreateTx({ ...opt,
192
+ callData: instance.calldata.encode(instance.aci.name, 'init', params),
193
+ code: instance.bytecode,
194
+ ownerId
195
+ });
196
+ const {
197
+ hash,
198
+ rawTx,
199
+ result,
200
+ txData
201
+ } = await sendAndProcess(tx, opt);
202
+ instance.deployInfo = Object.freeze({
203
+ result,
204
+ owner: ownerId,
205
+ transaction: hash,
206
+ rawTx,
207
+ txData,
208
+ address: contractId,
209
+ call: (name, args, options) => _this.contractCall(instance.source, contractId, name, args, { ...opt,
210
+ ...options
211
+ }),
212
+ callStatic: (name, args, options) => _this.contractCallStatic(instance.source, contractId, name, args, { ...opt,
213
+ ...options
214
+ }),
215
+ createdAt: new Date()
216
+ });
217
+ return instance.deployInfo;
218
+ };
219
+ /**
220
+ * Call contract function
221
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
222
+ * @rtype (init: Array, options: Object = { callStatic: false }) => CallResult: Object
223
+ * @param {String} fn Function name
224
+ * @param {Array} params Array of function arguments
225
+ * @param {Object} [options={}] Array of function arguments
226
+ * @param {Boolean} [options.callStatic=false] Static function call
227
+ * @return {Object} CallResult
228
+ */
229
+
230
+
231
+ instance.call = async function (fn) {
232
+ let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
233
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
234
+ const opt = { ...instance.options,
235
+ ...options
236
+ };
237
+ const fnACI = getFunctionACI(instance.aci, fn, instance.externalAci);
238
+ const contractId = instance.deployInfo.address;
239
+ if (!fn) throw new Error('Function name is required');
240
+ if (fn === 'init' && !opt.callStatic) throw new Error('"init" can be called only via dryRun');
241
+ if (!contractId && fn !== 'init') throw new Error('You need to deploy contract before calling!');
242
+ if (BigNumber(opt.amount).gt(0) && Object.prototype.hasOwnProperty.call(fnACI, 'payable') && !fnACI.payable) throw new Error(`You try to pay "${opt.amount}" to function "${fn}" which is not payable. Only payable function can accept tokens`);
243
+ const callerId = await _this.address(opt).catch(error => {
244
+ if (opt.callStatic) return DRY_RUN_ACCOUNT.pub;else throw error;
245
+ });
246
+ const callData = instance.calldata.encode(instance.aci.name, fn, params);
247
+ let res;
248
+
249
+ if (opt.callStatic) {
250
+ if (typeof opt.top === 'number') {
251
+ opt.top = (await _this.getKeyBlock(opt.top)).hash;
492
252
  }
493
- }, _callee2);
494
- }));
495
- };
496
-
497
- var compile = function compile(_ref7) {
498
- var client = _ref7.client,
499
- instance = _ref7.instance;
500
- return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
501
- var options,
502
- _yield$client$contrac2,
503
- bytecode,
504
- _args3 = arguments;
505
253
 
506
- return _regeneratorRuntime.wrap(function _callee3$(_context4) {
507
- while (1) {
508
- switch (_context4.prev = _context4.next) {
509
- case 0:
510
- options = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {};
511
- _context4.next = 3;
512
- return client.contractCompile(instance.source, _objectSpread(_objectSpread({}, instance.options), options));
254
+ const txOpt = { ...opt,
255
+ callData,
256
+ nonce: opt.top && (await _this.getAccount(callerId, {
257
+ hash: opt.top
258
+ })).nonce + 1
259
+ };
260
+ const tx = fn === 'init' ? (await _this.contractCreateTx({ ...txOpt,
261
+ code: instance.bytecode,
262
+ ownerId: callerId
263
+ })).tx : await _this.contractCallTx({ ...txOpt,
264
+ callerId,
265
+ contractId
266
+ });
267
+ const {
268
+ callObj,
269
+ ...dryRunOther
270
+ } = await _this.txDryRun(tx, callerId, opt);
271
+ await handleCallError(callObj);
272
+ res = { ...dryRunOther,
273
+ tx: TxObject({
274
+ tx
275
+ }),
276
+ result: callObj
277
+ };
278
+ } else {
279
+ const tx = await _this.contractCallTx({ ...opt,
280
+ callerId,
281
+ contractId,
282
+ callData
283
+ });
284
+ res = await sendAndProcess(tx, opt);
285
+ }
286
+
287
+ if (opt.waitMined || opt.callStatic) {
288
+ res.decodedResult = fnACI.returns && fnACI.returns !== 'unit' && fn !== 'init' && instance.calldata.decode(instance.aci.name, fn, res.result.returnValue);
289
+ res.decodedEvents = instance.decodeEvents(res.result.log);
290
+ }
291
+
292
+ return res;
293
+ };
294
+ /**
295
+ * Decode Events
296
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
297
+ * @rtype (events: Array) => DecodedEvents: Array
298
+ * @param {Array} events Array of encoded events(callRes.result.log)
299
+ * @return {Object} DecodedEvents
300
+ */
301
+
302
+
303
+ instance.decodeEvents = events => {
304
+ const eventsACI = instance.aci.event ? instance.aci.event.variant : [];
305
+ const eventsSchema = eventsACI.map(e => {
306
+ const name = Object.keys(e)[0];
307
+ return {
308
+ name,
309
+ types: e[name]
310
+ };
311
+ });
312
+ return decodeEvents(events, eventsSchema);
313
+ };
314
+ /**
315
+ * Generate proto function based on contract function using Contract ACI schema
316
+ * All function can be called like:
317
+ * 'await contract.methods.testFunction()' -> then sdk will decide to use dry-run or send tx on-chain base on if function stateful or not.
318
+ * Also you can manually do that:
319
+ * `await contract.methods.testFunction.get()` -> use call-static(dry-run)
320
+ * `await contract.methods.testFunction.send()` -> send tx on-chain
321
+ */
322
+
323
+
324
+ instance.methods = Object.fromEntries(instance.aci.functions.map(_ref2 => {
325
+ let {
326
+ name,
327
+ arguments: aciArgs,
328
+ stateful
329
+ } = _ref2;
330
+
331
+ const genHandler = callStatic => function () {
332
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
333
+ args[_key] = arguments[_key];
334
+ }
513
335
 
514
- case 3:
515
- _yield$client$contrac2 = _context4.sent;
516
- bytecode = _yield$client$contrac2.bytecode;
517
- instance.compiled = bytecode;
518
- return _context4.abrupt("return", instance.compiled);
336
+ const options = args.length === aciArgs.length + 1 ? args.pop() : {};
337
+ if (typeof options !== 'object') throw new Error(`Options should be an object: ${options}`);
338
+ if (name === 'init') return instance.deploy(args, {
339
+ callStatic,
340
+ ...options
341
+ });
342
+ return instance.call(name, args, {
343
+ callStatic,
344
+ ...options
345
+ });
346
+ };
519
347
 
520
- case 7:
521
- case "end":
522
- return _context4.stop();
523
- }
524
- }
525
- }, _callee3);
348
+ return [name, Object.assign(genHandler(name === 'init' ? false : !stateful), {
349
+ get: genHandler(true),
350
+ send: genHandler(false),
351
+ decodeEvents: events => instance.decodeEvents(events)
352
+ })];
526
353
  }));
527
- };
354
+ return instance;
355
+ }
528
356
  //# sourceMappingURL=index.js.map