moltspay 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -11
- package/dist/cli.js +88 -0
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +88 -0
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +88 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +88 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -224,25 +224,24 @@ Standard templates for natural A2A dialogue:
|
|
|
224
224
|
import { SellerTemplates, BuyerTemplates, parseStatusMarker } from 'moltspay';
|
|
225
225
|
|
|
226
226
|
// Seller templates
|
|
227
|
-
SellerTemplates.askPaymentCapability()
|
|
228
|
-
SellerTemplates.guideInstall()
|
|
229
|
-
SellerTemplates.guideFunding()
|
|
230
|
-
SellerTemplates.guidePermit(agentAddr, 10) // "请向 Boss 发送..."
|
|
227
|
+
SellerTemplates.askPaymentCapability() // "Do you have USDC payment capability?"
|
|
228
|
+
SellerTemplates.guideInstall() // "Install moltspay and init wallet..."
|
|
229
|
+
SellerTemplates.guideFunding(agentAddr, 10) // "Ask Owner to send USDC to your wallet"
|
|
231
230
|
SellerTemplates.quote({ service, price, recipientAddress })
|
|
232
231
|
SellerTemplates.verificationPassed(amount)
|
|
233
232
|
SellerTemplates.deliver({ downloadUrl, fileHash })
|
|
234
233
|
SellerTemplates.receipt(receipt)
|
|
235
234
|
|
|
236
235
|
// Buyer templates
|
|
237
|
-
BuyerTemplates.requestService('
|
|
238
|
-
BuyerTemplates.noCapability()
|
|
239
|
-
BuyerTemplates.walletCreated(address)
|
|
240
|
-
BuyerTemplates.
|
|
241
|
-
BuyerTemplates.
|
|
242
|
-
BuyerTemplates.paymentSent(txHash, amount)
|
|
236
|
+
BuyerTemplates.requestService('video generation')
|
|
237
|
+
BuyerTemplates.noCapability() // "I don't have a wallet"
|
|
238
|
+
BuyerTemplates.walletCreated(address) // "[status:wallet_ready]"
|
|
239
|
+
BuyerTemplates.fundingReceived(10) // "[status:funded USDC=10]"
|
|
240
|
+
BuyerTemplates.requestFunding(addr, 10) // "Owner, please send USDC to my wallet"
|
|
241
|
+
BuyerTemplates.paymentSent(txHash, amount) // "[status:payment_sent tx=...]"
|
|
243
242
|
|
|
244
243
|
// Parse status markers from messages
|
|
245
|
-
const status = parseStatusMarker('[
|
|
244
|
+
const status = parseStatusMarker('[status:payment_sent tx=0xabc amount=3.99 USDC]');
|
|
246
245
|
// { type: 'payment_sent', data: { txHash: '0xabc', amount: '3.99' } }
|
|
247
246
|
```
|
|
248
247
|
|
package/dist/cli.js
CHANGED
|
@@ -348,6 +348,94 @@ var init_AgentWallet = __esm({
|
|
|
348
348
|
};
|
|
349
349
|
}
|
|
350
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Get USDC balance
|
|
353
|
+
*/
|
|
354
|
+
async getBalance() {
|
|
355
|
+
const usdcContract = new import_ethers2.ethers.Contract(
|
|
356
|
+
this.chainConfig.usdc,
|
|
357
|
+
PERMIT_ABI,
|
|
358
|
+
this.provider
|
|
359
|
+
);
|
|
360
|
+
const [usdcBalance, ethBalance] = await Promise.all([
|
|
361
|
+
usdcContract.balanceOf(this.address),
|
|
362
|
+
this.provider.getBalance(this.address)
|
|
363
|
+
]);
|
|
364
|
+
return {
|
|
365
|
+
usdc: (Number(usdcBalance) / 1e6).toFixed(2),
|
|
366
|
+
eth: import_ethers2.ethers.formatEther(ethBalance)
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Transfer USDC to a recipient (direct payment)
|
|
371
|
+
*
|
|
372
|
+
* This is the simplest payment method - Agent pays directly from its wallet.
|
|
373
|
+
* Requires Agent wallet to have USDC (funded by Owner).
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```typescript
|
|
377
|
+
* const wallet = new AgentWallet({ chain: 'base' });
|
|
378
|
+
*
|
|
379
|
+
* // Check balance
|
|
380
|
+
* const balance = await wallet.getBalance();
|
|
381
|
+
* console.log('USDC:', balance.usdc);
|
|
382
|
+
*
|
|
383
|
+
* // Pay for service
|
|
384
|
+
* const result = await wallet.transfer({
|
|
385
|
+
* to: '0xServiceProvider...',
|
|
386
|
+
* amount: 0.99
|
|
387
|
+
* });
|
|
388
|
+
* console.log('Tx:', result.txHash);
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
async transfer(params) {
|
|
392
|
+
const { to, amount } = params;
|
|
393
|
+
try {
|
|
394
|
+
const toAddress = import_ethers2.ethers.getAddress(to);
|
|
395
|
+
const amountWei = BigInt(Math.floor(amount * 1e6));
|
|
396
|
+
const usdcContract = new import_ethers2.ethers.Contract(
|
|
397
|
+
this.chainConfig.usdc,
|
|
398
|
+
PERMIT_ABI,
|
|
399
|
+
this.wallet
|
|
400
|
+
);
|
|
401
|
+
const balance = await usdcContract.balanceOf(this.address);
|
|
402
|
+
if (BigInt(balance) < amountWei) {
|
|
403
|
+
return {
|
|
404
|
+
success: false,
|
|
405
|
+
error: `Insufficient USDC: have ${(Number(balance) / 1e6).toFixed(2)}, need ${amount}`
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
if (!await this.hasGas()) {
|
|
409
|
+
return {
|
|
410
|
+
success: false,
|
|
411
|
+
error: "Insufficient ETH for gas. Need at least 0.0005 ETH."
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
const tx = await usdcContract.transfer(toAddress, amountWei);
|
|
415
|
+
const receipt = await tx.wait();
|
|
416
|
+
if (receipt.status === 1) {
|
|
417
|
+
return {
|
|
418
|
+
success: true,
|
|
419
|
+
txHash: tx.hash,
|
|
420
|
+
from: this.address,
|
|
421
|
+
to: toAddress,
|
|
422
|
+
amount,
|
|
423
|
+
explorerUrl: `${this.chainConfig.explorerTx}${tx.hash}`
|
|
424
|
+
};
|
|
425
|
+
} else {
|
|
426
|
+
return {
|
|
427
|
+
success: false,
|
|
428
|
+
txHash: tx.hash,
|
|
429
|
+
error: "Transaction reverted"
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
} catch (error) {
|
|
433
|
+
return {
|
|
434
|
+
success: false,
|
|
435
|
+
error: error.message
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
}
|
|
351
439
|
/**
|
|
352
440
|
* Get gas balance (ETH needed for transactions)
|
|
353
441
|
*/
|