@dedot/contracts 0.17.0 → 0.17.1-next.db98ff20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/Contract.d.ts +5 -4
  2. package/Contract.js +42 -15
  3. package/ContractDeployer.d.ts +5 -4
  4. package/ContractDeployer.js +27 -10
  5. package/SolRegistry.d.ts +22 -0
  6. package/SolRegistry.js +64 -0
  7. package/TypinkRegistry.d.ts +2 -1
  8. package/TypinkRegistry.js +3 -1
  9. package/cjs/Contract.js +43 -16
  10. package/cjs/ContractDeployer.js +25 -8
  11. package/cjs/SolRegistry.js +68 -0
  12. package/cjs/TypinkRegistry.js +2 -0
  13. package/cjs/errors.js +75 -1
  14. package/cjs/executor/index.js +2 -5
  15. package/cjs/executor/{ConstructorQueryExecutor.js → ink/ConstructorQueryExecutor.js} +3 -2
  16. package/cjs/executor/{ConstructorTxExecutor.js → ink/ConstructorTxExecutor.js} +4 -3
  17. package/cjs/executor/{QueryExecutor.js → ink/QueryExecutor.js} +4 -3
  18. package/cjs/executor/{TxExecutor.js → ink/TxExecutor.js} +1 -1
  19. package/cjs/executor/{abstract → ink/abstract}/ContractExecutor.js +1 -1
  20. package/cjs/executor/{abstract → ink/abstract}/DeployerExecutor.js +1 -1
  21. package/cjs/executor/ink/index.js +22 -0
  22. package/cjs/executor/sol/SolConstructorQueryExecutor.js +81 -0
  23. package/cjs/executor/sol/SolConstructorTxExecutor.js +123 -0
  24. package/cjs/executor/sol/SolEventExecutor.js +57 -0
  25. package/cjs/executor/sol/SolQueryExecutor.js +83 -0
  26. package/cjs/executor/sol/SolTxExecutor.js +62 -0
  27. package/cjs/executor/sol/abstract/SolContractExecutor.js +12 -0
  28. package/cjs/executor/sol/abstract/SolDeployerExecutor.js +12 -0
  29. package/cjs/executor/sol/abstract/SolExecutor.js +17 -0
  30. package/cjs/executor/sol/abstract/index.js +19 -0
  31. package/cjs/executor/sol/index.js +22 -0
  32. package/cjs/types/index.js +2 -3
  33. package/cjs/types/ink/index.js +20 -0
  34. package/cjs/types/ink/v6.js +2 -0
  35. package/cjs/types/sol/abi.js +2 -0
  36. package/cjs/types/sol/index.js +17 -0
  37. package/cjs/utils/address.js +2 -2
  38. package/cjs/utils/ensure.js +40 -23
  39. package/cjs/utils/types.js +21 -1
  40. package/errors.d.ts +64 -5
  41. package/errors.js +70 -0
  42. package/executor/index.d.ts +2 -5
  43. package/executor/index.js +2 -5
  44. package/executor/{ConstructorQueryExecutor.d.ts → ink/ConstructorQueryExecutor.d.ts} +1 -2
  45. package/executor/{ConstructorQueryExecutor.js → ink/ConstructorQueryExecutor.js} +3 -2
  46. package/executor/{ConstructorTxExecutor.d.ts → ink/ConstructorTxExecutor.d.ts} +1 -2
  47. package/executor/{ConstructorTxExecutor.js → ink/ConstructorTxExecutor.js} +4 -3
  48. package/executor/{EventExecutor.d.ts → ink/EventExecutor.d.ts} +2 -2
  49. package/executor/{QueryExecutor.d.ts → ink/QueryExecutor.d.ts} +1 -2
  50. package/executor/{QueryExecutor.js → ink/QueryExecutor.js} +4 -3
  51. package/executor/{TxExecutor.d.ts → ink/TxExecutor.d.ts} +1 -2
  52. package/executor/{TxExecutor.js → ink/TxExecutor.js} +1 -1
  53. package/executor/{abstract → ink/abstract}/ContractExecutor.d.ts +2 -2
  54. package/executor/{abstract → ink/abstract}/ContractExecutor.js +1 -1
  55. package/executor/{abstract → ink/abstract}/DeployerExecutor.d.ts +2 -2
  56. package/executor/{abstract → ink/abstract}/DeployerExecutor.js +1 -1
  57. package/executor/{abstract → ink/abstract}/Executor.d.ts +2 -2
  58. package/executor/ink/index.d.ts +6 -0
  59. package/executor/ink/index.js +6 -0
  60. package/executor/sol/SolConstructorQueryExecutor.d.ts +5 -0
  61. package/executor/sol/SolConstructorQueryExecutor.js +77 -0
  62. package/executor/sol/SolConstructorTxExecutor.d.ts +5 -0
  63. package/executor/sol/SolConstructorTxExecutor.js +119 -0
  64. package/executor/sol/SolEventExecutor.d.ts +6 -0
  65. package/executor/sol/SolEventExecutor.js +53 -0
  66. package/executor/sol/SolQueryExecutor.d.ts +5 -0
  67. package/executor/sol/SolQueryExecutor.js +79 -0
  68. package/executor/sol/SolTxExecutor.d.ts +5 -0
  69. package/executor/sol/SolTxExecutor.js +58 -0
  70. package/executor/sol/abstract/SolContractExecutor.d.ts +9 -0
  71. package/executor/sol/abstract/SolContractExecutor.js +8 -0
  72. package/executor/sol/abstract/SolDeployerExecutor.d.ts +11 -0
  73. package/executor/sol/abstract/SolDeployerExecutor.js +8 -0
  74. package/executor/sol/abstract/SolExecutor.d.ts +13 -0
  75. package/executor/sol/abstract/SolExecutor.js +13 -0
  76. package/executor/sol/abstract/index.d.ts +3 -0
  77. package/executor/sol/abstract/index.js +3 -0
  78. package/executor/sol/index.d.ts +6 -0
  79. package/executor/sol/index.js +6 -0
  80. package/package.json +7 -7
  81. package/storage/BaseLazyObject.d.ts +1 -1
  82. package/types/index.d.ts +44 -121
  83. package/types/index.js +2 -3
  84. package/types/ink/index.d.ts +9 -0
  85. package/types/ink/index.js +4 -0
  86. package/types/ink/shared.d.ts +100 -0
  87. package/types/ink/v6.js +1 -0
  88. package/types/shared.d.ts +80 -99
  89. package/types/sol/abi.d.ts +45 -0
  90. package/types/sol/abi.js +1 -0
  91. package/types/sol/index.d.ts +1 -0
  92. package/types/sol/index.js +1 -0
  93. package/utils/address.js +2 -2
  94. package/utils/ensure.d.ts +8 -7
  95. package/utils/ensure.js +38 -22
  96. package/utils/types.d.ts +20 -2
  97. package/utils/types.js +18 -0
  98. /package/cjs/executor/{EventExecutor.js → ink/EventExecutor.js} +0 -0
  99. /package/cjs/executor/{abstract → ink/abstract}/Executor.js +0 -0
  100. /package/cjs/executor/{abstract → ink/abstract}/index.js +0 -0
  101. /package/cjs/types/{v4.js → ink/shared.js} +0 -0
  102. /package/cjs/types/{v5.js → ink/v4.js} +0 -0
  103. /package/cjs/types/{v6.js → ink/v5.js} +0 -0
  104. /package/executor/{EventExecutor.js → ink/EventExecutor.js} +0 -0
  105. /package/executor/{abstract → ink/abstract}/Executor.js +0 -0
  106. /package/executor/{abstract → ink/abstract}/index.d.ts +0 -0
  107. /package/executor/{abstract → ink/abstract}/index.js +0 -0
  108. /package/types/{v4.js → ink/shared.js} +0 -0
  109. /package/types/{v4.d.ts → ink/v4.d.ts} +0 -0
  110. /package/types/{v5.js → ink/v4.js} +0 -0
  111. /package/types/{v5.d.ts → ink/v5.d.ts} +0 -0
  112. /package/types/{v6.js → ink/v5.js} +0 -0
  113. /package/types/{v6.d.ts → ink/v6.d.ts} +0 -0
