dequanto 0.2.49 → 0.2.50

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 (125) hide show
  1. package/contracts/oz/Proxy.sol +1 -0
  2. package/docs/ai/INDEX.md +4 -3
  3. package/docs/ai/examples/config.spec.ts +113 -0
  4. package/docs/ai/examples/contract-read.spec.ts +81 -13
  5. package/docs/ai/examples/contract-write.spec.ts +61 -9
  6. package/docs/ai/examples/deploy-contract.spec.ts +53 -16
  7. package/docs/ai/examples/erc4337-userop.spec.ts +7 -5
  8. package/docs/ai/examples/explorer-abi.spec.ts +9 -6
  9. package/docs/ai/examples/{generate-client.spec.ts → generate-contract-class.spec.ts} +6 -3
  10. package/docs/ai/examples/index-events.spec.ts +8 -6
  11. package/docs/ai/examples/rpc-read.spec.ts +32 -5
  12. package/docs/ai/examples/safe-batch.spec.ts +5 -4
  13. package/docs/ai/examples/send-transaction.spec.ts +23 -11
  14. package/docs/ai/examples/storage-read.spec.ts +15 -7
  15. package/docs/ai/examples/token-transfer.spec.ts +22 -15
  16. package/docs/ai/references/generation.md +30 -7
  17. package/docs/ai/skill/dequanto/SKILL.md +5 -1
  18. package/lib/cjs/abi/$abi.js +33 -0
  19. package/lib/cjs/abi/$abi.js.map +1 -1
  20. package/lib/cjs/blocks/BlockDateResolver.js.map +1 -1
  21. package/lib/cjs/clients/Web3Client.js +43 -2
  22. package/lib/cjs/clients/Web3Client.js.map +1 -1
  23. package/lib/cjs/clients/Web3ClientFactory.js +26 -4
  24. package/lib/cjs/clients/Web3ClientFactory.js.map +1 -1
  25. package/lib/cjs/clients/debug/ClientDebugMethods.js +4 -0
  26. package/lib/cjs/clients/debug/ClientDebugMethods.js.map +1 -1
  27. package/lib/cjs/clients/utils/$web3Abi.js +1 -1
  28. package/lib/cjs/clients/utils/$web3Abi.js.map +1 -1
  29. package/lib/cjs/config/Config.js +28 -6
  30. package/lib/cjs/config/Config.js.map +1 -1
  31. package/lib/cjs/config/ConfigDefaults.js +6 -2
  32. package/lib/cjs/config/ConfigDefaults.js.map +1 -1
  33. package/lib/cjs/config/ConfigProvider.js +5 -0
  34. package/lib/cjs/config/ConfigProvider.js.map +1 -1
  35. package/lib/cjs/config/DefaultFactory.js +27 -0
  36. package/lib/cjs/config/DefaultFactory.js.map +1 -0
  37. package/lib/cjs/contracts/ContractBase.js +6 -2
  38. package/lib/cjs/contracts/ContractBase.js.map +1 -1
  39. package/lib/cjs/contracts/ContractReader.js +1 -35
  40. package/lib/cjs/contracts/ContractReader.js.map +1 -1
  41. package/lib/cjs/explorer/BlockchainExplorerFactory.js.map +1 -1
  42. package/lib/cjs/rpc/RpcContract.js +4 -32
  43. package/lib/cjs/rpc/RpcContract.js.map +1 -1
  44. package/lib/cjs/utils/$account.js +5 -0
  45. package/lib/cjs/utils/$account.js.map +1 -1
  46. package/lib/cjs/utils/$config.js +4 -5
  47. package/lib/cjs/utils/$config.js.map +1 -1
  48. package/lib/cjs/utils/$erc20.js +47 -0
  49. package/lib/cjs/utils/$erc20.js.map +1 -0
  50. package/lib/cjs/utils/$require.js +2 -2
  51. package/lib/cjs/utils/$require.js.map +1 -1
  52. package/lib/esm/abi/$abi.js.map +1 -1
  53. package/lib/esm/abi/$abi.mjs +33 -0
  54. package/lib/esm/blocks/BlockDateResolver.js.map +1 -1
  55. package/lib/esm/clients/Web3Client.js.map +1 -1
  56. package/lib/esm/clients/Web3Client.mjs +43 -2
  57. package/lib/esm/clients/Web3ClientFactory.js.map +1 -1
  58. package/lib/esm/clients/Web3ClientFactory.mjs +26 -4
  59. package/lib/esm/clients/debug/ClientDebugMethods.js.map +1 -1
  60. package/lib/esm/clients/debug/ClientDebugMethods.mjs +4 -0
  61. package/lib/esm/clients/utils/$web3Abi.js.map +1 -1
  62. package/lib/esm/clients/utils/$web3Abi.mjs +1 -1
  63. package/lib/esm/config/Config.js.map +1 -1
  64. package/lib/esm/config/Config.mjs +28 -6
  65. package/lib/esm/config/ConfigDefaults.js.map +1 -1
  66. package/lib/esm/config/ConfigDefaults.mjs +6 -2
  67. package/lib/esm/config/ConfigProvider.js.map +1 -1
  68. package/lib/esm/config/ConfigProvider.mjs +5 -0
  69. package/lib/esm/config/DefaultFactory.js.map +1 -0
  70. package/lib/esm/config/DefaultFactory.mjs +23 -0
  71. package/lib/esm/contracts/ContractBase.js.map +1 -1
  72. package/lib/esm/contracts/ContractBase.mjs +6 -2
  73. package/lib/esm/contracts/ContractReader.js.map +1 -1
  74. package/lib/esm/contracts/ContractReader.mjs +1 -35
  75. package/lib/esm/explorer/BlockchainExplorerFactory.js.map +1 -1
  76. package/lib/esm/rpc/RpcContract.js.map +1 -1
  77. package/lib/esm/rpc/RpcContract.mjs +4 -32
  78. package/lib/esm/utils/$account.js.map +1 -1
  79. package/lib/esm/utils/$account.mjs +5 -0
  80. package/lib/esm/utils/$config.js.map +1 -1
  81. package/lib/esm/utils/$config.mjs +4 -5
  82. package/lib/esm/utils/$erc20.js.map +1 -0
  83. package/lib/esm/utils/$erc20.mjs +41 -0
  84. package/lib/esm/utils/$require.js.map +1 -1
  85. package/lib/esm/utils/$require.mjs +2 -2
  86. package/lib/types/abi/$abi.d.ts +3 -1
  87. package/lib/types/blocks/BlockDateResolver.d.ts +1 -1
  88. package/lib/types/clients/Web3Client.d.ts +1 -1
  89. package/lib/types/clients/Web3ClientFactory.d.ts +10 -2
  90. package/lib/types/clients/debug/ClientDebugMethods.d.ts +1 -1
  91. package/lib/types/config/AConfigBase.d.ts +3 -0
  92. package/lib/types/config/Config.d.ts +2 -1
  93. package/lib/types/config/DefaultFactory.d.ts +2 -0
  94. package/lib/types/config/interface/IConfigData.d.ts +14 -2
  95. package/lib/types/contracts/ContractReader.d.ts +1 -1
  96. package/lib/types/explorer/IBlockchainExplorer.d.ts +6 -0
  97. package/lib/types/models/TPlatform.d.ts +1 -1
  98. package/lib/types/utils/$account.d.ts +1 -0
  99. package/lib/types/utils/$erc20.d.ts +5 -0
  100. package/lib/types/utils/$require.d.ts +2 -2
  101. package/package.json +1 -1
  102. package/src/abi/$abi.ts +34 -1
  103. package/src/blocks/BlockDateResolver.ts +1 -1
  104. package/src/clients/Web3Client.ts +48 -3
  105. package/src/clients/Web3ClientFactory.ts +33 -5
  106. package/src/clients/debug/ClientDebugMethods.ts +5 -1
  107. package/src/clients/utils/$web3Abi.ts +2 -2
  108. package/src/config/AConfigBase.ts +4 -0
  109. package/src/config/Config.ts +30 -8
  110. package/src/config/ConfigDefaults.ts +6 -2
  111. package/src/config/ConfigProvider.ts +5 -1
  112. package/src/config/DefaultFactory.ts +26 -0
  113. package/src/config/interface/IConfigData.ts +15 -2
  114. package/src/contracts/ContractBase.ts +6 -2
  115. package/src/contracts/ContractReader.ts +3 -40
  116. package/src/explorer/BlockchainExplorerFactory.ts +0 -1
  117. package/src/explorer/IBlockchainExplorer.ts +4 -0
  118. package/src/models/TPlatform.ts +1 -0
  119. package/src/rpc/RpcContract.ts +5 -33
  120. package/src/utils/$account.ts +5 -0
  121. package/src/utils/$config.ts +4 -6
  122. package/src/utils/$erc20.ts +53 -0
  123. package/src/utils/$require.ts +4 -4
  124. package/tsconfig.json +1 -2
  125. package/tsconfig.umd.json +1 -2
