@aeternity/aepp-sdk 8.2.1 → 10.0.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.
Files changed (159) hide show
  1. package/CHANGELOG.md +216 -337
  2. package/README.md +16 -9
  3. package/dist/aepp-sdk.browser-script.js +1 -1
  4. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
  5. package/dist/aepp-sdk.browser-script.js.map +1 -1
  6. package/dist/aepp-sdk.browser.js +1 -1
  7. package/dist/aepp-sdk.browser.js.map +1 -1
  8. package/dist/aepp-sdk.js +1 -1
  9. package/dist/aepp-sdk.js.map +1 -1
  10. package/es/account/base.js +36 -120
  11. package/es/account/base.js.map +1 -1
  12. package/es/account/memory.js +17 -19
  13. package/es/account/memory.js.map +1 -1
  14. package/es/account/multiple.js +79 -156
  15. package/es/account/multiple.js.map +1 -1
  16. package/es/ae/aens.js +205 -626
  17. package/es/ae/aens.js.map +1 -1
  18. package/es/ae/contract.js +114 -918
  19. package/es/ae/contract.js.map +1 -1
  20. package/es/ae/index.js +90 -250
  21. package/es/ae/index.js.map +1 -1
  22. package/es/ae/oracle.js +136 -504
  23. package/es/ae/oracle.js.map +1 -1
  24. package/es/ae/universal.js.map +1 -1
  25. package/es/chain/index.js +12 -27
  26. package/es/chain/index.js.map +1 -1
  27. package/es/chain/node.js +289 -1020
  28. package/es/chain/node.js.map +1 -1
  29. package/es/channel/handlers.js +597 -1086
  30. package/es/channel/handlers.js.map +1 -1
  31. package/es/channel/index.js +238 -465
  32. package/es/channel/index.js.map +1 -1
  33. package/es/channel/internal.js +129 -301
  34. package/es/channel/internal.js.map +1 -1
  35. package/es/contract/aci/index.js +328 -495
  36. package/es/contract/aci/index.js.map +1 -1
  37. package/es/contract/aci/transformation.js +35 -711
  38. package/es/contract/aci/transformation.js.map +1 -1
  39. package/es/contract/compiler.js +131 -203
  40. package/es/contract/compiler.js.map +1 -1
  41. package/es/contract/ga/helpers.js +17 -122
  42. package/es/contract/ga/helpers.js.map +1 -1
  43. package/es/contract/ga/index.js +108 -249
  44. package/es/contract/ga/index.js.map +1 -1
  45. package/es/contract/index.js +7 -3
  46. package/es/contract/index.js.map +1 -1
  47. package/es/index.js +3 -3
  48. package/es/index.js.map +1 -1
  49. package/es/node-pool/index.js +57 -74
  50. package/es/node-pool/index.js.map +1 -1
  51. package/es/node.js +65 -161
  52. package/es/node.js.map +1 -1
  53. package/es/other.d.ts +1 -1
  54. package/es/tx/builder/helpers.js +62 -115
  55. package/es/tx/builder/helpers.js.map +1 -1
  56. package/es/tx/builder/index.js +218 -309
  57. package/es/tx/builder/index.js.map +1 -1
  58. package/es/tx/builder/schema.js +586 -573
  59. package/es/tx/builder/schema.js.map +1 -1
  60. package/es/tx/index.js +102 -13
  61. package/es/tx/index.js.map +1 -1
  62. package/es/tx/tx-object.js +114 -138
  63. package/es/tx/tx-object.js.map +1 -1
  64. package/es/tx/tx.js +810 -1586
  65. package/es/tx/tx.js.map +1 -1
  66. package/es/tx/validator.js +191 -426
  67. package/es/tx/validator.js.map +1 -1
  68. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +20 -23
  69. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
  70. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +53 -80
  71. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
  72. package/es/utils/aepp-wallet-communication/connection/index.js +2 -1
  73. package/es/utils/aepp-wallet-communication/connection/index.js.map +1 -1
  74. package/es/utils/aepp-wallet-communication/content-script-bridge.js +24 -19
  75. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
  76. package/es/utils/aepp-wallet-communication/helpers.js +60 -105
  77. package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
  78. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +191 -514
  79. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
  80. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +78 -93
  81. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
  82. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +381 -613
  83. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
  84. package/es/utils/aepp-wallet-communication/schema.js +43 -39
  85. package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
  86. package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
  87. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
  88. package/es/utils/amount-formatter.js +41 -61
  89. package/es/utils/amount-formatter.js.map +1 -1
  90. package/es/utils/async-init.js +28 -64
  91. package/es/utils/async-init.js.map +1 -1
  92. package/es/utils/bignumber.js +3 -11
  93. package/es/utils/bignumber.js.map +1 -1
  94. package/es/utils/bytes.js +5 -9
  95. package/es/utils/bytes.js.map +1 -1
  96. package/es/utils/crypto.js +41 -106
  97. package/es/utils/crypto.js.map +1 -1
  98. package/es/utils/hd-wallet.js +45 -81
  99. package/es/utils/hd-wallet.js.map +1 -1
  100. package/es/utils/json-big.js +16 -49
  101. package/es/utils/json-big.js.map +1 -1
  102. package/es/utils/keystore.js +81 -246
  103. package/es/utils/keystore.js.map +1 -1
  104. package/es/utils/mptree.js +88 -134
  105. package/es/utils/mptree.js.map +1 -1
  106. package/es/utils/other.js +10 -60
  107. package/es/utils/other.js.map +1 -1
  108. package/es/utils/semver-satisfies.js +9 -28
  109. package/es/utils/semver-satisfies.js.map +1 -1
  110. package/es/utils/string.js +4 -13
  111. package/es/utils/string.js.map +1 -1
  112. package/es/utils/swagger.js +87 -224
  113. package/es/utils/swagger.js.map +1 -1
  114. package/package.json +51 -44
  115. package/src/account/base.js +11 -10
  116. package/src/account/multiple.js +24 -18
  117. package/src/ae/aens.js +64 -61
  118. package/src/ae/contract.js +71 -248
  119. package/src/ae/index.js +34 -9
  120. package/src/ae/oracle.js +30 -22
  121. package/src/ae/universal.js +3 -1
  122. package/src/chain/index.js +10 -16
  123. package/src/chain/node.js +38 -38
  124. package/src/channel/handlers.js +10 -4
  125. package/src/channel/index.js +44 -20
  126. package/src/contract/aci/index.js +234 -112
  127. package/src/contract/aci/transformation.js +30 -422
  128. package/src/contract/compiler.js +50 -5
  129. package/src/contract/ga/helpers.js +6 -14
  130. package/src/contract/ga/index.js +28 -28
  131. package/src/contract/index.js +6 -2
  132. package/src/index.js +4 -4
  133. package/src/node-pool/index.js +24 -13
  134. package/src/node.js +29 -30
  135. package/src/tx/builder/helpers.js +31 -56
  136. package/src/tx/builder/index.js +67 -28
  137. package/src/tx/builder/schema.js +79 -121
  138. package/src/tx/index.js +101 -12
  139. package/src/tx/tx-object.js +25 -8
  140. package/src/tx/tx.js +229 -150
  141. package/src/tx/validator.js +134 -202
  142. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +3 -2
  143. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +25 -7
  144. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  145. package/src/utils/aepp-wallet-communication/content-script-bridge.js +10 -4
  146. package/src/utils/aepp-wallet-communication/helpers.js +4 -2
  147. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +33 -12
  148. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +9 -5
  149. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +59 -29
  150. package/src/utils/aepp-wallet-communication/schema.js +1 -3
  151. package/src/utils/aepp-wallet-communication/wallet-detector.js +4 -1
  152. package/src/utils/amount-formatter.ts +1 -1
  153. package/src/utils/bignumber.ts +2 -1
  154. package/src/utils/crypto.js +4 -68
  155. package/src/utils/keystore.js +10 -2
  156. package/src/utils/other.ts +1 -1
  157. package/src/utils/semver-satisfies.ts +1 -1
  158. package/src/utils/swagger.js +22 -10
  159. package/src/contract/aci/helpers.js +0 -140