@@ -1,8 +1,8 @@
1
1
  import { ISubstrateClient } from '@dedot/api';
2
2
  import { SubstrateApi } from '@dedot/api/chaintypes';
3
3
  import { GenericSubstrateApi, RpcVersion } from '@dedot/types';
4
- import { TypinkRegistry } from '../../TypinkRegistry.js';
5
- import { ContractMessageArg, ContractMessage, ExecutionOptions, ContractMetadata } from '../../types/index.js';
4
+ import { TypinkRegistry } from '../../../TypinkRegistry.js';
5
+ import { ContractMessageArg, ContractMessage, ExecutionOptions, ContractMetadata } from '../../../types/index.js';
6
6
  export declare abstract class Executor<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]> {
7
7
  readonly client: ISubstrateClient<ChainApi>;
8
8
  readonly registry: TypinkRegistry;
@@ -0,0 +1,6 @@
1
+ export * from './abstract/index.js';
2
+ export * from './TxExecutor.js';
3
+ export * from './QueryExecutor.js';
4
+ export * from './ConstructorTxExecutor.js';
5
+ export * from './ConstructorQueryExecutor.js';
6
+ export * from './EventExecutor.js';
@@ -0,0 +1,6 @@
1
+ export * from './abstract/index.js';
2
+ export * from './TxExecutor.js';
3
+ export * from './QueryExecutor.js';
4
+ export * from './ConstructorTxExecutor.js';
5
+ export * from './ConstructorQueryExecutor.js';
6
+ export * from './EventExecutor.js';
@@ -0,0 +1,5 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { SolDeployerExecutor } from './abstract/index.js';
3
+ export declare class SolConstructorQueryExecutor<ChainApi extends GenericSubstrateApi> extends SolDeployerExecutor<ChainApi> {
4
+ doExecute(_: string): any;
5
+ }
@@ -0,0 +1,77 @@
1
+ import { assert, isPvm, isUndefined, toHex, toU8a } from '@dedot/utils';
2
+ import { decodeErrorResult, encodeDeployData } from 'viem/utils';
3
+ import { ContractInstantiateDispatchError, SolContractInstantiateError } from '../../errors.js';
4
+ import { ensureParamsLength, ensureValidAccountId32Address, toReturnFlags } from '../../utils/index.js';
5
+ import { SolDeployerExecutor } from './abstract/index.js';
6
+ export class SolConstructorQueryExecutor extends SolDeployerExecutor {
7
+ doExecute(_) {
8
+ const abiConstructor = this.registry.findAbiConstructor();
9
+ const callFn = async (...params) => {
10
+ const { inputs } = abiConstructor;
11
+ ensureParamsLength(inputs.length, params.length);
12
+ const callOptions = (params[inputs.length] || {});
13
+ const { caller = this.options.defaultCaller, // --
14
+ value = 0n, gasLimit, storageDepositLimit, salt, } = callOptions;
15
+ assert(caller, 'Expected a valid caller address in ConstructorCallOptions');
16
+ ensureValidAccountId32Address(caller);
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
+ const bytes = encodeDeployData({
19
+ abi: this.abi,
20
+ bytecode: '0x',
21
+ args: params.slice(0, inputs.length),
22
+ });
23
+ const isUpload = isPvm(this.code);
24
+ const code = {
25
+ type: isUpload ? 'Upload' : 'Existing',
26
+ value: this.code,
27
+ };
28
+ const client = this.client;
29
+ const raw = await (async () => {
30
+ const raw = await client.call.reviveApi.instantiate(caller, // --
31
+ value, gasLimit, storageDepositLimit, code, bytes, salt ? toHex(salt) : undefined);
32
+ const result = raw.result;
33
+ if (result.isOk) {
34
+ // @ts-ignore
35
+ result.value.address = result.value.addr;
36
+ // @ts-ignore
37
+ delete result.value.addr;
38
+ }
39
+ return {
40
+ gasConsumed: raw.gasConsumed,
41
+ gasRequired: raw.gasRequired,
42
+ storageDeposit: raw.storageDeposit,
43
+ result,
44
+ };
45
+ })();
46
+ if (raw.result.isErr) {
47
+ const moduleError = client.registry.findErrorMeta(raw.result.err);
48
+ throw new ContractInstantiateDispatchError(raw.result.err, raw, moduleError);
49
+ }
50
+ const flags = toReturnFlags(raw.result.value.result.flags.bits);
51
+ const data = raw.result.value.result.data;
52
+ if (flags.revert) {
53
+ let errorResult;
54
+ try {
55
+ errorResult = decodeErrorResult({
56
+ abi: this.abi,
57
+ data,
58
+ });
59
+ }
60
+ catch (e) {
61
+ // TODO make this better, handle panic!, assert! cases
62
+ throw new SolContractInstantiateError(raw, { message: `Failed to decode error. Details: ${e.message}` });
63
+ }
64
+ throw new SolContractInstantiateError(raw, { details: errorResult });
65
+ }
66
+ return {
67
+ data: [],
68
+ raw,
69
+ flags,
70
+ address: raw.result.value.address,
71
+ inputData: bytes,
72
+ };
73
+ };
74
+ callFn.meta = abiConstructor;
75
+ return callFn;
76
+ }
77
+ }
@@ -0,0 +1,5 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { SolDeployerExecutor } from './abstract/index.js';
3
+ export declare class SolConstructorTxExecutor<ChainApi extends GenericSubstrateApi> extends SolDeployerExecutor<ChainApi> {
4
+ doExecute(_: string): any;
5
+ }
@@ -0,0 +1,119 @@
1
+ import { assert, isPvm, isUndefined, toHex, toU8a } from '@dedot/utils';
2
+ import { encodeDeployData } from 'viem/utils';
3
+ import { Contract } from '../../Contract.js';
4
+ import { CREATE1, CREATE2, ensureContractPresence, ensureParamsLength, toEvmAddress } from '../../utils/index.js';
5
+ import { SolConstructorQueryExecutor } from './SolConstructorQueryExecutor.js';
6
+ import { SolDeployerExecutor } from './abstract/index.js';
7
+ export class SolConstructorTxExecutor extends SolDeployerExecutor {
8
+ doExecute(_) {
9
+ const abiConstructor = this.registry.findAbiConstructor();
10
+ // @ts-ignore
11
+ const callFn = (...params) => {
12
+ const { inputs } = abiConstructor;
13
+ ensureParamsLength(inputs.length, params.length);
14
+ const txCallOptions = (params[inputs.length] || {});
15
+ const { value = 0n, gasLimit, storageDepositLimit, salt } = txCallOptions;
16
+ assert(isUndefined(salt) || toU8a(salt).byteLength == 32, 'Invalid salt provided in ConstructorCallOptions: expected a 32-byte value as a hex string or a Uint8Array');
17
+ const bytes = encodeDeployData({
18
+ abi: this.abi,
19
+ bytecode: '0x',
20
+ args: params.slice(0, inputs.length),
21
+ });
22
+ const client = this.client;
23
+ const tx = (() => {
24
+ if (isPvm(this.code)) {
25
+ return client.tx.revive.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt ? toHex(salt) : undefined);
26
+ }
27
+ else {
28
+ return client.tx.revive.instantiate(value, gasLimit, storageDepositLimit, toHex(this.code), bytes, salt ? toHex(salt) : undefined);
29
+ }
30
+ })();
31
+ let deployerAddress, deployerNonce;
32
+ const calculateContractAddress = async (result) => {
33
+ assert(deployerAddress, 'Deployer address Not Found');
34
+ const { status, dispatchError } = result;
35
+ const onChain = status.type === 'BestChainBlockIncluded' || status.type === 'Finalized';
36
+ assert(onChain, 'The deployment transaction has not yet been included in the best chain block or finalized.');
37
+ assert(!dispatchError, 'The deployment transaction failed to execute. Refer to the dispatch error for more information.');
38
+ if (salt) {
39
+ let code;
40
+ if (isPvm(this.code)) {
41
+ code = this.code;
42
+ }
43
+ else {
44
+ // Pull the raw code in case we're using a code hash here
45
+ code = await client.query.revive.pristineCode(toHex(this.code));
46
+ }
47
+ assert(code, 'Contract code binary not found');
48
+ return CREATE2(toEvmAddress(deployerAddress), // --
49
+ code, bytes, salt);
50
+ }
51
+ else {
52
+ return CREATE1(toEvmAddress(deployerAddress), // --
53
+ deployerNonce);
54
+ }
55
+ };
56
+ tx.withHooks({
57
+ beforeSign: async (tx, signerAddress) => {
58
+ // Set data for next hooks
59
+ deployerAddress = signerAddress;
60
+ if (!salt) {
61
+ deployerNonce = await client.call.accountNonceApi.accountNonce(deployerAddress);
62
+ }
63
+ const callParams = { ...tx.call.palletCall.params };
64
+ const hasGasLimit = !!callParams.gasLimit;
65
+ const hasStorageDepositLimit = !!callParams.storageDepositLimit;
66
+ // Check if current tx provide gas limit and storage deposit limit
67
+ // If not, we need to do a dry run to get the actual value
68
+ const needsDryRun = !hasGasLimit || !hasStorageDepositLimit;
69
+ if (!needsDryRun)
70
+ return;
71
+ const executor = new SolConstructorQueryExecutor(this.client, // --
72
+ this.registry, this.code, {
73
+ defaultCaller: signerAddress,
74
+ ...this.options,
75
+ });
76
+ const { raw: { gasRequired, storageDeposit }, } = await executor.doExecute('new')(...params);
77
+ // Replace the params with gas limit and storage deposit limit
78
+ if (!hasGasLimit) {
79
+ callParams.gasLimit = gasRequired;
80
+ }
81
+ if (!hasStorageDepositLimit) {
82
+ callParams.storageDepositLimit = storageDeposit.value;
83
+ }
84
+ const newCall = { ...tx.call };
85
+ newCall.palletCall.params = callParams;
86
+ tx.call = newCall;
87
+ },
88
+ transformResult: (result) => {
89
+ let cachedAddress;
90
+ const contractAddress = async () => {
91
+ if (cachedAddress)
92
+ return cachedAddress;
93
+ cachedAddress = await calculateContractAddress(result);
94
+ return cachedAddress;
95
+ };
96
+ const contract = async (overrideOptions) => {
97
+ const address = await contractAddress();
98
+ // Check if the contract is existed on chain or not!
99
+ await ensureContractPresence(client, true, address);
100
+ return new Contract(client, // --
101
+ this.abi, address, {
102
+ defaultCaller: deployerAddress,
103
+ ...this.options,
104
+ ...overrideOptions,
105
+ });
106
+ };
107
+ Object.assign(result, {
108
+ contractAddress,
109
+ contract,
110
+ });
111
+ return result;
112
+ },
113
+ });
114
+ return tx;
115
+ };
116
+ callFn.meta = abiConstructor;
117
+ return callFn;
118
+ }
119
+ }
@@ -0,0 +1,6 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { GenericContractEvent } from '../../types/index.js';
3
+ import { SolContractExecutor } from './abstract/index.js';
4
+ export declare class SolEventExecutor<ChainApi extends GenericSubstrateApi> extends SolContractExecutor<ChainApi> {
5
+ doExecute(eventName: string): GenericContractEvent<string, any, 'sol'>;
6
+ }
@@ -0,0 +1,53 @@
1
+ import { isEventRecord } from '@dedot/api';
2
+ import { assert } from '@dedot/utils';
3
+ import { SolContractExecutor } from './abstract/index.js';
4
+ export class SolEventExecutor extends SolContractExecutor {
5
+ doExecute(eventName) {
6
+ const abiEvent = this.registry.findAbiEvent(eventName);
7
+ assert(abiEvent, `Abi item not found: ${eventName}`);
8
+ const is = (event) => {
9
+ if (isEventRecord(event)) {
10
+ try {
11
+ event = this.registry.decodeEvent(event, this.address);
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ }
17
+ return event.name === eventName;
18
+ };
19
+ const find = (events) => {
20
+ if (!events || events.length === 0)
21
+ return undefined;
22
+ if (isEventRecord(events[0])) {
23
+ return this.registry.decodeEvents(events, this.address).find(is);
24
+ }
25
+ else {
26
+ return events.find(is);
27
+ }
28
+ };
29
+ const filter = (events) => {
30
+ if (isEventRecord(events[0])) {
31
+ return this.registry.decodeEvents(events, this.address).filter(is);
32
+ }
33
+ else {
34
+ return events.filter(is);
35
+ }
36
+ };
37
+ const watch = (callback) => {
38
+ return this.client.query.system.events((records) => {
39
+ const events = filter(records);
40
+ if (events.length === 0)
41
+ return;
42
+ callback(filter(records));
43
+ });
44
+ };
45
+ return {
46
+ is,
47
+ find,
48
+ filter,
49
+ meta: abiEvent,
50
+ watch,
51
+ };
52
+ }
53
+ }
@@ -0,0 +1,5 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { SolContractExecutor } from './abstract/SolContractExecutor.js';
3
+ export declare class SolQueryExecutor<ChainApi extends GenericSubstrateApi> extends SolContractExecutor<ChainApi> {
4
+ doExecute(name: string): any;
5
+ }
@@ -0,0 +1,79 @@
1
+ import { assert } from '@dedot/utils';
2
+ import { decodeErrorResult, decodeFunctionResult, encodeFunctionData } from 'viem/utils';
3
+ import { ContractDispatchError, SolContractExecutionError } from '../../errors.js';
4
+ import { ensureContractPresence, ensureParamsLength, ensureValidAccountId32Address, toReturnFlags, } from '../../utils/index.js';
5
+ import { SolContractExecutor } from './abstract/SolContractExecutor.js';
6
+ export class SolQueryExecutor extends SolContractExecutor {
7
+ doExecute(name) {
8
+ const abiFunction = this.registry.findAbiFunction(name);
9
+ assert(abiFunction, `Abi item not found: ${name}`);
10
+ const callFn = async (...params) => {
11
+ const { inputs } = abiFunction;
12
+ ensureParamsLength(inputs.length, params.length);
13
+ const callOptions = (params[inputs.length] || {});
14
+ const { caller = this.options.defaultCaller, value = 0n, gasLimit, storageDepositLimit } = callOptions;
15
+ assert(caller, 'Expected a valid caller address in ContractCallOptions');
16
+ ensureValidAccountId32Address(caller);
17
+ const bytes = encodeFunctionData({
18
+ abi: this.abi,
19
+ functionName: abiFunction.name,
20
+ args: params.slice(0, inputs.length),
21
+ });
22
+ const client = this.client;
23
+ await ensureContractPresence(client, true, this.address, this.registry.cache);
24
+ const raw = await (async () => {
25
+ const raw = await client.call.reviveApi.call(caller, // --
26
+ this.address, value, gasLimit, storageDepositLimit, bytes);
27
+ return {
28
+ gasConsumed: raw.gasConsumed,
29
+ gasRequired: raw.gasRequired,
30
+ storageDeposit: raw.storageDeposit,
31
+ result: raw.result,
32
+ };
33
+ })();
34
+ if (raw.result.isErr) {
35
+ const moduleError = client.registry.findErrorMeta(raw.result.err);
36
+ throw new ContractDispatchError(raw.result.err, raw, moduleError);
37
+ }
38
+ const flags = toReturnFlags(raw.result.value.flags.bits);
39
+ const data = raw.result.value.data;
40
+ if (flags.revert) {
41
+ let errorResult;
42
+ try {
43
+ errorResult = decodeErrorResult({
44
+ abi: this.abi,
45
+ data,
46
+ });
47
+ }
48
+ catch (e) {
49
+ // TODO make this better, handle panic!, assert! cases
50
+ throw new SolContractExecutionError(raw, { message: `Failed to decode error. Details: ${e.message}` });
51
+ }
52
+ throw new SolContractExecutionError(raw, { details: errorResult });
53
+ }
54
+ try {
55
+ let decodedData = decodeFunctionResult({
56
+ abi: this.abi,
57
+ functionName: abiFunction.name,
58
+ data,
59
+ });
60
+ // if this is a tx, then this is a dry run, so the data will be default to []
61
+ const isTx = abiFunction.stateMutability === 'payable' || abiFunction.stateMutability === 'nonpayable';
62
+ if (isTx && data === '0x' && decodedData === undefined) {
63
+ decodedData = [];
64
+ }
65
+ return {
66
+ data: decodedData,
67
+ raw,
68
+ flags,
69
+ inputData: bytes,
70
+ };
71
+ }
72
+ catch (e) {
73
+ throw new SolContractExecutionError(raw, { message: `Failed to decode result. Details: ${e.message}` });
74
+ }
75
+ };
76
+ callFn.meta = abiFunction;
77
+ return callFn;
78
+ }
79
+ }
@@ -0,0 +1,5 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { SolContractExecutor } from './abstract/SolContractExecutor.js';
3
+ export declare class SolTxExecutor<ChainApi extends GenericSubstrateApi> extends SolContractExecutor<ChainApi> {
4
+ doExecute(name: string): any;
5
+ }
@@ -0,0 +1,58 @@
1
+ import { assert } from '@dedot/utils';
2
+ import { encodeFunctionData } from 'viem/utils';
3
+ import { ensureParamsLength } from '../../utils/index.js';
4
+ import { SolQueryExecutor } from './SolQueryExecutor.js';
5
+ import { SolContractExecutor } from './abstract/SolContractExecutor.js';
6
+ export class SolTxExecutor extends SolContractExecutor {
7
+ doExecute(name) {
8
+ const txAbiFunction = this.registry.findTxAbiFunction(name);
9
+ assert(txAbiFunction, `Abi item not found: ${name}`);
10
+ const callFn = (...params) => {
11
+ const { inputs } = txAbiFunction;
12
+ ensureParamsLength(inputs.length, params.length);
13
+ const txCallOptions = (params[inputs.length] || {});
14
+ const { value = 0n, gasLimit, storageDepositLimit } = txCallOptions;
15
+ const bytes = encodeFunctionData({
16
+ abi: this.abi,
17
+ functionName: txAbiFunction.name,
18
+ args: params.slice(0, inputs.length),
19
+ });
20
+ const client = this.client;
21
+ const tx = (() => {
22
+ return client.tx.revive.call(this.address, // --
23
+ value, gasLimit, storageDepositLimit || 0n, bytes);
24
+ })();
25
+ tx.withHooks({
26
+ beforeSign: async (tx, signerAddress) => {
27
+ const callParams = { ...tx.call.palletCall.params };
28
+ const hasGasLimit = !!callParams.gasLimit;
29
+ const hasStorageDepositLimit = !!callParams.storageDepositLimit;
30
+ // Check if current tx provide gas limit and storage deposit limit
31
+ // If not, we need to do a dry run to get the actual value
32
+ const needsDryRun = !hasGasLimit || !hasStorageDepositLimit;
33
+ if (!needsDryRun)
34
+ return;
35
+ const executor = new SolQueryExecutor(this.client, // --
36
+ this.registry, this.address, {
37
+ defaultCaller: signerAddress,
38
+ ...this.options,
39
+ });
40
+ const { raw } = await executor.doExecute(name)(...params);
41
+ const { gasRequired, storageDeposit } = raw;
42
+ if (!callParams.gasLimit) {
43
+ callParams.gasLimit = gasRequired;
44
+ }
45
+ if (!callParams.storageDepositLimit) {
46
+ callParams.storageDepositLimit = storageDeposit.value;
47
+ }
48
+ const newCall = { ...tx.call };
49
+ newCall.palletCall.params = callParams;
50
+ tx.call = newCall;
51
+ },
52
+ });
53
+ return tx;
54
+ };
55
+ callFn.meta = txAbiFunction;
56
+ return callFn;
57
+ }
58
+ }
@@ -0,0 +1,9 @@
1
+ import { ISubstrateClient } from '@dedot/api';
2
+ import { GenericSubstrateApi } from '@dedot/types';
3
+ import { SolRegistry } from '../../../SolRegistry.js';
4
+ import { ContractAddress, ExecutionOptions } from '../../../types/index.js';
5
+ import { SolExecutor } from './SolExecutor.js';
6
+ export declare abstract class SolContractExecutor<ChainApi extends GenericSubstrateApi> extends SolExecutor<ChainApi> {
7
+ readonly address: ContractAddress;
8
+ constructor(client: ISubstrateClient<ChainApi>, registry: SolRegistry, address: ContractAddress, options?: ExecutionOptions);
9
+ }
@@ -0,0 +1,8 @@
1
+ import { SolExecutor } from './SolExecutor.js';
2
+ export class SolContractExecutor extends SolExecutor {
3
+ address;
4
+ constructor(client, registry, address, options) {
5
+ super(client, registry, options);
6
+ this.address = address;
7
+ }
8
+ }
@@ -0,0 +1,11 @@
1
+ import { ISubstrateClient } from '@dedot/api';
2
+ import { Hash } from '@dedot/codecs';
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ import { HexString } from '@dedot/utils';
5
+ import { SolRegistry } from '../../../SolRegistry.js';
6
+ import { ExecutionOptions } from '../../../types/index.js';
7
+ import { SolExecutor } from './SolExecutor.js';
8
+ export declare abstract class SolDeployerExecutor<ChainApi extends GenericSubstrateApi> extends SolExecutor<ChainApi> {
9
+ readonly code: Hash | Uint8Array | HexString | string;
10
+ constructor(client: ISubstrateClient<ChainApi>, registry: SolRegistry, code: Hash | Uint8Array | HexString | string, options?: ExecutionOptions);
11
+ }
@@ -0,0 +1,8 @@
1
+ import { SolExecutor } from './SolExecutor.js';
2
+ export class SolDeployerExecutor extends SolExecutor {
3
+ code;
4
+ constructor(client, registry, code, options) {
5
+ super(client, registry, options);
6
+ this.code = code;
7
+ }
8
+ }
@@ -0,0 +1,13 @@
1
+ import { ISubstrateClient } from '@dedot/api';
2
+ import { SubstrateApi } from '@dedot/api/chaintypes';
3
+ import { GenericSubstrateApi, RpcVersion } from '@dedot/types';
4
+ import { SolRegistry } from '../../../SolRegistry.js';
5
+ import { ExecutionOptions, SolAbi } from '../../../types/index.js';
6
+ export declare abstract class SolExecutor<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]> {
7
+ readonly client: ISubstrateClient<ChainApi>;
8
+ readonly registry: SolRegistry;
9
+ readonly options: ExecutionOptions;
10
+ protected constructor(client: ISubstrateClient<ChainApi>, registry: SolRegistry, options?: ExecutionOptions);
11
+ get abi(): SolAbi;
12
+ abstract doExecute(...paths: string[]): unknown;
13
+ }
@@ -0,0 +1,13 @@
1
+ export class SolExecutor {
2
+ client;
3
+ registry;
4
+ options;
5
+ constructor(client, registry, options = {}) {
6
+ this.client = client;
7
+ this.registry = registry;
8
+ this.options = options;
9
+ }
10
+ get abi() {
11
+ return this.registry.abi;
12
+ }
13
+ }
@@ -0,0 +1,3 @@
1
+ export * from './SolExecutor.js';
2
+ export * from './SolDeployerExecutor.js';
3
+ export * from './SolContractExecutor.js';
@@ -0,0 +1,3 @@
1
+ export * from './SolExecutor.js';
2
+ export * from './SolDeployerExecutor.js';
3
+ export * from './SolContractExecutor.js';
@@ -0,0 +1,6 @@
1
+ export * from './SolTxExecutor.js';
2
+ export * from './SolQueryExecutor.js';
3
+ export * from './SolConstructorTxExecutor.js';
4
+ export * from './SolConstructorQueryExecutor.js';
5
+ export * from './SolEventExecutor.js';
6
+ export * from './abstract/index.js';
@@ -0,0 +1,6 @@
1
+ export * from './SolTxExecutor.js';
2
+ export * from './SolQueryExecutor.js';
3
+ export * from './SolConstructorTxExecutor.js';
4
+ export * from './SolConstructorQueryExecutor.js';
5
+ export * from './SolEventExecutor.js';
6
+ export * from './abstract/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/contracts",
3
- "version": "0.17.0",
3
+ "version": "0.17.1-next.db98ff20.1+db98ff20",
4
4
  "description": "Delightful ink! Contract APIs",
