@agentbadge/circle-payments 0.1.13 → 0.1.14

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.
@@ -0,0 +1,45 @@
1
+ import type { GatewayBalanceInfo } from "../balance";
2
+ import type { Caip2, SupportedChain } from "../chains";
3
+ export interface DepositWalletClient {
4
+ account?: {
5
+ address: `0x${string}`;
6
+ } | `0x${string}`;
7
+ writeContract(args: {
8
+ address: `0x${string}`;
9
+ abi: unknown;
10
+ functionName: string;
11
+ args: readonly unknown[];
12
+ }): Promise<`0x${string}`>;
13
+ }
14
+ export interface DepositResult {
15
+ txHash: `0x${string}`;
16
+ expectedCreditAt: string;
17
+ network: Caip2;
18
+ }
19
+ export interface GatewayDepositInfoEntry {
20
+ chain: string;
21
+ network: Caip2;
22
+ gatewayWallet: `0x${string}`;
23
+ usdc: `0x${string}`;
24
+ minDeposit: string;
25
+ creditEstimateSec: number;
26
+ transferUri: string;
27
+ explorerUrl?: string;
28
+ }
29
+ export interface GatewayDepositInfo {
30
+ chains: GatewayDepositInfoEntry[];
31
+ minDeposit: string;
32
+ howto: string;
33
+ }
34
+ export declare function gatewayDepositInfo(chains: SupportedChain[], minDepositUsd: string): GatewayDepositInfo;
35
+ export declare function depositToGateway(args: {
36
+ wallet: DepositWalletClient;
37
+ chain: SupportedChain;
38
+ amountUsd: string;
39
+ }): Promise<DepositResult>;
40
+ export declare function gatewayBalance(args: {
41
+ depositor: `0x${string}`;
42
+ chain: SupportedChain;
43
+ apiUrl?: string;
44
+ fetchImpl?: typeof fetch;
45
+ }): Promise<GatewayBalanceInfo | null>;
@@ -0,0 +1,84 @@
1
+ import { erc20Abi, parseUnits } from "viem";
2
+ const CREDIT_ESTIMATE_SEC = {
3
+ 6: 40,
4
+ 26: 10,
5
+ };
6
+ function estimateFor(chain) {
7
+ if (chain.domain !== undefined && CREDIT_ESTIMATE_SEC[chain.domain]) {
8
+ return CREDIT_ESTIMATE_SEC[chain.domain];
9
+ }
10
+ return 40;
11
+ }
12
+ export function gatewayDepositInfo(chains, minDepositUsd) {
13
+ return {
14
+ minDeposit: minDepositUsd,
15
+ chains: chains
16
+ .filter((c) => c.gatewayWallet !== undefined)
17
+ .map((c) => ({
18
+ chain: c.name,
19
+ network: c.caip2,
20
+ gatewayWallet: c.gatewayWallet,
21
+ usdc: c.usdc,
22
+ minDeposit: minDepositUsd,
23
+ creditEstimateSec: estimateFor(c),
24
+ transferUri: `ethereum:${c.gatewayWallet}@${c.chainId}?value=0`,
25
+ ...(c.explorerUrl ? { explorerUrl: c.explorerUrl } : {}),
26
+ })),
27
+ howto: "transfer USDC to gatewayWallet on your chain → spendable unified " +
28
+ "balance after source finality; then pay via signed burn intents",
29
+ };
30
+ }
31
+ export async function depositToGateway(args) {
32
+ const { wallet, chain } = args;
33
+ if (!chain.gatewayWallet) {
34
+ throw new Error(`chain ${chain.name} has no GatewayWallet`);
35
+ }
36
+ const amount = parseUnits(args.amountUsd, chain.usdcDecimals);
37
+ if (amount <= 0n) {
38
+ throw new Error(`deposit amount must be > 0 (got ${args.amountUsd})`);
39
+ }
40
+ const txHash = await wallet.writeContract({
41
+ address: chain.usdc,
42
+ abi: erc20Abi,
43
+ functionName: "transfer",
44
+ args: [chain.gatewayWallet, amount],
45
+ });
46
+ const eta = new Date(Date.now() + estimateFor(chain) * 1000);
47
+ return {
48
+ txHash,
49
+ expectedCreditAt: eta.toISOString(),
50
+ network: chain.caip2,
51
+ };
52
+ }
53
+ export async function gatewayBalance(args) {
54
+ const apiUrl = (args.apiUrl ?? args.chain.facilitatorUrl)?.replace(/\/+$/, "");
55
+ if (!apiUrl || args.chain.domain === undefined)
56
+ return null;
57
+ const fetchImpl = args.fetchImpl ?? fetch;
58
+ try {
59
+ const resp = await fetchImpl(`${apiUrl}/balances`, {
60
+ method: "POST",
61
+ headers: { "Content-Type": "application/json" },
62
+ body: JSON.stringify({
63
+ token: "USDC",
64
+ sources: [
65
+ { depositor: args.depositor, domain: args.chain.domain },
66
+ ],
67
+ }),
68
+ });
69
+ if (!resp.ok)
70
+ return null;
71
+ const data = (await resp.json());
72
+ const row = data.balances?.[0];
73
+ if (!row)
74
+ return null;
75
+ return {
76
+ available: row.balance ?? "0",
77
+ withdrawing: row.withdrawing ?? "0",
78
+ withdrawable: row.withdrawable ?? "0",
79
+ };
80
+ }
81
+ catch {
82
+ return null;
83
+ }
84
+ }
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./schemes/arc-self-settle.js";
6
6
  export * from "./router.js";
7
7
  export * from "./middleware.js";
8
8
  export * from "./gateway-probe.js";
9
+ export * from "./gateway/deposit.js";
9
10
  export * from "./identity.js";
10
11
  export * from "./ledger.js";
11
12
  export * from "./pricing.js";
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./schemes/arc-self-settle.js";
6
6
  export * from "./router.js";
7
7
  export * from "./middleware.js";
8
8
  export * from "./gateway-probe.js";
9
+ export * from "./gateway/deposit.js";
9
10
  export * from "./identity.js";
10
11
  export * from "./ledger.js";
11
12
  export * from "./pricing.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/circle-payments",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Circle Gateway nanopayments + x402 payment rails (Base Sepolia, Arc) for AgentBadge — trusted agent payments",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",