@oobe-protocol-labs/synapse-sap-sdk 0.6.2 → 0.7.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/dist/cjs/constants/seeds.js +7 -0
- package/dist/cjs/constants/seeds.js.map +1 -1
- package/dist/cjs/core/client.js +42 -0
- package/dist/cjs/core/client.js.map +1 -1
- package/dist/cjs/events/geyser.js +295 -0
- package/dist/cjs/events/geyser.js.map +1 -0
- package/dist/cjs/idl/synapse_agent_sap.json +7545 -3501
- package/dist/cjs/index.js +28 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/modules/escrow-v2.js +241 -0
- package/dist/cjs/modules/escrow-v2.js.map +1 -0
- package/dist/cjs/modules/escrow.js +4 -0
- package/dist/cjs/modules/escrow.js.map +1 -1
- package/dist/cjs/modules/index.js +7 -1
- package/dist/cjs/modules/index.js.map +1 -1
- package/dist/cjs/modules/staking.js +94 -0
- package/dist/cjs/modules/staking.js.map +1 -0
- package/dist/cjs/modules/subscription.js +96 -0
- package/dist/cjs/modules/subscription.js.map +1 -0
- package/dist/cjs/pda/index.js +143 -1
- package/dist/cjs/pda/index.js.map +1 -1
- package/dist/cjs/postgres/sync.js +72 -4
- package/dist/cjs/postgres/sync.js.map +1 -1
- package/dist/cjs/registries/x402.js +88 -51
- package/dist/cjs/registries/x402.js.map +1 -1
- package/dist/cjs/types/enums.js +51 -1
- package/dist/cjs/types/enums.js.map +1 -1
- package/dist/cjs/types/index.js +4 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/instructions.js.map +1 -1
- package/dist/cjs/utils/escrow-validation.js +219 -0
- package/dist/cjs/utils/escrow-validation.js.map +1 -0
- package/dist/cjs/utils/index.js +12 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/merchant-validator.js +246 -0
- package/dist/cjs/utils/merchant-validator.js.map +1 -0
- package/dist/cjs/utils/x402-direct.js +231 -0
- package/dist/cjs/utils/x402-direct.js.map +1 -0
- package/dist/esm/constants/seeds.js +7 -0
- package/dist/esm/constants/seeds.js.map +1 -1
- package/dist/esm/core/client.js +42 -0
- package/dist/esm/core/client.js.map +1 -1
- package/dist/esm/events/geyser.js +258 -0
- package/dist/esm/events/geyser.js.map +1 -0
- package/dist/esm/idl/synapse_agent_sap.json +7545 -3501
- package/dist/esm/index.js +7 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/modules/escrow-v2.js +237 -0
- package/dist/esm/modules/escrow-v2.js.map +1 -0
- package/dist/esm/modules/escrow.js +4 -0
- package/dist/esm/modules/escrow.js.map +1 -1
- package/dist/esm/modules/index.js +3 -0
- package/dist/esm/modules/index.js.map +1 -1
- package/dist/esm/modules/staking.js +90 -0
- package/dist/esm/modules/staking.js.map +1 -0
- package/dist/esm/modules/subscription.js +92 -0
- package/dist/esm/modules/subscription.js.map +1 -0
- package/dist/esm/pda/index.js +135 -0
- package/dist/esm/pda/index.js.map +1 -1
- package/dist/esm/postgres/sync.js +72 -4
- package/dist/esm/postgres/sync.js.map +1 -1
- package/dist/esm/registries/x402.js +89 -52
- package/dist/esm/registries/x402.js.map +1 -1
- package/dist/esm/types/enums.js +50 -0
- package/dist/esm/types/enums.js.map +1 -1
- package/dist/esm/types/index.js +1 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/instructions.js.map +1 -1
- package/dist/esm/utils/escrow-validation.js +212 -0
- package/dist/esm/utils/escrow-validation.js.map +1 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/merchant-validator.js +241 -0
- package/dist/esm/utils/merchant-validator.js.map +1 -0
- package/dist/esm/utils/x402-direct.js +228 -0
- package/dist/esm/utils/x402-direct.js.map +1 -0
- package/dist/types/constants/seeds.d.ts +7 -0
- package/dist/types/constants/seeds.d.ts.map +1 -1
- package/dist/types/core/client.d.ts +33 -0
- package/dist/types/core/client.d.ts.map +1 -1
- package/dist/types/events/geyser.d.ts +150 -0
- package/dist/types/events/geyser.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modules/escrow-v2.d.ts +51 -0
- package/dist/types/modules/escrow-v2.d.ts.map +1 -0
- package/dist/types/modules/escrow.d.ts +4 -0
- package/dist/types/modules/escrow.d.ts.map +1 -1
- package/dist/types/modules/index.d.ts +3 -0
- package/dist/types/modules/index.d.ts.map +1 -1
- package/dist/types/modules/staking.d.ts +32 -0
- package/dist/types/modules/staking.d.ts.map +1 -0
- package/dist/types/modules/subscription.d.ts +33 -0
- package/dist/types/modules/subscription.d.ts.map +1 -0
- package/dist/types/pda/index.d.ts +99 -0
- package/dist/types/pda/index.d.ts.map +1 -1
- package/dist/types/plugin/schemas.d.ts +2 -2
- package/dist/types/postgres/sync.d.ts +26 -2
- package/dist/types/postgres/sync.d.ts.map +1 -1
- package/dist/types/registries/x402.d.ts +14 -12
- package/dist/types/registries/x402.d.ts.map +1 -1
- package/dist/types/types/accounts.d.ts +157 -1
- package/dist/types/types/accounts.d.ts.map +1 -1
- package/dist/types/types/enums.d.ts +64 -0
- package/dist/types/types/enums.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +4 -4
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/instructions.d.ts +34 -0
- package/dist/types/types/instructions.d.ts.map +1 -1
- package/dist/types/utils/escrow-validation.d.ts +145 -0
- package/dist/types/utils/escrow-validation.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/merchant-validator.d.ts +176 -0
- package/dist/types/utils/merchant-validator.d.ts.map +1 -0
- package/dist/types/utils/x402-direct.d.ts +114 -0
- package/dist/types/utils/x402-direct.d.ts.map +1 -0
- package/package.json +5 -1
- package/src/constants/seeds.ts +7 -0
- package/src/core/client.ts +45 -0
- package/src/events/geyser.ts +384 -0
- package/src/events/yellowstone.d.ts +7 -0
- package/src/idl/synapse_agent_sap.json +7545 -3501
- package/src/index.ts +51 -0
- package/src/modules/escrow-v2.ts +396 -0
- package/src/modules/escrow.ts +4 -0
- package/src/modules/index.ts +3 -0
- package/src/modules/staking.ts +122 -0
- package/src/modules/subscription.ts +147 -0
- package/src/pda/index.ts +196 -0
- package/src/postgres/sync.ts +90 -4
- package/src/registries/x402.ts +108 -69
- package/src/types/accounts.ts +192 -1
- package/src/types/enums.ts +65 -0
- package/src/types/index.ts +15 -0
- package/src/types/instructions.ts +40 -0
- package/src/utils/escrow-validation.ts +301 -0
- package/src/utils/index.ts +28 -0
- package/src/utils/merchant-validator.ts +359 -0
- package/src/utils/x402-direct.ts +370 -0
package/src/registries/x402.ts
CHANGED
|
@@ -74,12 +74,14 @@ import {
|
|
|
74
74
|
deriveAgent,
|
|
75
75
|
deriveAgentStats,
|
|
76
76
|
deriveEscrow,
|
|
77
|
+
deriveEscrowV2,
|
|
77
78
|
} from "../pda";
|
|
78
79
|
import { sha256, hashToArray } from "../utils";
|
|
79
80
|
import { SapNetwork } from "../constants/network";
|
|
80
81
|
import type { SapNetworkId } from "../constants/network";
|
|
81
82
|
import type {
|
|
82
83
|
EscrowAccountData,
|
|
84
|
+
EscrowAccountV2Data,
|
|
83
85
|
AgentAccountData,
|
|
84
86
|
VolumeCurveBreakpoint,
|
|
85
87
|
Settlement,
|
|
@@ -371,18 +373,14 @@ export class X402Registry {
|
|
|
371
373
|
volumeCurve = opts.volumeCurve ?? [];
|
|
372
374
|
totalBefore = opts.totalCallsBefore ?? 0;
|
|
373
375
|
} else {
|
|
374
|
-
// Try to read from existing escrow
|
|
376
|
+
// Try to read from existing escrow (V2 first, then V1)
|
|
375
377
|
const [agentPda] = deriveAgent(agentWallet);
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (escrow) {
|
|
383
|
-
pricePerCall = escrow.pricePerCall;
|
|
384
|
-
volumeCurve = escrow.volumeCurve ?? [];
|
|
385
|
-
totalBefore = escrow.totalCallsSettled.toNumber();
|
|
378
|
+
const resolved = await this.resolveEscrow(agentPda, this.wallet);
|
|
379
|
+
|
|
380
|
+
if (resolved) {
|
|
381
|
+
pricePerCall = resolved.escrow.pricePerCall;
|
|
382
|
+
volumeCurve = resolved.escrow.volumeCurve ?? [];
|
|
383
|
+
totalBefore = resolved.escrow.totalCallsSettled.toNumber();
|
|
386
384
|
} else {
|
|
387
385
|
// Fall back to agent's first pricing tier
|
|
388
386
|
const agent = await this.fetchNullable<AgentAccountData>(
|
|
@@ -493,6 +491,8 @@ export class X402Registry {
|
|
|
493
491
|
* @param opts - Payment options (price, max calls, deposit, etc.).
|
|
494
492
|
* @returns A {@link PaymentContext} with escrow details and transaction signature.
|
|
495
493
|
* @since v0.1.0
|
|
494
|
+
* @deprecated Since v0.7.0 — Creates a V1 escrow. Use `client.escrowV2.create()` for
|
|
495
|
+
* V2 escrows with dispute windows, co-signing, and settlement security.
|
|
496
496
|
*
|
|
497
497
|
* @example
|
|
498
498
|
* ```ts
|
|
@@ -509,7 +509,6 @@ export class X402Registry {
|
|
|
509
509
|
): Promise<PaymentContext> {
|
|
510
510
|
const [agentPda] = deriveAgent(agentWallet);
|
|
511
511
|
const [escrowPda] = deriveEscrow(agentPda, this.wallet);
|
|
512
|
-
const [statsPda] = deriveAgentStats(agentPda);
|
|
513
512
|
|
|
514
513
|
const pricePerCall = new BN(opts.pricePerCall.toString());
|
|
515
514
|
const maxCalls = new BN((opts.maxCalls ?? 0).toString());
|
|
@@ -536,7 +535,6 @@ export class X402Registry {
|
|
|
536
535
|
.accounts({
|
|
537
536
|
depositor: this.wallet,
|
|
538
537
|
agent: agentPda,
|
|
539
|
-
agentStats: statsPda,
|
|
540
538
|
escrow: escrowPda,
|
|
541
539
|
systemProgram: SystemProgram.programId,
|
|
542
540
|
})
|
|
@@ -562,6 +560,7 @@ export class X402Registry {
|
|
|
562
560
|
* @param amount - Amount to deposit in smallest token unit.
|
|
563
561
|
* @returns The transaction signature.
|
|
564
562
|
* @since v0.1.0
|
|
563
|
+
* @deprecated Since v0.7.0 — Operates on V1 escrows only. Use `client.escrowV2.deposit()` instead.
|
|
565
564
|
*/
|
|
566
565
|
async addFunds(
|
|
567
566
|
agentWallet: PublicKey,
|
|
@@ -588,6 +587,7 @@ export class X402Registry {
|
|
|
588
587
|
* @param amount - Amount to withdraw in smallest token unit.
|
|
589
588
|
* @returns The transaction signature.
|
|
590
589
|
* @since v0.1.0
|
|
590
|
+
* @deprecated Since v0.7.0 — Operates on V1 escrows only. Use `client.escrowV2.withdraw()` instead.
|
|
591
591
|
*/
|
|
592
592
|
async withdrawFunds(
|
|
593
593
|
agentWallet: PublicKey,
|
|
@@ -614,6 +614,7 @@ export class X402Registry {
|
|
|
614
614
|
* @param agentWallet - Agent wallet of the escrow.
|
|
615
615
|
* @returns The transaction signature.
|
|
616
616
|
* @since v0.1.0
|
|
617
|
+
* @deprecated Since v0.7.0 — Operates on V1 escrows only. Use `client.escrowV2.close()` instead.
|
|
617
618
|
*/
|
|
618
619
|
async closeEscrow(agentWallet: PublicKey): Promise<TransactionSignature> {
|
|
619
620
|
const [agentPda] = deriveAgent(agentWallet);
|
|
@@ -678,17 +679,14 @@ export class X402Registry {
|
|
|
678
679
|
opts?: { network?: SapNetworkId | string },
|
|
679
680
|
): Promise<X402Headers | null> {
|
|
680
681
|
const [agentPda] = deriveAgent(agentWallet);
|
|
681
|
-
const
|
|
682
|
+
const resolved = await this.resolveEscrow(agentPda, this.wallet);
|
|
683
|
+
if (!resolved) return null;
|
|
682
684
|
|
|
683
|
-
const escrow =
|
|
684
|
-
"escrowAccount",
|
|
685
|
-
escrowPda,
|
|
686
|
-
);
|
|
687
|
-
if (!escrow) return null;
|
|
685
|
+
const escrow = resolved.escrow;
|
|
688
686
|
|
|
689
687
|
return {
|
|
690
688
|
"X-Payment-Protocol": "SAP-x402",
|
|
691
|
-
"X-Payment-Escrow": escrowPda.toBase58(),
|
|
689
|
+
"X-Payment-Escrow": resolved.escrowPda.toBase58(),
|
|
692
690
|
"X-Payment-Agent": agentPda.toBase58(),
|
|
693
691
|
"X-Payment-Depositor": this.wallet.toBase58(),
|
|
694
692
|
"X-Payment-MaxCalls": escrow.maxCalls.toString(),
|
|
@@ -742,14 +740,16 @@ export class X402Registry {
|
|
|
742
740
|
);
|
|
743
741
|
|
|
744
742
|
const [agentPda] = deriveAgent(this.wallet);
|
|
745
|
-
const [escrowPda] = deriveEscrow(agentPda, depositorWallet);
|
|
746
743
|
const [statsPda] = deriveAgentStats(agentPda);
|
|
747
744
|
|
|
748
|
-
//
|
|
749
|
-
const
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
745
|
+
// Auto-detect escrow version
|
|
746
|
+
const resolved = await this.resolveEscrow(agentPda, depositorWallet);
|
|
747
|
+
if (!resolved) {
|
|
748
|
+
throw new Error("No escrow found for this agent + depositor pair");
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const escrow = resolved.escrow;
|
|
752
|
+
const escrowPda = resolved.escrowPda;
|
|
753
753
|
const estimate = this.calculateCost(
|
|
754
754
|
escrow.pricePerCall,
|
|
755
755
|
escrow.volumeCurve,
|
|
@@ -761,15 +761,29 @@ export class X402Registry {
|
|
|
761
761
|
const preIxs = buildPriorityFeeIxs(opts);
|
|
762
762
|
const rpcOpts = buildRpcOptions(opts);
|
|
763
763
|
|
|
764
|
-
let builder
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
764
|
+
let builder;
|
|
765
|
+
if (resolved.version === 2) {
|
|
766
|
+
// V2: settleCallsV2 requires escrow_nonce (default 0)
|
|
767
|
+
builder = this.methods
|
|
768
|
+
.settleCallsV2(new BN(0), new BN(callsToSettle), serviceHash)
|
|
769
|
+
.accounts({
|
|
770
|
+
wallet: this.wallet,
|
|
771
|
+
agent: agentPda,
|
|
772
|
+
agentStats: statsPda,
|
|
773
|
+
escrow: escrowPda,
|
|
774
|
+
systemProgram: SystemProgram.programId,
|
|
775
|
+
});
|
|
776
|
+
} else {
|
|
777
|
+
builder = this.methods
|
|
778
|
+
.settleCalls(new BN(callsToSettle), serviceHash)
|
|
779
|
+
.accounts({
|
|
780
|
+
wallet: this.wallet,
|
|
781
|
+
agent: agentPda,
|
|
782
|
+
agentStats: statsPda,
|
|
783
|
+
escrow: escrowPda,
|
|
784
|
+
systemProgram: SystemProgram.programId,
|
|
785
|
+
});
|
|
786
|
+
}
|
|
773
787
|
|
|
774
788
|
if (preIxs.length > 0) {
|
|
775
789
|
builder = builder.preInstructions(preIxs);
|
|
@@ -825,14 +839,16 @@ export class X402Registry {
|
|
|
825
839
|
const totalCalls = entries.reduce((sum, e) => sum + e.calls, 0);
|
|
826
840
|
|
|
827
841
|
const [agentPda] = deriveAgent(this.wallet);
|
|
828
|
-
const [escrowPda] = deriveEscrow(agentPda, depositorWallet);
|
|
829
842
|
const [statsPda] = deriveAgentStats(agentPda);
|
|
830
843
|
|
|
831
|
-
//
|
|
832
|
-
const
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
844
|
+
// Auto-detect escrow version
|
|
845
|
+
const resolved = await this.resolveEscrow(agentPda, depositorWallet);
|
|
846
|
+
if (!resolved) {
|
|
847
|
+
throw new Error("No escrow found for this agent + depositor pair");
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const escrow = resolved.escrow;
|
|
851
|
+
const escrowPda = resolved.escrowPda;
|
|
836
852
|
const estimate = this.calculateCost(
|
|
837
853
|
escrow.pricePerCall,
|
|
838
854
|
escrow.volumeCurve,
|
|
@@ -887,21 +903,17 @@ export class X402Registry {
|
|
|
887
903
|
): Promise<EscrowBalance | null> {
|
|
888
904
|
const [agentPda] = deriveAgent(agentWallet);
|
|
889
905
|
const dep = depositor ?? this.wallet;
|
|
890
|
-
const [escrowPda] = deriveEscrow(agentPda, dep);
|
|
891
906
|
|
|
892
|
-
const
|
|
893
|
-
|
|
894
|
-
escrowPda,
|
|
895
|
-
);
|
|
896
|
-
if (!escrow) return null;
|
|
907
|
+
const resolved = await this.resolveEscrow(agentPda, dep);
|
|
908
|
+
if (!resolved) return null;
|
|
897
909
|
|
|
910
|
+
const escrow = resolved.escrow;
|
|
898
911
|
const now = Math.floor(Date.now() / 1000);
|
|
899
912
|
const isExpired = escrow.expiresAt.toNumber() > 0 && now >= escrow.expiresAt.toNumber();
|
|
900
913
|
const maxCalls = escrow.maxCalls.toNumber();
|
|
901
914
|
const settled = escrow.totalCallsSettled.toNumber();
|
|
902
915
|
const callsRemaining = maxCalls > 0 ? maxCalls - settled : Infinity;
|
|
903
916
|
|
|
904
|
-
// Estimate affordable calls with current balance
|
|
905
917
|
const pricePerCall = escrow.pricePerCall.toNumber();
|
|
906
918
|
const balance = escrow.balance.toNumber();
|
|
907
919
|
const affordableCalls = pricePerCall > 0
|
|
@@ -933,9 +945,18 @@ export class X402Registry {
|
|
|
933
945
|
depositor?: PublicKey,
|
|
934
946
|
): Promise<boolean> {
|
|
935
947
|
const [agentPda] = deriveAgent(agentWallet);
|
|
936
|
-
const
|
|
937
|
-
const
|
|
938
|
-
|
|
948
|
+
const dep = depositor ?? this.wallet;
|
|
949
|
+
const conn = this.program.provider.connection;
|
|
950
|
+
|
|
951
|
+
// Check V2 first (nonce=0)
|
|
952
|
+
const [v2Pda] = deriveEscrowV2(agentPda, dep, 0);
|
|
953
|
+
const v2Info = await conn.getAccountInfo(v2Pda);
|
|
954
|
+
if (v2Info !== null) return true;
|
|
955
|
+
|
|
956
|
+
// Fall back to V1
|
|
957
|
+
const [v1Pda] = deriveEscrow(agentPda, dep);
|
|
958
|
+
const v1Info = await conn.getAccountInfo(v1Pda);
|
|
959
|
+
return v1Info !== null;
|
|
939
960
|
}
|
|
940
961
|
|
|
941
962
|
/**
|
|
@@ -950,11 +971,11 @@ export class X402Registry {
|
|
|
950
971
|
async fetchEscrow(
|
|
951
972
|
agentWallet: PublicKey,
|
|
952
973
|
depositor?: PublicKey,
|
|
953
|
-
): Promise<EscrowAccountData | null> {
|
|
974
|
+
): Promise<EscrowAccountData | EscrowAccountV2Data | null> {
|
|
954
975
|
const [agentPda] = deriveAgent(agentWallet);
|
|
955
976
|
const dep = depositor ?? this.wallet;
|
|
956
|
-
const
|
|
957
|
-
return
|
|
977
|
+
const resolved = await this.resolveEscrow(agentPda, dep);
|
|
978
|
+
return resolved?.escrow ?? null;
|
|
958
979
|
}
|
|
959
980
|
|
|
960
981
|
// ── Internals ────────────────────────────────────────
|
|
@@ -971,21 +992,6 @@ export class X402Registry {
|
|
|
971
992
|
return this.program.methods;
|
|
972
993
|
}
|
|
973
994
|
|
|
974
|
-
/**
|
|
975
|
-
* @name fetch
|
|
976
|
-
* @description Fetch an on-chain account by name and PDA. Throws if not found.
|
|
977
|
-
* @param name - Anchor account discriminator name.
|
|
978
|
-
* @param pda - Account public key to fetch.
|
|
979
|
-
* @returns The deserialized account data.
|
|
980
|
-
* @throws If the account does not exist.
|
|
981
|
-
* @private
|
|
982
|
-
*/
|
|
983
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
984
|
-
private async fetch<T>(name: string, pda: PublicKey): Promise<T> {
|
|
985
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
986
|
-
return (this.program.account as any)[name].fetch(pda) as Promise<T>;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
995
|
/**
|
|
990
996
|
* @name fetchNullable
|
|
991
997
|
* @description Fetch an on-chain account by name and PDA. Returns `null` if not found.
|
|
@@ -999,4 +1005,37 @@ export class X402Registry {
|
|
|
999
1005
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
1000
1006
|
return (this.program.account as any)[name].fetchNullable(pda) as Promise<T | null>;
|
|
1001
1007
|
}
|
|
1008
|
+
|
|
1009
|
+
/**
|
|
1010
|
+
* @name resolveEscrow
|
|
1011
|
+
* @description Try to find an escrow: V2 (nonce=0) first, then V1 fallback.
|
|
1012
|
+
* Returns the escrow data, PDA, and version indicator.
|
|
1013
|
+
* @private
|
|
1014
|
+
*/
|
|
1015
|
+
private async resolveEscrow(
|
|
1016
|
+
agentPda: PublicKey,
|
|
1017
|
+
depositor: PublicKey,
|
|
1018
|
+
): Promise<{
|
|
1019
|
+
escrow: EscrowAccountData | EscrowAccountV2Data;
|
|
1020
|
+
escrowPda: PublicKey;
|
|
1021
|
+
version: 1 | 2;
|
|
1022
|
+
} | null> {
|
|
1023
|
+
// Try V2 first (nonce=0 is the default)
|
|
1024
|
+
const [v2Pda] = deriveEscrowV2(agentPda, depositor, 0);
|
|
1025
|
+
const v2 = await this.fetchNullable<EscrowAccountV2Data>(
|
|
1026
|
+
"escrowAccountV2",
|
|
1027
|
+
v2Pda,
|
|
1028
|
+
);
|
|
1029
|
+
if (v2) return { escrow: v2, escrowPda: v2Pda, version: 2 };
|
|
1030
|
+
|
|
1031
|
+
// Fall back to V1
|
|
1032
|
+
const [v1Pda] = deriveEscrow(agentPda, depositor);
|
|
1033
|
+
const v1 = await this.fetchNullable<EscrowAccountData>(
|
|
1034
|
+
"escrowAccount",
|
|
1035
|
+
v1Pda,
|
|
1036
|
+
);
|
|
1037
|
+
if (v1) return { escrow: v1, escrowPda: v1Pda, version: 1 };
|
|
1038
|
+
|
|
1039
|
+
return null;
|
|
1040
|
+
}
|
|
1002
1041
|
}
|
package/src/types/accounts.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import type { PublicKey } from "@solana/web3.js";
|
|
13
13
|
import type BN from "bn.js";
|
|
14
|
-
import type { ToolHttpMethodKind, ToolCategoryKind } from "./enums";
|
|
14
|
+
import type { ToolHttpMethodKind, ToolCategoryKind, SettlementSecurityKind, DisputeOutcomeKind, BillingIntervalKind } from "./enums";
|
|
15
15
|
import type { Capability, PricingTier, PluginRef, VolumeCurveBreakpoint } from "./common";
|
|
16
16
|
|
|
17
17
|
// ═══════════════════════════════════════════════════════════════════
|
|
@@ -479,6 +479,8 @@ export interface ToolDescriptorData {
|
|
|
479
479
|
*
|
|
480
480
|
* @category Types
|
|
481
481
|
* @since v0.1.0
|
|
482
|
+
* @deprecated Since v0.7.0 — Use {@link EscrowAccountV2Data} for V2 escrows with
|
|
483
|
+
* settlement security, dispute windows, and co-signing.
|
|
482
484
|
* @see {@link VolumeCurveBreakpoint} for discount curve details.
|
|
483
485
|
*/
|
|
484
486
|
export interface EscrowAccountData {
|
|
@@ -616,3 +618,192 @@ export interface LedgerPageData {
|
|
|
616
618
|
/** Raw archived data bytes. */
|
|
617
619
|
readonly data: number[]; // Vec<u8>
|
|
618
620
|
}
|
|
621
|
+
|
|
622
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
623
|
+
// Escrow V2 (V2.1)
|
|
624
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* @interface EscrowAccountV2Data
|
|
628
|
+
* @description V2 escrow with triple-mode settlement security.
|
|
629
|
+
* @category Types
|
|
630
|
+
* @since v0.5.0
|
|
631
|
+
*/
|
|
632
|
+
export interface EscrowAccountV2Data {
|
|
633
|
+
readonly bump: number;
|
|
634
|
+
readonly version: number;
|
|
635
|
+
readonly agent: PublicKey;
|
|
636
|
+
readonly depositor: PublicKey;
|
|
637
|
+
readonly agentWallet: PublicKey;
|
|
638
|
+
readonly escrowNonce: BN;
|
|
639
|
+
readonly balance: BN;
|
|
640
|
+
readonly totalDeposited: BN;
|
|
641
|
+
readonly totalSettled: BN;
|
|
642
|
+
readonly totalCallsSettled: BN;
|
|
643
|
+
readonly pricePerCall: BN;
|
|
644
|
+
readonly maxCalls: BN;
|
|
645
|
+
readonly createdAt: BN;
|
|
646
|
+
readonly lastSettledAt: BN;
|
|
647
|
+
readonly expiresAt: BN;
|
|
648
|
+
readonly volumeCurve: VolumeCurveBreakpoint[];
|
|
649
|
+
readonly tokenMint: PublicKey | null;
|
|
650
|
+
readonly tokenDecimals: number;
|
|
651
|
+
readonly settlementSecurity: SettlementSecurityKind;
|
|
652
|
+
readonly disputeWindowSlots: BN;
|
|
653
|
+
readonly settlementIndex: BN;
|
|
654
|
+
readonly coSigner: PublicKey | null;
|
|
655
|
+
readonly arbiter: PublicKey | null;
|
|
656
|
+
readonly pendingAmount: BN;
|
|
657
|
+
readonly pendingCalls: BN;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
661
|
+
// Pending Settlement (V2.1)
|
|
662
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* @interface PendingSettlementData
|
|
666
|
+
* @description Dispute-window settlement lock PDA.
|
|
667
|
+
* @category Types
|
|
668
|
+
* @since v0.5.0
|
|
669
|
+
*/
|
|
670
|
+
export interface PendingSettlementData {
|
|
671
|
+
readonly bump: number;
|
|
672
|
+
readonly escrow: PublicKey;
|
|
673
|
+
readonly agent: PublicKey;
|
|
674
|
+
readonly agentWallet: PublicKey;
|
|
675
|
+
readonly depositor: PublicKey;
|
|
676
|
+
readonly settlementIndex: BN;
|
|
677
|
+
readonly callsToSettle: BN;
|
|
678
|
+
readonly amount: BN;
|
|
679
|
+
readonly serviceHash: number[]; // [u8; 32]
|
|
680
|
+
readonly createdAt: BN;
|
|
681
|
+
readonly releaseSlot: BN;
|
|
682
|
+
readonly isFinalized: boolean;
|
|
683
|
+
readonly isDisputed: boolean;
|
|
684
|
+
readonly outcome: DisputeOutcomeKind;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
688
|
+
// Dispute Record (V2.1)
|
|
689
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* @interface DisputeRecordData
|
|
693
|
+
* @description On-chain dispute with arbiter resolution.
|
|
694
|
+
* @category Types
|
|
695
|
+
* @since v0.5.0
|
|
696
|
+
*/
|
|
697
|
+
export interface DisputeRecordData {
|
|
698
|
+
readonly bump: number;
|
|
699
|
+
readonly pendingSettlement: PublicKey;
|
|
700
|
+
readonly escrow: PublicKey;
|
|
701
|
+
readonly depositor: PublicKey;
|
|
702
|
+
readonly agent: PublicKey;
|
|
703
|
+
readonly evidenceHash: number[]; // [u8; 32]
|
|
704
|
+
readonly agentEvidenceHash: number[]; // [u8; 32]
|
|
705
|
+
readonly arbiter: PublicKey;
|
|
706
|
+
readonly outcome: DisputeOutcomeKind;
|
|
707
|
+
readonly createdAt: BN;
|
|
708
|
+
readonly resolvedAt: BN;
|
|
709
|
+
readonly resolutionHash: number[]; // [u8; 32]
|
|
710
|
+
readonly slashAmount: BN;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
714
|
+
// Agent Stake (V2.1)
|
|
715
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* @interface AgentStakeData
|
|
719
|
+
* @description Collateral staking account for honest behavior.
|
|
720
|
+
* @category Types
|
|
721
|
+
* @since v0.5.0
|
|
722
|
+
*/
|
|
723
|
+
export interface AgentStakeData {
|
|
724
|
+
readonly bump: number;
|
|
725
|
+
readonly agent: PublicKey;
|
|
726
|
+
readonly wallet: PublicKey;
|
|
727
|
+
readonly stakedAmount: BN;
|
|
728
|
+
readonly slashedAmount: BN;
|
|
729
|
+
readonly lastStakeAt: BN;
|
|
730
|
+
readonly unstakeRequestedAt: BN;
|
|
731
|
+
readonly unstakeAmount: BN;
|
|
732
|
+
readonly unstakeAvailableAt: BN;
|
|
733
|
+
readonly totalDisputesWon: number;
|
|
734
|
+
readonly totalDisputesLost: number;
|
|
735
|
+
readonly createdAt: BN;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
739
|
+
// Subscription (V2.1)
|
|
740
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* @interface SubscriptionData
|
|
744
|
+
* @description Recurring payment subscription PDA.
|
|
745
|
+
* @category Types
|
|
746
|
+
* @since v0.5.0
|
|
747
|
+
*/
|
|
748
|
+
export interface SubscriptionData {
|
|
749
|
+
readonly bump: number;
|
|
750
|
+
readonly agent: PublicKey;
|
|
751
|
+
readonly subscriber: PublicKey;
|
|
752
|
+
readonly agentWallet: PublicKey;
|
|
753
|
+
readonly subId: BN;
|
|
754
|
+
readonly pricePerInterval: BN;
|
|
755
|
+
readonly billingInterval: BillingIntervalKind;
|
|
756
|
+
readonly tokenMint: PublicKey | null;
|
|
757
|
+
readonly tokenDecimals: number;
|
|
758
|
+
readonly balance: BN;
|
|
759
|
+
readonly totalPaid: BN;
|
|
760
|
+
readonly intervalsPaid: number;
|
|
761
|
+
readonly startedAt: BN;
|
|
762
|
+
readonly lastClaimedAt: BN;
|
|
763
|
+
readonly cancelledAt: BN;
|
|
764
|
+
readonly nextDueAt: BN;
|
|
765
|
+
readonly createdAt: BN;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
769
|
+
// Counter Shard (V2.1)
|
|
770
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* @interface CounterShardData
|
|
774
|
+
* @description Sharded global counter PDA for write throughput.
|
|
775
|
+
* @category Types
|
|
776
|
+
* @since v0.5.0
|
|
777
|
+
*/
|
|
778
|
+
export interface CounterShardData {
|
|
779
|
+
readonly bump: number;
|
|
780
|
+
readonly shardIndex: number;
|
|
781
|
+
readonly totalAgents: BN;
|
|
782
|
+
readonly activeAgents: BN;
|
|
783
|
+
readonly totalFeedbacks: BN;
|
|
784
|
+
readonly totalTools: number;
|
|
785
|
+
readonly totalVaults: number;
|
|
786
|
+
readonly totalAttestations: number;
|
|
787
|
+
readonly totalSettlements: BN;
|
|
788
|
+
readonly totalDisputes: number;
|
|
789
|
+
readonly totalSubscriptions: number;
|
|
790
|
+
readonly lastUpdated: BN;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
794
|
+
// Index Page (V2.1)
|
|
795
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* @interface IndexPageData
|
|
799
|
+
* @description Overflow page PDA for discovery indexes.
|
|
800
|
+
* @category Types
|
|
801
|
+
* @since v0.5.0
|
|
802
|
+
*/
|
|
803
|
+
export interface IndexPageData {
|
|
804
|
+
readonly bump: number;
|
|
805
|
+
readonly parentIndex: PublicKey;
|
|
806
|
+
readonly pageIndex: number;
|
|
807
|
+
readonly agents: PublicKey[];
|
|
808
|
+
readonly lastUpdated: BN;
|
|
809
|
+
}
|
package/src/types/enums.ts
CHANGED
|
@@ -212,3 +212,68 @@ export const ToolCategory = {
|
|
|
212
212
|
*/
|
|
213
213
|
export type ToolCategoryKind =
|
|
214
214
|
(typeof ToolCategory)[keyof typeof ToolCategory];
|
|
215
|
+
|
|
216
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
217
|
+
// Settlement Security (V2.1)
|
|
218
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @name SettlementSecurity
|
|
222
|
+
* @description Anchor-compatible enum variants for V2 escrow settlement security levels.
|
|
223
|
+
*
|
|
224
|
+
* - `SelfReport` — Agent settles unilaterally (v1 compatible).
|
|
225
|
+
* - `CoSigned` — Agent + client must co-sign every settlement.
|
|
226
|
+
* - `DisputeWindow` — Settlement enters pending state, depositor can dispute.
|
|
227
|
+
*
|
|
228
|
+
* @category Types
|
|
229
|
+
* @since v0.5.0
|
|
230
|
+
*/
|
|
231
|
+
export const SettlementSecurity = {
|
|
232
|
+
SelfReport: { selfReport: {} },
|
|
233
|
+
CoSigned: { coSigned: {} },
|
|
234
|
+
DisputeWindow: { disputeWindow: {} },
|
|
235
|
+
} as const;
|
|
236
|
+
|
|
237
|
+
export type SettlementSecurityKind =
|
|
238
|
+
(typeof SettlementSecurity)[keyof typeof SettlementSecurity];
|
|
239
|
+
|
|
240
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
241
|
+
// Dispute Outcome (V2.1)
|
|
242
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @name DisputeOutcome
|
|
246
|
+
* @description Anchor-compatible enum variants for dispute resolution outcomes.
|
|
247
|
+
*
|
|
248
|
+
* @category Types
|
|
249
|
+
* @since v0.5.0
|
|
250
|
+
*/
|
|
251
|
+
export const DisputeOutcome = {
|
|
252
|
+
Pending: { pending: {} },
|
|
253
|
+
AutoReleased: { autoReleased: {} },
|
|
254
|
+
DepositorWins: { depositorWins: {} },
|
|
255
|
+
AgentWins: { agentWins: {} },
|
|
256
|
+
} as const;
|
|
257
|
+
|
|
258
|
+
export type DisputeOutcomeKind =
|
|
259
|
+
(typeof DisputeOutcome)[keyof typeof DisputeOutcome];
|
|
260
|
+
|
|
261
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
262
|
+
// Billing Interval (V2.1)
|
|
263
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @name BillingInterval
|
|
267
|
+
* @description Anchor-compatible enum variants for subscription billing intervals.
|
|
268
|
+
*
|
|
269
|
+
* @category Types
|
|
270
|
+
* @since v0.5.0
|
|
271
|
+
*/
|
|
272
|
+
export const BillingInterval = {
|
|
273
|
+
Daily: { daily: {} },
|
|
274
|
+
Weekly: { weekly: {} },
|
|
275
|
+
Monthly: { monthly: {} },
|
|
276
|
+
} as const;
|
|
277
|
+
|
|
278
|
+
export type BillingIntervalKind =
|
|
279
|
+
(typeof BillingInterval)[keyof typeof BillingInterval];
|
package/src/types/index.ts
CHANGED
|
@@ -27,6 +27,9 @@ export {
|
|
|
27
27
|
SettlementMode,
|
|
28
28
|
ToolHttpMethod,
|
|
29
29
|
ToolCategory,
|
|
30
|
+
SettlementSecurity,
|
|
31
|
+
DisputeOutcome,
|
|
32
|
+
BillingInterval,
|
|
30
33
|
} from "./enums";
|
|
31
34
|
|
|
32
35
|
export type {
|
|
@@ -35,6 +38,9 @@ export type {
|
|
|
35
38
|
SettlementModeKind,
|
|
36
39
|
ToolHttpMethodKind,
|
|
37
40
|
ToolCategoryKind,
|
|
41
|
+
SettlementSecurityKind,
|
|
42
|
+
DisputeOutcomeKind,
|
|
43
|
+
BillingIntervalKind,
|
|
38
44
|
} from "./enums";
|
|
39
45
|
|
|
40
46
|
// ── Common Structs ───────────────────────────────────
|
|
@@ -65,6 +71,13 @@ export type {
|
|
|
65
71
|
AgentAttestationData,
|
|
66
72
|
MemoryLedgerData,
|
|
67
73
|
LedgerPageData,
|
|
74
|
+
EscrowAccountV2Data,
|
|
75
|
+
PendingSettlementData,
|
|
76
|
+
DisputeRecordData,
|
|
77
|
+
AgentStakeData,
|
|
78
|
+
SubscriptionData,
|
|
79
|
+
CounterShardData,
|
|
80
|
+
IndexPageData,
|
|
68
81
|
} from "./accounts";
|
|
69
82
|
|
|
70
83
|
// ── Instruction Args ─────────────────────────────────
|
|
@@ -83,6 +96,8 @@ export type {
|
|
|
83
96
|
DelegatePermissionBit,
|
|
84
97
|
SchemaTypeValue,
|
|
85
98
|
CompressionTypeValue,
|
|
99
|
+
CreateEscrowV2Args,
|
|
100
|
+
CreateSubscriptionArgs,
|
|
86
101
|
} from "./instructions";
|
|
87
102
|
|
|
88
103
|
export {
|
|
@@ -269,6 +269,7 @@ export interface CompactInscribeArgs {
|
|
|
269
269
|
*
|
|
270
270
|
* @category Types
|
|
271
271
|
* @since v0.1.0
|
|
272
|
+
* @deprecated Since v0.7.0 — Use {@link CreateEscrowV2Args} for V2 escrows.
|
|
272
273
|
* @see {@link VolumeCurveBreakpoint} for discount curve details.
|
|
273
274
|
*/
|
|
274
275
|
export interface CreateEscrowArgs {
|
|
@@ -411,3 +412,42 @@ export const CompressionType = {
|
|
|
411
412
|
*/
|
|
412
413
|
export type CompressionTypeValue =
|
|
413
414
|
(typeof CompressionType)[keyof typeof CompressionType];
|
|
415
|
+
|
|
416
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
417
|
+
// Escrow V2 Instructions (V2.1)
|
|
418
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @interface CreateEscrowV2Args
|
|
422
|
+
* @description Arguments for the `createEscrowV2` instruction.
|
|
423
|
+
* @category Types
|
|
424
|
+
* @since v0.5.0
|
|
425
|
+
*/
|
|
426
|
+
export interface CreateEscrowV2Args {
|
|
427
|
+
readonly escrowNonce: BN;
|
|
428
|
+
readonly pricePerCall: BN;
|
|
429
|
+
readonly maxCalls: BN;
|
|
430
|
+
readonly initialDeposit: BN;
|
|
431
|
+
readonly expiresAt: BN;
|
|
432
|
+
readonly volumeCurve: VolumeCurveBreakpoint[];
|
|
433
|
+
readonly tokenMint: PublicKey | null;
|
|
434
|
+
readonly tokenDecimals: number;
|
|
435
|
+
/** 0=SelfReport, 1=CoSigned, 2=DisputeWindow */
|
|
436
|
+
readonly settlementSecurity: number;
|
|
437
|
+
readonly disputeWindowSlots: BN;
|
|
438
|
+
readonly coSigner: PublicKey | null;
|
|
439
|
+
readonly arbiter: PublicKey | null;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* @interface CreateSubscriptionArgs
|
|
444
|
+
* @description Arguments for the `createSubscription` instruction.
|
|
445
|
+
* @category Types
|
|
446
|
+
* @since v0.5.0
|
|
447
|
+
*/
|
|
448
|
+
export interface CreateSubscriptionArgs {
|
|
449
|
+
readonly subId: BN;
|
|
450
|
+
readonly pricePerInterval: BN;
|
|
451
|
+
readonly billingInterval: number;
|
|
452
|
+
readonly initialFund: BN;
|
|
453
|
+
}
|