@agirails/sdk 2.5.2 → 2.5.4

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 (172) hide show
  1. package/dist/ACTPClient.d.ts +18 -0
  2. package/dist/ACTPClient.d.ts.map +1 -1
  3. package/dist/ACTPClient.js +67 -22
  4. package/dist/ACTPClient.js.map +1 -1
  5. package/dist/adapters/BasicAdapter.d.ts +12 -0
  6. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  7. package/dist/adapters/BasicAdapter.js +30 -4
  8. package/dist/adapters/BasicAdapter.js.map +1 -1
  9. package/dist/adapters/StandardAdapter.d.ts +20 -3
  10. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  11. package/dist/adapters/StandardAdapter.js +45 -11
  12. package/dist/adapters/StandardAdapter.js.map +1 -1
  13. package/dist/cli/commands/publish.js +16 -4
  14. package/dist/cli/commands/publish.js.map +1 -1
  15. package/dist/cli/commands/register.js +16 -4
  16. package/dist/cli/commands/register.js.map +1 -1
  17. package/dist/cli/commands/tx.js +31 -3
  18. package/dist/cli/commands/tx.js.map +1 -1
  19. package/dist/cli/utils/client.d.ts.map +1 -1
  20. package/dist/cli/utils/client.js +1 -0
  21. package/dist/cli/utils/client.js.map +1 -1
  22. package/dist/config/networks.d.ts +2 -2
  23. package/dist/config/networks.d.ts.map +1 -1
  24. package/dist/config/networks.js +27 -22
  25. package/dist/config/networks.js.map +1 -1
  26. package/dist/level0/request.d.ts.map +1 -1
  27. package/dist/level0/request.js +2 -1
  28. package/dist/level0/request.js.map +1 -1
  29. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  30. package/dist/runtime/BlockchainRuntime.js +11 -5
  31. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  32. package/dist/runtime/MockStateManager.d.ts.map +1 -1
  33. package/dist/runtime/MockStateManager.js +2 -1
  34. package/dist/runtime/MockStateManager.js.map +1 -1
  35. package/dist/utils/IPFSClient.d.ts +3 -1
  36. package/dist/utils/IPFSClient.d.ts.map +1 -1
  37. package/dist/utils/IPFSClient.js +27 -7
  38. package/dist/utils/IPFSClient.js.map +1 -1
  39. package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
  40. package/dist/wallet/AutoWalletProvider.js +52 -18
  41. package/dist/wallet/AutoWalletProvider.js.map +1 -1
  42. package/dist/wallet/SmartWalletRouter.d.ts +116 -0
  43. package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
  44. package/dist/wallet/SmartWalletRouter.js +212 -0
  45. package/dist/wallet/SmartWalletRouter.js.map +1 -0
  46. package/dist/wallet/aa/DualNonceManager.d.ts +19 -0
  47. package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
  48. package/dist/wallet/aa/DualNonceManager.js +100 -5
  49. package/dist/wallet/aa/DualNonceManager.js.map +1 -1
  50. package/package.json +3 -6
  51. package/src/ACTPClient.ts +0 -1579
  52. package/src/abi/ACTPKernel.json +0 -1356
  53. package/src/abi/AgentRegistry.json +0 -915
  54. package/src/abi/ERC20.json +0 -40
  55. package/src/abi/EscrowVault.json +0 -134
  56. package/src/abi/IdentityRegistry.json +0 -316
  57. package/src/adapters/AdapterRegistry.ts +0 -173
  58. package/src/adapters/AdapterRouter.ts +0 -416
  59. package/src/adapters/BaseAdapter.ts +0 -498
  60. package/src/adapters/BasicAdapter.ts +0 -514
  61. package/src/adapters/IAdapter.ts +0 -292
  62. package/src/adapters/StandardAdapter.ts +0 -555
  63. package/src/adapters/X402Adapter.ts +0 -731
  64. package/src/adapters/index.ts +0 -60
  65. package/src/builders/DeliveryProofBuilder.ts +0 -327
  66. package/src/builders/QuoteBuilder.ts +0 -483
  67. package/src/builders/index.ts +0 -17
  68. package/src/cli/commands/balance.ts +0 -110
  69. package/src/cli/commands/batch.ts +0 -487
  70. package/src/cli/commands/config.ts +0 -231
  71. package/src/cli/commands/deploy-check.ts +0 -364
  72. package/src/cli/commands/deploy-env.ts +0 -120
  73. package/src/cli/commands/diff.ts +0 -141
  74. package/src/cli/commands/init.ts +0 -469
  75. package/src/cli/commands/mint.ts +0 -116
  76. package/src/cli/commands/pay.ts +0 -113
  77. package/src/cli/commands/publish.ts +0 -475
  78. package/src/cli/commands/pull.ts +0 -124
  79. package/src/cli/commands/register.ts +0 -247
  80. package/src/cli/commands/simulate.ts +0 -345
  81. package/src/cli/commands/time.ts +0 -302
  82. package/src/cli/commands/tx.ts +0 -448
  83. package/src/cli/commands/watch.ts +0 -211
  84. package/src/cli/index.ts +0 -134
  85. package/src/cli/utils/client.ts +0 -251
  86. package/src/cli/utils/config.ts +0 -389
  87. package/src/cli/utils/output.ts +0 -465
  88. package/src/cli/utils/wallet.ts +0 -109
  89. package/src/config/agirailsmd.ts +0 -262
  90. package/src/config/networks.ts +0 -275
  91. package/src/config/pendingPublish.ts +0 -237
  92. package/src/config/publishPipeline.ts +0 -359
  93. package/src/config/syncOperations.ts +0 -279
  94. package/src/erc8004/ERC8004Bridge.ts +0 -462
  95. package/src/erc8004/ReputationReporter.ts +0 -468
  96. package/src/erc8004/index.ts +0 -61
  97. package/src/errors/index.ts +0 -427
  98. package/src/index.ts +0 -364
  99. package/src/level0/Provider.ts +0 -117
  100. package/src/level0/ServiceDirectory.ts +0 -131
  101. package/src/level0/index.ts +0 -10
  102. package/src/level0/provide.ts +0 -132
  103. package/src/level0/request.ts +0 -432
  104. package/src/level1/Agent.ts +0 -1426
  105. package/src/level1/index.ts +0 -10
  106. package/src/level1/pricing/PriceCalculator.ts +0 -255
  107. package/src/level1/pricing/PricingStrategy.ts +0 -198
  108. package/src/level1/types/Job.ts +0 -179
  109. package/src/level1/types/Options.ts +0 -291
  110. package/src/level1/types/index.ts +0 -8
  111. package/src/protocol/ACTPKernel.ts +0 -808
  112. package/src/protocol/AgentRegistry.ts +0 -559
  113. package/src/protocol/DIDManager.ts +0 -629
  114. package/src/protocol/DIDResolver.ts +0 -554
  115. package/src/protocol/EASHelper.ts +0 -378
  116. package/src/protocol/EscrowVault.ts +0 -255
  117. package/src/protocol/EventMonitor.ts +0 -204
  118. package/src/protocol/MessageSigner.ts +0 -510
  119. package/src/protocol/ProofGenerator.ts +0 -339
  120. package/src/protocol/QuoteBuilder.ts +0 -15
  121. package/src/registry/AgentRegistryClient.ts +0 -202
  122. package/src/runtime/BlockchainRuntime.ts +0 -1015
  123. package/src/runtime/IACTPRuntime.ts +0 -306
  124. package/src/runtime/MockRuntime.ts +0 -1298
  125. package/src/runtime/MockStateManager.ts +0 -576
  126. package/src/runtime/index.ts +0 -25
  127. package/src/runtime/types/MockState.ts +0 -237
  128. package/src/storage/ArchiveBundleBuilder.ts +0 -561
  129. package/src/storage/ArweaveClient.ts +0 -946
  130. package/src/storage/FilebaseClient.ts +0 -790
  131. package/src/storage/index.ts +0 -96
  132. package/src/storage/types.ts +0 -348
  133. package/src/types/adapter.ts +0 -310
  134. package/src/types/agent.ts +0 -79
  135. package/src/types/did.ts +0 -223
  136. package/src/types/eip712.ts +0 -175
  137. package/src/types/erc8004.ts +0 -293
  138. package/src/types/escrow.ts +0 -27
  139. package/src/types/index.ts +0 -17
  140. package/src/types/message.ts +0 -145
  141. package/src/types/state.ts +0 -87
  142. package/src/types/transaction.ts +0 -69
  143. package/src/types/x402.ts +0 -251
  144. package/src/utils/ErrorRecoveryGuide.ts +0 -676
  145. package/src/utils/Helpers.ts +0 -688
  146. package/src/utils/IPFSClient.ts +0 -368
  147. package/src/utils/Logger.ts +0 -484
  148. package/src/utils/NonceManager.ts +0 -591
  149. package/src/utils/RateLimiter.ts +0 -534
  150. package/src/utils/ReceivedNonceTracker.ts +0 -567
  151. package/src/utils/SDKLifecycle.ts +0 -416
  152. package/src/utils/SecureNonce.ts +0 -78
  153. package/src/utils/Semaphore.ts +0 -276
  154. package/src/utils/UsedAttestationTracker.ts +0 -385
  155. package/src/utils/canonicalJson.ts +0 -38
  156. package/src/utils/circuitBreaker.ts +0 -324
  157. package/src/utils/computeTypeHash.ts +0 -48
  158. package/src/utils/fsSafe.ts +0 -80
  159. package/src/utils/index.ts +0 -80
  160. package/src/utils/retry.ts +0 -364
  161. package/src/utils/security.ts +0 -418
  162. package/src/utils/validation.ts +0 -540
  163. package/src/wallet/AutoWalletProvider.ts +0 -299
  164. package/src/wallet/EOAWalletProvider.ts +0 -69
  165. package/src/wallet/IWalletProvider.ts +0 -135
  166. package/src/wallet/aa/BundlerClient.ts +0 -274
  167. package/src/wallet/aa/DualNonceManager.ts +0 -173
  168. package/src/wallet/aa/PaymasterClient.ts +0 -174
  169. package/src/wallet/aa/TransactionBatcher.ts +0 -353
  170. package/src/wallet/aa/UserOpBuilder.ts +0 -246
  171. package/src/wallet/aa/constants.ts +0 -60
  172. package/src/wallet/keystore.ts +0 -240
