@dedot/contracts 0.12.1-next.f19f185a.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.
Files changed (71) hide show
  1. package/Contract.d.ts +4 -4
  2. package/Contract.js +41 -6
  3. package/ContractDeployer.d.ts +2 -2
  4. package/ContractDeployer.js +8 -6
  5. package/TypinkRegistry.d.ts +29 -6
  6. package/TypinkRegistry.js +144 -24
  7. package/cjs/Contract.js +44 -9
  8. package/cjs/ContractDeployer.js +10 -8
  9. package/cjs/TypinkRegistry.js +144 -24
  10. package/cjs/errors.js +3 -3
  11. package/cjs/executor/ConstructorQueryExecutor.js +49 -9
  12. package/cjs/executor/ConstructorTxExecutor.js +17 -6
  13. package/cjs/executor/QueryExecutor.js +30 -5
  14. package/cjs/executor/TxExecutor.js +10 -1
  15. package/cjs/executor/abstract/ContractExecutor.js +4 -5
  16. package/cjs/executor/abstract/DeployerExecutor.js +2 -2
  17. package/cjs/index.js +1 -1
  18. package/cjs/storage/BaseLazyObject.js +25 -0
  19. package/cjs/storage/LazyMapping.js +22 -0
  20. package/cjs/storage/LazyObject.js +18 -0
  21. package/cjs/storage/LazyStorageVec.js +54 -0
  22. package/cjs/storage/index.js +19 -0
  23. package/cjs/types/index.js +3 -0
  24. package/cjs/types/v6.js +2 -0
  25. package/cjs/utils/address.js +67 -0
  26. package/cjs/utils/ensure.js +68 -0
  27. package/cjs/utils/index.js +21 -0
  28. package/cjs/utils/proxy.js +11 -0
  29. package/cjs/utils/storage.js +44 -0
  30. package/cjs/{utils.js → utils/types.js} +31 -34
  31. package/errors.js +2 -2
  32. package/executor/ConstructorQueryExecutor.js +47 -7
  33. package/executor/ConstructorTxExecutor.js +18 -7
  34. package/executor/QueryExecutor.js +27 -2
  35. package/executor/TxExecutor.js +11 -2
  36. package/executor/abstract/ContractExecutor.d.ts +3 -4
  37. package/executor/abstract/ContractExecutor.js +2 -3
  38. package/executor/abstract/DeployerExecutor.js +1 -1
  39. package/index.d.ts +1 -1
  40. package/index.js +1 -1
  41. package/package.json +7 -6
  42. package/storage/BaseLazyObject.d.ts +10 -0
  43. package/storage/BaseLazyObject.js +21 -0
  44. package/storage/LazyMapping.d.ts +4 -0
  45. package/storage/LazyMapping.js +18 -0
  46. package/storage/LazyObject.d.ts +4 -0
  47. package/storage/LazyObject.js +14 -0
  48. package/storage/LazyStorageVec.d.ts +5 -0
  49. package/storage/LazyStorageVec.js +27 -0
  50. package/storage/index.d.ts +3 -0
  51. package/storage/index.js +3 -0
  52. package/types/index.d.ts +73 -11
  53. package/types/index.js +3 -0
  54. package/types/shared.d.ts +6 -18
  55. package/types/v4.d.ts +47 -3
  56. package/types/v5.d.ts +43 -4
  57. package/types/v6.d.ts +13 -0
  58. package/types/v6.js +1 -0
  59. package/utils/address.d.ts +30 -0
  60. package/utils/address.js +61 -0
  61. package/utils/ensure.d.ts +12 -0
  62. package/utils/ensure.js +59 -0
  63. package/utils/index.d.ts +5 -0
  64. package/utils/index.js +5 -0
  65. package/utils/proxy.d.ts +3 -0
  66. package/utils/proxy.js +7 -0
  67. package/utils/storage.d.ts +2 -0
  68. package/utils/storage.js +40 -0
  69. package/utils/types.d.ts +17 -0
  70. package/{utils.js → utils/types.js} +29 -30
  71. package/utils.d.ts +0 -13
