8004agent-cli 1.0.0 → 1.0.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.
- package/package.json +1 -1
- package/src/lib/payment.js +17 -10
package/package.json
CHANGED
package/src/lib/payment.js
CHANGED
|
@@ -3,7 +3,19 @@ import { Facinet } from 'facinet';
|
|
|
3
3
|
import { FACINET_NETWORK_MAP, TREASURY_ADDRESS, CONTRACTS, RELAYER_PRIVATE_KEY } from './constants.js';
|
|
4
4
|
import { USDC_ERC3009_ABI } from './abi.js';
|
|
5
5
|
import { getProvider, getSigner } from './web3.js';
|
|
6
|
-
import { useBackend, apiSubmitGaslessPayment } from './api.js';
|
|
6
|
+
import { useBackend, apiSubmitGaslessPayment, apiPaymentConfig } from './api.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the treasury address: use local env, or fetch from backend.
|
|
10
|
+
*/
|
|
11
|
+
const getTreasuryAddress = async () => {
|
|
12
|
+
if (TREASURY_ADDRESS) return TREASURY_ADDRESS;
|
|
13
|
+
if (useBackend()) {
|
|
14
|
+
const config = await apiPaymentConfig();
|
|
15
|
+
if (config.treasuryAddress) return config.treasuryAddress;
|
|
16
|
+
}
|
|
17
|
+
throw new Error('TREASURY_ADDRESS is not configured and backend is unreachable.');
|
|
18
|
+
};
|
|
7
19
|
|
|
8
20
|
/**
|
|
9
21
|
* Pay via Facinet x402 (gasless ERC-3009 USDC transfer).
|
|
@@ -16,13 +28,11 @@ export const payWithFacinet = async ({ amount, privateKey, networkKey }) => {
|
|
|
16
28
|
throw new Error(`Network "${networkKey}" not supported by Facinet. Supported: ${Object.keys(FACINET_NETWORK_MAP).join(', ')}`);
|
|
17
29
|
}
|
|
18
30
|
|
|
19
|
-
|
|
20
|
-
throw new Error('TREASURY_ADDRESS is not configured. Set it in .env.');
|
|
21
|
-
}
|
|
31
|
+
const treasury = await getTreasuryAddress();
|
|
22
32
|
|
|
23
33
|
const result = await Facinet.quickPay({
|
|
24
34
|
amount,
|
|
25
|
-
recipient:
|
|
35
|
+
recipient: treasury,
|
|
26
36
|
privateKey,
|
|
27
37
|
network: facinetNetwork,
|
|
28
38
|
});
|
|
@@ -66,10 +76,7 @@ const getUsdcAddress = (networkKey) => {
|
|
|
66
76
|
export const buildGaslessPaymentData = async (userAddress, amount, networkKey) => {
|
|
67
77
|
const netConfig = CONTRACTS[networkKey];
|
|
68
78
|
const usdcAddress = getUsdcAddress(networkKey);
|
|
69
|
-
|
|
70
|
-
if (!TREASURY_ADDRESS) {
|
|
71
|
-
throw new Error('TREASURY_ADDRESS is not configured. Set it in .env.');
|
|
72
|
-
}
|
|
79
|
+
const treasury = await getTreasuryAddress();
|
|
73
80
|
|
|
74
81
|
const provider = getProvider(netConfig.rpc);
|
|
75
82
|
const usdc = new ethers.Contract(usdcAddress, USDC_ERC3009_ABI, provider);
|
|
@@ -113,7 +120,7 @@ export const buildGaslessPaymentData = async (userAddress, amount, networkKey) =
|
|
|
113
120
|
|
|
114
121
|
const message = {
|
|
115
122
|
from: userAddress,
|
|
116
|
-
to:
|
|
123
|
+
to: treasury,
|
|
117
124
|
value: value.toString(),
|
|
118
125
|
validAfter,
|
|
119
126
|
validBefore,
|