@@ -1,247 +0,0 @@
1
- /**
2
- * Register Command — Register agent on AgentRegistry for gas-free transactions.
3
- *
4
- * Parses AGIRAILS.md for service descriptors and endpoint, then registers
5
- * on-chain via a single gasless UserOp (bootstrap-allowed).
6
- *
7
- * For testnet: also mints 1000 test USDC in the same UserOp.
8
- *
9
- * WARNING: This creates a NEW Smart Wallet address (different from EOA).
10
- * Old EOA reputation/history does not transfer.
11
- *
12
- * @module cli/commands/register
13
- */
14
-
15
- import * as fs from 'fs';
16
- import * as path from 'path';
17
- import { Command } from 'commander';
18
- import { Output, ExitCode } from '../utils/output';
19
- import { loadConfig, updateConfig } from '../utils/config';
20
- import { resolvePrivateKey } from '../../wallet/keystore';
21
-
22
- // ============================================================================
23
- // Command Definition
24
- // ============================================================================
25
-
26
- export function createRegisterCommand(): Command {
27
- const cmd = new Command('register')
28
- .description('Register agent on AgentRegistry for gas-free transactions')
29
- .option('--endpoint <url>', 'Service endpoint URL (overrides AGIRAILS.md)')
30
- .option('--force-legacy', 'Bypass deprecation and run legacy registration')
31
- .option('--json', 'Output as JSON')
32
- .option('-q, --quiet', 'Minimal output')
33
- .action(async (options) => {
34
- const output = new Output(
35
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
36
- );
37
-
38
- // Deprecation warning
39
- output.warning(
40
- 'DEPRECATED: "actp register" is deprecated. Use "actp publish" instead.\n' +
41
- ' "actp publish" saves config locally. On-chain activation happens\n' +
42
- ' automatically on your first payment (lazy publish).'
43
- );
44
- output.blank();
45
-
46
- if (!options.forceLegacy) {
47
- output.print('To proceed anyway, use: actp register --force-legacy');
48
- return;
49
- }
50
-
51
- try {
52
- await runRegister(options, output);
53
- } catch (error) {
54
- output.errorResult({
55
- code: 'REGISTER_FAILED',
56
- message: (error as Error).message,
57
- });
58
- process.exit(ExitCode.ERROR);
59
- }
60
- });
61
-
62
- return cmd;
63
- }
64
-
65
- // ============================================================================
66
- // Implementation
67
- // ============================================================================
68
-
69
- async function runRegister(
70
- options: { endpoint?: string },
71
- output: Output
72
- ): Promise<void> {
73
- const projectRoot = process.cwd();
74
-
75
- // Load config
76
- const config = loadConfig(projectRoot);
77
- if (!config) {
78
- throw new Error(
79
- 'ACTP not initialized. Run "actp init" first.'
80
- );
81
- }
82
-
83
- if (config.mode === 'mock') {
84
- throw new Error('Registration is not available in mock mode.');
85
- }
86
-
87
- // Resolve private key
88
- const privateKey = await resolvePrivateKey(projectRoot, { network: config.mode });
89
- if (!privateKey) {
90
- throw new Error(
91
- 'No wallet found. Run "actp init" first to generate a wallet.'
92
- );
93
- }
94
-
95
- // Parse AGIRAILS.md for service descriptors
96
- const { parseAgirailsMd } = await import('../../config/agirailsmd');
97
- const { extractRegistrationParams } = await import('../../config/publishPipeline');
98
-
99
- const agirailsMdPath = path.join(projectRoot, 'AGIRAILS.md');
100
- let endpoint = options.endpoint || '';
101
- let serviceDescriptors;
102
-
103
- if (fs.existsSync(agirailsMdPath)) {
104
- const content = fs.readFileSync(agirailsMdPath, 'utf-8');
105
- const parsed = parseAgirailsMd(content);
106
- const regParams = extractRegistrationParams(parsed.frontmatter);
107
- endpoint = options.endpoint || regParams.endpoint;
108
- serviceDescriptors = regParams.serviceDescriptors;
109
- output.info(`Parsed ${serviceDescriptors.length} service(s) from AGIRAILS.md`);
110
- } else {
111
- // No AGIRAILS.md — use minimal defaults
112
- const { ethers: ethersLib } = await import('ethers');
113
- const serviceType = 'general';
114
- serviceDescriptors = [{
115
- serviceTypeHash: ethersLib.keccak256(ethersLib.toUtf8Bytes(serviceType)),
116
- serviceType,
117
- schemaURI: '',
118
- minPrice: 0n,
119
- maxPrice: 1_000_000_000n, // 1000 USDC
120
- avgCompletionTime: 3600,
121
- metadataCID: '',
122
- }];
123
- output.warning('No AGIRAILS.md found. Using default "general" service descriptor.');
124
- output.info('Create AGIRAILS.md with services to customize registration.');
125
- }
126
-
127
- // Dynamic imports
128
- const { ethers } = await import('ethers');
129
- const { getNetwork } = await import('../../config/networks');
130
- const { AutoWalletProvider } = await import('../../wallet/AutoWalletProvider');
131
- const { buildRegisterAgentBatch, buildTestnetInitBatch } = await import('../../wallet/aa/TransactionBatcher');
132
-
133
- const network = config.mode === 'testnet' ? 'base-sepolia' : 'base-mainnet';
134
- const networkConfig = getNetwork(network);
135
-
136
- if (!networkConfig.aa) {
137
- throw new Error(
138
- `AA configuration not available for ${config.mode}. ` +
139
- 'Smart Wallet registration requires AA support.'
140
- );
141
- }
142
-
143
- if (!networkConfig.contracts.agentRegistry) {
144
- throw new Error(
145
- 'AgentRegistry contract not deployed yet. Registration not available.'
146
- );
147
- }
148
-
149
- const rpcUrl = networkConfig.rpcUrl;
150
- const provider = new ethers.JsonRpcProvider(rpcUrl);
151
- const signer = new ethers.Wallet(privateKey, provider);
152
-
153
- output.info('Creating Smart Wallet...');
154
-
155
- const autoWallet = await AutoWalletProvider.create({
156
- signer,
157
- provider,
158
- chainId: networkConfig.chainId,
159
- actpKernelAddress: networkConfig.contracts.actpKernel,
160
- bundler: {
161
- primaryUrl: networkConfig.aa.bundlerUrls.coinbase,
162
- backupUrl: networkConfig.aa.bundlerUrls.pimlico,
163
- },
164
- paymaster: {
165
- primaryUrl: networkConfig.aa.paymasterUrls.coinbase,
166
- backupUrl: networkConfig.aa.paymasterUrls.pimlico,
167
- },
168
- });
169
-
170
- const smartWalletAddress = autoWallet.getAddress();
171
- output.info(`Smart Wallet: ${smartWalletAddress}`);
172
-
173
- if (smartWalletAddress.toLowerCase() !== config.address.toLowerCase()) {
174
- output.warning(
175
- `This creates a NEW address: ${smartWalletAddress}\n` +
176
- ` Your current address: ${config.address}\n` +
177
- ' Old address reputation/history does NOT transfer.'
178
- );
179
- }
180
-
181
- // Build batch — testnet gets register + mint, mainnet gets register only
182
- let calls;
183
- if (config.mode === 'testnet') {
184
- output.info('Testnet mode: will register + mint 1000 test USDC in one UserOp');
185
- calls = buildTestnetInitBatch({
186
- agentRegistryAddress: networkConfig.contracts.agentRegistry,
187
- endpoint,
188
- serviceDescriptors,
189
- mockUsdcAddress: networkConfig.contracts.usdc,
190
- recipient: smartWalletAddress,
191
- mintAmount: '1000000000', // 1000 USDC
192
- });
193
- } else {
194
- calls = buildRegisterAgentBatch(
195
- networkConfig.contracts.agentRegistry,
196
- endpoint,
197
- serviceDescriptors
198
- );
199
- }
200
-
201
- // Convert SmartWalletCall[] to TransactionRequest[]
202
- const txRequests = calls.map((c) => ({
203
- to: c.target,
204
- data: c.data,
205
- value: c.value.toString(),
206
- }));
207
-
208
- output.info('Submitting registration (gasless UserOp)...');
209
-
210
- const receipt = await autoWallet.sendBatchTransaction(txRequests);
211
-
212
- if (!receipt.success) {
213
- throw new Error(`Registration UserOp failed: ${receipt.hash}`);
214
- }
215
-
216
- output.success('Agent registered on AgentRegistry');
217
- if (config.mode === 'testnet') {
218
- output.success('Minted 1,000 test USDC to Smart Wallet');
219
- }
220
-
221
- // Update config: flip address to Smart Wallet, mark as registered
222
- updateConfig({
223
- address: smartWalletAddress.toLowerCase(),
224
- smartWallet: smartWalletAddress.toLowerCase(),
225
- registered: true,
226
- }, projectRoot);
227
- output.info('Config updated: address set to Smart Wallet');
228
-
229
- output.blank();
230
- output.result(
231
- {
232
- registered: true,
233
- smartWallet: smartWalletAddress,
234
- services: serviceDescriptors.length,
235
- txHash: receipt.hash,
236
- ...(config.mode === 'testnet' && { mintedUSDC: '1000' }),
237
- },
238
- { quietKey: 'smartWallet' }
239
- );
240
-
241
- output.blank();
242
- output.print('Your agent now has gas-free transactions!');
243
- output.print(`Smart Wallet address: ${smartWalletAddress}`);
244
- output.print('');
245
- output.print('To use gas-free mode, add to your agent config:');
246
- output.print(' wallet: "auto"');
247
- }
@@ -1,345 +0,0 @@
1
- /**
2
- * Simulate Command - Dry-run commands without executing
3
- *
4
- * Agent-first feature: Preview what a command would do
5
- * without actually executing it. Perfect for:
6
- * - Testing scripts before running on mainnet
7
- * - Understanding fee calculations
8
- * - Validating input parameters
9
- *
10
- * @module cli/commands/simulate
11
- */
12
-
13
- import { Command } from 'commander';
14
- import { Output, ExitCode, fmt } from '../utils/output';
15
- import { loadConfig } from '../utils/config';
16
- import { mapError } from '../utils/client';
17
- import { BaseAdapter } from '../../adapters/BaseAdapter';
18
-
19
- // ============================================================================
20
- // Command Definition
21
- // ============================================================================
22
-
23
- export function createSimulateCommand(): Command {
24
- const cmd = new Command('simulate')
25
- .description('Dry-run a command without executing (agent-first feature)');
26
-
27
- cmd.addCommand(createSimulatePayCommand());
28
- cmd.addCommand(createSimulateFeeCommand());
29
-
30
- return cmd;
31
- }
32
-
33
- // ============================================================================
34
- // simulate pay
35
- // ============================================================================
36
-
37
- function createSimulatePayCommand(): Command {
38
- return new Command('pay')
39
- .description('Simulate a payment transaction')
40
- .argument('<to>', 'Provider address')
41
- .argument('<amount>', 'Amount to pay')
42
- .option('-d, --deadline <deadline>', 'Deadline (+24h, +7d, or Unix timestamp)', '+24h')
43
- .option('--json', 'Output as JSON')
44
- .action(async (to, amount, options) => {
45
- const output = new Output(options.json ? 'json' : 'human');
46
-
47
- try {
48
- await runSimulatePay(to, amount, options, output);
49
- } catch (error) {
50
- const structuredError = mapError(error);
51
- output.errorResult({
52
- code: structuredError.code,
53
- message: structuredError.message,
54
- });
55
- process.exit(ExitCode.ERROR);
56
- }
57
- });
58
- }
59
-
60
- interface SimulatePayOptions {
61
- deadline: string;
62
- json?: boolean;
63
- }
64
-
65
- async function runSimulatePay(
66
- to: string,
67
- amount: string,
68
- options: SimulatePayOptions,
69
- output: Output
70
- ): Promise<void> {
71
- const config = loadConfig();
72
-
73
- // Create a mock adapter just for parsing
74
- const adapter = new SimulationAdapter(config.address);
75
-
76
- // Validate and parse inputs
77
- const validation = adapter.validatePayment(to, amount, options.deadline);
78
-
79
- if (!validation.valid) {
80
- output.result({
81
- valid: false,
82
- errors: validation.errors,
83
- });
84
- process.exit(ExitCode.INVALID_INPUT);
85
- }
86
-
87
- // Calculate fee
88
- const amountWei = validation.parsedAmount!;
89
- const feeCalculation = calculateFee(amountWei);
90
-
91
- const result = {
92
- valid: true,
93
- simulation: {
94
- action: 'CREATE_AND_FUND_TRANSACTION',
95
- provider: to.toLowerCase(),
96
- requester: config.address.toLowerCase(),
97
- amount: formatUsdc(amountWei.toString()) + ' USDC',
98
- amountWei: amountWei.toString(),
99
- deadline: new Date(validation.parsedDeadline! * 1000).toISOString(),
100
- deadlineUnix: validation.parsedDeadline,
101
- },
102
- fees: {
103
- platformFee: formatUsdc(feeCalculation.fee.toString()) + ' USDC',
104
- platformFeeWei: feeCalculation.fee.toString(),
105
- feeRate: feeCalculation.effectiveRate,
106
- providerReceives: formatUsdc(feeCalculation.providerReceives.toString()) + ' USDC',
107
- minimumApplied: feeCalculation.minimumApplied,
108
- },
109
- requirements: {
110
- requiredBalance: formatUsdc((amountWei + feeCalculation.fee).toString()) + ' USDC',
111
- mode: config.mode,
112
- },
113
- warnings: validation.warnings,
114
- };
115
-
116
- if (options.json) {
117
- output.result(result);
118
- } else {
119
- output.section('Simulation Results');
120
- output.print('');
121
- output.print(fmt.bold('Transaction Details:'));
122
- output.keyValue(' Action', 'Create and fund transaction');
123
- output.keyValue(' Provider', to);
124
- output.keyValue(' Amount', result.simulation.amount);
125
- output.keyValue(' Deadline', result.simulation.deadline);
126
- output.print('');
127
- output.print(fmt.bold('Fee Breakdown (1% + $0.05 min):'));
128
- output.keyValue(' Platform Fee', result.fees.platformFee);
129
- output.keyValue(' Effective Rate', result.fees.feeRate);
130
- output.keyValue(' Provider Receives', result.fees.providerReceives);
131
- if (feeCalculation.minimumApplied) {
132
- output.print(fmt.yellow(' (Minimum fee applied - amount < $5)'));
133
- }
134
- output.print('');
135
- output.print(fmt.bold('Requirements:'));
136
- output.keyValue(' Required Balance', result.requirements.requiredBalance);
137
- output.keyValue(' Mode', result.requirements.mode);
138
-
139
- if (validation.warnings && validation.warnings.length > 0) {
140
- output.print('');
141
- output.print(fmt.yellow('Warnings:'));
142
- for (const warning of validation.warnings) {
143
- output.print(fmt.yellow(` - ${warning}`));
144
- }
145
- }
146
-
147
- output.print('');
148
- output.success('Simulation complete - no transaction created');
149
- }
150
- }
151
-
152
- // ============================================================================
153
- // simulate fee
154
- // ============================================================================
155
-
156
- function createSimulateFeeCommand(): Command {
157
- return new Command('fee')
158
- .description('Calculate fee for a given amount')
159
- .argument('<amount>', 'Amount to calculate fee for')
160
- .option('--json', 'Output as JSON')
161
- .action(async (amount, options) => {
162
- const output = new Output(options.json ? 'json' : 'human');
163
-
164
- try {
165
- // Parse amount
166
- const adapter = new SimulationAdapter('0x0000000000000000000000000000000000000000');
167
- const parsedAmount = adapter.parseAmountPublic(amount);
168
-
169
- const feeCalc = calculateFee(parsedAmount);
170
-
171
- const result = {
172
- amount: formatUsdc(parsedAmount.toString()) + ' USDC',
173
- amountWei: parsedAmount.toString(),
174
- fee: formatUsdc(feeCalc.fee.toString()) + ' USDC',
175
- feeWei: feeCalc.fee.toString(),
176
- effectiveRate: feeCalc.effectiveRate,
177
- providerReceives: formatUsdc(feeCalc.providerReceives.toString()) + ' USDC',
178
- totalRequired: formatUsdc((parsedAmount + feeCalc.fee).toString()) + ' USDC',
179
- minimumApplied: feeCalc.minimumApplied,
180
- };
181
-
182
- if (options.json) {
183
- output.result(result);
184
- } else {
185
- output.section('Fee Calculation');
186
- output.keyValue('Amount', result.amount);
187
- output.keyValue('Platform Fee', result.fee);
188
- output.keyValue('Effective Rate', result.effectiveRate);
189
- output.keyValue('Provider Receives', result.providerReceives);
190
- output.keyValue('Total Required', result.totalRequired);
191
- if (feeCalc.minimumApplied) {
192
- output.print('');
193
- output.info('Minimum fee ($0.05) applied because amount < $5');
194
- }
195
- }
196
- } catch (error) {
197
- const structuredError = mapError(error);
198
- output.errorResult({
199
- code: structuredError.code,
200
- message: structuredError.message,
201
- });
202
- process.exit(ExitCode.ERROR);
203
- }
204
- });
205
- }
206
-
207
- // ============================================================================
208
- // Helpers
209
- // ============================================================================
210
-
211
- /**
212
- * Extended adapter for simulation purposes
213
- */
214
- class SimulationAdapter extends BaseAdapter {
215
- constructor(requesterAddress: string) {
216
- super(requesterAddress);
217
- }
218
-
219
- /**
220
- * Validate payment parameters without executing
221
- */
222
- validatePayment(
223
- to: string,
224
- amount: string | number,
225
- deadline?: string | number
226
- ): {
227
- valid: boolean;
228
- errors: string[];
229
- warnings: string[];
230
- parsedAmount?: bigint;
231
- parsedDeadline?: number;
232
- } {
233
- const errors: string[] = [];
234
- const warnings: string[] = [];
235
- let parsedAmount: bigint | undefined;
236
- let parsedDeadline: number | undefined;
237
-
238
- // Validate address
239
- try {
240
- this.validateAddress(to, 'to');
241
- } catch (e) {
242
- errors.push((e as Error).message);
243
- }
244
-
245
- // Validate amount
246
- try {
247
- parsedAmount = this.parseAmount(amount);
248
-
249
- // Check for small amount warning
250
- if (parsedAmount < 5_000_000n) {
251
- // Less than $5
252
- warnings.push('Amount is below $5 - minimum fee ($0.05) will apply');
253
- }
254
- } catch (e) {
255
- errors.push((e as Error).message);
256
- }
257
-
258
- // Validate deadline
259
- try {
260
- const currentTime = Math.floor(Date.now() / 1000);
261
- parsedDeadline = this.parseDeadline(deadline, currentTime);
262
-
263
- if (parsedDeadline <= currentTime) {
264
- errors.push('Deadline must be in the future');
265
- }
266
-
267
- // Check for short deadline warning
268
- const hoursUntilDeadline = (parsedDeadline - currentTime) / 3600;
269
- if (hoursUntilDeadline < 1) {
270
- warnings.push('Deadline is less than 1 hour from now');
271
- }
272
- } catch (e) {
273
- errors.push((e as Error).message);
274
- }
275
-
276
- // Check self-payment
277
- if (to.toLowerCase() === this.requesterAddress.toLowerCase()) {
278
- errors.push('Cannot pay yourself');
279
- }
280
-
281
- return {
282
- valid: errors.length === 0,
283
- errors,
284
- warnings,
285
- parsedAmount,
286
- parsedDeadline,
287
- };
288
- }
289
-
290
- /**
291
- * Public access to parseAmount for fee calculation
292
- */
293
- parseAmountPublic(amount: string | number): bigint {
294
- return this.parseAmount(amount);
295
- }
296
- }
297
-
298
- /**
299
- * Calculate platform fee based on AGIRAILS fee model:
300
- * - 1% of transaction amount
301
- * - $0.05 minimum
302
- */
303
- function calculateFee(amountWei: bigint): {
304
- fee: bigint;
305
- providerReceives: bigint;
306
- effectiveRate: string;
307
- minimumApplied: boolean;
308
- } {
309
- // 1% fee
310
- const percentFee = amountWei / 100n;
311
-
312
- // $0.05 minimum in wei (6 decimals)
313
- const minimumFeeWei = 50_000n;
314
-
315
- // Apply minimum
316
- const fee = percentFee > minimumFeeWei ? percentFee : minimumFeeWei;
317
- const minimumApplied = percentFee < minimumFeeWei;
318
-
319
- // Calculate what provider receives (amount minus fee)
320
- const providerReceives = amountWei - fee;
321
-
322
- // Calculate effective rate
323
- const effectiveRatePercent = (Number(fee) / Number(amountWei)) * 100;
324
- const effectiveRate = effectiveRatePercent.toFixed(2) + '%';
325
-
326
- return {
327
- fee,
328
- providerReceives,
329
- effectiveRate,
330
- minimumApplied,
331
- };
332
- }
333
-
334
- /**
335
- * Format USDC amount from wei to decimal
336
- */
337
- function formatUsdc(weiAmount: string): string {
338
- const amount = BigInt(weiAmount);
339
- const whole = amount / 1_000_000n;
340
- const decimal = amount % 1_000_000n;
341
- const decimalStr = decimal.toString().padStart(6, '0').slice(0, 2);
342
- return `${whole}.${decimalStr}`;
343
- }
344
-
345
- export { runSimulatePay, calculateFee };