package/Contract.d.ts CHANGED
@@ -1,19 +1,19 @@
1
1
  import { ISubstrateClient } from '@dedot/api';
2
- import { AccountId32, AccountId32Like } from '@dedot/codecs';
3
2
  import { IEventRecord } from '@dedot/types';
4
3
  import { TypinkRegistry } from './TypinkRegistry.js';
5
- import { ContractEvent, ContractMetadata, GenericContractApi, ExecutionOptions } from './types/index.js';
4
+ import { ContractAddress, ContractEvent, ContractMetadata, ExecutionOptions, GenericContractApi, LooseContractMetadata } from './types/index.js';
6
5
  export declare class Contract<ContractApi extends GenericContractApi = GenericContractApi> {
7
6
  #private;
8
7
  readonly client: ISubstrateClient<ContractApi['types']['ChainApi']>;
9
- constructor(client: ISubstrateClient<ContractApi['types']['ChainApi']>, metadata: ContractMetadata | string, address: AccountId32Like, options?: ExecutionOptions);
8
+ constructor(client: ISubstrateClient<ContractApi['types']['ChainApi']>, metadata: LooseContractMetadata | string, address: ContractAddress, options?: ExecutionOptions);
10
9
  decodeEvent(eventRecord: IEventRecord): ContractEvent;
11
10
  decodeEvents(eventRecords: IEventRecord[]): ContractEvent[];
12
11
  get metadata(): ContractMetadata;
13
- get address(): AccountId32;
12
+ get address(): ContractAddress;
14
13
  get registry(): TypinkRegistry;
15
14
  get query(): ContractApi['query'];
16
15
  get tx(): ContractApi['tx'];
17
16
  get events(): ContractApi['events'];
18
17
  get options(): ExecutionOptions | undefined;
18
+ get storage(): ContractApi['storage'];
19
19
  }
package/Contract.js CHANGED
@@ -1,7 +1,7 @@
1
- import { AccountId32 } from '@dedot/codecs';
1
+ import { DedotError, toHex, toU8a } from '@dedot/utils';
2
2
  import { TypinkRegistry } from './TypinkRegistry.js';
3
3
  import { EventExecutor, QueryExecutor, TxExecutor } from './executor/index.js';
