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