@dedot/contracts 0.12.1-next.c7fd0a6c.2 → 0.13.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.
- package/Contract.d.ts +4 -4
- package/Contract.js +41 -6
- package/ContractDeployer.d.ts +2 -2
- package/ContractDeployer.js +8 -6
- package/TypinkRegistry.d.ts +29 -6
- package/TypinkRegistry.js +144 -24
- package/cjs/Contract.js +44 -9
- package/cjs/ContractDeployer.js +10 -8
- package/cjs/TypinkRegistry.js +144 -24
- package/cjs/errors.js +3 -3
- package/cjs/executor/ConstructorQueryExecutor.js +49 -9
- package/cjs/executor/ConstructorTxExecutor.js +17 -6
- package/cjs/executor/QueryExecutor.js +30 -5
- package/cjs/executor/TxExecutor.js +10 -1
- package/cjs/executor/abstract/ContractExecutor.js +4 -5
- package/cjs/executor/abstract/DeployerExecutor.js +2 -2
- package/cjs/index.js +1 -1
- package/cjs/storage/BaseLazyObject.js +25 -0
- package/cjs/storage/LazyMapping.js +22 -0
- package/cjs/storage/LazyObject.js +18 -0
- package/cjs/storage/LazyStorageVec.js +54 -0
- package/cjs/storage/index.js +19 -0
- package/cjs/types/index.js +3 -0
- package/cjs/types/v6.js +2 -0
- package/cjs/utils/address.js +67 -0
- package/cjs/utils/ensure.js +68 -0
- package/cjs/utils/index.js +21 -0
- package/cjs/utils/proxy.js +11 -0
- package/cjs/utils/storage.js +44 -0
- package/cjs/{utils.js → utils/types.js} +31 -34
- package/errors.js +2 -2
- package/executor/ConstructorQueryExecutor.js +47 -7
- package/executor/ConstructorTxExecutor.js +18 -7
- package/executor/QueryExecutor.js +27 -2
- package/executor/TxExecutor.js +11 -2
- package/executor/abstract/ContractExecutor.d.ts +3 -4
- package/executor/abstract/ContractExecutor.js +2 -3
- package/executor/abstract/DeployerExecutor.js +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +7 -6
- package/storage/BaseLazyObject.d.ts +10 -0
- package/storage/BaseLazyObject.js +21 -0
- package/storage/LazyMapping.d.ts +4 -0
- package/storage/LazyMapping.js +18 -0
- package/storage/LazyObject.d.ts +4 -0
- package/storage/LazyObject.js +14 -0
- package/storage/LazyStorageVec.d.ts +5 -0
- package/storage/LazyStorageVec.js +27 -0
- package/storage/index.d.ts +3 -0
- package/storage/index.js +3 -0
- package/types/index.d.ts +73 -11
- package/types/index.js +3 -0
- package/types/shared.d.ts +6 -18
- package/types/v4.d.ts +47 -3
- package/types/v5.d.ts +43 -4
- package/types/v6.d.ts +13 -0
- package/types/v6.js +1 -0
- package/utils/address.d.ts +30 -0
- package/utils/address.js +61 -0
- package/utils/ensure.d.ts +12 -0
- package/utils/ensure.js +59 -0
- package/utils/index.d.ts +5 -0
- package/utils/index.js +5 -0
- package/utils/proxy.d.ts +3 -0
- package/utils/proxy.js +7 -0
- package/utils/storage.d.ts +2 -0
- package/utils/storage.js +40 -0
- package/utils/types.d.ts +17 -0
- package/{utils.js → utils/types.js} +29 -30
- package/utils.d.ts +0 -13
package/cjs/types/index.js
CHANGED
|
@@ -15,3 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./shared.js"), exports);
|
|
18
|
+
__exportStar(require("./v4.js"), exports);
|
|
19
|
+
__exportStar(require("./v5.js"), exports);
|
|
20
|
+
__exportStar(require("./v6.js"), exports);
|
package/cjs/types/v6.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toEvmAddress = exports.CREATE2 = exports.CREATE1 = void 0;
|
|
4
|
+
const codecs_1 = require("@dedot/codecs");
|
|
5
|
+
const utils_1 = require("@dedot/utils");
|
|
6
|
+
const rlp_1 = require("@ethereumjs/rlp");
|
|
7
|
+
/**
|
|
8
|
+
* Calculate new contract's address is based on the deployer's address and a nonce
|
|
9
|
+
*
|
|
10
|
+
* Ref: https://github.com/paritytech/polkadot-sdk/blob/5405e473854b139f1d0735550d90687eaf1a13f9/substrate/frame/revive/src/address.rs#L197-L204
|
|
11
|
+
*
|
|
12
|
+
* @param deployer
|
|
13
|
+
* @param nonce
|
|
14
|
+
*/
|
|
15
|
+
function CREATE1(deployer, nonce) {
|
|
16
|
+
const encodedData = rlp_1.RLP.encode([new codecs_1.AccountId20(deployer).raw, (0, utils_1.toHex)(nonce)]);
|
|
17
|
+
const hash = (0, utils_1.keccakAsU8a)(encodedData);
|
|
18
|
+
return (0, utils_1.u8aToHex)(hash.subarray(12));
|
|
19
|
+
}
|
|
20
|
+
exports.CREATE1 = CREATE1;
|
|
21
|
+
/**
|
|
22
|
+
* Calculate contract's address deterministically based on the salt, deployer's address, and the code & input data.
|
|
23
|
+
*
|
|
24
|
+
* Ref: https://github.com/paritytech/polkadot-sdk/blob/5405e473854b139f1d0735550d90687eaf1a13f9/substrate/frame/revive/src/address.rs#L206-L219
|
|
25
|
+
*
|
|
26
|
+
* @param deployer
|
|
27
|
+
* @param code
|
|
28
|
+
* @param inputData Input data for the constructor call
|
|
29
|
+
* @param salt Salt for the deployment
|
|
30
|
+
*/
|
|
31
|
+
function CREATE2(deployer, code, inputData, salt) {
|
|
32
|
+
const initCodeHash = (0, utils_1.keccakAsU8a)((0, utils_1.concatU8a)((0, utils_1.toU8a)(code), (0, utils_1.toU8a)(inputData)));
|
|
33
|
+
const bytes = new Uint8Array(1 + (20 + 32 + 32)); // 0xff + deployer + salt + initCodeHash
|
|
34
|
+
bytes[0] = 0xff;
|
|
35
|
+
bytes.set((0, utils_1.hexToU8a)(new codecs_1.AccountId20(deployer).raw), 1);
|
|
36
|
+
bytes.set((0, utils_1.toU8a)(salt), 21);
|
|
37
|
+
bytes.set(initCodeHash, 53);
|
|
38
|
+
return (0, utils_1.u8aToHex)((0, utils_1.keccakAsU8a)(bytes).subarray(12));
|
|
39
|
+
}
|
|
40
|
+
exports.CREATE2 = CREATE2;
|
|
41
|
+
function isEthDerived(accountId) {
|
|
42
|
+
if (accountId.length >= 32) {
|
|
43
|
+
return accountId.slice(20, accountId.length).every((byte) => byte === 0xee);
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convert a substrate address (H256) to an evm address (H160)
|
|
49
|
+
*
|
|
50
|
+
* Ref: https://github.com/paritytech/polkadot-sdk/blob/5405e473854b139f1d0735550d90687eaf1a13f9/substrate/frame/revive/src/address.rs#L101-L113
|
|
51
|
+
*
|
|
52
|
+
* @param accountId
|
|
53
|
+
*/
|
|
54
|
+
function toEvmAddress(accountId) {
|
|
55
|
+
const accountBytes = (0, utils_1.hexToU8a)(new codecs_1.AccountId32(accountId).raw);
|
|
56
|
+
const accountBuffer = new Uint8Array(32);
|
|
57
|
+
accountBuffer.set(accountBytes.slice(0, 32));
|
|
58
|
+
if (isEthDerived(accountBytes)) {
|
|
59
|
+
// This was originally an eth address
|
|
60
|
+
// We just strip the 0xEE suffix to get the original address
|
|
61
|
+
return ('0x' + Buffer.from(accountBuffer.slice(0, 20)).toString('hex'));
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return (0, utils_1.u8aToHex)((0, utils_1.keccakAsU8a)(accountBuffer).subarray(12));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.toEvmAddress = toEvmAddress;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureSupportedContractMetadataVersion = exports.ensureValidCodeHashOrCode = exports.ensureValidContractAddress = exports.ensureContractPresence = exports.ensurePalletPresence = exports.ensureStorageApiSupports = void 0;
|
|
4
|
+
const codecs_1 = require("@dedot/codecs");
|
|
5
|
+
const utils_1 = require("@dedot/utils");
|
|
6
|
+
const ensureStorageApiSupports = (version) => {
|
|
7
|
+
const numberedVersion = typeof version === 'number' ? version : parseInt(version);
|
|
8
|
+
(0, utils_1.assert)(numberedVersion >= 5, `Contract Storage Api Only Available for metadata version >= 5, current version: ${version}`);
|
|
9
|
+
};
|
|
10
|
+
exports.ensureStorageApiSupports = ensureStorageApiSupports;
|
|
11
|
+
function ensurePalletPresence(client, registry) {
|
|
12
|
+
if (registry.isRevive()) {
|
|
13
|
+
try {
|
|
14
|
+
!!client.call.reviveApi.call.meta && !!client.tx.revive.call.meta;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
throw new utils_1.DedotError('Pallet Revive is not available');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
try {
|
|
22
|
+
!!client.call.contractsApi.call.meta && !!client.tx.contracts.call.meta;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new utils_1.DedotError('Pallet Contracts is not available');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.ensurePalletPresence = ensurePalletPresence;
|
|
30
|
+
async function ensureContractPresence(client, registry, address) {
|
|
31
|
+
const contractInfo = await (() => {
|
|
32
|
+
if (registry.isRevive()) {
|
|
33
|
+
return client.query.revive.contractInfoOf(address);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return client.query.contracts.contractInfoOf(address);
|
|
37
|
+
}
|
|
38
|
+
})();
|
|
39
|
+
(0, utils_1.ensurePresence)(contractInfo, `Contract with address ${address} does not exist on chain!`);
|
|
40
|
+
}
|
|
41
|
+
exports.ensureContractPresence = ensureContractPresence;
|
|
42
|
+
function ensureValidContractAddress(address, registry) {
|
|
43
|
+
if (registry.isRevive()) {
|
|
44
|
+
(0, utils_1.assert)((0, utils_1.isEvmAddress)(address), `Invalid contract address: ${address}. Expected an EVM 20-byte address as a hex string or a Uint8Array`);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
(0, utils_1.assert)((0, utils_1.hexToU8a)((0, codecs_1.accountId32ToHex)(address)).length === 32, `Invalid contract address: ${address}. Expected a Substrate 32-byte address as a hex string or a Uint8Array`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.ensureValidContractAddress = ensureValidContractAddress;
|
|
51
|
+
function ensureValidCodeHashOrCode(codeHashOrCode, registry) {
|
|
52
|
+
(0, utils_1.assert)((0, utils_1.toU8a)(codeHashOrCode).length === 32 || (registry.isRevive() ? (0, utils_1.isPvm)(codeHashOrCode) : (0, utils_1.isWasm)(codeHashOrCode)), 'Invalid code hash or code: expected a hash of 32-byte or a valid PVM/WASM code as a hex string or a Uint8Array');
|
|
53
|
+
}
|
|
54
|
+
exports.ensureValidCodeHashOrCode = ensureValidCodeHashOrCode;
|
|
55
|
+
const UNSUPPORTED_VERSIONS = ['V3', 'V2', 'V1'];
|
|
56
|
+
const SUPPORTED_VERSIONS = [6, 5, '4'];
|
|
57
|
+
function ensureSupportedContractMetadataVersion(metadata) {
|
|
58
|
+
// This is for V1, V2, V3
|
|
59
|
+
const unsupportedVersion = UNSUPPORTED_VERSIONS.find((o) => metadata[o]);
|
|
60
|
+
if (unsupportedVersion) {
|
|
61
|
+
throw new utils_1.DedotError(`Unsupported metadata version: ${unsupportedVersion}`);
|
|
62
|
+
}
|
|
63
|
+
// This is for V4, V5, V6
|
|
64
|
+
if (!SUPPORTED_VERSIONS.includes(metadata.version)) {
|
|
65
|
+
throw new utils_1.DedotError(`Unsupported metadata version: ${metadata.version}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.ensureSupportedContractMetadataVersion = ensureSupportedContractMetadataVersion;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types.js"), exports);
|
|
18
|
+
__exportStar(require("./proxy.js"), exports);
|
|
19
|
+
__exportStar(require("./ensure.js"), exports);
|
|
20
|
+
__exportStar(require("./address.js"), exports);
|
|
21
|
+
__exportStar(require("./storage.js"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.newProxyChain = void 0;
|
|
4
|
+
function newProxyChain(carrier) {
|
|
5
|
+
return new Proxy(carrier, {
|
|
6
|
+
get(target, property) {
|
|
7
|
+
return target.doExecute(property.toString());
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
exports.newProxyChain = newProxyChain;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findStorageRootKey = void 0;
|
|
4
|
+
function findStorageRootKey(layout, targetId) {
|
|
5
|
+
if (layout.root) {
|
|
6
|
+
if (layout.root.ty === targetId) {
|
|
7
|
+
return layout.root.root_key;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return findStorageRootKey(layout.root.layout, targetId);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
else if (layout.array) {
|
|
14
|
+
return findStorageRootKey(layout.array.layout, targetId);
|
|
15
|
+
}
|
|
16
|
+
else if (layout.enum) {
|
|
17
|
+
for (const one of Object.values(layout.enum.variants)) {
|
|
18
|
+
for (const structField of one.fields) {
|
|
19
|
+
const potentialKey = findStorageRootKey(structField.layout, targetId);
|
|
20
|
+
if (potentialKey)
|
|
21
|
+
return potentialKey;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
else if (layout.leaf) {
|
|
27
|
+
if (layout.leaf.ty === targetId) {
|
|
28
|
+
return layout.leaf.key;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (layout.struct) {
|
|
35
|
+
for (const structField of layout.struct.fields) {
|
|
36
|
+
const potentialKey = findStorageRootKey(structField.layout, targetId);
|
|
37
|
+
if (potentialKey)
|
|
38
|
+
return potentialKey;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`Layout Not Supported: ${JSON.stringify(layout)}`);
|
|
43
|
+
}
|
|
44
|
+
exports.findStorageRootKey = findStorageRootKey;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.isLazyType = exports.KnownLazyType = exports.toReturnFlags = exports.normalizeLabel = exports.normalizeContractTypeDef = exports.extractContractTypes = void 0;
|
|
4
4
|
const utils_1 = require("@dedot/utils");
|
|
5
5
|
const extractContractTypes = (contractMetadata) => {
|
|
6
6
|
const { types } = contractMetadata;
|
|
@@ -80,39 +80,6 @@ const normalizeContractTypeDef = (def) => {
|
|
|
80
80
|
return { type, value };
|
|
81
81
|
};
|
|
82
82
|
exports.normalizeContractTypeDef = normalizeContractTypeDef;
|
|
83
|
-
const UNSUPPORTED_VERSIONS = ['V3', 'V2', 'V1'];
|
|
84
|
-
const SUPPORTED_VERSIONS = [5, '4'];
|
|
85
|
-
const parseRawMetadata = (rawMetadata) => {
|
|
86
|
-
const metadata = JSON.parse(rawMetadata);
|
|
87
|
-
// This is for V1, V2, V3
|
|
88
|
-
const unsupportedVersion = UNSUPPORTED_VERSIONS.find((o) => metadata[o]);
|
|
89
|
-
if (unsupportedVersion) {
|
|
90
|
-
throw new Error(`Unsupported metadata version: ${unsupportedVersion}`);
|
|
91
|
-
}
|
|
92
|
-
// This is for V4, V5
|
|
93
|
-
if (!SUPPORTED_VERSIONS.includes(metadata.version)) {
|
|
94
|
-
throw new Error(`Unsupported metadata version: ${metadata.version}`);
|
|
95
|
-
}
|
|
96
|
-
return metadata;
|
|
97
|
-
};
|
|
98
|
-
exports.parseRawMetadata = parseRawMetadata;
|
|
99
|
-
function newProxyChain(carrier) {
|
|
100
|
-
return new Proxy(carrier, {
|
|
101
|
-
get(target, property) {
|
|
102
|
-
return target.doExecute(property.toString());
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
exports.newProxyChain = newProxyChain;
|
|
107
|
-
function ensureSupportContractsPallet(client) {
|
|
108
|
-
try {
|
|
109
|
-
!!client.call.contractsApi.call.meta && !!client.tx.contracts.call.meta;
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
throw new Error('Contracts pallet is not available');
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.ensureSupportContractsPallet = ensureSupportContractsPallet;
|
|
116
83
|
function normalizeLabel(label) {
|
|
117
84
|
if (!label)
|
|
118
85
|
return '';
|
|
@@ -128,3 +95,33 @@ function toReturnFlags(bits) {
|
|
|
128
95
|
};
|
|
129
96
|
}
|
|
130
97
|
exports.toReturnFlags = toReturnFlags;
|
|
98
|
+
const KNOWN_LAZY_TYPES = {
|
|
99
|
+
LAZY: ['ink_storage', 'lazy', 'Lazy'].join('::'),
|
|
100
|
+
MAPPING: ['ink_storage', 'lazy', 'mapping', 'Mapping'].join('::'),
|
|
101
|
+
STORAGE_VEC: ['ink_storage', 'lazy', 'vec', 'StorageVec'].join('::'),
|
|
102
|
+
};
|
|
103
|
+
var KnownLazyType;
|
|
104
|
+
(function (KnownLazyType) {
|
|
105
|
+
KnownLazyType["LAZY"] = "LAZY";
|
|
106
|
+
KnownLazyType["MAPPING"] = "MAPPING";
|
|
107
|
+
KnownLazyType["STORAGE_VEC"] = "STORAGE_VEC";
|
|
108
|
+
})(KnownLazyType || (exports.KnownLazyType = KnownLazyType = {}));
|
|
109
|
+
/**
|
|
110
|
+
* Check if a type is lazy/non-packed storage
|
|
111
|
+
*
|
|
112
|
+
* @param typePath
|
|
113
|
+
*/
|
|
114
|
+
function isLazyType(typePath) {
|
|
115
|
+
if (!typePath)
|
|
116
|
+
return;
|
|
117
|
+
if (Array.isArray(typePath)) {
|
|
118
|
+
typePath = typePath.join('::');
|
|
119
|
+
}
|
|
120
|
+
if (typePath === KNOWN_LAZY_TYPES.LAZY)
|
|
121
|
+
return KnownLazyType.LAZY;
|
|
122
|
+
if (typePath === KNOWN_LAZY_TYPES.MAPPING)
|
|
123
|
+
return KnownLazyType.MAPPING;
|
|
124
|
+
if (typePath === KNOWN_LAZY_TYPES.STORAGE_VEC)
|
|
125
|
+
return KnownLazyType.STORAGE_VEC;
|
|
126
|
+
}
|
|
127
|
+
exports.isLazyType = isLazyType;
|
package/errors.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toReturnFlags } from './utils.js';
|
|
1
|
+
import { assert, DedotError } from '@dedot/utils';
|
|
2
|
+
import { toReturnFlags } from './utils/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Represents an error that occurred during the instantiation of a smart contract.
|
|
5
5
|
* This class extends the base `DedotError` and includes a `raw` property of type `ContractInstantiateResult`.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { assert, assertFalse, concatU8a, hexToU8a,
|
|
1
|
+
import { assert, assertFalse, concatU8a, hexToU8a, isPvm, isUndefined, isWasm, toHex, toU8a, u8aToHex, } from '@dedot/utils';
|
|
2
2
|
import { ContractInstantiateDispatchError, ContractInstantiateLangError } from '../errors.js';
|
|
3
|
-
import { toReturnFlags } from '../utils.js';
|
|
3
|
+
import { toReturnFlags } from '../utils/index.js';
|
|
4
4
|
import { DeployerExecutor } from './abstract/index.js';
|
|
5
5
|
export class ConstructorQueryExecutor extends DeployerExecutor {
|
|
6
6
|
doExecute(constructor) {
|
|
@@ -11,16 +11,55 @@ export class ConstructorQueryExecutor extends DeployerExecutor {
|
|
|
11
11
|
assertFalse(params.length < args.length, `Expected at least ${args.length} arguments, got ${params.length}`);
|
|
12
12
|
assertFalse(params.length > args.length + 1, `Expected at most ${args.length + 1} arguments, got ${params.length}`);
|
|
13
13
|
const callOptions = (params[args.length] || {});
|
|
14
|
-
const { caller = this.options.defaultCaller,
|
|
14
|
+
const { caller = this.options.defaultCaller, // --
|
|
15
|
+
value = 0n, gasLimit, storageDepositLimit, salt, } = callOptions;
|
|
15
16
|
assert(caller, 'Expected a valid caller address in ConstructorCallOptions');
|
|
16
|
-
assertFalse(isNull(salt) || isUndefined(salt), 'Expected a salt in ConstructorCallOptions');
|
|
17
17
|
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
18
18
|
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
19
|
+
const isUpload = isPvm(this.code) || isWasm(this.code);
|
|
19
20
|
const code = {
|
|
20
|
-
type:
|
|
21
|
+
type: isUpload ? 'Upload' : 'Existing',
|
|
21
22
|
value: this.code,
|
|
22
23
|
};
|
|
23
|
-
const
|
|
24
|
+
const client = this.client;
|
|
25
|
+
const raw = await (async () => {
|
|
26
|
+
if (this.registry.isRevive()) {
|
|
27
|
+
assert(isUndefined(salt) || toU8a(salt).byteLength == 32, 'Invalid salt provided in ConstructorCallOptions: expected a 32-byte value as a hex string or a Uint8Array');
|
|
28
|
+
const raw = await client.call.reviveApi.instantiate(caller, // --
|
|
29
|
+
value, gasLimit, storageDepositLimit, code, bytes, salt ? toHex(salt) : undefined);
|
|
30
|
+
const result = raw.result;
|
|
31
|
+
if (result.isOk) {
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
result.value.address = result.value.addr;
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
delete result.value.addr;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
gasConsumed: raw.gasConsumed,
|
|
39
|
+
gasRequired: raw.gasRequired,
|
|
40
|
+
storageDeposit: raw.storageDeposit,
|
|
41
|
+
result,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const raw = await client.call.contractsApi.instantiate(caller, // --
|
|
46
|
+
value, gasLimit, storageDepositLimit, code, bytes, salt || '0x');
|
|
47
|
+
const result = raw.result;
|
|
48
|
+
if (result.isOk) {
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
result.value.address = result.value.accountId.address();
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
delete result.value.accountId;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
gasConsumed: raw.gasConsumed,
|
|
56
|
+
gasRequired: raw.gasRequired,
|
|
57
|
+
storageDeposit: raw.storageDeposit,
|
|
58
|
+
debugMessage: raw.debugMessage,
|
|
59
|
+
result,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
24
63
|
if (raw.result.isErr) {
|
|
25
64
|
throw new ContractInstantiateDispatchError(raw.result.err, raw);
|
|
26
65
|
}
|
|
@@ -32,8 +71,9 @@ export class ConstructorQueryExecutor extends DeployerExecutor {
|
|
|
32
71
|
return {
|
|
33
72
|
data: data.value,
|
|
34
73
|
raw,
|
|
35
|
-
address: raw.result.value.
|
|
74
|
+
address: raw.result.value.address,
|
|
36
75
|
flags: toReturnFlags(bits),
|
|
76
|
+
inputData: bytes,
|
|
37
77
|
};
|
|
38
78
|
};
|
|
39
79
|
callFn.meta = meta;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assert,
|
|
1
|
+
import { assert, concatU8a, hexToU8a, isPvm, isUndefined, isWasm, toHex, toU8a, u8aToHex } from '@dedot/utils';
|
|
2
2
|
import { DeployerExecutor } from './abstract/index.js';
|
|
3
3
|
export class ConstructorTxExecutor extends DeployerExecutor {
|
|
4
4
|
doExecute(constructor) {
|
|
@@ -8,17 +8,28 @@ export class ConstructorTxExecutor extends DeployerExecutor {
|
|
|
8
8
|
const { args } = meta;
|
|
9
9
|
assert(params.length === args.length + 1, `Expected ${args.length + 1} arguments, got ${params.length}`);
|
|
10
10
|
const txCallOptions = params[args.length];
|
|
11
|
-
const { value = 0n, gasLimit, storageDepositLimit, salt
|
|
11
|
+
const { value = 0n, gasLimit, storageDepositLimit, salt } = txCallOptions;
|
|
12
12
|
assert(gasLimit, 'Expected a gas limit in ConstructorTxOptions');
|
|
13
|
-
assertFalse(isNull(salt) || isUndefined(salt), 'Expected a salt in ConstructorCallOptions');
|
|
14
13
|
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
15
14
|
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const client = this.client;
|
|
16
|
+
if (this.registry.isRevive()) {
|
|
17
|
+
assert(isUndefined(salt) || toU8a(salt).byteLength == 32, 'Invalid salt provided in ConstructorCallOptions: expected a 32-byte value as a hex string or a Uint8Array');
|
|
18
|
+
assert(!isUndefined(storageDepositLimit), 'Expected a storage deposit limit in ConstructorTxOptions');
|
|
19
|
+
if (isPvm(this.code)) {
|
|
20
|
+
return client.tx.revive.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt ? toHex(salt) : undefined);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return client.tx.revive.instantiate(value, gasLimit, storageDepositLimit, toHex(this.code), bytes, salt ? toHex(salt) : undefined);
|
|
24
|
+
}
|
|
18
25
|
}
|
|
19
26
|
else {
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
if (isWasm(this.code)) {
|
|
28
|
+
return client.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt || '0x');
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return client.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, toHex(this.code), bytes, salt || '0x');
|
|
32
|
+
}
|
|
22
33
|
}
|
|
23
34
|
};
|
|
24
35
|
callFn.meta = meta;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assert, assertFalse, concatU8a, hexToU8a, u8aToHex } from '@dedot/utils';
|
|
2
2
|
import { ContractDispatchError, ContractLangError } from '../errors.js';
|
|
3
|
-
import { toReturnFlags } from '../utils.js';
|
|
3
|
+
import { ensureContractPresence, toReturnFlags } from '../utils/index.js';
|
|
4
4
|
import { ContractExecutor } from './abstract/index.js';
|
|
5
5
|
export class QueryExecutor extends ContractExecutor {
|
|
6
6
|
doExecute(message) {
|
|
@@ -15,7 +15,31 @@ export class QueryExecutor extends ContractExecutor {
|
|
|
15
15
|
assert(caller, 'Expected a valid caller address in ContractCallOptions');
|
|
16
16
|
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
17
17
|
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
18
|
-
const
|
|
18
|
+
const client = this.client;
|
|
19
|
+
await ensureContractPresence(client, this.registry, this.address);
|
|
20
|
+
const raw = await (async () => {
|
|
21
|
+
if (this.registry.isRevive()) {
|
|
22
|
+
const raw = await client.call.reviveApi.call(caller, // --
|
|
23
|
+
this.address, value, gasLimit, storageDepositLimit, bytes);
|
|
24
|
+
return {
|
|
25
|
+
gasConsumed: raw.gasConsumed,
|
|
26
|
+
gasRequired: raw.gasRequired,
|
|
27
|
+
storageDeposit: raw.storageDeposit,
|
|
28
|
+
result: raw.result,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const raw = await client.call.contractsApi.call(caller, // --
|
|
33
|
+
this.address, value, gasLimit, storageDepositLimit, bytes);
|
|
34
|
+
return {
|
|
35
|
+
gasConsumed: raw.gasConsumed,
|
|
36
|
+
gasRequired: raw.gasRequired,
|
|
37
|
+
storageDeposit: raw.storageDeposit,
|
|
38
|
+
debugMessage: raw.debugMessage,
|
|
39
|
+
result: raw.result,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
19
43
|
if (raw.result.isErr) {
|
|
20
44
|
throw new ContractDispatchError(raw.result.err, raw);
|
|
21
45
|
}
|
|
@@ -28,6 +52,7 @@ export class QueryExecutor extends ContractExecutor {
|
|
|
28
52
|
data: data.value,
|
|
29
53
|
raw,
|
|
30
54
|
flags: toReturnFlags(bits),
|
|
55
|
+
inputData: bytes,
|
|
31
56
|
};
|
|
32
57
|
};
|
|
33
58
|
callFn.meta = meta;
|
package/executor/TxExecutor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assert, concatU8a, hexToU8a, u8aToHex } from '@dedot/utils';
|
|
1
|
+
import { assert, concatU8a, hexToU8a, isUndefined, u8aToHex } from '@dedot/utils';
|
|
2
2
|
import { ContractExecutor } from './abstract/index.js';
|
|
3
3
|
export class TxExecutor extends ContractExecutor {
|
|
4
4
|
doExecute(message) {
|
|
@@ -12,7 +12,16 @@ export class TxExecutor extends ContractExecutor {
|
|
|
12
12
|
assert(gasLimit, 'Expected a gas limit in ContractTxOptions');
|
|
13
13
|
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
14
14
|
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
15
|
-
|
|
15
|
+
const client = this.client;
|
|
16
|
+
if (this.registry.isRevive()) {
|
|
17
|
+
assert(!isUndefined(storageDepositLimit), 'Expected a storage deposit limit in ContractTxOptions');
|
|
18
|
+
return client.tx.revive.call(this.address, // --
|
|
19
|
+
value, gasLimit, storageDepositLimit || 0n, bytes);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return client.tx.contracts.call(this.address, // --
|
|
23
|
+
value, gasLimit, storageDepositLimit, bytes);
|
|
24
|
+
}
|
|
16
25
|
};
|
|
17
26
|
callFn.meta = meta;
|
|
18
27
|
return callFn;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { ISubstrateClient } from '@dedot/api';
|
|
2
|
-
import { AccountId32, AccountId32Like } from '@dedot/codecs';
|
|
3
2
|
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
3
|
import { TypinkRegistry } from '../../TypinkRegistry.js';
|
|
5
|
-
import { ContractCallMessage, ExecutionOptions } from '../../types/index.js';
|
|
4
|
+
import { ContractAddress, ContractCallMessage, ExecutionOptions } from '../../types/index.js';
|
|
6
5
|
import { Executor } from './Executor.js';
|
|
7
6
|
export declare abstract class ContractExecutor<ChainApi extends GenericSubstrateApi> extends Executor<ChainApi> {
|
|
8
|
-
readonly address:
|
|
9
|
-
constructor(client: ISubstrateClient<ChainApi>, registry: TypinkRegistry, address:
|
|
7
|
+
readonly address: ContractAddress;
|
|
8
|
+
constructor(client: ISubstrateClient<ChainApi>, registry: TypinkRegistry, address: ContractAddress, options?: ExecutionOptions);
|
|
10
9
|
protected findMessage(message: string): ContractCallMessage | undefined;
|
|
11
10
|
protected findTxMessage(message: string): ContractCallMessage | undefined;
|
|
12
11
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { normalizeLabel } from '../../utils.js';
|
|
1
|
+
import { normalizeLabel } from '../../utils/index.js';
|
|
3
2
|
import { Executor } from './Executor.js';
|
|
4
3
|
export class ContractExecutor extends Executor {
|
|
5
4
|
address;
|
|
6
5
|
constructor(client, registry, address, options) {
|
|
7
6
|
super(client, registry, options);
|
|
8
|
-
this.address =
|
|
7
|
+
this.address = address;
|
|
9
8
|
}
|
|
10
9
|
findMessage(message) {
|
|
11
10
|
return this.metadata.spec.messages.find((one) => normalizeLabel(one.label) === message);
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/contracts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Delightful ink! Contract APIs",
|
|
5
5
|
"author": "Tung Vu <tung@dedot.dev>",
|
|
6
6
|
"homepage": "https://dedot.dev",
|
|
@@ -18,10 +18,11 @@
|
|
|
18
18
|
"test": "npx vitest --watch=false"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dedot/api": "0.
|
|
22
|
-
"@dedot/codecs": "0.
|
|
23
|
-
"@dedot/types": "0.
|
|
24
|
-
"@dedot/utils": "0.
|
|
21
|
+
"@dedot/api": "0.13.0",
|
|
22
|
+
"@dedot/codecs": "0.13.0",
|
|
23
|
+
"@dedot/types": "0.13.0",
|
|
24
|
+
"@dedot/utils": "0.13.0",
|
|
25
|
+
"@ethereumjs/rlp": "^10.0.0"
|
|
25
26
|
},
|
|
26
27
|
"publishConfig": {
|
|
27
28
|
"access": "public",
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
"node": ">=18"
|
|
32
33
|
},
|
|
33
34
|
"license": "Apache-2.0",
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "b9193d170fc63ebd64b75f37f8276a7159e2af55",
|
|
35
36
|
"module": "./index.js",
|
|
36
37
|
"types": "./index.d.ts",
|
|
37
38
|
"exports": {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HexString } from '@dedot/utils';
|
|
2
|
+
import { TypinkRegistry } from 'src/TypinkRegistry';
|
|
3
|
+
import { ContractType } from 'src/types';
|
|
4
|
+
export declare abstract class BaseLazyObject {
|
|
5
|
+
readonly typeDef: ContractType;
|
|
6
|
+
readonly registry: TypinkRegistry;
|
|
7
|
+
constructor(typeDef: ContractType, registry: TypinkRegistry);
|
|
8
|
+
findStorageRootLayout(typeId: number): HexString;
|
|
9
|
+
getContractStorage(key: Uint8Array | HexString): Promise<HexString | undefined>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { assert, toU8a } from '@dedot/utils';
|
|
2
|
+
import { findStorageRootKey } from '../utils/index.js';
|
|
3
|
+
export class BaseLazyObject {
|
|
4
|
+
typeDef;
|
|
5
|
+
registry;
|
|
6
|
+
constructor(typeDef, registry) {
|
|
7
|
+
this.typeDef = typeDef;
|
|
8
|
+
this.registry = registry;
|
|
9
|
+
}
|
|
10
|
+
findStorageRootLayout(typeId) {
|
|
11
|
+
const rootLayout = this.registry.metadata.storage;
|
|
12
|
+
const rootKey = findStorageRootKey(rootLayout, typeId);
|
|
13
|
+
assert(rootKey, 'Root Key Not Found');
|
|
14
|
+
return rootKey;
|
|
15
|
+
}
|
|
16
|
+
getContractStorage(key) {
|
|
17
|
+
const { getStorage } = this.registry.options || {};
|
|
18
|
+
assert(getStorage, 'getStorage is not available');
|
|
19
|
+
return getStorage(toU8a(key));
|
|
20
|
+
}
|
|
21
|
+
}
|