@@ -1,528 +1,361 @@
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
- * Generate contract ACI object with predefined js methods for contract usage - can be used for creating a reference to already deployed contracts
32
+ * Generate contract ACI object with predefined js methods for contract usage - can be used for
33
+ * creating a reference to already deployed contracts
28
34
  * @alias module:@aeternity/aepp-sdk/es/contract/aci
29
- * @param {String} source Contract source code
30
35
  * @param {Object} [options={}] Options object
31
- * @param {String} [options.aci] Contract ACI
36
+ * @param {String} [options.source] Contract source code
37
+ * @param {String} [options.bytecode] Contract bytecode
38
+ * @param {Object} [options.aci] Contract ACI
32
39
  * @param {String} [options.contractAddress] Contract address
33
40
  * @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
41
+ * @param {Boolean} [options.validateBytecode] Compare source code with on-chain version
36
42
  * @return {ContractInstance} JS Contract API
37
43
  * @example
38
- * const contractIns = await client.getContractInstance(sourceCode)
44
+ * const contractIns = await client.getContractInstance({ source })
39
45
  * await contractIns.deploy([321]) or await contractIns.methods.init(321)
40
- * const callResult = await contractIns.call('setState', [123]) or await contractIns.methods.setState.send(123, options)
41
- * const staticCallResult = await contractIns.call('setState', [123], { callStatic: true }) or await contractIns.methods.setState.get(123, options)
46
+ * const callResult = await contractIns.call('setState', [123]) or
47
+ * await contractIns.methods.setState.send(123, options)
48
+ * const staticCallResult = await contractIns.call('setState', [123], { callStatic: true }) or
49
+ * await contractIns.methods.setState.get(123, options)
42
50
  * Also you can call contract like: await contractIns.methods.setState(123, options)