5
5
  "author": "Tung Vu <tung@dedot.dev>",
6
6
  "homepage": "https://dedot.dev",
@@ -18,11 +18,11 @@
18
18
  "test": "npx vitest --watch=false"
19
19
  },
20
20
  "dependencies": {
21
- "@dedot/api": "0.17.0",
22
- "@dedot/codecs": "0.17.0",
23
- "@dedot/types": "0.17.0",
24
- "@dedot/utils": "0.17.0",
25
- "@ethereumjs/rlp": "^10.0.0"
21
+ "@dedot/api": "0.17.1-next.db98ff20.1+db98ff20",
22
+ "@dedot/codecs": "0.17.1-next.db98ff20.1+db98ff20",
23
+ "@dedot/types": "0.17.1-next.db98ff20.1+db98ff20",
24
+ "@dedot/utils": "0.17.1-next.db98ff20.1+db98ff20",
25
+ "viem": "^2.37.7"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public",
@@ -32,7 +32,7 @@
32
32
  "node": ">=18"
33
33
  },
34
34
  "license": "Apache-2.0",
35
- "gitHead": "77e751248ad7bca0cb374e17e65afef791fb3e4d",
35
+ "gitHead": "db98ff20684d2a74b5e5402cabf43ef601d05d31",
36
36
  "module": "./index.js",
37
37
  "types": "./index.d.ts",
38
38
  "exports": {
@@ -1,6 +1,6 @@
1
1
  import { HexString } from '@dedot/utils';
2
2
  import { TypinkRegistry } from 'src/TypinkRegistry';
3
- import { ContractType } from 'src/types';
3
+ import { ContractType } from '../types/index.js';
4
4
  export declare abstract class BaseLazyObject {
5
5
  readonly typeDef: ContractType;
6
6
  readonly registry: TypinkRegistry;