@priors/agentkit 0.1.0 → 0.2.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/README.md +19 -5
- package/dist/constants.d.ts +106 -0
- package/dist/constants.js +19 -1
- package/dist/priorsActionProvider.d.ts +31 -2
- package/dist/priorsActionProvider.js +143 -13
- package/dist/schemas.d.ts +10 -4
- package/dist/schemas.js +13 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,13 +35,30 @@ const agentKit = await AgentKit.from({ walletProvider, actionProviders: [priorsA
|
|
|
35
35
|
|
|
36
36
|
Then hand `agentKit` to your framework as usual (`getVercelAITools(agentKit)`, `getLangChainTools(agentKit)`, ...).
|
|
37
37
|
|
|
38
|
+
**Inference on credit (0.2.0).** Give the provider `autoBorrow`, set by you in code, never by the model:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
priorsActionProvider(undefined, { autoBorrow: { perCall: "5", perDay: "10", hosts: ["models.example"] } })
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
With it, `pay_x402_with_priors_credit` borrows the gap on its own when the wallet is short, for the listed hosts only.
|
|
45
|
+
There is no default host: `hosts` names each one, exactly (here `models.example` stands for the model API you choose).
|
|
46
|
+
The limits are `perCall` USDG a call and `perDay` USDG a UTC day in the process, for 14 days (or the pool's maximum
|
|
47
|
+
term). It borrows only while this wallet's Autopay (AutoRepay v2) covers the new loan: the wallet is the agent's
|
|
48
|
+
declared wallet, the plan is on under the agent's current owner, not held or paused, its limit per loan and its USDG
|
|
49
|
+
approval cover the loan's due. Otherwise nothing is borrowed or paid, and the answer says to turn Autopay on. Autopay
|
|
50
|
+
then repays the loan in its window. The model's `maxBorrow` can only lower the amount. A chat completion is one call
|
|
51
|
+
to an OpenAI-compatible model API you choose that charges over x402 in USDG on Robinhood Chain: `url` its chat
|
|
52
|
+
completions URL (for example `https://models.example/v1/chat/completions`, its host in `hosts`), `body` the
|
|
53
|
+
OpenAI-style request as JSON (sent with POST), `maxPrice` the most to pay for the call (for example `"0.02"`).
|
|
54
|
+
|
|
38
55
|
## Actions
|
|
39
56
|
|
|
40
57
|
| Action | What it does | Moves money |
|
|
41
58
|
|---|---|---|
|
|
42
59
|
| `get_priors_agent_record` | reads an agent's repayment record: loans repaid, late repayments, defaults, score | no |
|
|
43
60
|
| `get_priors_credit_line` | reads an agent's line: size, drawn, available | no |
|
|
44
|
-
| `pay_x402_with_priors_credit` | pays an x402 URL in USDG, at most `maxPrice`; if the wallet is short, first borrows the gap (at least the pool's minimum loan, 5 USDG today)
|
|
61
|
+
| `pay_x402_with_priors_credit` | pays an x402 URL in USDG, at most `maxPrice`, with GET or with POST and a `body`; if the wallet is short, first borrows the gap (at least the pool's minimum loan, 5 USDG today) when `maxBorrow` covers it, or, with `autoBorrow`, on its own within its limits | yes |
|
|
45
62
|
| `borrow_from_priors` | borrows USDG from the agent's line into the wallet | yes |
|
|
46
63
|
| `repay_priors_loan` | repays a loan in full; approves exactly what is due | yes |
|
|
47
64
|
|
|
@@ -64,11 +81,8 @@ process.on("unhandledRejection", (e) => {
|
|
|
64
81
|
|
|
65
82
|
## Demo
|
|
66
83
|
|
|
67
|
-

|
|
68
|
-
|
|
69
84
|
A production run on 2026-10-30 (agent #437, Robinhood Chain mainnet; the model calling these actions through
|
|
70
|
-
`@coinbase/agentkit-vercel-ai-sdk`), its
|
|
71
|
-
seller's record (Blockpin, agent #9233), pays its x402 check (0.10 USDG) from an empty wallet by borrowing 5 USDG (loan
|
|
85
|
+
`@coinbase/agentkit-vercel-ai-sdk`). The agent reads the seller's record, pays its x402 check (0.10 USDG) from an empty wallet by borrowing 5 USDG (loan
|
|
72
86
|
#50884), and repays 5.011666 USDG after its income arrives.
|
|
73
87
|
Transactions: [borrow](https://robinhoodchain.blockscout.com/tx/0x297608c51007480ec8c5aa510d78926d6f14bf58ed467cb797aedc5922884a9e),
|
|
74
88
|
[payment](https://robinhoodchain.blockscout.com/tx/0xbb7fd5dcc13ac547873516713e4940cecadcda76873d980d3ad402de48288c19),
|
package/dist/constants.d.ts
CHANGED
|
@@ -10,6 +10,101 @@ export declare const USDG_DECIMALS = 6;
|
|
|
10
10
|
export declare const PRIORS_API = "https://priors.trade";
|
|
11
11
|
/** Default loan term when borrowing the gap of a payment: 7 days. */
|
|
12
12
|
export declare const DEFAULT_TERM_DAYS = 7;
|
|
13
|
+
/** Auto-borrow's term: 14 days, so a loan Autopay repays in its window (the last hours before due) still counts as
|
|
14
|
+
* seasoned; brought down to the pool's maxTerm. */
|
|
15
|
+
export declare const AUTO_TERM_DAYS = 14;
|
|
16
|
+
/** The ERC-8004 identity registry on Robinhood Chain (the agent's owner and declared wallet). */
|
|
17
|
+
export declare const REGISTRY: "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
|
|
18
|
+
/** AutoRepay v2 (Priors' Autopay): repays a loan from the agent's declared wallet in its window. */
|
|
19
|
+
export declare const AUTOREPAY: "0x72B510AF4e66e5960aB9b975e0BdCf80239e83A0";
|
|
20
|
+
export declare const REGISTRY_ABI: readonly [{
|
|
21
|
+
readonly name: "ownerOf";
|
|
22
|
+
readonly type: "function";
|
|
23
|
+
readonly stateMutability: "view";
|
|
24
|
+
readonly inputs: readonly [{
|
|
25
|
+
readonly type: "uint256";
|
|
26
|
+
readonly name: "agentId";
|
|
27
|
+
}];
|
|
28
|
+
readonly outputs: readonly [{
|
|
29
|
+
readonly type: "address";
|
|
30
|
+
}];
|
|
31
|
+
}, {
|
|
32
|
+
readonly name: "getAgentWallet";
|
|
33
|
+
readonly type: "function";
|
|
34
|
+
readonly stateMutability: "view";
|
|
35
|
+
readonly inputs: readonly [{
|
|
36
|
+
readonly type: "uint256";
|
|
37
|
+
readonly name: "agentId";
|
|
38
|
+
}];
|
|
39
|
+
readonly outputs: readonly [{
|
|
40
|
+
readonly type: "address";
|
|
41
|
+
}];
|
|
42
|
+
}];
|
|
43
|
+
export declare const AUTOREPAY_ABI: readonly [{
|
|
44
|
+
readonly name: "planOf";
|
|
45
|
+
readonly type: "function";
|
|
46
|
+
readonly stateMutability: "view";
|
|
47
|
+
readonly inputs: readonly [{
|
|
48
|
+
readonly type: "address";
|
|
49
|
+
readonly name: "wallet";
|
|
50
|
+
}, {
|
|
51
|
+
readonly type: "uint256";
|
|
52
|
+
readonly name: "agentId";
|
|
53
|
+
}];
|
|
54
|
+
readonly outputs: readonly [{
|
|
55
|
+
readonly type: "uint32";
|
|
56
|
+
readonly name: "cap";
|
|
57
|
+
}, {
|
|
58
|
+
readonly type: "address";
|
|
59
|
+
readonly name: "owner";
|
|
60
|
+
}, {
|
|
61
|
+
readonly type: "bool";
|
|
62
|
+
readonly name: "on";
|
|
63
|
+
}, {
|
|
64
|
+
readonly type: "bool";
|
|
65
|
+
readonly name: "useSavings";
|
|
66
|
+
}, {
|
|
67
|
+
readonly type: "bool";
|
|
68
|
+
readonly name: "late";
|
|
69
|
+
}, {
|
|
70
|
+
readonly type: "uint32";
|
|
71
|
+
readonly name: "epoch";
|
|
72
|
+
}];
|
|
73
|
+
}, {
|
|
74
|
+
readonly name: "epochOf";
|
|
75
|
+
readonly type: "function";
|
|
76
|
+
readonly stateMutability: "view";
|
|
77
|
+
readonly inputs: readonly [{
|
|
78
|
+
readonly type: "uint256";
|
|
79
|
+
readonly name: "agentId";
|
|
80
|
+
}];
|
|
81
|
+
readonly outputs: readonly [{
|
|
82
|
+
readonly type: "uint32";
|
|
83
|
+
readonly name: "epoch";
|
|
84
|
+
}, {
|
|
85
|
+
readonly type: "bool";
|
|
86
|
+
readonly name: "stale";
|
|
87
|
+
}];
|
|
88
|
+
}, {
|
|
89
|
+
readonly name: "held";
|
|
90
|
+
readonly type: "function";
|
|
91
|
+
readonly stateMutability: "view";
|
|
92
|
+
readonly inputs: readonly [{
|
|
93
|
+
readonly type: "address";
|
|
94
|
+
readonly name: "who";
|
|
95
|
+
}];
|
|
96
|
+
readonly outputs: readonly [{
|
|
97
|
+
readonly type: "bool";
|
|
98
|
+
}];
|
|
99
|
+
}, {
|
|
100
|
+
readonly name: "pausedUntil";
|
|
101
|
+
readonly type: "function";
|
|
102
|
+
readonly stateMutability: "view";
|
|
103
|
+
readonly inputs: readonly [];
|
|
104
|
+
readonly outputs: readonly [{
|
|
105
|
+
readonly type: "uint64";
|
|
106
|
+
}];
|
|
107
|
+
}];
|
|
13
108
|
export declare const POOL_ABI: readonly [{
|
|
14
109
|
readonly name: "getParams";
|
|
15
110
|
readonly type: "function";
|
|
@@ -120,6 +215,17 @@ export declare const POOL_ABI: readonly [{
|
|
|
120
215
|
readonly name: "maxDue";
|
|
121
216
|
}];
|
|
122
217
|
readonly outputs: readonly [];
|
|
218
|
+
}, {
|
|
219
|
+
readonly name: "loansOf";
|
|
220
|
+
readonly type: "function";
|
|
221
|
+
readonly stateMutability: "view";
|
|
222
|
+
readonly inputs: readonly [{
|
|
223
|
+
readonly type: "uint256";
|
|
224
|
+
readonly name: "agentId";
|
|
225
|
+
}];
|
|
226
|
+
readonly outputs: readonly [{
|
|
227
|
+
readonly type: "uint256[]";
|
|
228
|
+
}];
|
|
123
229
|
}, {
|
|
124
230
|
readonly name: "getLoan";
|
|
125
231
|
readonly type: "function";
|
package/dist/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LOAN_ACTIVE = exports.POOL_ABI = exports.DEFAULT_TERM_DAYS = exports.PRIORS_API = exports.USDG_DECIMALS = exports.USDG = exports.PRIORS_POOL = exports.ROBINHOOD_NETWORK = exports.ROBINHOOD_CHAIN_ID = void 0;
|
|
3
|
+
exports.LOAN_ACTIVE = exports.POOL_ABI = exports.AUTOREPAY_ABI = exports.REGISTRY_ABI = exports.AUTOREPAY = exports.REGISTRY = exports.AUTO_TERM_DAYS = exports.DEFAULT_TERM_DAYS = exports.PRIORS_API = exports.USDG_DECIMALS = exports.USDG = exports.PRIORS_POOL = exports.ROBINHOOD_NETWORK = exports.ROBINHOOD_CHAIN_ID = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
/** Robinhood Chain, where Priors' pool and USDG live. */
|
|
6
6
|
exports.ROBINHOOD_CHAIN_ID = "4663";
|
|
@@ -14,11 +14,29 @@ exports.USDG_DECIMALS = 6;
|
|
|
14
14
|
exports.PRIORS_API = "https://priors.trade";
|
|
15
15
|
/** Default loan term when borrowing the gap of a payment: 7 days. */
|
|
16
16
|
exports.DEFAULT_TERM_DAYS = 7;
|
|
17
|
+
/** Auto-borrow's term: 14 days, so a loan Autopay repays in its window (the last hours before due) still counts as
|
|
18
|
+
* seasoned; brought down to the pool's maxTerm. */
|
|
19
|
+
exports.AUTO_TERM_DAYS = 14;
|
|
20
|
+
/** The ERC-8004 identity registry on Robinhood Chain (the agent's owner and declared wallet). */
|
|
21
|
+
exports.REGISTRY = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432";
|
|
22
|
+
/** AutoRepay v2 (Priors' Autopay): repays a loan from the agent's declared wallet in its window. */
|
|
23
|
+
exports.AUTOREPAY = "0x72B510AF4e66e5960aB9b975e0BdCf80239e83A0";
|
|
24
|
+
exports.REGISTRY_ABI = (0, viem_1.parseAbi)([
|
|
25
|
+
"function ownerOf(uint256 agentId) view returns (address)",
|
|
26
|
+
"function getAgentWallet(uint256 agentId) view returns (address)",
|
|
27
|
+
]);
|
|
28
|
+
exports.AUTOREPAY_ABI = (0, viem_1.parseAbi)([
|
|
29
|
+
"function planOf(address wallet, uint256 agentId) view returns (uint32 cap, address owner, bool on, bool useSavings, bool late, uint32 epoch)",
|
|
30
|
+
"function epochOf(uint256 agentId) view returns (uint32 epoch, bool stale)",
|
|
31
|
+
"function held(address who) view returns (bool)",
|
|
32
|
+
"function pausedUntil() view returns (uint64)",
|
|
33
|
+
]);
|
|
17
34
|
exports.POOL_ABI = (0, viem_1.parseAbi)([
|
|
18
35
|
"function getParams() view returns ((uint256 minLoan, uint256 maxLoan, uint64 minTerm, uint64 maxTerm, uint64 grace, uint64 minScoreTerm, uint256 feeBps, uint256 sponsorFeeBps, uint256 protocolFeeBps, uint256 minStake, uint256 maxUtilizationBps, uint256 keeperBounty))",
|
|
19
36
|
"function quoteFee(uint256 agentId, uint256 principal, uint64 term) view returns (uint256 fee, uint256 sponsorCut, uint256 reserveCut, uint256 premium)",
|
|
20
37
|
"function borrow(uint256 agentId, uint256 amount, uint64 term, address to, uint256 maxFee) returns (uint256)",
|
|
21
38
|
"function repay(uint256 loanId, uint256 expectedAgentId, uint256 maxDue)",
|
|
39
|
+
"function loansOf(uint256 agentId) view returns (uint256[])",
|
|
22
40
|
"function getLoan(uint256 loanId) view returns ((uint256 agentId, uint256 sponsorId, uint256 principal, uint256 fee, uint256 sponsorCut, uint256 reserveCut, uint256 premium, address owner, uint64 issuedAt, uint64 dueAt, uint64 defaultableAt, uint64 minScoreTerm, uint64 closedAt, uint8 status))",
|
|
23
41
|
"event Borrowed(uint256 indexed loanId, uint256 indexed agentId, uint256 indexed sponsorId, uint256 principal, uint256 fee, uint64 dueAt, address to)",
|
|
24
42
|
]);
|
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { ActionProvider, EvmWalletProvider, Network } from "@coinbase/agentkit";
|
|
3
3
|
import { BorrowSchema, GetAgentRecordSchema, GetCreditLineSchema, PayX402WithCreditSchema, RepaySchema } from "./schemas";
|
|
4
|
+
/**
|
|
5
|
+
* Auto-borrow, set by the owner in code (never by the model): pay_x402_with_priors_credit borrows the gap on its own,
|
|
6
|
+
* up to `perCall` USDG a call and `perDay` USDG a UTC day in this process, for the `hosts` listed only (exact host
|
|
7
|
+
* names), for 14 days, and only while this wallet's Autopay (AutoRepay v2) covers the new loan. The model's maxBorrow
|
|
8
|
+
* can only lower the amount.
|
|
9
|
+
*/
|
|
10
|
+
export type AutoBorrowOptions = {
|
|
11
|
+
perCall: string;
|
|
12
|
+
perDay: string;
|
|
13
|
+
hosts: string[];
|
|
14
|
+
};
|
|
15
|
+
export type PriorsOptions = {
|
|
16
|
+
autoBorrow?: AutoBorrowOptions;
|
|
17
|
+
};
|
|
4
18
|
/**
|
|
5
19
|
* PriorsActionProvider gives an agent on Robinhood Chain a USDG credit line from Priors: pay an x402 URL and borrow
|
|
6
20
|
* the gap when the wallet is short, borrow and repay, and read another agent's repayment record before paying it.
|
|
7
21
|
*/
|
|
8
22
|
export declare class PriorsActionProvider extends ActionProvider<EvmWalletProvider> {
|
|
9
23
|
private readonly fetchImpl;
|
|
24
|
+
private readonly auto;
|
|
25
|
+
private borrowedToday;
|
|
10
26
|
/**
|
|
11
27
|
* Constructor for the PriorsActionProvider.
|
|
12
28
|
*
|
|
13
29
|
* @param fetchImpl - The fetch used for Priors' API and the x402 URLs (injectable for tests).
|
|
30
|
+
* @param options - `autoBorrow` (off unless given): see AutoBorrowOptions.
|
|
14
31
|
*/
|
|
15
|
-
constructor(fetchImpl?: typeof fetch);
|
|
32
|
+
constructor(fetchImpl?: typeof fetch, options?: PriorsOptions);
|
|
16
33
|
/**
|
|
17
34
|
* Reads an agent's Priors repayment record.
|
|
18
35
|
*
|
|
@@ -53,6 +70,17 @@ export declare class PriorsActionProvider extends ActionProvider<EvmWalletProvid
|
|
|
53
70
|
* @returns What was paid and borrowed and the response, or an error message.
|
|
54
71
|
*/
|
|
55
72
|
payX402WithCredit(walletProvider: EvmWalletProvider, args: z.infer<typeof PayX402WithCreditSchema>): Promise<string>;
|
|
73
|
+
/**
|
|
74
|
+
* Auto-borrow's condition, read before the borrow is sent: this wallet is the agent's declared wallet, its Autopay
|
|
75
|
+
* plan is on and set under the agent's current owner and key, not held or paused, its limit per loan is at least
|
|
76
|
+
* `due`, and its USDG approval to AutoRepay covers `due` on top of the agent's open loans it already repays (those
|
|
77
|
+
* within its limit), as @priors/mcp's check. Throws a Refusal saying what to turn on otherwise.
|
|
78
|
+
*
|
|
79
|
+
* @param walletProvider - The wallet provider.
|
|
80
|
+
* @param agentId - The agent id.
|
|
81
|
+
* @param due - The new loan's principal plus fee, atomic USDG.
|
|
82
|
+
*/
|
|
83
|
+
private autopayCovers;
|
|
56
84
|
/**
|
|
57
85
|
* Checks if the action provider supports the given network: Robinhood Chain only.
|
|
58
86
|
*
|
|
@@ -74,8 +102,9 @@ export declare class PriorsActionProvider extends ActionProvider<EvmWalletProvid
|
|
|
74
102
|
* @param agentId - The agent id.
|
|
75
103
|
* @param amount - Atomic USDG.
|
|
76
104
|
* @param days - The term in days.
|
|
105
|
+
* @param check - Called with the quoted fee before the borrow is sent; a throw refuses it.
|
|
77
106
|
* @returns The loan.
|
|
78
107
|
*/
|
|
79
108
|
private borrowAmount;
|
|
80
109
|
}
|
|
81
|
-
export declare const priorsActionProvider: (fetchImpl?: typeof fetch) => PriorsActionProvider;
|
|
110
|
+
export declare const priorsActionProvider: (fetchImpl?: typeof fetch, options?: PriorsOptions) => PriorsActionProvider;
|
|
@@ -19,20 +19,42 @@ const schemas_1 = require("./schemas");
|
|
|
19
19
|
const constants_1 = require("./constants");
|
|
20
20
|
const usdg = (atomic) => (0, viem_1.formatUnits)(atomic, constants_1.USDG_DECIMALS);
|
|
21
21
|
const sameAddress = (a, b) => String(a).toLowerCase() === b.toLowerCase();
|
|
22
|
+
const utcDay = () => new Date().toISOString().slice(0, 10);
|
|
23
|
+
/** A refusal before anything is sent (its message is the answer). */
|
|
24
|
+
class Refusal extends Error {
|
|
25
|
+
}
|
|
22
26
|
/**
|
|
23
27
|
* PriorsActionProvider gives an agent on Robinhood Chain a USDG credit line from Priors: pay an x402 URL and borrow
|
|
24
28
|
* the gap when the wallet is short, borrow and repay, and read another agent's repayment record before paying it.
|
|
25
29
|
*/
|
|
26
30
|
class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
27
31
|
fetchImpl;
|
|
32
|
+
auto;
|
|
33
|
+
borrowedToday = { day: "", amount: 0n };
|
|
28
34
|
/**
|
|
29
35
|
* Constructor for the PriorsActionProvider.
|
|
30
36
|
*
|
|
31
37
|
* @param fetchImpl - The fetch used for Priors' API and the x402 URLs (injectable for tests).
|
|
38
|
+
* @param options - `autoBorrow` (off unless given): see AutoBorrowOptions.
|
|
32
39
|
*/
|
|
33
|
-
constructor(fetchImpl = (input, init) => globalThis.fetch(input, init)) {
|
|
40
|
+
constructor(fetchImpl = (input, init) => globalThis.fetch(input, init), options = {}) {
|
|
34
41
|
super("priors", []);
|
|
35
42
|
this.fetchImpl = fetchImpl;
|
|
43
|
+
const a = options.autoBorrow;
|
|
44
|
+
if (!a) {
|
|
45
|
+
this.auto = null;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const perCall = (0, viem_1.parseUnits)(String(a.perCall), constants_1.USDG_DECIMALS);
|
|
49
|
+
const perDay = (0, viem_1.parseUnits)(String(a.perDay), constants_1.USDG_DECIMALS);
|
|
50
|
+
if (perCall <= 0n)
|
|
51
|
+
throw new Error("autoBorrow.perCall must be above 0");
|
|
52
|
+
if (perDay < perCall)
|
|
53
|
+
throw new Error("autoBorrow.perDay must be at least autoBorrow.perCall");
|
|
54
|
+
const hosts = (a.hosts ?? []).map(h => String(h).trim().toLowerCase());
|
|
55
|
+
if (!hosts.length || hosts.some(h => !/^[a-z0-9.-]+(:\d{1,5})?$/.test(h)))
|
|
56
|
+
throw new Error("autoBorrow.hosts must list the host names it may borrow to pay, e.g. [\"models.example\"]");
|
|
57
|
+
this.auto = { perCall, perDay, hosts: new Set(hosts) };
|
|
36
58
|
}
|
|
37
59
|
/**
|
|
38
60
|
* Reads an agent's Priors repayment record.
|
|
@@ -172,9 +194,37 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
172
194
|
try {
|
|
173
195
|
if (!/^https:\/\//i.test(args.url))
|
|
174
196
|
return "Error: only https URLs are paid.";
|
|
197
|
+
const method = args.method ?? (args.body !== undefined ? "POST" : "GET");
|
|
198
|
+
if (method === "GET" && args.body !== undefined)
|
|
199
|
+
return "Error: a GET request cannot carry a body: use POST.";
|
|
200
|
+
let json = false;
|
|
201
|
+
if (args.body !== undefined) {
|
|
202
|
+
try {
|
|
203
|
+
JSON.parse(args.body);
|
|
204
|
+
json = true;
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
json = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const init = () => args.body !== undefined
|
|
211
|
+
? { method, body: args.body, headers: { "content-type": json ? "application/json" : "text/plain; charset=utf-8" } }
|
|
212
|
+
: { method };
|
|
175
213
|
const maxPrice = (0, viem_1.parseUnits)(args.maxPrice, constants_1.USDG_DECIMALS);
|
|
176
|
-
|
|
177
|
-
const
|
|
214
|
+
// autoBorrow: the owner's amount per call, which the model's maxBorrow can only lower, and only for its hosts
|
|
215
|
+
const auto = this.auto;
|
|
216
|
+
const host = new URL(args.url).host.toLowerCase();
|
|
217
|
+
const autoHere = !!auto && auto.hosts.has(host);
|
|
218
|
+
const named = (0, viem_1.parseUnits)(args.maxBorrow ?? "0", constants_1.USDG_DECIMALS);
|
|
219
|
+
const maxBorrow = !auto
|
|
220
|
+
? named
|
|
221
|
+
: !autoHere
|
|
222
|
+
? 0n
|
|
223
|
+
: args.maxBorrow === undefined || named > auto.perCall
|
|
224
|
+
? auto.perCall
|
|
225
|
+
: named;
|
|
226
|
+
const days = args.days ?? (auto ? constants_1.AUTO_TERM_DAYS : constants_1.DEFAULT_TERM_DAYS);
|
|
227
|
+
const probe = await this.fetchImpl(args.url, init());
|
|
178
228
|
if (probe.status !== 402)
|
|
179
229
|
return `No payment asked (HTTP ${probe.status}):\n${(await probe.text()).slice(0, 2000)}`;
|
|
180
230
|
const requirement = await this.usdgRequirement(probe);
|
|
@@ -199,25 +249,98 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
199
249
|
}));
|
|
200
250
|
const gap = price - balance;
|
|
201
251
|
const amount = gap > params.minLoan ? gap : params.minLoan;
|
|
252
|
+
if (auto && !autoHere)
|
|
253
|
+
return `Error: the wallet holds ${usdg(balance)} USDG, short of ${usdg(price)}, and auto-borrow borrows only to pay ${[...auto.hosts].join(", ")}. Nothing was borrowed or paid.`;
|
|
202
254
|
if (amount > maxBorrow) {
|
|
203
|
-
return `Error: the wallet holds ${usdg(balance)} USDG, short of ${usdg(price)}; borrowing ${usdg(amount)} (the pool's minimum loan is ${usdg(params.minLoan)}) is above maxBorrow ${args.maxBorrow || "0"}. Nothing was paid.`;
|
|
255
|
+
return `Error: the wallet holds ${usdg(balance)} USDG, short of ${usdg(price)}; borrowing ${usdg(amount)} (the pool's minimum loan is ${usdg(params.minLoan)}) is above ${auto ? `the most this call may borrow, ${usdg(maxBorrow)}` : `maxBorrow ${args.maxBorrow || "0"}`}. Nothing was paid.`;
|
|
256
|
+
}
|
|
257
|
+
// auto: the day's room is taken here, before any await, so two calls running at once never both pass the
|
|
258
|
+
// limit; it is given back only when the borrow is refused before anything is sent (a borrow sent whose answer
|
|
259
|
+
// was lost stays counted, the safe direction)
|
|
260
|
+
let reserved = null;
|
|
261
|
+
if (auto) {
|
|
262
|
+
const today = utcDay();
|
|
263
|
+
if (this.borrowedToday.day !== today)
|
|
264
|
+
this.borrowedToday = { day: today, amount: 0n };
|
|
265
|
+
if (this.borrowedToday.amount + amount > auto.perDay)
|
|
266
|
+
return `Error: the wallet holds ${usdg(balance)} USDG, short of ${usdg(price)}, and auto-borrow's limit of ${usdg(auto.perDay)} USDG a day has ${usdg(auto.perDay - this.borrowedToday.amount)} left in this process, less than ${usdg(amount)}. Nothing was borrowed or paid.`;
|
|
267
|
+
this.borrowedToday.amount += amount;
|
|
268
|
+
reserved = { day: today, amount };
|
|
204
269
|
}
|
|
205
|
-
|
|
206
|
-
|
|
270
|
+
// auto: 14 days, brought down to the pool's maxTerm; otherwise the term asked, as before
|
|
271
|
+
const termDays = auto && params.maxTerm !== undefined && BigInt(days) * 86400n > BigInt(params.maxTerm) ? Number(BigInt(params.maxTerm) / 86400n) : days;
|
|
272
|
+
const loan = await this.borrowAmount(walletProvider, args.agentId, amount, termDays, auto ? (fee) => this.autopayCovers(walletProvider, args.agentId, amount + fee) : undefined)
|
|
273
|
+
.catch((error) => {
|
|
274
|
+
if (reserved && error instanceof Refusal && this.borrowedToday.day === reserved.day)
|
|
275
|
+
this.borrowedToday.amount -= reserved.amount;
|
|
276
|
+
throw error;
|
|
277
|
+
});
|
|
278
|
+
borrowed = `Borrowed ${usdg(loan.amount)} USDG first: loan #${loan.loanId}, fee ${usdg(loan.fee)}, due ${loan.dueAt} (transaction ${loan.hash}). ${auto ? "Auto-borrow: Autopay repays it from this wallet in its window, the last hours before it is due; keep its due in the wallet by then." : "Repay it with repay_priors_loan before then."}\n`;
|
|
207
279
|
}
|
|
208
280
|
const client = new fetch_1.x402Client()
|
|
209
281
|
.register(constants_1.ROBINHOOD_NETWORK, new client_1.ExactEvmScheme(walletProvider.toSigner()))
|
|
210
282
|
.registerPolicy((_version, reqs) => reqs.filter(r => r.network === constants_1.ROBINHOOD_NETWORK &&
|
|
211
283
|
sameAddress(r.asset, constants_1.USDG) &&
|
|
212
284
|
BigInt(r.amount ?? "0") <= maxPrice));
|
|
213
|
-
const paid = await (0, fetch_1.wrapFetchWithPayment)(this.fetchImpl, client)(args.url);
|
|
285
|
+
const paid = await (0, fetch_1.wrapFetchWithPayment)(this.fetchImpl, client)(args.url, init());
|
|
214
286
|
const receipt = paid.headers.get("payment-response") || paid.headers.get("x-payment-response");
|
|
215
287
|
return `${borrowed}Paid ${usdg(price)} USDG to ${requirement.payTo} (HTTP ${paid.status})${receipt ? `; settlement: ${receipt.slice(0, 200)}` : ""}.\nResponse:\n${(await paid.text()).slice(0, 2000)}`;
|
|
216
288
|
}
|
|
217
289
|
catch (error) {
|
|
290
|
+
if (error instanceof Refusal)
|
|
291
|
+
return `Error: ${error.message}`;
|
|
218
292
|
return `Error paying ${args.url} with Priors credit: ${error}`;
|
|
219
293
|
}
|
|
220
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Auto-borrow's condition, read before the borrow is sent: this wallet is the agent's declared wallet, its Autopay
|
|
297
|
+
* plan is on and set under the agent's current owner and key, not held or paused, its limit per loan is at least
|
|
298
|
+
* `due`, and its USDG approval to AutoRepay covers `due` on top of the agent's open loans it already repays (those
|
|
299
|
+
* within its limit), as @priors/mcp's check. Throws a Refusal saying what to turn on otherwise.
|
|
300
|
+
*
|
|
301
|
+
* @param walletProvider - The wallet provider.
|
|
302
|
+
* @param agentId - The agent id.
|
|
303
|
+
* @param due - The new loan's principal plus fee, atomic USDG.
|
|
304
|
+
*/
|
|
305
|
+
async autopayCovers(walletProvider, agentId, due) {
|
|
306
|
+
const me = walletProvider.getAddress();
|
|
307
|
+
const id = BigInt(agentId);
|
|
308
|
+
const read = (address, abi, functionName, args = []) => walletProvider.readContract({ address, abi, functionName, args });
|
|
309
|
+
const [wallet, owner, plan, epoch, held, pausedUntil, allowance] = (await Promise.all([
|
|
310
|
+
read(constants_1.REGISTRY, constants_1.REGISTRY_ABI, "getAgentWallet", [id]),
|
|
311
|
+
read(constants_1.REGISTRY, constants_1.REGISTRY_ABI, "ownerOf", [id]),
|
|
312
|
+
read(constants_1.AUTOREPAY, constants_1.AUTOREPAY_ABI, "planOf", [me, id]),
|
|
313
|
+
read(constants_1.AUTOREPAY, constants_1.AUTOREPAY_ABI, "epochOf", [id]),
|
|
314
|
+
read(constants_1.AUTOREPAY, constants_1.AUTOREPAY_ABI, "held", [me]),
|
|
315
|
+
read(constants_1.AUTOREPAY, constants_1.AUTOREPAY_ABI, "pausedUntil"),
|
|
316
|
+
read(constants_1.USDG, viem_1.erc20Abi, "allowance", [me, constants_1.AUTOREPAY]),
|
|
317
|
+
]));
|
|
318
|
+
const no = (why) => new Refusal(`auto-borrow borrows only when Autopay covers the new loan (${usdg(due)} USDG due), and ${why}. Nothing was borrowed or paid. Turn Autopay on for this wallet with a limit of at least ${usdg(due)} USDG per loan (Go mode, or @priors/mcp's autopay_on).`);
|
|
319
|
+
const [cap, planOwner, on, , , planEpoch] = plan;
|
|
320
|
+
if (!sameAddress(wallet, me))
|
|
321
|
+
throw no(`this wallet is not agent #${agentId}'s declared wallet`);
|
|
322
|
+
if (!on)
|
|
323
|
+
throw no("Autopay is off for this wallet");
|
|
324
|
+
if (!sameAddress(planOwner, owner) || BigInt(planEpoch) !== BigInt(epoch[0]))
|
|
325
|
+
throw no("this wallet's Autopay plan was set under an earlier owner or key of the agent");
|
|
326
|
+
if (held)
|
|
327
|
+
throw no("Autopay is on hold for this wallet");
|
|
328
|
+
if (BigInt(pausedUntil) > BigInt(Math.floor(Date.now() / 1000)))
|
|
329
|
+
throw no("Autopay is paused by the Safe");
|
|
330
|
+
if (BigInt(cap) < due)
|
|
331
|
+
throw no(`Autopay's limit is ${usdg(BigInt(cap))} USDG per loan`);
|
|
332
|
+
// what the agent's open loans already need from that approval: each active loan within the plan's limit
|
|
333
|
+
const ids = (await walletProvider.readContract({ address: constants_1.PRIORS_POOL, abi: constants_1.POOL_ABI, functionName: "loansOf", args: [id] }));
|
|
334
|
+
let owed = 0n;
|
|
335
|
+
for (const loanId of ids) {
|
|
336
|
+
const l = (await walletProvider.readContract({ address: constants_1.PRIORS_POOL, abi: constants_1.POOL_ABI, functionName: "getLoan", args: [loanId] }));
|
|
337
|
+
const loanDue = BigInt(l.principal) + BigInt(l.fee);
|
|
338
|
+
if (Number(l.status) === constants_1.LOAN_ACTIVE && loanDue <= BigInt(cap))
|
|
339
|
+
owed += loanDue;
|
|
340
|
+
}
|
|
341
|
+
if (BigInt(allowance) < owed + due)
|
|
342
|
+
throw no(`Autopay may pull ${usdg(BigInt(allowance))} USDG from this wallet, and its open loans already need ${usdg(owed)} USDG`);
|
|
343
|
+
}
|
|
221
344
|
/**
|
|
222
345
|
* Checks if the action provider supports the given network: Robinhood Chain only.
|
|
223
346
|
*
|
|
@@ -262,9 +385,10 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
262
385
|
* @param agentId - The agent id.
|
|
263
386
|
* @param amount - Atomic USDG.
|
|
264
387
|
* @param days - The term in days.
|
|
388
|
+
* @param check - Called with the quoted fee before the borrow is sent; a throw refuses it.
|
|
265
389
|
* @returns The loan.
|
|
266
390
|
*/
|
|
267
|
-
async borrowAmount(walletProvider, agentId, amount, days) {
|
|
391
|
+
async borrowAmount(walletProvider, agentId, amount, days, check) {
|
|
268
392
|
const me = walletProvider.getAddress();
|
|
269
393
|
const term = BigInt(days * 86400);
|
|
270
394
|
const [fee] = (await walletProvider.readContract({
|
|
@@ -273,6 +397,8 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
273
397
|
functionName: "quoteFee",
|
|
274
398
|
args: [BigInt(agentId), amount, term],
|
|
275
399
|
}));
|
|
400
|
+
if (check)
|
|
401
|
+
await check(fee);
|
|
276
402
|
const hash = await walletProvider.sendTransaction({
|
|
277
403
|
to: constants_1.PRIORS_POOL,
|
|
278
404
|
data: (0, viem_1.encodeFunctionData)({
|
|
@@ -367,20 +493,24 @@ __decorate([
|
|
|
367
493
|
name: "pay_x402_with_priors_credit",
|
|
368
494
|
description: `
|
|
369
495
|
This tool fetches a URL that charges per call with x402 (HTTP 402) and pays it in USDG on Robinhood Chain from this
|
|
370
|
-
wallet
|
|
371
|
-
|
|
496
|
+
wallet, with GET, or with POST and a body (an OpenAI-style chat completion, for one). If the wallet is short of the
|
|
497
|
+
price, it first borrows the gap from the agent's Priors line, only when maxBorrow covers it, or, when the provider was
|
|
498
|
+
set up with autoBorrow, on its own within the owner's limits while Autopay covers the loan. Real money: confirm the URL,
|
|
499
|
+
the price and the most to borrow with the user first.
|
|
372
500
|
|
|
373
501
|
Inputs:
|
|
374
502
|
- url: the https URL.
|
|
375
503
|
- agentId: the agent's ERC-8004 id whose line may be drawn.
|
|
376
504
|
- maxPrice: the most to pay for this call, in USDG (e.g. 0.10).
|
|
377
|
-
- maxBorrow: the most to borrow if the wallet is short, in USDG (default 0: never borrow
|
|
378
|
-
|
|
505
|
+
- maxBorrow: the most to borrow if the wallet is short, in USDG (default 0: never borrow; with autoBorrow, its amount
|
|
506
|
+
per call, which this can only lower).
|
|
507
|
+
- days: the term of a loan taken for the gap (default 7; 14 with autoBorrow).
|
|
508
|
+
- method, body: POST and its body (default GET with no body).`,
|
|
379
509
|
schema: schemas_1.PayX402WithCreditSchema,
|
|
380
510
|
}),
|
|
381
511
|
__metadata("design:type", Function),
|
|
382
512
|
__metadata("design:paramtypes", [agentkit_1.EvmWalletProvider, void 0]),
|
|
383
513
|
__metadata("design:returntype", Promise)
|
|
384
514
|
], PriorsActionProvider.prototype, "payX402WithCredit", null);
|
|
385
|
-
const priorsActionProvider = (fetchImpl) => new PriorsActionProvider(fetchImpl);
|
|
515
|
+
const priorsActionProvider = (fetchImpl, options) => new PriorsActionProvider(fetchImpl, options);
|
|
386
516
|
exports.priorsActionProvider = priorsActionProvider;
|
package/dist/schemas.d.ts
CHANGED
|
@@ -37,18 +37,24 @@ export declare const PayX402WithCreditSchema: z.ZodObject<{
|
|
|
37
37
|
url: z.ZodString;
|
|
38
38
|
agentId: z.ZodNumber;
|
|
39
39
|
maxPrice: z.ZodString;
|
|
40
|
-
maxBorrow: z.
|
|
41
|
-
days: z.
|
|
40
|
+
maxBorrow: z.ZodOptional<z.ZodString>;
|
|
41
|
+
days: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
method: z.ZodOptional<z.ZodEnum<["GET", "POST"]>>;
|
|
43
|
+
body: z.ZodOptional<z.ZodString>;
|
|
42
44
|
}, "strip", z.ZodTypeAny, {
|
|
43
45
|
agentId: number;
|
|
44
|
-
days: number;
|
|
45
46
|
url: string;
|
|
46
47
|
maxPrice: string;
|
|
47
|
-
|
|
48
|
+
days?: number | undefined;
|
|
49
|
+
maxBorrow?: string | undefined;
|
|
50
|
+
method?: "GET" | "POST" | undefined;
|
|
51
|
+
body?: string | undefined;
|
|
48
52
|
}, {
|
|
49
53
|
agentId: number;
|
|
50
54
|
url: string;
|
|
51
55
|
maxPrice: string;
|
|
52
56
|
days?: number | undefined;
|
|
53
57
|
maxBorrow?: string | undefined;
|
|
58
|
+
method?: "GET" | "POST" | undefined;
|
|
59
|
+
body?: string | undefined;
|
|
54
60
|
}>;
|
package/dist/schemas.js
CHANGED
|
@@ -32,14 +32,23 @@ exports.PayX402WithCreditSchema = zod_1.z
|
|
|
32
32
|
maxPrice: zod_1.z.string().describe("Most to pay for this call, in USDG, e.g. 0.10"),
|
|
33
33
|
maxBorrow: zod_1.z
|
|
34
34
|
.string()
|
|
35
|
-
.
|
|
36
|
-
.describe("Most to borrow from the Priors line if the wallet is short, in USDG; 0 never borrows"),
|
|
35
|
+
.optional()
|
|
36
|
+
.describe("Most to borrow from the Priors line if the wallet is short, in USDG; 0 never borrows. Default 0, or, when the provider was given autoBorrow, its amount per call (a value here can only lower it)"),
|
|
37
37
|
days: zod_1.z
|
|
38
38
|
.number()
|
|
39
39
|
.int()
|
|
40
40
|
.positive()
|
|
41
41
|
.max(90)
|
|
42
|
-
.
|
|
43
|
-
.describe("Term of a loan taken for the gap (default 7)"),
|
|
42
|
+
.optional()
|
|
43
|
+
.describe("Term of a loan taken for the gap (default 7, or 14 with the provider's autoBorrow)"),
|
|
44
|
+
method: zod_1.z
|
|
45
|
+
.enum(["GET", "POST"])
|
|
46
|
+
.optional()
|
|
47
|
+
.describe("HTTP method (default GET, or POST when a body is given)"),
|
|
48
|
+
body: zod_1.z
|
|
49
|
+
.string()
|
|
50
|
+
.max(100_000)
|
|
51
|
+
.optional()
|
|
52
|
+
.describe("Request body, e.g. an OpenAI-style chat completion request as JSON; sent as application/json when it parses"),
|
|
44
53
|
})
|
|
45
54
|
.describe("Pay an x402 URL in USDG on Robinhood Chain, borrowing the gap from the Priors line if needed");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priors/agentkit",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Priors action provider for Coinbase AgentKit: pay x402 APIs in USDG on Robinhood Chain and borrow the gap from an ERC-8004 agent's Priors credit line, repay, and read another agent's repayment record before paying it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Priors <contact@priors.trade>",
|