@@ -2,11 +2,12 @@ import di from 'a-di';
2
2
  import alot from 'alot';
3
3
  import { Rpc, RpcTypes } from './Rpc';
4
4
 
5
- import { TAbiItem, TAbiOutput } from '@dequanto/types/TAbi';
5
+ import { TAbiItem } from '@dequanto/types/TAbi';
6
6
  import { TAddress } from '@dequanto/models/TAddress';
7
7
  import { $require } from '@dequanto/utils/$require';
8
8
  import { $abiUtils } from '@dequanto/utils/$abiUtils';
9
9
  import { $abiCoder } from '@dequanto/abi/$abiCoder';
10
+ import { $abi } from '@dequanto/abi/$abi';
10
11
  import { RpcError } from './RpcError';
11
12
  import { $hex } from '@dequanto/utils/$hex';
12
13
  import { TEth } from '@dequanto/models/TEth';
@@ -54,7 +55,7 @@ export class RpcContract {
54
55
  if (methodAbi == null) {
55
56
  return hex;
56
57
  }
57
- return utils.deserializeOutput(hex, methodAbi.outputs);
58
+ return $abi.decodeReturn(methodAbi, hex);
58
59
  } catch (err) {
59
60
  if (err instanceof RpcError) {
60
61
  err.message = `RpcCall ${req.method} (${JSON.stringify(req.params)}) ${err.message}`;
@@ -82,7 +83,7 @@ export class RpcContract {
82
83
  let defaults = null;
83
84
  try {
84
85
  const defaultHex = $abiCoder.encode(returnABI, [null]);
85
- defaults = utils.deserializeOutput(defaultHex, returnABI);
86
+ defaults = $abi.decodeReturn(abi, defaultHex);
86
87
  } catch (error) {}
87
88
  return {
88
89
  defaults,
@@ -90,7 +91,7 @@ export class RpcContract {
90
91
  };
91
92
  }
92
93
  let hex = typeof resp === 'string' ? resp : resp.data;
93
- let result = utils.deserializeOutput(hex, returnABI);
94
+ let result = $abi.decodeReturn(abi, hex);
94
95
  return result;
95
96
  });
96
97
 
@@ -191,32 +192,3 @@ export class RpcContract {
191
192
  // }
192
193
  }
193
194
 
194
-
195
- namespace utils {
196
- export function deserializeOutput(hex: string, outputs: TAbiOutput[]) {
197
-
198
- let abi = outputs;
199
- let isDynamic: boolean = null;
200
- if (outputs.length > 1) {
201
- let isNamedTuple = outputs.every(x => x.name != null && x.name !== '');
202
- if (isNamedTuple) {
203
- // Return as an object
204
- abi = [ { type: 'tuple', components: outputs, name: null } ];
205
- isDynamic = false;
206
- }
207
- }
208
-
209
- try {
210
- let arr = $abiCoder.decode(abi as any, hex, {
211
- dynamic: isDynamic
212
- });
213
- let value = abi.length === 1 ? arr[0] : arr;
214
- return value;
215
- } catch (error) {
216
- if (outputs.length === 1 && abi.length === 1) {
217
- return $abiCoder.decodeSingle(abi[0], hex);
218
- }
219
- throw error;
220
- }
221
- }
222
- }
@@ -1,6 +1,7 @@
1
1
  import { EoAccount, Erc4337Account, SafeAccount, TAccount, TimelockAccount } from "@dequanto/models/TAccount";
2
2
  import { $address } from './$address';
3
3
  import { $require } from './$require';
4
+ import { $sig } from './$sig';
4
5
 
5
6
  export namespace $account {
6
7
 
@@ -64,4 +65,8 @@ export namespace $account {
64
65
  // Optimistic match by pattern "timelock/";
65
66
  return rgx.test(account.name);
66
67
  }
68
+
69
+ export function generate(name: string): EoAccount {
70
+ return $sig.$account.generate({ name });
71
+ }
67
72
  }
@@ -16,8 +16,8 @@ export namespace $config {
16
16
  let envOptions = null;
17
17
 
18
18
  export function get <T = any> (path: string, $default?: T): T {
19
- let value = (typeof $global.app !== 'undefined' ? $global.app.config?.$get?.(path) : null)
20
- ?? obj_getProperty(config, path)
19
+ let value = obj_getProperty(config, path)
20
+ ?? (typeof $global.app !== 'undefined' ? $global.app.config?.$get?.(path) : null)
21
21
  ?? obj_getProperty(envOptions, path);
22
22
 
23
23
  if (value == null && envOptions == null) {
@@ -79,11 +79,10 @@ export namespace $config {
79
79
  let explorer = $config.get<IConfigData['blockchainExplorer']>(`blockchainExplorer`);
80
80
  let chains = $config.get<IConfigData['chains']>('chains', []);
81
81
 
82
-
83
82
  let explorerConfig = explorer[platform];
84
83
  let chainConfig = chains.find(x => x.platform === platform || x.aliases?.includes(platform));
85
84
 
86
- $require.notNull(explorerConfig || chainConfig, `Unsupported platform ${platform} for web3 client`);
85
+ $require.notNull(explorerConfig || chainConfig, `Unsupported platform ${platform} for blockchain explorer`);
87
86
  return {
88
87
  platform,
89
88
  ...(chainConfig?.explorers?.[0] ?? {}),
@@ -92,11 +91,10 @@ export namespace $config {
92
91
  }
93
92
 
94
93
  export function set <T = any> (path: string, value: T) {
94
+ obj_setProperty(config, path, value);
95
95
  if (typeof $global.app?.config?.$set === 'function') {
96
96
  $global.app.config.$set(path, value);
97
- return;
98
97
  }
99
- obj_setProperty(config, path, value);
100
98
  }
101
99
 
102
100
  /**
@@ -0,0 +1,53 @@
1
+ import alot from 'alot';
2
+ import { $require } from './$require';
3
+ import { $hex } from './$hex';
4
+ import { $bigint } from './$bigint';
5
+ import { TEth } from '@dequanto/models/TEth';
6
+ import { Web3Client } from '@dequanto/clients/Web3Client';
7
+ import { ERC20 } from '@dequanto/prebuilt/openzeppelin/ERC20';
8
+
9
+ export namespace $erc20 {
10
+ export async function setBalanceAny(client: Web3Client, token: TEth.Address, account: TEth.Address, amount: bigint | number) {
11
+ $require.eq(client.platform, 'hardhat', `Set Balance available only on Hardhat/Forked network`);
12
+ const erc20 = new ERC20(token, client);
13
+ const rpc = await client.getRpc();
14
+
15
+ const req = await (erc20.$data() as any).balanceOf(account);
16
+ const balanceTrace = await rpc.request({
17
+ method: 'debug_traceCall',
18
+ params: [
19
+ req,
20
+ 'latest',
21
+ {
22
+ disableMemory: true,
23
+ disableStack: true,
24
+ disableStorage: false,
25
+ }
26
+ ]
27
+ }) as {
28
+ returnValue: TEth.Hex
29
+ failed: boolean
30
+ structLogs: {
31
+ "op": "RETURN",
32
+ "pc": 1698,
33
+ "storage": Record<string, TEth.Hex>
34
+ }[]
35
+ };
36
+
37
+ $require.True(balanceTrace.failed === false, `erc20 balance retrieval failed for ${await erc20.symbol()}`);
38
+ let returnOp = alot(balanceTrace.structLogs.reverse()).find(x => x.op === 'RETURN');
39
+ $require.notNull(returnOp, `RETURN opcode not found`);
40
+ $require.notNull(returnOp.storage, `Storage not found`);
41
+
42
+ let returnValue = $hex.raw(balanceTrace.returnValue);
43
+ let slots = alot.fromObject(returnOp.storage).filter(x => x.value === returnValue).toArray();
44
+ $require.gt(slots.length, 0, `Slot not found`);
45
+ let SLOT = $hex.ensure(slots[slots.length - 1].key);
46
+
47
+ let balance = typeof amount === 'number'
48
+ ? await $bigint.toWei(amount, await erc20.decimals())
49
+ : amount;
50
+
51
+ await client.debug.setStorageAt(erc20.address, SLOT, $bigint.toHex(balance));
52
+ }
53
+ }
@@ -61,17 +61,17 @@ export namespace $require {
61
61
  }
62
62
  return val;
63
63
  }
64
- export function notNull<T> (val: T, message: string, ...logs): NonNullable<T> {
64
+ export function notNull<T> (val: T, message?: string, ...logs): NonNullable<T> {
65
65
  if (val == null) {
66
66
  logs?.forEach(log => console.error(log));
67
- throw new Error(`Value is undefined. ${message}`);
67
+ throw new Error(`Value is undefined. ${message ?? ''}`);
68
68
  }
69
69
  return val;
70
70
  }
71
- export function Null<T> (val: T, message: string, ...logs): T {
71
+ export function Null<T> (val: T, message?: string, ...logs): T {
72
72
  if (val != null) {
73
73
  logs?.forEach(log => console.error(log));
74
- throw new Error(`Value ${val} expects to be undefined. ${message}`);
74
+ throw new Error(`Value ${val} expects to be undefined. ${message ?? ''}`);
75
75
  }
76
76
  return val;
77
77
  }
package/tsconfig.json CHANGED
@@ -6,8 +6,7 @@
6
6
  "baseUrl": "./",
7
7
  "paths": {
8
8
  "@dequanto/*": [ "src/*" ],
9
- "dequanto/*": [ "src/*" ],
10
- "@dequanto-contracts/*": [ "src/prebuilt/*" ]
9
+ "dequanto/*": [ "src/*" ]
11
10
  },
12
11
  "typeRoots": [
13
12
  "node_modules/@types",
package/tsconfig.umd.json CHANGED
@@ -6,8 +6,7 @@
6
6
  "compilerOptions": {
7
7
  "baseUrl": "./",
8
8
  "paths": {
9
- "@dequanto/*": [ "src/*" ],
10
- "@dequanto-contracts/*": [ "src/prebuilt/*" ]
9
+ "@dequanto/*": [ "src/*" ]
11
10
  },
12
11
  "lib": [ "ES2019", "dom" ],
13
12
  "typeRoots": [