43
- * Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is stateful or not
51
+ * Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is
52
+ * stateful or not
44
53
  */
45
54
 
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
55
 
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);
56
+ export default async function getContractInstance() {
57
+ var _this = this;
58
+
59
+ let {
60
+ source,
61
+ bytecode,
62
+ aci,
63
+ contractAddress,
64
+ filesystem = {},
65
+ validateBytecode,
66
+ ...otherOptions
67
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
68
+ aci = aci || source && (await this.contractGetACI(source, {
69
+ filesystem
258
70
  }));
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
- }));
71
+ if (!aci) throw new Error('Either ACI or source code is required');
72
+ contractAddress = contractAddress && (await this.resolveName(contractAddress, 'contract_pubkey', {
73
+ resolveByNode: true
74
+ }));
75
+ if (!contractAddress && !source && !bytecode) throw new Error('Can\'t create instance by ACI without address');
76
+
77
+ if (contractAddress) {
78
+ const contract = await this.getContract(contractAddress).catch(() => null);
79
+ if (!contract) throw new Error(`Contract with address ${contractAddress} not found on-chain`);
80
+ if (!contract.active) throw new Error(`Contract with address ${contractAddress} not active`);
81
+ }
82
+
83
+ const instance = {
84
+ interface: _defaultTo(null, _prop('interface', aci)),
85
+ aci: _defaultTo(null, _path(['encoded_aci', 'contract'], aci)),
86
+ calldata: new Calldata([aci.encoded_aci, ...aci.external_encoded_aci]),
87
+ externalAci: (aci.external_encoded_aci ?? []).map(a => a.contract || a.namespace),
88
+ source,
89
+ bytecode,
90
+ deployInfo: {
91
+ address: contractAddress
92
+ },
93
+ options: { ...this.Ae.defaults,
94
+ callStatic: false,
95
+ filesystem,
96
+ ...otherOptions
97
+ },
98
+ compilerVersion: this.compilerVersion
268
99
  };
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
100
 
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
- }));
101
+ if (validateBytecode) {
102
+ if (!contractAddress) throw new Error('Can\'t validate bytecode without contract address');
103
+ const onChanBytecode = (await this.getContractByteCode(contractAddress)).bytecode;
104
+ const isValid = source && (await this.validateByteCodeAPI(onChanBytecode, source, instance.options).catch(() => false)) || bytecode === onChanBytecode;
105
+ if (!isValid) throw new Error(`Contract ${source ? 'source' : 'bytecode'} do not correspond to the bytecode deployed on the chain`);
106
+ }
107
+ /**
108
+ * Compile contract
109
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
110
+ * @rtype () => ContractInstance: Object
111
+ * @return {String} bytecode bytecode
112
+ */
113
+
114
+
115
+ instance.compile = async function () {
116
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
117
+ if (instance.bytecode) throw new Error('Contract already compiled');
118
+ if (!instance.source) throw new Error('Can\'t compile without source code');
119
+ const {
120
+ bytecode
121
+ } = await _this.contractCompile(instance.source, { ...instance.options,
122
+ ...options
123
+ });
124
+ instance.bytecode = bytecode;
125
+ return instance.bytecode;
126
+ };
392
127
 
