@unslept/sdk 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/abi.d.ts +1425 -0
- package/dist/abi.js +5 -0
- package/dist/chain.d.ts +67 -0
- package/dist/chain.js +9 -0
- package/dist/client.d.ts +90 -0
- package/dist/client.js +237 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +19 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/viem.d.ts +20 -0
- package/dist/viem.js +134 -0
- package/dist/x402.d.ts +112 -0
- package/dist/x402.js +82 -0
- package/package.json +51 -4
package/dist/abi.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Generated by contracts/scripts/export-abi.py. Do not edit by hand.
|
|
2
|
+
export const agentAccountAbi = [{ "type": "function", "name": "agentSigner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "type": "function", "name": "allowedPayee", "inputs": [{ "name": "", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "type": "function", "name": "isPayeeAllowed", "inputs": [{ "name": "payee", "type": "address", "internalType": "address" }, { "name": "domain", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "type": "function", "name": "liquid", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "owner", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "type": "function", "name": "parkedValue", "inputs": [], "outputs": [{ "name": "v", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "paused", "inputs": [], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "type": "function", "name": "policy", "inputs": [], "outputs": [{ "name": "perCallCap", "type": "uint128", "internalType": "uint128" }, { "name": "dailyCap", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" }, { "type": "function", "name": "positions", "inputs": [], "outputs": [{ "name": "", "type": "address[]", "internalType": "address[]" }], "stateMutability": "view" }, { "type": "function", "name": "remainingToday", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "requestPayment", "inputs": [{ "name": "r", "type": "tuple", "internalType": "struct AgentAccount.PaymentRequest", "components": [{ "name": "payee", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" }, { "name": "paymentId", "type": "bytes32", "internalType": "bytes32" }, { "name": "domain", "type": "string", "internalType": "string" }, { "name": "endpoint", "type": "string", "internalType": "string" }, { "name": "mode", "type": "uint8", "internalType": "enum ISettlementRouter.Mode" }] }], "outputs": [{ "name": "fee", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "revoke", "inputs": [], "outputs": [], "stateMutability": "nonpayable" }, { "type": "function", "name": "revoked", "inputs": [], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "type": "function", "name": "setDomain", "inputs": [{ "name": "domain", "type": "string", "internalType": "string" }, { "name": "allowed", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" }, { "type": "function", "name": "setPaused", "inputs": [{ "name": "p", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" }, { "type": "function", "name": "setPayee", "inputs": [{ "name": "payee", "type": "address", "internalType": "address" }, { "name": "allowed", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" }, { "type": "function", "name": "setPolicy", "inputs": [{ "name": "p", "type": "tuple", "internalType": "struct IAgentAccount.Policy", "components": [{ "name": "perCallCap", "type": "uint128", "internalType": "uint128" }, { "name": "dailyCap", "type": "uint128", "internalType": "uint128" }] }], "outputs": [], "stateMutability": "nonpayable" }, { "type": "function", "name": "spentToday", "inputs": [], "outputs": [{ "name": "", "type": "uint128", "internalType": "uint128" }], "stateMutability": "view" }, { "type": "function", "name": "totalDebt", "inputs": [], "outputs": [{ "name": "d", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "totalValue", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "withdraw", "inputs": [{ "name": "to", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" }, { "type": "event", "name": "PaymentRequested", "inputs": [{ "name": "paymentId", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "payee", "type": "address", "indexed": true, "internalType": "address" }, { "name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "fee", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "buffer", "type": "address", "indexed": false, "internalType": "address" }, { "name": "endpoint", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false }, { "type": "event", "name": "PolicySet", "inputs": [{ "name": "perCallCap", "type": "uint128", "indexed": false, "internalType": "uint128" }, { "name": "dailyCap", "type": "uint128", "indexed": false, "internalType": "uint128" }], "anonymous": false }, { "type": "error", "name": "AdapterNotRegistered", "inputs": [{ "name": "adapter", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "AgentPaused", "inputs": [] }, { "type": "error", "name": "AgentRevoked", "inputs": [] }, { "type": "error", "name": "AutoAllocateOff", "inputs": [] }, { "type": "error", "name": "DailyCapExceeded", "inputs": [{ "name": "spentToday", "type": "uint256", "internalType": "uint256" }, { "name": "amount", "type": "uint256", "internalType": "uint256" }, { "name": "cap", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "InsufficientLiquid", "inputs": [{ "name": "requested", "type": "uint256", "internalType": "uint256" }, { "name": "liquid", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "InvalidInitialization", "inputs": [] }, { "type": "error", "name": "NotAllocator", "inputs": [] }, { "type": "error", "name": "NotInitializing", "inputs": [] }, { "type": "error", "name": "NotOwner", "inputs": [] }, { "type": "error", "name": "NotSigner", "inputs": [] }, { "type": "error", "name": "PayeeNotAllowed", "inputs": [{ "name": "payee", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "PaymentIdUsed", "inputs": [{ "name": "paymentId", "type": "bytes32", "internalType": "bytes32" }] }, { "type": "error", "name": "PerCallCapExceeded", "inputs": [{ "name": "amount", "type": "uint256", "internalType": "uint256" }, { "name": "cap", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ReentrancyGuardReentrantCall", "inputs": [] }, { "type": "error", "name": "SafeERC20FailedOperation", "inputs": [{ "name": "token", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "TooManyPositions", "inputs": [] }, { "type": "error", "name": "WouldLeaveDebtUncovered", "inputs": [{ "name": "debt", "type": "uint256", "internalType": "uint256" }, { "name": "valueAfter", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ZeroAmount", "inputs": [] }];
|
|
3
|
+
export const settlementRouterAbi = [{ "type": "function", "name": "buffers", "inputs": [], "outputs": [{ "name": "list", "type": "address[]", "internalType": "address[]" }], "stateMutability": "view" }, { "type": "event", "name": "Settled", "inputs": [{ "name": "paymentId", "type": "bytes32", "indexed": true, "internalType": "bytes32" }, { "name": "agent", "type": "address", "indexed": true, "internalType": "address" }, { "name": "payee", "type": "address", "indexed": true, "internalType": "address" }, { "name": "buffer", "type": "address", "indexed": false, "internalType": "address" }, { "name": "recipient", "type": "address", "indexed": false, "internalType": "address" }, { "name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "fee", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "mode", "type": "uint8", "indexed": false, "internalType": "enum ISettlementRouter.Mode" }, { "name": "endpoint", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false }, { "type": "error", "name": "AccessControlBadConfirmation", "inputs": [] }, { "type": "error", "name": "AccessControlUnauthorizedAccount", "inputs": [{ "name": "account", "type": "address", "internalType": "address" }, { "name": "neededRole", "type": "bytes32", "internalType": "bytes32" }] }, { "type": "error", "name": "EnforcedPause", "inputs": [] }, { "type": "error", "name": "ExpectedPause", "inputs": [] }, { "type": "error", "name": "InsufficientBufferLiquidity", "inputs": [{ "name": "amount", "type": "uint256", "internalType": "uint256" }, { "name": "available", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "InsufficientCollateral", "inputs": [{ "name": "debtAfter", "type": "uint256", "internalType": "uint256" }, { "name": "accountValue", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "NoSigner", "inputs": [] }, { "type": "error", "name": "NotAgentAccount", "inputs": [{ "name": "caller", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "ReentrancyGuardReentrantCall", "inputs": [] }];
|
|
4
|
+
export const agentAccountFactoryAbi = [{ "type": "function", "name": "accountsOf", "inputs": [{ "name": "owner", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "address[]", "internalType": "address[]" }], "stateMutability": "view" }, { "type": "function", "name": "createAccount", "inputs": [{ "name": "signer", "type": "address", "internalType": "address" }, { "name": "policy", "type": "tuple", "internalType": "struct IAgentAccount.Policy", "components": [{ "name": "perCallCap", "type": "uint128", "internalType": "uint128" }, { "name": "dailyCap", "type": "uint128", "internalType": "uint128" }] }, { "name": "salt", "type": "bytes32", "internalType": "bytes32" }], "outputs": [{ "name": "account", "type": "address", "internalType": "address" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "isAccount", "inputs": [{ "name": "", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "type": "function", "name": "predictAddress", "inputs": [{ "name": "owner", "type": "address", "internalType": "address" }, { "name": "salt", "type": "bytes32", "internalType": "bytes32" }], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "type": "event", "name": "AccountCreated", "inputs": [{ "name": "account", "type": "address", "indexed": true, "internalType": "address" }, { "name": "owner", "type": "address", "indexed": true, "internalType": "address" }, { "name": "signer", "type": "address", "indexed": true, "internalType": "address" }, { "name": "treasury", "type": "bool", "indexed": false, "internalType": "bool" }], "anonymous": false }, { "type": "error", "name": "AccessControlBadConfirmation", "inputs": [] }, { "type": "error", "name": "AccessControlUnauthorizedAccount", "inputs": [{ "name": "account", "type": "address", "internalType": "address" }, { "name": "neededRole", "type": "bytes32", "internalType": "bytes32" }] }, { "type": "error", "name": "FailedDeployment", "inputs": [] }, { "type": "error", "name": "InsufficientBalance", "inputs": [{ "name": "balance", "type": "uint256", "internalType": "uint256" }, { "name": "needed", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ZeroAddress", "inputs": [] }];
|
|
5
|
+
export const unsleptBufferAbi = [{ "type": "function", "name": "asset", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "type": "function", "name": "available", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "balanceOf", "inputs": [{ "name": "account", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "claimWithdraw", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "assets", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "convertToAssets", "inputs": [{ "name": "shares", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "debtOf", "inputs": [{ "name": "agent", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "decimals", "inputs": [], "outputs": [{ "name": "", "type": "uint8", "internalType": "uint8" }], "stateMutability": "view" }, { "type": "function", "name": "deposit", "inputs": [{ "name": "assets", "type": "uint256", "internalType": "uint256" }, { "name": "receiver", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "maxDeposit", "inputs": [{ "name": "receiver", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "maxWithdraw", "inputs": [{ "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "outstandingPrincipal", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "previewRedeem", "inputs": [{ "name": "shares", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "processQueue", "inputs": [{ "name": "maxTickets", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "queueThresholdBps", "inputs": [], "outputs": [{ "name": "", "type": "uint16", "internalType": "uint16" }], "stateMutability": "view" }, { "type": "function", "name": "redeem", "inputs": [{ "name": "shares", "type": "uint256", "internalType": "uint256" }, { "name": "receiver", "type": "address", "internalType": "address" }, { "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "requestWithdraw", "inputs": [{ "name": "shares", "type": "uint256", "internalType": "uint256" }, { "name": "receiver", "type": "address", "internalType": "address" }, { "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "function", "name": "ticket", "inputs": [{ "name": "id", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "tuple", "internalType": "struct UnsleptBuffer.Ticket", "components": [{ "name": "owner", "type": "address", "internalType": "address" }, { "name": "receiver", "type": "address", "internalType": "address" }, { "name": "assets", "type": "uint128", "internalType": "uint128" }, { "name": "funded", "type": "bool", "internalType": "bool" }, { "name": "claimed", "type": "bool", "internalType": "bool" }] }], "stateMutability": "view" }, { "type": "function", "name": "ticketCount", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "totalAssets", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "utilizationBps", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "type": "function", "name": "withdraw", "inputs": [{ "name": "assets", "type": "uint256", "internalType": "uint256" }, { "name": "receiver", "type": "address", "internalType": "address" }, { "name": "owner_", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "nonpayable" }, { "type": "event", "name": "Advanced", "inputs": [{ "name": "agent", "type": "address", "indexed": true, "internalType": "address" }, { "name": "to", "type": "address", "indexed": true, "internalType": "address" }, { "name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "fee", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "Deposit", "inputs": [{ "name": "sender", "type": "address", "indexed": true, "internalType": "address" }, { "name": "owner", "type": "address", "indexed": true, "internalType": "address" }, { "name": "assets", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "shares", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "Repaid", "inputs": [{ "name": "agent", "type": "address", "indexed": true, "internalType": "address" }, { "name": "payer", "type": "address", "indexed": true, "internalType": "address" }, { "name": "principal", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "fee", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "Withdraw", "inputs": [{ "name": "sender", "type": "address", "indexed": true, "internalType": "address" }, { "name": "receiver", "type": "address", "indexed": true, "internalType": "address" }, { "name": "owner", "type": "address", "indexed": true, "internalType": "address" }, { "name": "assets", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "shares", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "WithdrawClaimed", "inputs": [{ "name": "ticket", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "receiver", "type": "address", "indexed": true, "internalType": "address" }, { "name": "assets", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "WithdrawFunded", "inputs": [{ "name": "ticket", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "assets", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "event", "name": "WithdrawRequested", "inputs": [{ "name": "ticket", "type": "uint256", "indexed": true, "internalType": "uint256" }, { "name": "owner", "type": "address", "indexed": true, "internalType": "address" }, { "name": "receiver", "type": "address", "indexed": false, "internalType": "address" }, { "name": "shares", "type": "uint256", "indexed": false, "internalType": "uint256" }, { "name": "assets", "type": "uint256", "indexed": false, "internalType": "uint256" }], "anonymous": false }, { "type": "error", "name": "AccessControlBadConfirmation", "inputs": [] }, { "type": "error", "name": "AccessControlUnauthorizedAccount", "inputs": [{ "name": "account", "type": "address", "internalType": "address" }, { "name": "neededRole", "type": "bytes32", "internalType": "bytes32" }] }, { "type": "error", "name": "AdapterLimit", "inputs": [] }, { "type": "error", "name": "DepositNotAllowed", "inputs": [] }, { "type": "error", "name": "ERC20InsufficientAllowance", "inputs": [{ "name": "spender", "type": "address", "internalType": "address" }, { "name": "allowance", "type": "uint256", "internalType": "uint256" }, { "name": "needed", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ERC20InsufficientBalance", "inputs": [{ "name": "sender", "type": "address", "internalType": "address" }, { "name": "balance", "type": "uint256", "internalType": "uint256" }, { "name": "needed", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ERC20InvalidApprover", "inputs": [{ "name": "approver", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "ERC20InvalidReceiver", "inputs": [{ "name": "receiver", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "ERC20InvalidSender", "inputs": [{ "name": "sender", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "ERC20InvalidSpender", "inputs": [{ "name": "spender", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "ERC4626ExceededMaxDeposit", "inputs": [{ "name": "receiver", "type": "address", "internalType": "address" }, { "name": "assets", "type": "uint256", "internalType": "uint256" }, { "name": "max", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ERC4626ExceededMaxMint", "inputs": [{ "name": "receiver", "type": "address", "internalType": "address" }, { "name": "shares", "type": "uint256", "internalType": "uint256" }, { "name": "max", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ERC4626ExceededMaxRedeem", "inputs": [{ "name": "owner", "type": "address", "internalType": "address" }, { "name": "shares", "type": "uint256", "internalType": "uint256" }, { "name": "max", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ERC4626ExceededMaxWithdraw", "inputs": [{ "name": "owner", "type": "address", "internalType": "address" }, { "name": "assets", "type": "uint256", "internalType": "uint256" }, { "name": "max", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "EnforcedPause", "inputs": [] }, { "type": "error", "name": "ExpectedPause", "inputs": [] }, { "type": "error", "name": "InsufficientLiquidity", "inputs": [{ "name": "requested", "type": "uint256", "internalType": "uint256" }, { "name": "available", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "InvalidParam", "inputs": [] }, { "type": "error", "name": "NotTicketOwner", "inputs": [] }, { "type": "error", "name": "ReentrancyGuardReentrantCall", "inputs": [] }, { "type": "error", "name": "SafeCastOverflowedUintDowncast", "inputs": [{ "name": "bits", "type": "uint8", "internalType": "uint8" }, { "name": "value", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "SafeERC20FailedOperation", "inputs": [{ "name": "token", "type": "address", "internalType": "address" }] }, { "type": "error", "name": "TicketClaimed", "inputs": [] }, { "type": "error", "name": "TicketNotFunded", "inputs": [] }, { "type": "error", "name": "UseWithdrawQueue", "inputs": [{ "name": "requested", "type": "uint256", "internalType": "uint256" }, { "name": "instantMax", "type": "uint256", "internalType": "uint256" }] }, { "type": "error", "name": "ZeroAmount", "inputs": [] }];
|
package/dist/chain.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SDK talks to the chain through this interface. `createViemChain` (./viem.ts) is the production
|
|
3
|
+
* implementation. Tests and custom stacks can provide their own.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum SettlementMode {
|
|
6
|
+
Direct = 0,
|
|
7
|
+
Exact = 1
|
|
8
|
+
}
|
|
9
|
+
export interface Policy {
|
|
10
|
+
perCallCap: bigint;
|
|
11
|
+
dailyCap: bigint;
|
|
12
|
+
}
|
|
13
|
+
export interface AccountState {
|
|
14
|
+
policy: Policy;
|
|
15
|
+
remainingToday: bigint;
|
|
16
|
+
paused: boolean;
|
|
17
|
+
revoked: boolean;
|
|
18
|
+
liquid: bigint;
|
|
19
|
+
parked: bigint;
|
|
20
|
+
debt: bigint;
|
|
21
|
+
}
|
|
22
|
+
export interface PaymentRequestInput {
|
|
23
|
+
payee: `0x${string}`;
|
|
24
|
+
amount: bigint;
|
|
25
|
+
paymentId: `0x${string}`;
|
|
26
|
+
domain: string;
|
|
27
|
+
endpoint: string;
|
|
28
|
+
mode: SettlementMode;
|
|
29
|
+
}
|
|
30
|
+
export interface SettlementResult {
|
|
31
|
+
transactionHash: `0x${string}`;
|
|
32
|
+
fee: bigint;
|
|
33
|
+
buffer: `0x${string}`;
|
|
34
|
+
}
|
|
35
|
+
export interface PaymentRecord {
|
|
36
|
+
paymentId: `0x${string}`;
|
|
37
|
+
payee: `0x${string}`;
|
|
38
|
+
amount: bigint;
|
|
39
|
+
fee: bigint;
|
|
40
|
+
endpoint: string;
|
|
41
|
+
transactionHash: `0x${string}`;
|
|
42
|
+
blockNumber: bigint;
|
|
43
|
+
timestamp?: number;
|
|
44
|
+
}
|
|
45
|
+
export interface TypedDataSigner {
|
|
46
|
+
address: `0x${string}`;
|
|
47
|
+
signTypedData(args: {
|
|
48
|
+
domain: Record<string, unknown>;
|
|
49
|
+
types: Record<string, readonly {
|
|
50
|
+
name: string;
|
|
51
|
+
type: string;
|
|
52
|
+
}[]>;
|
|
53
|
+
primaryType: string;
|
|
54
|
+
message: Record<string, unknown>;
|
|
55
|
+
}): Promise<`0x${string}`>;
|
|
56
|
+
}
|
|
57
|
+
export interface UnsleptChain {
|
|
58
|
+
readonly chainId: number;
|
|
59
|
+
readonly usdg: `0x${string}`;
|
|
60
|
+
isPayeeAllowed(payee: `0x${string}`, domain: string): Promise<boolean>;
|
|
61
|
+
getAccountState(): Promise<AccountState>;
|
|
62
|
+
requestPayment(input: PaymentRequestInput): Promise<SettlementResult>;
|
|
63
|
+
setPolicy(policy: Policy): Promise<`0x${string}`>;
|
|
64
|
+
getPayments(opts?: {
|
|
65
|
+
limit?: number;
|
|
66
|
+
}): Promise<PaymentRecord[]>;
|
|
67
|
+
}
|
package/dist/chain.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SDK talks to the chain through this interface. `createViemChain` (./viem.ts) is the production
|
|
3
|
+
* implementation. Tests and custom stacks can provide their own.
|
|
4
|
+
*/
|
|
5
|
+
export var SettlementMode;
|
|
6
|
+
(function (SettlementMode) {
|
|
7
|
+
SettlementMode[SettlementMode["Direct"] = 0] = "Direct";
|
|
8
|
+
SettlementMode[SettlementMode["Exact"] = 1] = "Exact";
|
|
9
|
+
})(SettlementMode || (SettlementMode = {}));
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type PaymentRecord, type Policy, type TypedDataSigner, type UnsleptChain } from "./chain.js";
|
|
2
|
+
import { type SettlementResponse } from "./x402.js";
|
|
3
|
+
export interface UnsleptOptions {
|
|
4
|
+
/** AgentAccount address. */
|
|
5
|
+
agentAccount: `0x${string}` | string | undefined;
|
|
6
|
+
/** The agent signer. A viem account, or anything with an address and signTypedData. */
|
|
7
|
+
signer: TypedDataSigner | unknown;
|
|
8
|
+
/** Network preset. "robinhood-chain" reads its RPC, chain ID and addresses from the environment. */
|
|
9
|
+
network: "robinhood-chain" | (string & {});
|
|
10
|
+
/** Chain implementation. Defaults to the viem implementation built from the environment. */
|
|
11
|
+
chain?: UnsleptChain;
|
|
12
|
+
/** fetch implementation. Defaults to globalThis.fetch. */
|
|
13
|
+
fetch?: typeof fetch;
|
|
14
|
+
/** Scheme preference. Defaults to Unslept direct settlement first, then standard x402 "exact". */
|
|
15
|
+
schemes?: string[];
|
|
16
|
+
/** Called after every settled payment. */
|
|
17
|
+
onPayment?: (p: LocalPayment) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Unslept app base URL. When set, policy violations are reported (signed by the agent signer) so the
|
|
20
|
+
* account owner's webhooks receive them. Best effort; never blocks or changes the error thrown.
|
|
21
|
+
*/
|
|
22
|
+
reportUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface FetchOptions {
|
|
25
|
+
/** Refuse to pay more than this many atomic USDG units for this request. */
|
|
26
|
+
maxAmount?: bigint;
|
|
27
|
+
}
|
|
28
|
+
export interface LocalPayment {
|
|
29
|
+
agentId: string;
|
|
30
|
+
endpoint: string;
|
|
31
|
+
payee: string;
|
|
32
|
+
amount: bigint;
|
|
33
|
+
fee: bigint;
|
|
34
|
+
scheme: string;
|
|
35
|
+
transactionHash: string;
|
|
36
|
+
paymentId: string;
|
|
37
|
+
settlement?: SettlementResponse;
|
|
38
|
+
at: Date;
|
|
39
|
+
}
|
|
40
|
+
export interface Balances {
|
|
41
|
+
/** USDG held in the account, ready for repayments. */
|
|
42
|
+
liquid: bigint;
|
|
43
|
+
/** USDG value parked in tokenized assets. */
|
|
44
|
+
working: bigint;
|
|
45
|
+
/** Owed to the buffer, repaid in the background. */
|
|
46
|
+
debt: bigint;
|
|
47
|
+
/** liquid + working - debt */
|
|
48
|
+
net: bigint;
|
|
49
|
+
}
|
|
50
|
+
type LowBalanceCallback = (b: Balances) => void;
|
|
51
|
+
export declare class Unslept {
|
|
52
|
+
readonly agentAccount: `0x${string}`;
|
|
53
|
+
private readonly opts;
|
|
54
|
+
private chainPromise?;
|
|
55
|
+
private readonly fetchImpl;
|
|
56
|
+
private readonly log;
|
|
57
|
+
private readonly lowBalance;
|
|
58
|
+
constructor(opts: UnsleptOptions);
|
|
59
|
+
private chain;
|
|
60
|
+
/** Drop-in replacement for fetch. Pays x402 paywalls from a balance that is always working. */
|
|
61
|
+
fetch(input: string | URL | Request, init?: RequestInit, options?: FetchOptions): Promise<Response>;
|
|
62
|
+
private report;
|
|
63
|
+
private fetchInner;
|
|
64
|
+
private checkPolicy;
|
|
65
|
+
private signExact;
|
|
66
|
+
getBalances(): Promise<Balances>;
|
|
67
|
+
getPolicy(): Promise<Policy & {
|
|
68
|
+
remainingToday: bigint;
|
|
69
|
+
paused: boolean;
|
|
70
|
+
revoked: boolean;
|
|
71
|
+
}>;
|
|
72
|
+
/** Sends setPolicy from the configured signer. Only the account owner can change policy on-chain. */
|
|
73
|
+
setPolicy(policy: Policy): Promise<`0x${string}`>;
|
|
74
|
+
/** On-chain payment history for this agent, newest first. */
|
|
75
|
+
getPayments(opts?: {
|
|
76
|
+
limit?: number;
|
|
77
|
+
}): Promise<PaymentRecord[]>;
|
|
78
|
+
/** Payments made by this SDK instance, with endpoint, amount, payee, agent ID and transaction hash. */
|
|
79
|
+
localPayments(): readonly LocalPayment[];
|
|
80
|
+
/**
|
|
81
|
+
* Calls `callback` when the net balance falls below `threshold` (atomic USDG). Checked after every payment
|
|
82
|
+
* and every `intervalMs` if given. Returns a function that stops the watch.
|
|
83
|
+
*/
|
|
84
|
+
onLowBalance(callback: LowBalanceCallback, opts: {
|
|
85
|
+
threshold: bigint;
|
|
86
|
+
intervalMs?: number;
|
|
87
|
+
}): () => void;
|
|
88
|
+
private checkLowBalance;
|
|
89
|
+
}
|
|
90
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { SettlementMode } from "./chain.js";
|
|
2
|
+
import { UnsleptError, fmtUsdg } from "./errors.js";
|
|
3
|
+
import { HEADER_RESPONSE, HEADER_SIGNATURE, SCHEME_EXACT, SCHEME_UNSLEPT, X402_VERSION, decodeHeader, eip3009Types, encodeHeader, readPaymentRequired, selectRequirement, } from "./x402.js";
|
|
4
|
+
const REPORTED = new Set(["PER_CALL_CAP", "DAILY_CAP", "PAYEE_NOT_ALLOWED", "AGENT_PAUSED", "AGENT_REVOKED", "MAX_AMOUNT"]);
|
|
5
|
+
const randomBytes32 = () => {
|
|
6
|
+
const b = new Uint8Array(32);
|
|
7
|
+
globalThis.crypto.getRandomValues(b);
|
|
8
|
+
return `0x${Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("")}`;
|
|
9
|
+
};
|
|
10
|
+
const isBytes32 = (v) => typeof v === "string" && /^0x[0-9a-fA-F]{64}$/.test(v);
|
|
11
|
+
export class Unslept {
|
|
12
|
+
agentAccount;
|
|
13
|
+
opts;
|
|
14
|
+
chainPromise;
|
|
15
|
+
fetchImpl;
|
|
16
|
+
log = [];
|
|
17
|
+
lowBalance = [];
|
|
18
|
+
constructor(opts) {
|
|
19
|
+
if (!opts.agentAccount || !/^0x[0-9a-fA-F]{40}$/.test(opts.agentAccount)) {
|
|
20
|
+
throw new UnsleptError("CONFIG", "agentAccount must be an AgentAccount address (0x followed by 40 hex characters).");
|
|
21
|
+
}
|
|
22
|
+
this.agentAccount = opts.agentAccount;
|
|
23
|
+
this.opts = opts;
|
|
24
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
25
|
+
if (opts.chain)
|
|
26
|
+
this.chainPromise = Promise.resolve(opts.chain);
|
|
27
|
+
}
|
|
28
|
+
chain() {
|
|
29
|
+
if (!this.chainPromise) {
|
|
30
|
+
this.chainPromise = import("./viem.js").then((m) => m.createViemChainFromEnv({ agentAccount: this.agentAccount, signer: this.opts.signer, network: this.opts.network }));
|
|
31
|
+
}
|
|
32
|
+
return this.chainPromise;
|
|
33
|
+
}
|
|
34
|
+
/** Drop-in replacement for fetch. Pays x402 paywalls from a balance that is always working. */
|
|
35
|
+
async fetch(input, init, options = {}) {
|
|
36
|
+
try {
|
|
37
|
+
return await this.fetchInner(input, init, options);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e instanceof UnsleptError && REPORTED.has(e.code) && this.opts.reportUrl) {
|
|
41
|
+
void this.report(e, input instanceof Request ? input.url : input.toString());
|
|
42
|
+
}
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async report(e, endpoint) {
|
|
47
|
+
const signer = this.opts.signer;
|
|
48
|
+
if (typeof signer?.signMessage !== "function")
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
const at = new Date().toISOString();
|
|
52
|
+
const text = `Unslept policy violation\nAccount: ${this.agentAccount}\nCode: ${e.code}\nEndpoint: ${endpoint}\nAt: ${at}\n${e.message}`;
|
|
53
|
+
const signature = await signer.signMessage({ message: text });
|
|
54
|
+
await this.fetchImpl(`${this.opts.reportUrl.replace(/\/$/, "")}/api/alerts`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: { "content-type": "application/json" },
|
|
57
|
+
body: JSON.stringify({ account: this.agentAccount, code: e.code, message: e.message, endpoint, at, signature }),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
/* reporting is best effort */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async fetchInner(input, init, options) {
|
|
65
|
+
const retryInput = input instanceof Request ? input.clone() : input;
|
|
66
|
+
const res = await this.fetchImpl(input, init);
|
|
67
|
+
if (res.status !== 402)
|
|
68
|
+
return res;
|
|
69
|
+
const chain = await this.chain();
|
|
70
|
+
const url = new URL(input instanceof Request ? input.url : input.toString());
|
|
71
|
+
const pr = readPaymentRequired(res);
|
|
72
|
+
const schemes = this.opts.schemes ?? [SCHEME_UNSLEPT, SCHEME_EXACT];
|
|
73
|
+
const network = `eip155:${chain.chainId}`;
|
|
74
|
+
const req = selectRequirement(pr, { network, asset: chain.usdg, schemes });
|
|
75
|
+
if (!req) {
|
|
76
|
+
const offered = pr.accepts.map((a) => `${a.scheme} on ${a.network} in ${a.asset}`).join("; ");
|
|
77
|
+
throw new UnsleptError("NO_MATCHING_REQUIREMENT", `The server does not accept USDG on ${network} through ${schemes.join(" or ")}. It offers: ${offered}.`, { offered: pr.accepts });
|
|
78
|
+
}
|
|
79
|
+
const amount = BigInt(req.amount);
|
|
80
|
+
if (options.maxAmount !== undefined && amount > options.maxAmount) {
|
|
81
|
+
throw new UnsleptError("MAX_AMOUNT", `The server asks ${fmtUsdg(amount)}, above the ${fmtUsdg(options.maxAmount)} limit set for this request. Raise maxAmount to pay it.`, { amount, maxAmount: options.maxAmount });
|
|
82
|
+
}
|
|
83
|
+
await this.checkPolicy(chain, req, amount, url.hostname);
|
|
84
|
+
const exact = req.scheme === SCHEME_EXACT;
|
|
85
|
+
const paymentId = isBytes32(req.extra?.paymentId) ? req.extra.paymentId : randomBytes32();
|
|
86
|
+
const settlement = await chain.requestPayment({
|
|
87
|
+
payee: req.payTo,
|
|
88
|
+
amount,
|
|
89
|
+
paymentId,
|
|
90
|
+
domain: url.hostname,
|
|
91
|
+
endpoint: url.toString(),
|
|
92
|
+
mode: exact ? SettlementMode.Exact : SettlementMode.Direct,
|
|
93
|
+
});
|
|
94
|
+
const payload = {
|
|
95
|
+
x402Version: X402_VERSION,
|
|
96
|
+
resource: pr.resource,
|
|
97
|
+
accepted: req,
|
|
98
|
+
payload: exact
|
|
99
|
+
? await this.signExact(chain, req, amount, paymentId)
|
|
100
|
+
: { transaction: settlement.transactionHash, paymentId, agentAccount: this.agentAccount },
|
|
101
|
+
...(pr.extensions ? { extensions: pr.extensions } : {}),
|
|
102
|
+
};
|
|
103
|
+
const headers = new Headers(init?.headers ?? (retryInput instanceof Request ? retryInput.headers : undefined));
|
|
104
|
+
headers.set(HEADER_SIGNATURE, encodeHeader(payload));
|
|
105
|
+
const paid = await this.fetchImpl(retryInput, { ...init, headers });
|
|
106
|
+
const responseHeader = paid.headers.get(HEADER_RESPONSE);
|
|
107
|
+
const sr = responseHeader ? safeDecode(responseHeader) : undefined;
|
|
108
|
+
const record = {
|
|
109
|
+
agentId: this.agentAccount,
|
|
110
|
+
endpoint: url.toString(),
|
|
111
|
+
payee: req.payTo,
|
|
112
|
+
amount,
|
|
113
|
+
fee: settlement.fee,
|
|
114
|
+
scheme: req.scheme,
|
|
115
|
+
transactionHash: sr?.transaction || settlement.transactionHash,
|
|
116
|
+
paymentId,
|
|
117
|
+
settlement: sr,
|
|
118
|
+
at: new Date(),
|
|
119
|
+
};
|
|
120
|
+
this.log.push(record);
|
|
121
|
+
this.opts.onPayment?.(record);
|
|
122
|
+
void this.checkLowBalance();
|
|
123
|
+
if (paid.status === 402) {
|
|
124
|
+
throw new UnsleptError("PAYMENT_REJECTED", `Paid ${fmtUsdg(amount)} (transaction ${settlement.transactionHash}) but the server still answered 402${sr?.errorReason ? `: ${sr.errorReason}` : ""}. Contact the merchant with the transaction hash.`, { record });
|
|
125
|
+
}
|
|
126
|
+
return paid;
|
|
127
|
+
}
|
|
128
|
+
async checkPolicy(chain, req, amount, domain) {
|
|
129
|
+
const s = await chain.getAccountState();
|
|
130
|
+
if (s.revoked)
|
|
131
|
+
throw new UnsleptError("AGENT_REVOKED", "This agent was revoked by its owner. Create a new agent account to keep paying.");
|
|
132
|
+
if (s.paused)
|
|
133
|
+
throw new UnsleptError("AGENT_PAUSED", "This agent is paused. Its owner can resume it in the Unslept app.");
|
|
134
|
+
if (amount > s.policy.perCallCap) {
|
|
135
|
+
throw new UnsleptError("PER_CALL_CAP", `Payment of ${fmtUsdg(amount)} is above the per-call cap of ${fmtUsdg(s.policy.perCallCap)}. Raise the cap in the agent's spending policy to allow it.`, { amount, cap: s.policy.perCallCap });
|
|
136
|
+
}
|
|
137
|
+
if (amount > s.remainingToday) {
|
|
138
|
+
throw new UnsleptError("DAILY_CAP", `Payment of ${fmtUsdg(amount)} would pass today's cap. ${fmtUsdg(s.remainingToday)} is left until 00:00 UTC.`, { amount, remaining: s.remainingToday });
|
|
139
|
+
}
|
|
140
|
+
if (!(await chain.isPayeeAllowed(req.payTo, domain))) {
|
|
141
|
+
throw new UnsleptError("PAYEE_NOT_ALLOWED", `Payee ${req.payTo} (${domain}) is not on this agent's allowlist. Add the address or the domain in the agent's spending policy.`, { payee: req.payTo, domain });
|
|
142
|
+
}
|
|
143
|
+
const needed = amount + s.debt;
|
|
144
|
+
if (needed > s.liquid + s.parked) {
|
|
145
|
+
throw new UnsleptError("INSUFFICIENT_COLLATERAL", `The agent holds ${fmtUsdg(s.liquid + s.parked)} and owes ${fmtUsdg(s.debt)}, not enough to cover ${fmtUsdg(amount)}. Fund the agent account.`, { amount, liquid: s.liquid, parked: s.parked, debt: s.debt });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async signExact(chain, req, amount, nonce) {
|
|
149
|
+
const signer = this.opts.signer;
|
|
150
|
+
if (!signer || typeof signer.signTypedData !== "function") {
|
|
151
|
+
throw new UnsleptError("CONFIG", "The exact scheme needs a signer with signTypedData, such as a viem account.");
|
|
152
|
+
}
|
|
153
|
+
const name = req.extra?.name;
|
|
154
|
+
const version = req.extra?.version;
|
|
155
|
+
if (typeof name !== "string" || typeof version !== "string") {
|
|
156
|
+
throw new UnsleptError("CONFIG", "The server's exact requirement is missing extra.name or extra.version for the EIP-712 domain.");
|
|
157
|
+
}
|
|
158
|
+
const now = Math.floor(Date.now() / 1000);
|
|
159
|
+
const authorization = {
|
|
160
|
+
from: signer.address,
|
|
161
|
+
to: req.payTo,
|
|
162
|
+
value: amount.toString(),
|
|
163
|
+
validAfter: "0",
|
|
164
|
+
validBefore: String(now + req.maxTimeoutSeconds),
|
|
165
|
+
nonce,
|
|
166
|
+
};
|
|
167
|
+
const signature = await signer.signTypedData({
|
|
168
|
+
domain: { name, version, chainId: chain.chainId, verifyingContract: req.asset },
|
|
169
|
+
types: eip3009Types,
|
|
170
|
+
primaryType: "TransferWithAuthorization",
|
|
171
|
+
message: {
|
|
172
|
+
...authorization,
|
|
173
|
+
value: amount,
|
|
174
|
+
validAfter: 0n,
|
|
175
|
+
validBefore: BigInt(authorization.validBefore),
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
return { signature, authorization };
|
|
179
|
+
}
|
|
180
|
+
async getBalances() {
|
|
181
|
+
const s = await (await this.chain()).getAccountState();
|
|
182
|
+
return { liquid: s.liquid, working: s.parked, debt: s.debt, net: s.liquid + s.parked - s.debt };
|
|
183
|
+
}
|
|
184
|
+
async getPolicy() {
|
|
185
|
+
const s = await (await this.chain()).getAccountState();
|
|
186
|
+
return { ...s.policy, remainingToday: s.remainingToday, paused: s.paused, revoked: s.revoked };
|
|
187
|
+
}
|
|
188
|
+
/** Sends setPolicy from the configured signer. Only the account owner can change policy on-chain. */
|
|
189
|
+
async setPolicy(policy) {
|
|
190
|
+
return (await this.chain()).setPolicy(policy);
|
|
191
|
+
}
|
|
192
|
+
/** On-chain payment history for this agent, newest first. */
|
|
193
|
+
async getPayments(opts) {
|
|
194
|
+
return (await this.chain()).getPayments(opts);
|
|
195
|
+
}
|
|
196
|
+
/** Payments made by this SDK instance, with endpoint, amount, payee, agent ID and transaction hash. */
|
|
197
|
+
localPayments() {
|
|
198
|
+
return this.log;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Calls `callback` when the net balance falls below `threshold` (atomic USDG). Checked after every payment
|
|
202
|
+
* and every `intervalMs` if given. Returns a function that stops the watch.
|
|
203
|
+
*/
|
|
204
|
+
onLowBalance(callback, opts) {
|
|
205
|
+
const entry = { threshold: opts.threshold, cb: callback };
|
|
206
|
+
this.lowBalance.push(entry);
|
|
207
|
+
const timer = opts.intervalMs ? setInterval(() => void this.checkLowBalance(), opts.intervalMs) : undefined;
|
|
208
|
+
return () => {
|
|
209
|
+
if (timer)
|
|
210
|
+
clearInterval(timer);
|
|
211
|
+
const i = this.lowBalance.indexOf(entry);
|
|
212
|
+
if (i >= 0)
|
|
213
|
+
this.lowBalance.splice(i, 1);
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
async checkLowBalance() {
|
|
217
|
+
if (this.lowBalance.length === 0)
|
|
218
|
+
return;
|
|
219
|
+
try {
|
|
220
|
+
const b = await this.getBalances();
|
|
221
|
+
for (const l of this.lowBalance)
|
|
222
|
+
if (b.net < l.threshold)
|
|
223
|
+
l.cb(b);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
/* balance reads are best effort; the next check retries */
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function safeDecode(h) {
|
|
231
|
+
try {
|
|
232
|
+
return decodeHeader(h);
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Errors say what happened and how to fix it. */
|
|
2
|
+
export type UnsleptErrorCode = "PER_CALL_CAP" | "DAILY_CAP" | "PAYEE_NOT_ALLOWED" | "AGENT_PAUSED" | "AGENT_REVOKED" | "NO_MATCHING_REQUIREMENT" | "INSUFFICIENT_COLLATERAL" | "INSUFFICIENT_BUFFER_LIQUIDITY" | "SETTLEMENT_FAILED" | "PAYMENT_REJECTED" | "MAX_AMOUNT" | "CONFIG";
|
|
3
|
+
export declare class UnsleptError extends Error {
|
|
4
|
+
readonly code: UnsleptErrorCode;
|
|
5
|
+
readonly details: Record<string, unknown>;
|
|
6
|
+
constructor(code: UnsleptErrorCode, message: string, details?: Record<string, unknown>);
|
|
7
|
+
}
|
|
8
|
+
export declare const fmtUsdg: (atomic: bigint, decimals?: number) => string;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Errors say what happened and how to fix it. */
|
|
2
|
+
export class UnsleptError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
details;
|
|
5
|
+
constructor(code, message, details = {}) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.details = details;
|
|
9
|
+
this.name = "UnsleptError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const fmtUsdg = (atomic, decimals = 6) => {
|
|
13
|
+
const neg = atomic < 0n;
|
|
14
|
+
const v = neg ? -atomic : atomic;
|
|
15
|
+
const base = 10n ** BigInt(decimals);
|
|
16
|
+
const whole = v / base;
|
|
17
|
+
const frac = (v % base).toString().padStart(decimals, "0").replace(/0+$/, "");
|
|
18
|
+
return `${neg ? "-" : ""}${whole}${frac ? "." + frac : ""} USDG`;
|
|
19
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Unslept, type UnsleptOptions, type FetchOptions, type LocalPayment, type Balances } from "./client.js";
|
|
2
|
+
export { UnsleptError, type UnsleptErrorCode, fmtUsdg } from "./errors.js";
|
|
3
|
+
export { SettlementMode, type UnsleptChain, type AccountState, type Policy, type PaymentRecord, type PaymentRequestInput, type SettlementResult, type TypedDataSigner, } from "./chain.js";
|
|
4
|
+
export * from "./x402.js";
|
|
5
|
+
export * as abi from "./abi.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Unslept } from "./client.js";
|
|
2
|
+
export { UnsleptError, fmtUsdg } from "./errors.js";
|
|
3
|
+
export { SettlementMode, } from "./chain.js";
|
|
4
|
+
export * from "./x402.js";
|
|
5
|
+
export * as abi from "./abi.js";
|
|
6
|
+
// The viem implementation is exported from "@unslept/sdk/viem" so the core stays dependency-free.
|
package/dist/viem.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Account, type PublicClient, type WalletClient } from "viem";
|
|
2
|
+
import type { UnsleptChain } from "./chain.js";
|
|
3
|
+
export interface ViemChainConfig {
|
|
4
|
+
agentAccount: `0x${string}`;
|
|
5
|
+
usdg: `0x${string}`;
|
|
6
|
+
router: `0x${string}`;
|
|
7
|
+
buffers: `0x${string}`[];
|
|
8
|
+
publicClient: PublicClient;
|
|
9
|
+
walletClient: WalletClient;
|
|
10
|
+
account: Account;
|
|
11
|
+
/** First block to scan for payment history. */
|
|
12
|
+
fromBlock?: bigint;
|
|
13
|
+
}
|
|
14
|
+
/** Builds the chain client for the "robinhood-chain" preset from environment variables. Nothing is assumed. */
|
|
15
|
+
export declare function createViemChainFromEnv(args: {
|
|
16
|
+
agentAccount: `0x${string}`;
|
|
17
|
+
signer: unknown;
|
|
18
|
+
network: string;
|
|
19
|
+
}): UnsleptChain;
|
|
20
|
+
export declare function createViemChain(cfg: ViemChainConfig): UnsleptChain;
|