4
- import { ensureSupportContractsPallet, newProxyChain, parseRawMetadata } from './utils.js';
4
+ import { ensurePalletPresence, ensureStorageApiSupports, ensureSupportedContractMetadataVersion, ensureValidContractAddress, newProxyChain, } from './utils/index.js';
5
5
  export class Contract {
6
6
  client;
7
7
  #registry;
@@ -10,10 +10,13 @@ export class Contract {
10
10
  #options;
11
11
  constructor(client, metadata, address, options) {
12
12
  this.client = client;
13
- ensureSupportContractsPallet(client);
14
- this.#address = new AccountId32(address);
15
- this.#metadata = typeof metadata === 'string' ? parseRawMetadata(metadata) : metadata;
16
- this.#registry = new TypinkRegistry(this.#metadata);
13
+ this.#metadata = (typeof metadata === 'string' ? JSON.parse(metadata) : metadata);
14
+ ensureSupportedContractMetadataVersion(this.metadata);
15
+ const getStorage = this.#getStorage.bind(this);
16
+ this.#registry = new TypinkRegistry(this.metadata, { getStorage });
17
+ ensurePalletPresence(client, this.registry);
18
+ ensureValidContractAddress(address, this.registry);
19
+ this.#address = address;
17
20
  this.#options = options;
18
21
  }
19
22
  decodeEvent(eventRecord) {
@@ -43,4 +46,36 @@ export class Contract {
43
46
  get options() {
44
47
  return this.#options;
45
48
  }
49
+ get storage() {
50
+ return {
51
+ root: async () => {
52
+ ensureStorageApiSupports(this.metadata.version);
53
+ const { ty, root_key } = this.metadata.storage.root;
54
+ const rawValue = await this.#getStorage(root_key);
55
+ return this.registry.findCodec(ty).tryDecode(rawValue);
56
+ },
57
+ lazy: () => {
58
+ ensureStorageApiSupports(this.metadata.version);
59
+ const { ty } = this.metadata.storage.root;
60
+ const $lazyCodec = this.registry.createLazyCodec(ty);
61
+ return $lazyCodec ? $lazyCodec.tryDecode('0x') : {};
62
+ },
63
+ };
64
+ }
65
+ #getStorage = async (key) => {
66
+ const result = await (async () => {
67
+ if (this.registry.isRevive()) {
68
+ return await this.client.call.reviveApi.getStorageVarKey(this.address, //--
69
+ toHex(key));
70
+ }
71
+ else {
72
+ return await this.client.call.contractsApi.getStorage(this.address, //--
73
+ toU8a(key));
74
+ }
75
+ })();
76
+ if (result.isOk) {
77
+ return result.value;
78
+ }
79
+ throw new DedotError(result.err);
80
+ };
46
81
  }
@@ -1,11 +1,11 @@
1
1
  import { ISubstrateClient } from '@dedot/api';
2
2
  import { Hash } from '@dedot/codecs';
3
3
  import { TypinkRegistry } from './TypinkRegistry.js';
4
- import { ContractMetadata, GenericContractApi, ExecutionOptions } from './types/index.js';
4
+ import { ContractMetadata, ExecutionOptions, GenericContractApi, LooseContractMetadata } from './types/index.js';
5
5
  export declare class ContractDeployer<ContractApi extends GenericContractApi = GenericContractApi> {
6
6
  #private;
7
7
  readonly client: ISubstrateClient<ContractApi['types']['ChainApi']>;
8
- constructor(client: ISubstrateClient<ContractApi['types']['ChainApi']>, metadata: ContractMetadata | string, codeHashOrWasm: Hash | Uint8Array | string, options?: ExecutionOptions);
8
+ constructor(client: ISubstrateClient<ContractApi['types']['ChainApi']>, metadata: LooseContractMetadata | string, codeHashOrCode: Hash | Uint8Array | string, options?: ExecutionOptions);
9
9
  get metadata(): ContractMetadata;
10
10
  get registry(): TypinkRegistry;
11
11
  get tx(): ContractApi['constructorTx'];
@@ -1,19 +1,21 @@
1
1
  import { TypinkRegistry } from './TypinkRegistry.js';
2
2
  import { ConstructorQueryExecutor } from './executor/ConstructorQueryExecutor.js';
3
3
  import { ConstructorTxExecutor } from './executor/index.js';
4
- import { ensureSupportContractsPallet, newProxyChain, parseRawMetadata } from './utils.js';
4
+ import { ensurePalletPresence, ensureSupportedContractMetadataVersion, ensureValidCodeHashOrCode, newProxyChain, } from './utils/index.js';
5
5
  export class ContractDeployer {
6
6
  client;
7
7
  #metadata;
8
8
  #registry;
9
9
  #code;
10
10
  #options;
11
- constructor(client, metadata, codeHashOrWasm, options) {
11
+ constructor(client, metadata, codeHashOrCode, options) {
12
12
  this.client = client;
13
- ensureSupportContractsPallet(client);
14
- this.#metadata = typeof metadata === 'string' ? parseRawMetadata(metadata) : metadata;
15
- this.#registry = new TypinkRegistry(this.#metadata);
16
- this.#code = codeHashOrWasm;
13
+ this.#metadata = (typeof metadata === 'string' ? JSON.parse(metadata) : metadata);
14
+ ensureSupportedContractMetadataVersion(this.metadata);
15
+ this.#registry = new TypinkRegistry(this.metadata);
16
+ ensurePalletPresence(client, this.registry);
17
+ ensureValidCodeHashOrCode(codeHashOrCode, this.registry);
18
+ this.#code = codeHashOrCode;
17
19
  this.#options = options;
18
20
  }
19
21
  get metadata() {
@@ -1,10 +1,33 @@
1
- import { AccountId32Like, TypeRegistry } from '@dedot/codecs';
1
+ import { TypeId, TypeRegistry } from '@dedot/codecs';
2
+ import * as $ from '@dedot/shape';
2
3
  import { IEventRecord } from '@dedot/types';
3
- import { ContractEvent, ContractMetadata } from './types/index.js';
4
+ import { HexString } from '@dedot/utils';
5
+ import { ContractAddress, ContractEvent, ContractMetadata } from './types/index.js';
6
+ export interface TypinkRegistryOptions {
7
+ /**
8
+ * Get raw contract storage value given a key
9
+ *
10
+ * @param key
11
+ */
12
+ getStorage?: (key: Uint8Array | HexString) => Promise<HexString | undefined>;
13
+ }
4
14
  export declare class TypinkRegistry extends TypeRegistry {
5
15
  #private;
6
- constructor(metadata: ContractMetadata);
7
- get metadata(): ContractMetadata;
8
- decodeEvents(records: IEventRecord[], contract?: AccountId32Like): ContractEvent[];
9
- decodeEvent(eventRecord: IEventRecord, contract?: AccountId32Like): ContractEvent;
16
+ readonly metadata: ContractMetadata;
17
+ readonly options?: TypinkRegistryOptions | undefined;
18
+ constructor(metadata: ContractMetadata, options?: TypinkRegistryOptions | undefined);
19
+ findCodec<I = unknown, O = I>(typeId: TypeId): $.Shape<I, O>;
20
+ /**
21
+ * Creates a lazy codec for a given type ID, extracting only storage types that is lazy/non-packed storage.
22
+ *
23
+ * @param typeId The type ID to create a lazy codec for
24
+ * @returns A shape codec containing only lazy/non-packed storage fields, or null if no lazy fields exist
25
+ */
26
+ createLazyCodec(typeId: TypeId): $.AnyShape | null;
27
+ decodeEvents(records: IEventRecord[], contract?: ContractAddress): ContractEvent[];
28
+ decodeEvent(eventRecord: IEventRecord, contract?: ContractAddress): ContractEvent;
29
+ /**
30
+ * Check if the contract is Revive Compatible (ink!v6)
31
+ */
32
+ isRevive(): boolean;
10
33
  }
package/TypinkRegistry.js CHANGED
@@ -1,15 +1,99 @@
1
1
  import { AccountId32, TypeRegistry } from '@dedot/codecs';
2
2
  import * as $ from '@dedot/shape';
3
- import { DedotError, assert, hexToU8a, stringCamelCase, stringPascalCase } from '@dedot/utils';
4
- import { extractContractTypes } from './utils.js';
3
+ import { assert, DedotError, hexToU8a, stringCamelCase, stringPascalCase } from '@dedot/utils';
4
+ import { LazyMapping, LazyObject, LazyStorageVec } from './storage/index.js';
5
+ import { ensureSupportedContractMetadataVersion, extractContractTypes, isLazyType, KnownLazyType, } from './utils/index.js';
5
6
  export class TypinkRegistry extends TypeRegistry {
6
- #metadata;
7
- constructor(metadata) {
7
+ metadata;
8
+ options;
9
+ constructor(metadata, options) {
10
+ ensureSupportedContractMetadataVersion(metadata);
8
11
  super(extractContractTypes(metadata));
9
- this.#metadata = metadata;
12
+ this.metadata = metadata;
13
+ this.options = options;
10
14
  }
11
- get metadata() {
12
- return this.#metadata;
15
+ findCodec(typeId) {
16
+ const types = this.metadata.types;
17
+ const typeDef = types.find(({ id }) => id == typeId);
18
+ const $codec = super.findCodec(typeId);
19
+ const lazyType = isLazyType(typeDef.type.path);
20
+ if (lazyType === KnownLazyType.MAPPING) {
21
+ return this.#createLazyCodec(LazyMapping, $codec, typeDef);
22
+ }
23
+ else if (lazyType === KnownLazyType.LAZY) {
24
+ return this.#createLazyCodec(LazyObject, $codec, typeDef);
25
+ }
26
+ else if (lazyType === KnownLazyType.STORAGE_VEC) {
27
+ return this.#createLazyCodec(LazyStorageVec, $codec, typeDef);
28
+ }
29
+ return $codec;
30
+ }
31
+ /**
32
+ * Creates a lazy codec for a given type ID, extracting only storage types that is lazy/non-packed storage.
33
+ *
34
+ * @param typeId The type ID to create a lazy codec for
35
+ * @returns A shape codec containing only lazy/non-packed storage fields, or null if no lazy fields exist
36
+ */
37
+ createLazyCodec(typeId) {
38
+ const typeDef = this.findType(typeId);
39
+ // Check if this is a lazy storage type we want to keep
40
+ const lazyType = isLazyType(typeDef.path);
41
+ if (lazyType)
42
+ return this.findCodec(typeId);
43
+ // For non-lazy types, we need to recursively process the structure
44
+ const { typeDef: def } = typeDef;
45
+ // We only support Struct type for now.
46
+ if (def.type === 'Struct') {
47
+ const { fields } = def.value;
48
+ if (fields.length === 0) {
49
+ return null;
50
+ }
51
+ // Handle tuple-like structs (fields with undefined names)
52
+ if (fields[0].name === undefined) {
53
+ if (fields.length === 1) {
54
+ // Single unnamed field - check if it contains lazy types
55
+ return this.createLazyCodec(fields[0].typeId);
56
+ }
57
+ else {
58
+ return null; // Unsupported Lazy Codec Structure
59
+ }
60
+ }
61
+ else {
62
+ // Named struct fields - create struct with only lazy fields
63
+ const lazyFields = {};
64
+ let hasLazyFields = false;
65
+ for (const field of fields) {
66
+ // Skip fields with undefined names in named structs
67
+ if (field.name === undefined)
68
+ continue;
69
+ // Recursively check if this field contains lazy types
70
+ const fieldCodec = this.createLazyCodec(field.typeId);
71
+ // Only include fields that have lazy types
72
+ if (fieldCodec) {
73
+ lazyFields[field.name] = fieldCodec;
74
+ hasLazyFields = true;
75
+ }
76
+ }
77
+ // If no lazy fields, return null
78
+ if (!hasLazyFields) {
79
+ return null;
80
+ }
81
+ return $.Struct(lazyFields);
82
+ }
83
+ }
84
+ return null;
85
+ }
86
+ #enhanceLazyCodec($codec, typeDef) {
87
+ const $Codec = $.Tuple($codec);
88
+ // @ts-ignore
89
+ $Codec.subDecode = () => {
90
+ return [typeDef, this];
91
+ };
92
+ return $Codec;
93
+ }
94
+ #createLazyCodec(LazyClazz, $codec, typeDef) {
95
+ // @ts-ignore
96
+ return $.instance(LazyClazz, this.#enhanceLazyCodec($codec, typeDef), () => ({}));
13
97
  }
14
98
  decodeEvents(records, contract) {
15
99
  return records
@@ -18,27 +102,35 @@ export class TypinkRegistry extends TypeRegistry {
18
102
  }
19
103
  decodeEvent(eventRecord, contract) {
20
104
  assert(this.#isContractEmittedEvent(eventRecord.event, contract), 'Invalid ContractEmitted Event');
21
- const { version } = this.#metadata;
105
+ const { version } = this.metadata;
22
106
  switch (version) {
107
+ case 6:
23
108
  case 5:
24
- return this.#decodeEventV5(eventRecord);
109
+ return this.isRevive() // --
110
+ ? this.#decodeEventV6(eventRecord)
111
+ : this.#decodeEventV5(eventRecord);
25
112
  case '4':
26
113
  return this.#decodeEventV4(eventRecord);
27
114
  default:
28
115
  throw new DedotError('Unsupported metadata version!');
29
116
  }
30
117
  }
31
- #isContractEmittedEvent(event, contract) {
32
- const eventMatched = event.pallet === 'Contracts' &&
118
+ #isContractEmittedEvent(event, contractAddress) {
119
+ const eventMatched = (this.isRevive() // --
120
+ ? event.pallet === 'Revive'
121
+ : event.pallet === 'Contracts') &&
33
122
  typeof event.palletEvent === 'object' &&
34
123
  event.palletEvent.name === 'ContractEmitted';
35
124
  if (!eventMatched)
36
125
  return false;
37
- if (contract) {
126
+ if (contractAddress) {
38
127
  // @ts-ignore
39
128
  const emittedContract = event.palletEvent.data?.contract;
40
- if (emittedContract instanceof AccountId32) {
41
- return emittedContract.eq(contract);
129
+ if (this.isRevive()) {
130
+ return emittedContract === contractAddress;
131
+ }
132
+ else if (emittedContract instanceof AccountId32) {
133
+ return emittedContract.eq(contractAddress);
42
134
  }
43
135
  else {
44
136
  return false;
@@ -47,32 +139,51 @@ export class TypinkRegistry extends TypeRegistry {
47
139
  return true;
48
140
  }
49
141
  #decodeEventV4(eventRecord) {
50
- assert(this.#metadata.version === '4', 'Invalid metadata version!');
142
+ assert(this.metadata.version === '4', 'Invalid metadata version!');
51
143
  assert(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
52
144
  const data = hexToU8a(eventRecord.event.palletEvent.data.data);
53
145
  const index = data.at(0);
54
146
  assert(index !== undefined, 'Unable to decode event index!');
55
- const event = this.#metadata.spec.events[index];
147
+ const event = this.metadata.spec.events[index];
56
148
  assert(event, `Event index not found: ${index.toString()}`);
57
149
  return this.#tryDecodeEvent(event, data.subarray(1));
58
150
  }
59
- #decodeEventV5(eventRecord) {
60
- assert(this.#metadata.version === 5, 'Invalid metadata version!');
61
- assert(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
62
- const data = hexToU8a(eventRecord.event.palletEvent.data.data);
63
- const signatureTopic = eventRecord.topics.at(0);
151
+ #detectEventMeta(signatureTopics) {
152
+ // Only version >= 5 metadata supports signature topics
153
+ assert(+this.metadata.version >= 5, 'Invalid metadata version!');
64
154
  let eventMeta;
65
- if (signatureTopic) {
66
- eventMeta = this.#metadata.spec.events.find((one) => one.signature_topic === signatureTopic);
155
+ const targetSignatureTopic = signatureTopics.at(0);
156
+ if (targetSignatureTopic) {
157
+ // @ts-ignore
158
+ eventMeta = this.metadata.spec.events.find((one) => one.signature_topic === targetSignatureTopic);
67
159
  }
68
160
  // TODO: Handle multiple anonymous events
69
161
  // If `event` does not exist, it means it's an anonymous event
70
162
  // that does not contain a signature topic in the metadata.
71
163
  if (!eventMeta) {
72
- const potentialEvents = this.#metadata.spec.events.filter((one) => !one.signature_topic && one.args.filter((arg) => arg.indexed).length === eventRecord.topics.length);
164
+ const potentialEvents = this.metadata.spec.events.filter(
165
+ // @ts-ignore
166
+ (one) => !one.signature_topic && one.args.filter((arg) => arg.indexed).length === signatureTopics.length);
73
167
  assert(potentialEvents.length === 1, 'Unable to determine event!');
74
168
  eventMeta = potentialEvents[0];
75
169
  }
170
+ return eventMeta;
171
+ }
172
+ #decodeEventV5(eventRecord) {
173
+ assert(this.metadata.version === 5, 'Invalid metadata version!');
174
+ assert(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
175
+ const data = hexToU8a(eventRecord.event.palletEvent.data.data);
176
+ const signatureTopics = eventRecord.topics;
177
+ const eventMeta = this.#detectEventMeta(signatureTopics);
178
+ return this.#tryDecodeEvent(eventMeta, data);
179
+ }
180
+ #decodeEventV6(eventRecord) {
181
+ assert(this.isRevive(), 'Invalid metadata version!');
182
+ assert(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
183
+ const eventData = eventRecord.event.palletEvent.data;
184
+ const data = hexToU8a(eventData.data);
185
+ const signatureTopics = eventData.topics;
186
+ const eventMeta = this.#detectEventMeta(signatureTopics);
76
187
  return this.#tryDecodeEvent(eventMeta, data);
77
188
  }
78
189
  #tryDecodeEvent(eventMeta, raw) {
@@ -88,4 +199,13 @@ export class TypinkRegistry extends TypeRegistry {
88
199
  const name = stringPascalCase(label);
89
200
  return args.length ? { name, data } : { name };
90
201
  }
202
+ /**
203
+ * Check if the contract is Revive Compatible (ink!v6)
204
+ */
205
+ isRevive() {
206
+ return (+this.metadata.version >= 6 ||
207
+ this.metadata.source.language // TODO remove this
208
+ .toLowerCase() // --
209
+ .startsWith('ink! 6.'));
210
+ }
91
211
  }
package/cjs/Contract.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Contract = void 0;
4
- const codecs_1 = require("@dedot/codecs");
4
+ const utils_1 = require("@dedot/utils");
5
5
  const TypinkRegistry_js_1 = require("./TypinkRegistry.js");
6
6
  const index_js_1 = require("./executor/index.js");
7
- const utils_js_1 = require("./utils.js");
7
+ const index_js_2 = require("./utils/index.js");
8
8
  class Contract {
9
9
  client;
10
10
  #registry;
@@ -13,10 +13,13 @@ class Contract {
13
13
  #options;
14
14
  constructor(client, metadata, address, options) {
15
15
  this.client = client;
16
- (0, utils_js_1.ensureSupportContractsPallet)(client);
17
- this.#address = new codecs_1.AccountId32(address);
18
- this.#metadata = typeof metadata === 'string' ? (0, utils_js_1.parseRawMetadata)(metadata) : metadata;
19
- this.#registry = new TypinkRegistry_js_1.TypinkRegistry(this.#metadata);
16
+ this.#metadata = (typeof metadata === 'string' ? JSON.parse(metadata) : metadata);
17
+ (0, index_js_2.ensureSupportedContractMetadataVersion)(this.metadata);
18
+ const getStorage = this.#getStorage.bind(this);
19
+ this.#registry = new TypinkRegistry_js_1.TypinkRegistry(this.metadata, { getStorage });
20
+ (0, index_js_2.ensurePalletPresence)(client, this.registry);
21
+ (0, index_js_2.ensureValidContractAddress)(address, this.registry);
22
+ this.#address = address;
20
23
  this.#options = options;
21
24
  }
22
25
  decodeEvent(eventRecord) {
@@ -35,16 +38,48 @@ class Contract {
35
38
  return this.#registry;
36
39
  }
37
40
  get query() {
38
- return (0, utils_js_1.newProxyChain)(new index_js_1.QueryExecutor(this.client, this.#registry, this.#address, this.#options));
41
+ return (0, index_js_2.newProxyChain)(new index_js_1.QueryExecutor(this.client, this.#registry, this.#address, this.#options));
39
42
  }
40
43
  get tx() {
41
- return (0, utils_js_1.newProxyChain)(new index_js_1.TxExecutor(this.client, this.#registry, this.#address, this.#options));
44
+ return (0, index_js_2.newProxyChain)(new index_js_1.TxExecutor(this.client, this.#registry, this.#address, this.#options));
42
45
  }
43
46
  get events() {
44
- return (0, utils_js_1.newProxyChain)(new index_js_1.EventExecutor(this.client, this.#registry, this.#address, this.#options));
47
+ return (0, index_js_2.newProxyChain)(new index_js_1.EventExecutor(this.client, this.#registry, this.#address, this.#options));
45
48
  }
46
49
  get options() {
47
50
  return this.#options;
48
51
  }
52
+ get storage() {
53
+ return {
54
+ root: async () => {
55
+ (0, index_js_2.ensureStorageApiSupports)(this.metadata.version);
56
+ const { ty, root_key } = this.metadata.storage.root;
57
+ const rawValue = await this.#getStorage(root_key);
58
+ return this.registry.findCodec(ty).tryDecode(rawValue);
59
+ },
60
+ lazy: () => {
61
+ (0, index_js_2.ensureStorageApiSupports)(this.metadata.version);
62
+ const { ty } = this.metadata.storage.root;
63
+ const $lazyCodec = this.registry.createLazyCodec(ty);
64
+ return $lazyCodec ? $lazyCodec.tryDecode('0x') : {};
65
+ },
66
+ };
67
+ }
68
+ #getStorage = async (key) => {
69
+ const result = await (async () => {
70
+ if (this.registry.isRevive()) {
71
+ return await this.client.call.reviveApi.getStorageVarKey(this.address, //--
72
+ (0, utils_1.toHex)(key));
73
+ }
74
+ else {
75
+ return await this.client.call.contractsApi.getStorage(this.address, //--
76
+ (0, utils_1.toU8a)(key));
77
+ }
78
+ })();
79
+ if (result.isOk) {
80
+ return result.value;
81
+ }
82
+ throw new utils_1.DedotError(result.err);
83
+ };
49
84
  }
50
85
  exports.Contract = Contract;
@@ -4,19 +4,21 @@ exports.ContractDeployer = void 0;
4
4
  const TypinkRegistry_js_1 = require("./TypinkRegistry.js");
5
5
  const ConstructorQueryExecutor_js_1 = require("./executor/ConstructorQueryExecutor.js");
6
6
  const index_js_1 = require("./executor/index.js");
7
- const utils_js_1 = require("./utils.js");
7
+ const index_js_2 = require("./utils/index.js");
8
8
  class ContractDeployer {
9
9
  client;
10
10
  #metadata;
11
11
  #registry;
12
12
  #code;
13
13
  #options;
14
- constructor(client, metadata, codeHashOrWasm, options) {
14
+ constructor(client, metadata, codeHashOrCode, options) {
15
15
  this.client = client;
16
- (0, utils_js_1.ensureSupportContractsPallet)(client);
17
- this.#metadata = typeof metadata === 'string' ? (0, utils_js_1.parseRawMetadata)(metadata) : metadata;
18
- this.#registry = new TypinkRegistry_js_1.TypinkRegistry(this.#metadata);
19
- this.#code = codeHashOrWasm;
16
+ this.#metadata = (typeof metadata === 'string' ? JSON.parse(metadata) : metadata);
17
+ (0, index_js_2.ensureSupportedContractMetadataVersion)(this.metadata);
18
+ this.#registry = new TypinkRegistry_js_1.TypinkRegistry(this.metadata);
19
+ (0, index_js_2.ensurePalletPresence)(client, this.registry);
20
+ (0, index_js_2.ensureValidCodeHashOrCode)(codeHashOrCode, this.registry);
21
+ this.#code = codeHashOrCode;
20
22
  this.#options = options;
21
23
  }
22
24
  get metadata() {
@@ -26,10 +28,10 @@ class ContractDeployer {
26
28
  return this.#registry;
27
29
  }
28
30
  get tx() {
29
- return (0, utils_js_1.newProxyChain)(new index_js_1.ConstructorTxExecutor(this.client, this.#registry, this.#code, this.#options));
31
+ return (0, index_js_2.newProxyChain)(new index_js_1.ConstructorTxExecutor(this.client, this.#registry, this.#code, this.#options));
30
32
  }
31
33
  get query() {
32
- return (0, utils_js_1.newProxyChain)(new ConstructorQueryExecutor_js_1.ConstructorQueryExecutor(this.client, this.#registry, this.#code, this.#options));
34
+ return (0, index_js_2.newProxyChain)(new ConstructorQueryExecutor_js_1.ConstructorQueryExecutor(this.client, this.#registry, this.#code, this.#options));
33
35
  }
34
36
  get options() {
35
37
  return this.#options;