@priors/agentkit 0.2.4 → 0.2.5
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 +5 -2
- package/dist/priorsActionProvider.d.ts +25 -3
- package/dist/priorsActionProvider.js +51 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,8 +43,11 @@ priorsActionProvider(undefined, { autoBorrow: { perCall: "5", perDay: "10", host
|
|
|
43
43
|
|
|
44
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
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
|
|
47
|
-
|
|
46
|
+
The limits are `perCall` USDG a call and `perDay` USDG a UTC day (each read exactly, at most 6 decimals), for 14 days
|
|
47
|
+
whatever term the model names (or the pool's maximum term). The day counts what the pool lent the agent since 00:00 UTC,
|
|
48
|
+
every loan of the agent whoever took it, as well as this process's own auto-borrows, so a restart does not start the
|
|
49
|
+
day again (0.2.5); if those loans cannot be read, nothing is borrowed. A borrow another process sent that is not mined
|
|
50
|
+
yet is not seen, and after a restart a provider that auto-borrows for several agents counts each agent's loans only. It borrows only while this wallet's Autopay
|
|
48
51
|
(AutoRepay v2) covers the new loan: the wallet is the agent's
|
|
49
52
|
declared wallet, the plan is on under the agent's current owner, not held or paused, its limit per loan and its USDG
|
|
50
53
|
approval cover the loan's due. Otherwise nothing is borrowed or paid, and the answer says to turn Autopay on. The
|
|
@@ -3,9 +3,11 @@ import { ActionProvider, EvmWalletProvider, Network } from "@coinbase/agentkit";
|
|
|
3
3
|
import { BorrowSchema, GetAgentRecordSchema, GetCreditLineSchema, PayX402WithCreditSchema, RepaySchema } from "./schemas";
|
|
4
4
|
/**
|
|
5
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
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* up to `perCall` USDG a call and `perDay` USDG a UTC day (each read exactly, at most 6 decimals), for the `hosts` listed
|
|
7
|
+
* only (exact host names), for 14 days whatever term the model names, and only while this wallet's Autopay (AutoRepay
|
|
8
|
+
* v2) covers the new loan. The model's maxBorrow can only lower the amount. The day counts what the pool lent the agent
|
|
9
|
+
* since 00:00 UTC, every loan of the agent whoever took it, as well as this process's own auto-borrows, so a restart
|
|
10
|
+
* does not start the day again (GHSA-j679).
|
|
9
11
|
*/
|
|
10
12
|
export type AutoBorrowOptions = {
|
|
11
13
|
perCall: string;
|
|
@@ -92,6 +94,26 @@ export declare class PriorsActionProvider extends ActionProvider<EvmWalletProvid
|
|
|
92
94
|
* @param due - The new loan's principal plus fee, atomic USDG.
|
|
93
95
|
*/
|
|
94
96
|
private autopayCovers;
|
|
97
|
+
/**
|
|
98
|
+
* Auto-borrow's day limit as the pool records the day (GHSA-j679): `amount` more on top of what the pool lent agent
|
|
99
|
+
* `agentId` since 00:00 UTC of `day` must stay within `perDay`. Every loan of the agent issued that day counts, whoever
|
|
100
|
+
* took it (the pool records the agent and its owner, not the key that borrowed), so a restart, or another process of
|
|
101
|
+
* this wallet, starts the day where the pool is; that can only lower auto-borrow's room. A read that fails, or a loan
|
|
102
|
+
* whose record does not read as one, refuses (nothing is sent). The pool lists an agent's loans oldest first
|
|
103
|
+
* (CreditPoolV2 appends each in borrow), so they are read from the newest back to the first one issued before the day.
|
|
104
|
+
*
|
|
105
|
+
* SHORTCUT: what the pool shows: a borrow another process sent and the chain has not mined yet is not seen, and after a
|
|
106
|
+
* restart a provider that auto-borrows for several agents counts each agent's own loans only (its count across agents
|
|
107
|
+
* starts again). Upgrade when the provider gets a place to keep state (@priors/mcp's daily ledger under
|
|
108
|
+
* PRIORS_STATE_DIR), or on a report of either.
|
|
109
|
+
*
|
|
110
|
+
* @param walletProvider - The wallet provider.
|
|
111
|
+
* @param agentId - The agent id.
|
|
112
|
+
* @param amount - The borrow about to be sent, atomic USDG.
|
|
113
|
+
* @param perDay - The owner's limit a UTC day, atomic USDG.
|
|
114
|
+
* @param day - The UTC day the borrow counts in (YYYY-MM-DD).
|
|
115
|
+
*/
|
|
116
|
+
private dayRoom;
|
|
95
117
|
/**
|
|
96
118
|
* Before this wallet's money moves for agent `agentId` (a borrow into it, a repayment from it), @priors/mcp's rule
|
|
97
119
|
* (needController, GHSA-9pw7): the wallet controls the agent at the pool AND is its current owner or its declared
|
|
@@ -174,6 +174,8 @@ function settledTx(header) {
|
|
|
174
174
|
class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
175
175
|
fetchImpl;
|
|
176
176
|
auto;
|
|
177
|
+
// what this process auto-borrowed today, sent or about to be: the borrows the pool does not list yet. The pool's own
|
|
178
|
+
// record of the agent's loans today is read before each auto-borrow too (dayRoom), so a restart starts from it.
|
|
177
179
|
borrowedToday = { day: "", amount: 0n };
|
|
178
180
|
// One signature per purchase (GHSA-pr4v), @priors/x402's rule (createPayer's unsettled payments, @priors/mcp's
|
|
179
181
|
// outstanding ones): a payment this provider signed for a wallet's purchase (the method, the URL as the seller reads
|
|
@@ -512,7 +514,9 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
512
514
|
// auto: the day's room is taken here, before any await, so two calls running at once never both pass the
|
|
513
515
|
// limit; it is given back whenever the borrow stops before it is sent, refused or a read failing, the
|
|
514
516
|
// controller check's included (GHSA-q4cg, GHSA-87h5), and when it is mined and reverts (it moved nothing:
|
|
515
|
-
// GHSA-mc89); a borrow whose sending began and whose answer is lost stays counted (the safe direction)
|
|
517
|
+
// GHSA-mc89); a borrow whose sending began and whose answer is lost stays counted (the safe direction).
|
|
518
|
+
// Before it is sent, what the pool lent the agent today is checked against the same limit (dayRoom), so a
|
|
519
|
+
// restart, which starts this count at 0, does not start the day again (GHSA-j679)
|
|
516
520
|
let reserved = null;
|
|
517
521
|
let sending = false;
|
|
518
522
|
if (auto) {
|
|
@@ -520,13 +524,19 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
520
524
|
if (this.borrowedToday.day !== today)
|
|
521
525
|
this.borrowedToday = { day: today, amount: 0n };
|
|
522
526
|
if (this.borrowedToday.amount + amount > auto.perDay)
|
|
523
|
-
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
|
|
527
|
+
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, after what this process borrowed today, less than ${usdg(amount)}. Nothing was borrowed or paid.`;
|
|
524
528
|
this.borrowedToday.amount += amount;
|
|
525
529
|
reserved = { day: today, amount };
|
|
526
530
|
}
|
|
531
|
+
const day = reserved?.day ?? "";
|
|
527
532
|
// auto: 14 days, brought down to the pool's maxTerm; otherwise the term asked, as before
|
|
528
533
|
const termDays = auto && params.maxTerm !== undefined && BigInt(days) * 86400n > BigInt(params.maxTerm) ? Number(BigInt(params.maxTerm) / 86400n) : days;
|
|
529
|
-
const loan = await this.borrowAmount(walletProvider, args.agentId, amount, termDays, auto
|
|
534
|
+
const loan = await this.borrowAmount(walletProvider, args.agentId, amount, termDays, auto
|
|
535
|
+
? async (fee) => {
|
|
536
|
+
await this.autopayCovers(walletProvider, args.agentId, amount + fee);
|
|
537
|
+
await this.dayRoom(walletProvider, args.agentId, amount, auto.perDay, day);
|
|
538
|
+
}
|
|
539
|
+
: undefined, "borrowed or paid", () => {
|
|
530
540
|
sending = true; // right before the borrow's transaction is sent, every check passed
|
|
531
541
|
})
|
|
532
542
|
.catch((error) => {
|
|
@@ -616,6 +626,44 @@ class PriorsActionProvider extends agentkit_1.ActionProvider {
|
|
|
616
626
|
if (BigInt(allowance) < owed + due)
|
|
617
627
|
throw no(`Autopay may pull ${usdg(BigInt(allowance))} USDG from this wallet, and its open loans already need ${usdg(owed)} USDG`);
|
|
618
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Auto-borrow's day limit as the pool records the day (GHSA-j679): `amount` more on top of what the pool lent agent
|
|
631
|
+
* `agentId` since 00:00 UTC of `day` must stay within `perDay`. Every loan of the agent issued that day counts, whoever
|
|
632
|
+
* took it (the pool records the agent and its owner, not the key that borrowed), so a restart, or another process of
|
|
633
|
+
* this wallet, starts the day where the pool is; that can only lower auto-borrow's room. A read that fails, or a loan
|
|
634
|
+
* whose record does not read as one, refuses (nothing is sent). The pool lists an agent's loans oldest first
|
|
635
|
+
* (CreditPoolV2 appends each in borrow), so they are read from the newest back to the first one issued before the day.
|
|
636
|
+
*
|
|
637
|
+
* SHORTCUT: what the pool shows: a borrow another process sent and the chain has not mined yet is not seen, and after a
|
|
638
|
+
* restart a provider that auto-borrows for several agents counts each agent's own loans only (its count across agents
|
|
639
|
+
* starts again). Upgrade when the provider gets a place to keep state (@priors/mcp's daily ledger under
|
|
640
|
+
* PRIORS_STATE_DIR), or on a report of either.
|
|
641
|
+
*
|
|
642
|
+
* @param walletProvider - The wallet provider.
|
|
643
|
+
* @param agentId - The agent id.
|
|
644
|
+
* @param amount - The borrow about to be sent, atomic USDG.
|
|
645
|
+
* @param perDay - The owner's limit a UTC day, atomic USDG.
|
|
646
|
+
* @param day - The UTC day the borrow counts in (YYYY-MM-DD).
|
|
647
|
+
*/
|
|
648
|
+
async dayRoom(walletProvider, agentId, amount, perDay, day) {
|
|
649
|
+
const id = BigInt(agentId);
|
|
650
|
+
let lent = 0n;
|
|
651
|
+
try {
|
|
652
|
+
const start = BigInt(Date.parse(`${day}T00:00:00Z`) / 1000);
|
|
653
|
+
const ids = (await walletProvider.readContract({ address: constants_1.PRIORS_POOL, abi: constants_1.POOL_ABI, functionName: "loansOf", args: [id] }));
|
|
654
|
+
for (let i = ids.length - 1; i >= 0; i--) {
|
|
655
|
+
const l = (await walletProvider.readContract({ address: constants_1.PRIORS_POOL, abi: constants_1.POOL_ABI, functionName: "getLoan", args: [ids[i]] }));
|
|
656
|
+
if (BigInt(l.issuedAt) < start)
|
|
657
|
+
break;
|
|
658
|
+
lent += BigInt(l.principal);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
catch (error) {
|
|
662
|
+
throw new Refusal(`auto-borrow's day limit could not be checked: agent #${agentId}'s loans of today could not be read from the pool (${(0, fence_js_1.unfenced)(String(error))}). Nothing was borrowed or paid.`);
|
|
663
|
+
}
|
|
664
|
+
if (lent + amount > perDay)
|
|
665
|
+
throw new Refusal(`auto-borrow's limit of ${usdg(perDay)} USDG a day has ${usdg(lent < perDay ? perDay - lent : 0n)} left: the pool lent agent #${agentId} ${usdg(lent)} USDG since 00:00 UTC (every loan of the agent today counts, whoever took it, so a restart does not start the day again), less than ${usdg(amount)}. Nothing was borrowed or paid.`);
|
|
666
|
+
}
|
|
619
667
|
/**
|
|
620
668
|
* Before this wallet's money moves for agent `agentId` (a borrow into it, a repayment from it), @priors/mcp's rule
|
|
621
669
|
* (needController, GHSA-9pw7): the wallet controls the agent at the pool AND is its current owner or its declared
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priors/agentkit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
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>",
|