@aeternity/aepp-sdk 10.0.0-beta.1 → 11.0.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.
- package/README.md +15 -14
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -27
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +1 -1
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +1 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/account/{base.js → base.mjs} +23 -11
- package/es/account/base.mjs.map +1 -0
- package/es/account/{memory.js → memory.mjs} +29 -14
- package/es/account/memory.mjs.map +1 -0
- package/es/account/{multiple.js → multiple.mjs} +14 -11
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/{aens.js → aens.mjs} +69 -59
- package/es/ae/aens.mjs.map +1 -0
- package/es/ae/{aepp.js → aepp.mjs} +9 -9
- package/es/ae/aepp.mjs.map +1 -0
- package/es/ae/contract.mjs +159 -0
- package/es/ae/contract.mjs.map +1 -0
- package/es/ae/{index.js → index.mjs} +12 -10
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/{oracle.js → oracle.mjs} +37 -20
- package/es/ae/oracle.mjs.map +1 -0
- package/es/ae/{universal.js → universal.mjs} +10 -10
- package/es/ae/universal.mjs.map +1 -0
- package/es/ae/{wallet.js → wallet.mjs} +10 -10
- package/es/ae/wallet.mjs.map +1 -0
- package/es/aepp-wallet-communication/schema.d.ts +74 -0
- package/es/bignumber.d.ts +1 -7
- package/es/chain/{index.js → index.mjs} +28 -8
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/{node.js → node.mjs} +133 -90
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/{handlers.js → handlers.mjs} +18 -20
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +43 -22
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/{internal.js → internal.mjs} +51 -35
- package/es/channel/internal.mjs.map +1 -0
- package/es/contract/aci.mjs +477 -0
- package/es/contract/aci.mjs.map +1 -0
- package/es/contract/compiler.mjs +115 -0
- package/es/contract/compiler.mjs.map +1 -0
- package/es/contract/ga/helpers.mjs +30 -0
- package/es/contract/ga/helpers.mjs.map +1 -0
- package/es/contract/ga/{index.js → index.mjs} +40 -28
- package/es/contract/ga/index.mjs.map +1 -0
- package/es/encoder.d.ts +26 -0
- package/es/errors.d.ts +284 -0
- package/es/index.mjs +57 -0
- package/es/index.mjs.map +1 -0
- package/es/node-pool/{index.js → index.mjs} +15 -12
- package/es/node-pool/index.mjs.map +1 -0
- package/es/{node.js → node.mjs} +17 -11
- package/es/node.mjs.map +1 -0
- package/es/tx/builder/constants.mjs +108 -0
- package/es/tx/builder/constants.mjs.map +1 -0
- package/es/tx/builder/field-types.mjs +74 -0
- package/es/tx/builder/field-types.mjs.map +1 -0
- package/es/tx/builder/helpers.mjs +321 -0
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/{index.js → index.mjs} +92 -74
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/{schema.js → schema.mjs} +16 -172
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +106 -14
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/{tx-object.js → tx-object.mjs} +31 -18
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/{tx.js → tx.mjs} +15 -14
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/{validator.js → validator.mjs} +72 -33
- package/es/tx/validator.mjs.map +1 -0
- package/es/typings/blakejs/index.d.mjs +2 -0
- package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
- package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +10 -8
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +25 -17
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
- package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +15 -8
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +126 -72
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +23 -14
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +116 -91
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
- package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/{async-init.js → async-init.mjs} +8 -11
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -8
- package/es/utils/bytes.mjs.map +1 -0
- package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
- package/es/utils/crypto-ts.mjs.map +1 -0
- package/es/utils/{crypto.js → crypto.mjs} +42 -156
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/encoder.mjs +106 -0
- package/es/utils/encoder.mjs.map +1 -0
- package/es/utils/errors.mjs +689 -0
- package/es/utils/errors.mjs.map +1 -0
- package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/{json-big.js → json-big.mjs} +1 -1
- package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
- package/es/utils/{keystore.js → keystore.mjs} +63 -51
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/{mptree.js → mptree.mjs} +21 -12
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/{other.js → other.mjs} +2 -2
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
- package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
- package/es/utils/{string.js → string.mjs} +1 -1
- package/es/utils/{string.js.map → string.mjs.map} +1 -1
- package/es/utils/{swagger.js → swagger.mjs} +49 -15
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +49 -58
- package/src/account/base.js +3 -1
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +16 -6
- package/src/ae/aens.js +52 -58
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +21 -154
- package/src/ae/index.js +6 -4
- package/src/ae/oracle.js +33 -14
- package/src/ae/universal.js +4 -2
- package/src/ae/wallet.js +2 -2
- package/src/chain/index.js +29 -6
- package/src/chain/node.js +95 -65
- package/src/channel/handlers.js +30 -20
- package/src/channel/index.js +46 -16
- package/src/channel/internal.js +35 -30
- package/src/contract/aci.js +376 -0
- package/src/contract/compiler.js +22 -113
- package/src/contract/ga/helpers.js +10 -6
- package/src/contract/ga/index.js +31 -24
- package/src/index.js +9 -8
- package/src/node-pool/index.js +13 -10
- package/src/node.js +11 -10
- package/src/tx/builder/constants.js +100 -0
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +99 -103
- package/src/tx/builder/index.js +81 -40
- package/src/tx/builder/schema.js +44 -132
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +35 -15
- package/src/tx/tx.js +110 -35
- package/src/tx/validator.js +25 -5
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
- package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
- package/src/utils/aepp-wallet-communication/helpers.js +6 -14
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +106 -57
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +95 -53
- package/src/utils/aepp-wallet-communication/schema.ts +80 -0
- package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
- package/src/utils/amount-formatter.ts +4 -3
- package/src/utils/async-init.js +10 -6
- package/src/utils/bignumber.ts +1 -9
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +11 -155
- package/src/utils/encoder.ts +101 -0
- package/src/utils/errors.ts +683 -0
- package/src/utils/hd-wallet.js +11 -26
- package/src/utils/keystore.js +57 -53
- package/src/utils/mptree.ts +12 -6
- package/src/utils/swagger.js +68 -45
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -308
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js.map +0 -1
- package/es/ae/oracle.js.map +0 -1
- package/es/ae/universal.js.map +0 -1
- package/es/ae/wallet.js.map +0 -1
- package/es/chain/index.js.map +0 -1
- package/es/chain/node.js.map +0 -1
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- package/es/channel/internal.js.map +0 -1
- package/es/contract/aci/helpers.js +0 -298
- package/es/contract/aci/helpers.js.map +0 -1
- package/es/contract/aci/index.js +0 -356
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -66
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -242
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -22
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js.map +0 -1
- package/es/contract/index.js +0 -158
- package/es/contract/index.js.map +0 -1
- package/es/index.js +0 -56
- package/es/index.js.map +0 -1
- package/es/node-pool/index.js.map +0 -1
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js +0 -318
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js.map +0 -1
- package/es/tx/validator.js.map +0 -1
- package/es/typings/blakejs/index.d.js +0 -2
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
- package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
- package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -107
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js.map +0 -1
- package/es/utils/bignumber.js.map +0 -1
- package/es/utils/bytes.js.map +0 -1
- package/es/utils/crypto-ts.js.map +0 -1
- package/es/utils/crypto.js.map +0 -1
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js.map +0 -1
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/index.js +0 -310
- package/src/contract/aci/transformation.js +0 -72
- package/src/contract/index.js +0 -159
- package/src/utils/aepp-wallet-communication/schema.js +0 -108
package/es/contract/aci/index.js
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import _path from "ramda/src/path";
|
|
2
|
-
import _prop from "ramda/src/prop";
|
|
3
|
-
import _defaultTo from "ramda/src/defaultTo";
|
|
4
|
-
import _pick from "ramda/src/pick";
|
|
5
|
-
import BigNumber from 'bignumber.js';
|
|
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
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Generate contract ACI object with predefined js methods for contract usage - can be used for creating a reference to already deployed contracts
|
|
33
|
-
* @alias module:@aeternity/aepp-sdk/es/contract/aci
|
|
34
|
-
* @param {Object} [options={}] Options object
|
|
35
|
-
* @param {String} [options.source] Contract source code
|
|
36
|
-
* @param {String} [options.bytecode] Contract bytecode
|
|
37
|
-
* @param {Object} [options.aci] Contract ACI
|
|
38
|
-
* @param {String} [options.contractAddress] Contract address
|
|
39
|
-
* @param {Object} [options.filesystem] Contact source external namespaces map
|
|
40
|
-
* @param {Boolean} [options.validateBytecode] Compare source code with on-chain version
|
|
41
|
-
* @return {ContractInstance} JS Contract API
|
|
42
|
-
* @example
|
|
43
|
-
* const contractIns = await client.getContractInstance({ source })
|
|
44
|
-
* await contractIns.deploy([321]) or await contractIns.methods.init(321)
|
|
45
|
-
* const callResult = await contractIns.call('setState', [123]) or await contractIns.methods.setState.send(123, options)
|
|
46
|
-
* const staticCallResult = await contractIns.call('setState', [123], { callStatic: true }) or await contractIns.methods.setState.get(123, options)
|
|
47
|
-
* Also you can call contract like: await contractIns.methods.setState(123, options)
|
|
48
|
-
* Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is stateful or not
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
66
|
-
}));
|
|
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
|
|
95
|
-
};
|
|
96
|
-
|
|
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
|
-
};
|
|
123
|
-
|
|
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
|
|
133
|
-
};
|
|
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
|
-
};
|
|
141
|
-
|
|
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
|
|
148
|
-
|
|
149
|
-
switch (returnType) {
|
|
150
|
-
case 'ok':
|
|
151
|
-
return;
|
|
152
|
-
|
|
153
|
-
case 'revert':
|
|
154
|
-
message = instance.calldata.decodeFateString(returnValue);
|
|
155
|
-
break;
|
|
156
|
-
|
|
157
|
-
case 'error':
|
|
158
|
-
message = decode(returnValue).toString();
|
|
159
|
-
break;
|
|
160
|
-
|
|
161
|
-
default:
|
|
162
|
-
throw new Error(`Unknown returnType: ${returnType}`);
|
|
163
|
-
}
|
|
164
|
-
|
|
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;
|
|
252
|
-
}
|
|
253
|
-
|
|
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
|
-
}
|
|
335
|
-
|
|
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
|
-
};
|
|
347
|
-
|
|
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
|
-
})];
|
|
353
|
-
}));
|
|
354
|
-
return instance;
|
|
355
|
-
}
|
|
356
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/contract/aci/index.js"],"names":["BigNumber","Encoder","Calldata","decodeEvents","DRY_RUN_ACCOUNT","DEPOSIT","TxObject","decode","getFunctionACI","aci","name","external","fn","functions","find","f","Error","bindings","state","type_defs","map","event","variant","getContractInstance","source","bytecode","contractAddress","filesystem","validateBytecode","otherOptions","contractGetACI","resolveName","resolveByNode","contract","getContract","catch","active","instance","interface","calldata","encoded_aci","external_encoded_aci","externalAci","a","namespace","deployInfo","address","options","Ae","defaults","callStatic","compilerVersion","onChanBytecode","getContractByteCode","isValid","validateByteCodeAPI","compile","contractCompile","sendAndProcess","tx","txData","send","result","hash","rawTx","waitMined","txInfo","getTxInfo","handleCallError","returnType","returnValue","message","decodeFateString","toString","deploy","params","opt","deposit","call","ownerId","contractId","contractCreateTx","callData","encode","code","Object","freeze","owner","transaction","args","contractCall","contractCallStatic","createdAt","Date","fnACI","amount","gt","prototype","hasOwnProperty","payable","callerId","error","pub","res","top","getKeyBlock","txOpt","nonce","getAccount","contractCallTx","callObj","dryRunOther","txDryRun","decodedResult","returns","decodedEvents","log","events","eventsACI","eventsSchema","e","keys","types","methods","fromEntries","arguments","aciArgs","stateful","genHandler","length","pop","assign","get"],"mappings":";;;;AAyBA,OAAOA,SAAP,MA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AClC,YAAT,CAAsBkH,MAAtB;AAH1B,KAFF,CAFK,CAAP;AAWD,GAnBmC,CAAnB,CAAnB;AAsBA,SAAOhF,QAAP;AACD","sourcesContent":["/*\n * ISC License (ISC)\n * Copyright (c) 2018 aeternity developers\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\n\n/**\n * ContractACI module\n *\n * @module @aeternity/aepp-sdk/es/contract/aci\n * @export getContractInstance\n */\n\nimport * as R from 'ramda'\nimport BigNumber from 'bignumber.js'\nimport { Encoder as Calldata } from '@aeternity/aepp-calldata'\nimport { decodeEvents } from './transformation'\nimport { DRY_RUN_ACCOUNT, DEPOSIT } from '../../tx/builder/schema'\nimport TxObject from '../../tx/tx-object'\nimport { decode } from '../../tx/builder/helpers'\n\n/**\n * Get function schema from contract ACI object\n * @param {Object} aci Contract ACI object\n * @param {String} name Function name\n * @param external\n * @return {Object} function ACI\n */\nfunction getFunctionACI (aci, name, external) {\n const fn = aci.functions.find(f => f.name === name)\n if (!fn && name !== 'init') throw new Error(`Function ${name} doesn't exist in contract`)\n\n return {\n ...fn,\n bindings: [\n {\n state: aci.state,\n type_defs: aci.type_defs,\n name: aci.name\n },\n ...external.map(R.pick(['state', 'type_defs', 'name']))\n ],\n event: aci.event ? aci.event.variant : []\n }\n}\n\n/**\n * Generate contract ACI object with predefined js methods for contract usage - can be used for creating a reference to already deployed contracts\n * @alias module:@aeternity/aepp-sdk/es/contract/aci\n * @param {Object} [options={}] Options object\n * @param {String} [options.source] Contract source code\n * @param {String} [options.bytecode] Contract bytecode\n * @param {Object} [options.aci] Contract ACI\n * @param {String} [options.contractAddress] Contract address\n * @param {Object} [options.filesystem] Contact source external namespaces map\n * @param {Boolean} [options.validateBytecode] Compare source code with on-chain version\n * @return {ContractInstance} JS Contract API\n * @example\n * const contractIns = await client.getContractInstance({ source })\n * await contractIns.deploy([321]) or await contractIns.methods.init(321)\n * const callResult = await contractIns.call('setState', [123]) or await contractIns.methods.setState.send(123, options)\n * const staticCallResult = await contractIns.call('setState', [123], { callStatic: true }) or await contractIns.methods.setState.get(123, options)\n * Also you can call contract like: await contractIns.methods.setState(123, options)\n * Then sdk decide to make on-chain or static call(dry-run API) transaction based on function is stateful or not\n */\nexport default async function getContractInstance ({\n source,\n bytecode,\n aci,\n contractAddress,\n filesystem = {},\n validateBytecode,\n ...otherOptions\n} = {}) {\n aci = aci || (source && await this.contractGetACI(source, { filesystem }))\n if (!aci) throw new Error('Either ACI or source code is required')\n contractAddress = contractAddress && await this.resolveName(\n contractAddress, 'ct', { resolveByNode: true }\n )\n if (!contractAddress && !source && !bytecode) throw new Error('Can\\'t create instance by ACI without address')\n\n if (contractAddress) {\n const contract = await this.getContract(contractAddress).catch(() => null)\n if (!contract) throw new Error(`Contract with address ${contractAddress} not found on-chain`)\n if (!contract.active) throw new Error(`Contract with address ${contractAddress} not active`)\n }\n\n const instance = {\n interface: R.defaultTo(null, R.prop('interface', aci)),\n aci: R.defaultTo(null, R.path(['encoded_aci', 'contract'], aci)),\n calldata: new Calldata([aci.encoded_aci, ...aci.external_encoded_aci]),\n externalAci: aci.external_encoded_aci ? aci.external_encoded_aci.map(a => a.contract || a.namespace) : [],\n source,\n bytecode,\n deployInfo: { address: contractAddress },\n options: {\n ...this.Ae.defaults,\n callStatic: false,\n filesystem,\n ...otherOptions\n },\n compilerVersion: this.compilerVersion\n }\n\n if (validateBytecode) {\n if (!contractAddress) throw new Error('Can\\'t validate bytecode without contract address')\n const onChanBytecode = (await this.getContractByteCode(contractAddress)).bytecode\n const isValid = (source && await this\n .validateByteCodeAPI(onChanBytecode, source, instance.options).catch(() => false)) ||\n bytecode === onChanBytecode\n if (!isValid) throw new Error(`Contract ${source ? 'source' : 'bytecode'} do not correspond to the bytecode deployed on the chain`)\n }\n\n /**\n * Compile contract\n * @alias module:@aeternity/aepp-sdk/es/contract/aci\n * @rtype () => ContractInstance: Object\n * @return {String} bytecode bytecode\n */\n instance.compile = async (options = {}) => {\n if (instance.bytecode) throw new Error('Contract already compiled')\n if (!instance.source) throw new Error('Can\\'t compile without source code')\n const { bytecode } = await this.contractCompile(instance.source, { ...instance.options, ...options })\n instance.bytecode = bytecode\n return instance.bytecode\n }\n\n const sendAndProcess = async (tx, options) => {\n const txData = await this.send(tx, options)\n const result = {\n hash: txData.hash, tx: TxObject({ tx: txData.rawTx }), txData, rawTx: txData.rawTx\n }\n if (options.waitMined === false) return result\n const txInfo = await this.getTxInfo(txData.hash)\n await handleCallError(txInfo)\n return { ...result, result: txInfo }\n }\n\n const handleCallError = ({ returnType, returnValue }) => {\n let message\n // TODO: ensure that it works correctly https://github.com/aeternity/aepp-calldata-js/issues/88\n switch (returnType) {\n case 'ok': return\n case 'revert':\n message = instance.calldata.decodeFateString(returnValue)\n break\n case 'error':\n message = decode(returnValue).toString()\n break\n default: throw new Error(`Unknown returnType: ${returnType}`)\n }\n throw new Error(`Invocation failed${message ? `: \"${message}\"` : ''}`)\n }\n\n /**\n * Deploy contract\n * @alias module:@aeternity/aepp-sdk/es/contract/aci\n * @rtype (init: Array, options: Object) => ContractInstance: Object\n * @param {Array} params Contract init function arguments array\n * @param {Object} [options] options\n * @return {Object} deploy info\n */\n instance.deploy = async (params = [], options) => {\n const opt = { ...instance.options, ...options, deposit: DEPOSIT }\n if (!instance.bytecode) await instance.compile(opt)\n if (opt.callStatic) return instance.call('init', params, opt)\n if (instance.deployInfo.address) throw new Error('Contract already deployed')\n\n const ownerId = await this.address(opt)\n const { tx, contractId } = await this.contractCreateTx({\n ...opt,\n callData: instance.calldata.encode(instance.aci.name, 'init', params),\n code: instance.bytecode,\n ownerId\n })\n const { hash, rawTx, result, txData } = await sendAndProcess(tx, opt)\n instance.deployInfo = Object.freeze({\n result,\n owner: ownerId,\n transaction: hash,\n rawTx,\n txData,\n address: contractId,\n call: (name, args, options) =>\n this.contractCall(instance.source, contractId, name, args, { ...opt, ...options }),\n callStatic: (name, args, options) =>\n this.contractCallStatic(instance.source, contractId, name, args, { ...opt, ...options }),\n createdAt: new Date()\n })\n return instance.deployInfo\n }\n\n /**\n * Call contract function\n * @alias module:@aeternity/aepp-sdk/es/contract/aci\n * @rtype (init: Array, options: Object = { callStatic: false }) => CallResult: Object\n * @param {String} fn Function name\n * @param {Array} params Array of function arguments\n * @param {Object} [options={}] Array of function arguments\n * @param {Boolean} [options.callStatic=false] Static function call\n * @return {Object} CallResult\n */\n instance.call = async (fn, params = [], options = {}) => {\n const opt = { ...instance.options, ...options }\n const fnACI = getFunctionACI(instance.aci, fn, instance.externalAci)\n const contractId = instance.deployInfo.address\n\n if (!fn) throw new Error('Function name is required')\n if (fn === 'init' && !opt.callStatic) throw new Error('\"init\" can be called only via dryRun')\n if (!contractId && fn !== 'init') throw new Error('You need to deploy contract before calling!')\n if (\n BigNumber(opt.amount).gt(0) &&\n (Object.prototype.hasOwnProperty.call(fnACI, 'payable') && !fnACI.payable)\n ) throw new Error(`You try to pay \"${opt.amount}\" to function \"${fn}\" which is not payable. Only payable function can accept tokens`)\n\n const callerId = await this.address(opt).catch(error => {\n if (opt.callStatic) return DRY_RUN_ACCOUNT.pub\n else throw error\n })\n const callData = instance.calldata.encode(instance.aci.name, fn, params)\n\n let res\n if (opt.callStatic) {\n if (typeof opt.top === 'number') {\n opt.top = (await this.getKeyBlock(opt.top)).hash\n }\n const txOpt = {\n ...opt,\n callData,\n nonce: opt.top && (await this.getAccount(callerId, { hash: opt.top })).nonce + 1\n }\n const tx = fn === 'init'\n ? (await this.contractCreateTx({ ...txOpt, code: instance.bytecode, ownerId: callerId })).tx\n : await this.contractCallTx({ ...txOpt, callerId, contractId })\n\n const { callObj, ...dryRunOther } = await this.txDryRun(tx, callerId, opt)\n await handleCallError(callObj)\n res = { ...dryRunOther, tx: TxObject({ tx }), result: callObj }\n } else {\n const tx = await this.contractCallTx({ ...opt, callerId, contractId, callData })\n res = await sendAndProcess(tx, opt)\n }\n if (opt.waitMined || opt.callStatic) {\n res.decodedResult = fnACI.returns && fnACI.returns !== 'unit' && fn !== 'init' &&\n instance.calldata.decode(instance.aci.name, fn, res.result.returnValue)\n res.decodedEvents = instance.decodeEvents(res.result.log)\n }\n return res\n }\n\n /**\n * Decode Events\n * @alias module:@aeternity/aepp-sdk/es/contract/aci\n * @rtype (events: Array) => DecodedEvents: Array\n * @param {Array} events Array of encoded events(callRes.result.log)\n * @return {Object} DecodedEvents\n */\n instance.decodeEvents = (events) => {\n const eventsACI = instance.aci.event ? instance.aci.event.variant : []\n const eventsSchema = eventsACI.map(e => {\n const name = Object.keys(e)[0]\n return { name, types: e[name] }\n })\n\n return decodeEvents(events, eventsSchema)\n }\n\n /**\n * Generate proto function based on contract function using Contract ACI schema\n * All function can be called like:\n * 'await contract.methods.testFunction()' -> then sdk will decide to use dry-run or send tx on-chain base on if function stateful or not.\n * Also you can manually do that:\n * `await contract.methods.testFunction.get()` -> use call-static(dry-run)\n * `await contract.methods.testFunction.send()` -> send tx on-chain\n */\n instance.methods = Object.fromEntries(instance.aci.functions\n .map(({ name, arguments: aciArgs, stateful }) => {\n const genHandler = callStatic => (...args) => {\n const options = args.length === aciArgs.length + 1 ? args.pop() : {}\n if (typeof options !== 'object') throw new Error(`Options should be an object: ${options}`)\n if (name === 'init') return instance.deploy(args, { callStatic, ...options })\n return instance.call(name, args, { callStatic, ...options })\n }\n return [\n name,\n Object.assign(\n genHandler(name === 'init' ? false : !stateful),\n {\n get: genHandler(true),\n send: genHandler(false),\n decodeEvents: events => instance.decodeEvents(events)\n }\n )\n ]\n })\n )\n\n return instance\n}\n"],"file":"index.js"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { toBytes } from '../../utils/bytes';
|
|
2
|
-
import { decode } from '../../tx/builder/helpers';
|
|
3
|
-
import { parseBigNumber } from '../../utils/bignumber';
|
|
4
|
-
import { addressFromDecimal, hash } from '../../utils/crypto';
|
|
5
|
-
export const SOPHIA_TYPES = ['int', 'string', 'tuple', 'address', 'bool', 'list', 'map', 'record', 'option', 'oracle', 'oracleQuery', 'hash', 'signature', 'bytes', 'variant'].reduce((acc, type) => ({ ...acc,
|
|
6
|
-
[type]: type
|
|
7
|
-
}), {
|
|
8
|
-
ChainTtl: 'Chain.ttl'
|
|
9
|
-
});
|
|
10
|
-
/**
|
|
11
|
-
* Decode events fields
|
|
12
|
-
* @param {Object[]} events Array of events with encoded fields
|
|
13
|
-
* @param {Array} schemas Smart contract event ACI schemas
|
|
14
|
-
* @return {Object}
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
export const decodeEvents = function (events) {
|
|
18
|
-
let schemas = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
19
|
-
return events.reduce((acc, event) => {
|
|
20
|
-
const [nameHash, ...params] = event.topics;
|
|
21
|
-
const schema = schemas.find(s => hash(s.name).equals(toBytes(nameHash, true)));
|
|
22
|
-
if (!schema) return acc;
|
|
23
|
-
const stringCount = schema.types.filter(t => t === SOPHIA_TYPES.string).length;
|
|
24
|
-
if (stringCount > 1) throw new Error(`Event schema contains more than one string: ${schema.types}`);
|
|
25
|
-
const topicsCount = schema.types.length - stringCount;
|
|
26
|
-
|
|
27
|
-
if (topicsCount !== params.length) {
|
|
28
|
-
throw new Error(`Schema defines ${topicsCount} types, but ${params.length} topics present`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
acc.push({ ...event,
|
|
32
|
-
name: schema.name,
|
|
33
|
-
decoded: schema.types.map(type => decodeEventField(type === SOPHIA_TYPES.string ? event.data : params.shift(), type))
|
|
34
|
-
});
|
|
35
|
-
return acc;
|
|
36
|
-
}, []);
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Transform Event based on type
|
|
40
|
-
* @param {String|Number} field Event data
|
|
41
|
-
* @param {String} type Event type from schema
|
|
42
|
-
* @return {*}
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
function decodeEventField(field, type) {
|
|
46
|
-
switch (type) {
|
|
47
|
-
case SOPHIA_TYPES.int:
|
|
48
|
-
return parseBigNumber(field);
|
|
49
|
-
|
|
50
|
-
case SOPHIA_TYPES.bool:
|
|
51
|
-
return !!field;
|
|
52
|
-
|
|
53
|
-
case SOPHIA_TYPES.hash:
|
|
54
|
-
return toBytes(field, true).toString('hex');
|
|
55
|
-
|
|
56
|
-
case SOPHIA_TYPES.address:
|
|
57
|
-
return addressFromDecimal(field);
|
|
58
|
-
|
|
59
|
-
case SOPHIA_TYPES.string:
|
|
60
|
-
return decode(field).toString('utf-8');
|
|
61
|
-
|
|
62
|
-
default:
|
|
63
|
-
return toBytes(field, true);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
//# sourceMappingURL=transformation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/contract/aci/transformation.js"],"names":["toBytes","decode","parseBigNumber","addressFromDecimal","hash","SOPHIA_TYPES","reduce","acc","type","ChainTtl","decodeEvents","events","schemas","event","nameHash","params","topics","schema","find","s","name","equals","stringCount","types","filter","t","string","length","Error","topicsCount","push","decoded","map","decodeEventField","data","shift","field","int","bool","toString","address"],"mappings":"AAAA,SAASA,OAAT,QAAwB,mBAAxB;AACA,SAASC,MAAT,QAAuB,0BAAvB;AACA,SAASC,cAAT,QAA+B,uBAA/B;AACA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decode } from '../../tx/builder/helpers'\nimport { parseBigNumber } from '../../utils/bignumber'\nimport { addressFromDecimal, hash } from '../../utils/crypto'\n\nexport const SOPHIA_TYPES = [\n 'int',\n 'string',\n 'tuple',\n 'address',\n 'bool',\n 'list',\n 'map',\n 'record',\n 'option',\n 'oracle',\n 'oracleQuery',\n 'hash',\n 'signature',\n 'bytes',\n 'variant'\n].reduce((acc, type) => ({ ...acc, [type]: type }), { ChainTtl: 'Chain.ttl' })\n\n/**\n * Decode events fields\n * @param {Object[]} events Array of events with encoded fields\n * @param {Array} schemas Smart contract event ACI schemas\n * @return {Object}\n */\nexport const decodeEvents = (events, schemas = []) => events.reduce((acc, event) => {\n const [nameHash, ...params] = event.topics\n const schema = schemas.find((s) => hash(s.name).equals(toBytes(nameHash, true)))\n if (!schema) return acc\n const stringCount = schema.types.filter(t => t === SOPHIA_TYPES.string).length\n if (stringCount > 1) throw new Error(`Event schema contains more than one string: ${schema.types}`)\n const topicsCount = schema.types.length - stringCount\n if (topicsCount !== params.length) {\n throw new Error(`Schema defines ${topicsCount} types, but ${params.length} topics present`)\n }\n\n acc.push({\n ...event,\n name: schema.name,\n decoded: schema.types.map((type) =>\n decodeEventField(type === SOPHIA_TYPES.string ? event.data : params.shift(), type))\n })\n\n return acc\n}, [])\n\n/**\n * Transform Event based on type\n * @param {String|Number} field Event data\n * @param {String} type Event type from schema\n * @return {*}\n */\nfunction decodeEventField (field, type) {\n switch (type) {\n case SOPHIA_TYPES.int:\n return parseBigNumber(field)\n case SOPHIA_TYPES.bool:\n return !!field\n case SOPHIA_TYPES.hash:\n return toBytes(field, true).toString('hex')\n case SOPHIA_TYPES.address:\n return addressFromDecimal(field)\n case SOPHIA_TYPES.string:\n return decode(field).toString('utf-8')\n default:\n return toBytes(field, true)\n }\n}\n"],"file":"transformation.js"}
|