393
- return function (_x2) {
394
- return _ref4.apply(this, arguments);
128
+ const sendAndProcess = async (tx, options) => {
129
+ const txData = await this.send(tx, options);
130
+ const result = {
131
+ hash: txData.hash,
132
+ tx: TxObject({
133
+ tx: txData.rawTx
134
+ }),
135
+ txData,
136
+ rawTx: txData.rawTx
395
137
  };
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;
138
+ if (options.waitMined === false) return result;
139
+ const txInfo = await this.getTxInfo(txData.hash);
140
+ await handleCallError(txInfo);
141
+ return { ...result,
142
+ result: txInfo
143
+ };
144
+ };
453
145
 
454
- case 15:
455
- init = _context3.t0;
146
+ const handleCallError = _ref => {
147
+ let {
148
+ returnType,
149
+ returnValue
150
+ } = _ref;
151
+ let message; // TODO: ensure that it works correctly https://github.com/aeternity/aepp-calldata-js/issues/88
456
152
 
457
- if (!opt.callStatic) {
458
- _context3.next = 20;
459
- break;
460
- }
153
+ switch (returnType) {
154
+ case 'ok':
155
+ return;
461
156
 
462
- return _context3.abrupt("return", client.contractCallStatic(source, null, 'init', init, _objectSpread(_objectSpread({}, opt), {}, {
463
- bytecode: instance.compiled
464
- })));
157
+ case 'revert':
158
+ message = instance.calldata.decodeFateString(returnValue);
159
+ break;
465
160
 
466
- case 20:
467
- _context3.next = 22;
468
- return client.contractDeploy(instance.compiled, opt.source || instance.source, init, opt);
161
+ case 'error':
162
+ message = decode(returnValue).toString();
163
+ break;
469
164
 
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);
165
+ default:
166
+ throw new Error(`Unknown returnType: ${returnType}`);
167
+ }
487
168
 
488
- case 31:
489
- case "end":
490
- return _context3.stop();
491
- }
169
+ throw new Error(`Invocation failed${message ? `: "${message}"` : ''}`);
170
+ };
171
+ /**
172
+ * Deploy contract
173
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
174
+ * @rtype (init: Array, options: Object) => ContractInstance: Object
175
+ * @param {Array} params Contract init function arguments array
176
+ * @param {Object} [options] options
177
+ * @return {Object} deploy info
178
+ */
179
+
180
+
181
+ instance.deploy = async function () {
182
+ let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
183
+ let options = arguments.length > 1 ? arguments[1] : undefined;
184
+ const opt = { ...instance.options,
185
+ ...options,
186
+ deposit: DEPOSIT
187
+ };
188
+ if (!instance.bytecode) await instance.compile(opt);
189
+ if (opt.callStatic) return instance.call('init', params, opt);
190
+ if (instance.deployInfo.address) throw new Error('Contract already deployed');
191
+ const ownerId = await _this.address(opt);
192
+ const {
193
+ tx,
194
+ contractId
195
+ } = await _this.contractCreateTx({ ...opt,
196
+ callData: instance.calldata.encode(instance.aci.name, 'init', params),
197
+ code: instance.bytecode,
198
+ ownerId
199
+ });
200
+ const {
201
+ hash,
202
+ rawTx,
203
+ result,
204
+ txData
205
+ } = await sendAndProcess(tx, opt);
206
+ instance.deployInfo = Object.freeze({
207
+ result,
208
+ owner: ownerId,
209
+ transaction: hash,
210
+ rawTx,
211
+ txData,
212
+ address: contractId,
213
+ call: (name, args, options) => _this.contractCall(instance.source, contractId, name, args, { ...opt,
214
+ ...options
215
+ }),
216
+ callStatic: (name, args, options) => _this.contractCallStatic(instance.source, contractId, name, args, { ...opt,
217
+ ...options
218
+ }),
219
+ createdAt: new Date()
220
+ });
221
+ return instance.deployInfo;
222
+ };
223
+ /**
224
+ * Call contract function
225
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
226
+ * @rtype (init: Array, options: Object = { callStatic: false }) => CallResult: Object
227
+ * @param {String} fn Function name
228
+ * @param {Array} params Array of function arguments
229
+ * @param {Object} [options={}] Array of function arguments
230
+ * @param {Boolean} [options.callStatic=false] Static function call
231
+ * @return {Object} CallResult
232
+ */
233
+
234
+
235
+ instance.call = async function (fn) {
236
+ let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
237
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
238
+ const opt = { ...instance.options,
239
+ ...options
240
+ };
241
+ const fnACI = getFunctionACI(instance.aci, fn, instance.externalAci);
242
+ const contractId = instance.deployInfo.address;
243
+ if (!fn) throw new Error('Function name is required');
244
+ if (fn === 'init' && !opt.callStatic) throw new Error('"init" can be called only via dryRun');
245
+ if (!contractId && fn !== 'init') throw new Error('You need to deploy contract before calling!');
246
+ 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`);
247
+ const callerId = await _this.address(opt).catch(error => {
248
+ if (opt.callStatic) return DRY_RUN_ACCOUNT.pub;else throw error;
249
+ });
250
+ const callData = instance.calldata.encode(instance.aci.name, fn, params);
251
+ let res;
252
+
253
+ if (opt.callStatic) {
254
+ if (typeof opt.top === 'number') {
255
+ opt.top = (await _this.getKeyBlock(opt.top)).hash;
492
256
  }
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
257
 
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));
258
+ const txOpt = { ...opt,
259
+ callData,
260
+ nonce: opt.top && (await _this.getAccount(callerId, {
261
+ hash: opt.top
262
+ })).nonce + 1
263
+ };
264
+ const tx = fn === 'init' ? (await _this.contractCreateTx({ ...txOpt,
265
+ code: instance.bytecode,
266
+ ownerId: callerId
267
+ })).tx : await _this.contractCallTx({ ...txOpt,
268
+ callerId,
269
+ contractId
270
+ });
271
+ const {
272
+ callObj,
273
+ ...dryRunOther
274
+ } = await _this.txDryRun(tx, callerId, opt);
275
+ await handleCallError(callObj);
276
+ res = { ...dryRunOther,
277
+ tx: TxObject({
278
+ tx
279
+ }),
280
+ result: callObj
281
+ };
282
+ } else {
283
+ const tx = await _this.contractCallTx({ ...opt,
284
+ callerId,
285
+ contractId,
286
+ callData
287
+ });
288
+ res = await sendAndProcess(tx, opt);
289
+ }
290
+
291
+ if (opt.waitMined || opt.callStatic) {
292
+ res.decodedResult = fnACI.returns && fnACI.returns !== 'unit' && fn !== 'init' && instance.calldata.decode(instance.aci.name, fn, res.result.returnValue);
293
+ res.decodedEvents = instance.decodeEvents(res.result.log);
294
+ }
295
+
296
+ return res;
297
+ };
298
+ /**
299
+ * Decode Events
300
+ * @alias module:@aeternity/aepp-sdk/es/contract/aci
301
+ * @rtype (events: Array) => DecodedEvents: Array
302
+ * @param {Array} events Array of encoded events(callRes.result.log)
303
+ * @return {Object} DecodedEvents
304
+ */
305
+
306
+
307
+ instance.decodeEvents = events => {
308
+ const eventsACI = instance.aci.event ? instance.aci.event.variant : [];
309
+ const eventsSchema = eventsACI.map(e => {
310
+ const name = Object.keys(e)[0];
311
+ return {
312
+ name,
313
+ types: e[name]
314
+ };
315
+ });
316
+ return decodeEvents(events, eventsSchema);
317
+ };
318
+ /**
319
+ * Generate proto function based on contract function using Contract ACI schema
320
+ * All function can be called like:
321
+ * 'await contract.methods.testFunction()' -> then sdk will decide to use dry-run or send tx
322
+ * on-chain base on if function stateful or not.
323
+ * Also you can manually do that:
324
+ * `await contract.methods.testFunction.get()` -> use call-static(dry-run)
325
+ * `await contract.methods.testFunction.send()` -> send tx on-chain
326
+ */
327
+
328
+
329
+ instance.methods = Object.fromEntries(instance.aci.functions.map(_ref2 => {
330
+ let {
331
+ name,
332
+ arguments: aciArgs,
333
+ stateful
334
+ } = _ref2;
335
+
336
+ const genHandler = callStatic => function () {
337
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
338
+ args[_key] = arguments[_key];
339
+ }
513
340
 
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);
341
+ const options = args.length === aciArgs.length + 1 ? args.pop() : {};
342
+ if (typeof options !== 'object') throw new Error(`Options should be an object: ${options}`);
343
+ if (name === 'init') return instance.deploy(args, {
344
+ callStatic,
345
+ ...options
346
+ });
347
+ return instance.call(name, args, {
348
+ callStatic,
349
+ ...options
350
+ });
351
+ };
519
352
 
520
- case 7:
521
- case "end":
522
- return _context4.stop();
523
- }
524
- }
525
- }, _callee3);
353
+ return [name, Object.assign(genHandler(name === 'init' ? false : !stateful), {
354
+ get: genHandler(true),
355
+ send: genHandler(false),
356
+ decodeEvents: events => instance.decodeEvents(events)
357
+ })];
526
358
  }));
527
- };
359
+ return instance;
360
+ }
528
361
  //# sourceMappingURL=index.js.map