@agent402-dev/bond-client 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 +202 -0
- package/NOTICE +6 -0
- package/README.md +74 -2
- package/dist/index.d.ts +114 -0
- package/dist/index.js +147 -0
- package/package.json +46 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agent402-dev/bond-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Instruction builders for the **Agent402 Bond** Solana program (beta): a performance bond for one deal between a seller and a buyer.
|
|
4
|
+
|
|
5
|
+
Part of the [Agent402 toolkit](https://github.com/agent402-dev/agent402-toolkit) (Solana).
|
|
6
|
+
Not related to `agent402.tools`, `@agents402` or other similarly named sites, accounts or projects, including projects named "a402" on other chains.
|
|
7
|
+
|
|
8
|
+
> This package only builds instructions and decodes accounts. It holds no keys and sends nothing on its own. The program was reviewed internally, **not independently audited**. Not investment advice.
|
|
9
|
+
|
|
10
|
+
## The program
|
|
11
|
+
- Program id `9hiCZZ6ddFG9oJBR1Su43Pj4JRFHeHC5cGiCgXtTuAaK` on Solana mainnet, **no upgrade authority**.
|
|
12
|
+
- The seller locks a bond (at most 20% of the price), the buyer may lock a deposit. The outcome follows only the parties' signatures and the Solana clock. The program does not judge quality; quality disputes are resolved between the parties.
|
|
13
|
+
- Beta limits: 100 USDC locked per deal, 2,000 USDC across open deals.
|
|
14
|
+
- Source, tests and how to check the deployed code: [programs/](https://github.com/agent402-dev/agent402-toolkit/tree/main/programs). Terms and outcome table: [TERMS.md](https://github.com/agent402-dev/agent402-toolkit/blob/main/docs/bond/TERMS.md). Internal review: [INTERNAL-REVIEW.md](https://github.com/agent402-dev/agent402-toolkit/blob/main/docs/bond/INTERNAL-REVIEW.md).
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
```bash
|
|
18
|
+
npm install @agent402-dev/bond-client @solana/web3.js @solana/spl-token
|
|
19
|
+
```
|
|
20
|
+
`@solana/web3.js` (v1) and `@solana/spl-token` are peer dependencies. Node 20+.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
```ts
|
|
24
|
+
import { Transaction } from '@solana/web3.js';
|
|
25
|
+
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
|
26
|
+
import * as bond from '@agent402-dev/bond-client';
|
|
27
|
+
|
|
28
|
+
const U = 1_000_000n; // USDC has 6 decimals; all amounts are in the smallest unit
|
|
29
|
+
const now = BigInt((await connection.getBlockTime(await connection.getSlot()))!); // Solana clock
|
|
30
|
+
const deal: bond.DealRef = {
|
|
31
|
+
seller, buyer, // PublicKey
|
|
32
|
+
mint: bond.MINTS.mainnet.USDC,
|
|
33
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
34
|
+
nonce: 1n, // unique per seller + buyer pair
|
|
35
|
+
};
|
|
36
|
+
const terms: bond.Terms = {
|
|
37
|
+
price: 20n * U,
|
|
38
|
+
sellerBond: 2n * U,
|
|
39
|
+
buyerDeposit: 5n * U,
|
|
40
|
+
deliverBy: now + 3_600n, // unix seconds
|
|
41
|
+
lateWindow: 0n, // 0..=7 days
|
|
42
|
+
payWindow: 86_400n, // 1..=30 days
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// 1. Both sign: locks the bond and the deposit.
|
|
46
|
+
new Transaction().add(bond.open(deal, terms));
|
|
47
|
+
// 2. Seller marks delivery.
|
|
48
|
+
new Transaction().add(bond.markDelivered(deal));
|
|
49
|
+
// 3. Buyer confirms and pays the balance (price - deposit) straight to the seller.
|
|
50
|
+
new Transaction().add(bond.confirmReceipt(deal), bond.payBalance(deal));
|
|
51
|
+
// 4. Anyone: settle, then each side's claim (order matters).
|
|
52
|
+
new Transaction().add(...bond.settleAndClaim(deal));
|
|
53
|
+
```
|
|
54
|
+
Both sides can end a deal early with `cancelAndClaim` (both sign; each side gets its own money back). `decodeDeal(accountData)` reads a deal's state and outcome.
|
|
55
|
+
|
|
56
|
+
| Export | What it builds |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `open`, `markDelivered`, `confirmReceipt`, `payBalance`, `extend` | Deal steps |
|
|
59
|
+
| `settle`, `claimBuyer`, `claimSeller`, `settleAndClaim` | Closing a deal whose outcome is fixed |
|
|
60
|
+
| `cancel`, `cancelAndClaim` | Closing a deal by mutual agreement |
|
|
61
|
+
| `dealPda`, `vaultPda`, `counterPda`, `configPda`, `dealOf` | Addresses |
|
|
62
|
+
| `decodeDeal`, `OUTCOMES` | Reading a deal |
|
|
63
|
+
| `setPaused`, `setAdmin`, `acceptAdmin`, `proposeCaps`, `observePrice`, `activatePriceMode` | Operator and price-record instructions (the operator is a Squads multisig) |
|
|
64
|
+
|
|
65
|
+
## Things to get right
|
|
66
|
+
- **Verify before you sign.** If the other side builds a transaction for you to sign, rebuild the instruction yourself from terms you accept and compare it byte for byte. The [bonded-job example](https://github.com/agent402-dev/agent402-toolkit/tree/main/packages/bond-client/examples/job) shows how.
|
|
67
|
+
- **Seller: mark before `deliverBy`.** If nothing is marked or confirmed by the deadline, the bond and the deposit go to the buyer.
|
|
68
|
+
- **Never send tokens directly to a deal vault.** Anything beyond the locked amount is paid to the buyer.
|
|
69
|
+
- If one side's token account is frozen, send `settle` and the other side's claim on their own; the frozen side can claim later into another account it owns.
|
|
70
|
+
|
|
71
|
+
## Example
|
|
72
|
+
Two agents running a bonded job end to end (seller HTTP service, buyer agent, local mainnet-fork demo with no real funds): [examples/job](https://github.com/agent402-dev/agent402-toolkit/tree/main/packages/bond-client/examples/job).
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent402 performance bond: instruction builders (beta).
|
|
3
|
+
*
|
|
4
|
+
* The program never judges quality. Outcomes come only from signatures and
|
|
5
|
+
* the on-chain clock. Settlement records the outcome; each side then claims
|
|
6
|
+
* its own share. `settleAndClaim` puts settle + claim_buyer + claim_seller in
|
|
7
|
+
* one transaction, which is the normal path. If one side's token account is
|
|
8
|
+
* frozen, send `settle` and the other side's claim on their own.
|
|
9
|
+
*
|
|
10
|
+
* Never send tokens straight to a deal vault. Anything beyond the locked
|
|
11
|
+
* amount is returned to the buyer.
|
|
12
|
+
*/
|
|
13
|
+
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
14
|
+
/** Same program id on devnet and mainnet. */
|
|
15
|
+
export declare const BOND_PROGRAM_ID: PublicKey;
|
|
16
|
+
export declare const MINTS: {
|
|
17
|
+
readonly mainnet: {
|
|
18
|
+
readonly USDC: PublicKey;
|
|
19
|
+
readonly WSOL: PublicKey;
|
|
20
|
+
readonly PREQ: PublicKey;
|
|
21
|
+
};
|
|
22
|
+
readonly devnet: {
|
|
23
|
+
readonly USDC: PublicKey;
|
|
24
|
+
readonly WSOL: PublicKey;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export declare const PREQ_POOL: PublicKey;
|
|
28
|
+
/** sha256("global:<name>")[..8], checked in test/discriminators.test.ts. */
|
|
29
|
+
export declare const DISC: {
|
|
30
|
+
readonly init_counter: readonly [247, 168, 146, 45, 125, 26, 142, 80];
|
|
31
|
+
readonly init_config: readonly [23, 235, 115, 232, 168, 96, 1, 231];
|
|
32
|
+
readonly set_paused: readonly [91, 60, 125, 192, 176, 225, 166, 218];
|
|
33
|
+
readonly set_admin: readonly [251, 163, 0, 52, 91, 194, 187, 92];
|
|
34
|
+
readonly accept_admin: readonly [112, 42, 45, 90, 116, 181, 13, 170];
|
|
35
|
+
readonly propose_caps: readonly [63, 24, 101, 104, 86, 149, 67, 243];
|
|
36
|
+
readonly observe_price: readonly [147, 178, 4, 21, 14, 213, 81, 159];
|
|
37
|
+
readonly activate_price_mode: readonly [181, 205, 243, 174, 176, 192, 111, 71];
|
|
38
|
+
readonly open: readonly [228, 220, 155, 71, 199, 189, 60, 45];
|
|
39
|
+
readonly mark_delivered: readonly [240, 118, 188, 142, 64, 85, 107, 18];
|
|
40
|
+
readonly confirm_receipt: readonly [203, 36, 80, 115, 249, 12, 141, 170];
|
|
41
|
+
readonly pay_balance: readonly [121, 220, 140, 105, 242, 163, 148, 24];
|
|
42
|
+
readonly extend: readonly [228, 127, 0, 1, 227, 154, 54, 168];
|
|
43
|
+
readonly cancel: readonly [232, 219, 223, 41, 219, 236, 220, 190];
|
|
44
|
+
readonly settle: readonly [175, 42, 185, 87, 144, 131, 102, 212];
|
|
45
|
+
readonly claim_seller: readonly [238, 218, 144, 171, 49, 203, 29, 50];
|
|
46
|
+
readonly claim_buyer: readonly [13, 61, 90, 109, 208, 223, 195, 211];
|
|
47
|
+
};
|
|
48
|
+
export declare const configPda: (programId?: PublicKey) => PublicKey;
|
|
49
|
+
export declare const counterPda: (mint: PublicKey, programId?: PublicKey) => PublicKey;
|
|
50
|
+
export declare const dealPda: (seller: PublicKey, buyer: PublicKey, nonce: bigint, programId?: PublicKey) => PublicKey;
|
|
51
|
+
export declare const vaultPda: (deal: PublicKey, programId?: PublicKey) => PublicKey;
|
|
52
|
+
export declare const initConfig: (payer: PublicKey) => TransactionInstruction;
|
|
53
|
+
export declare const initCounter: (payer: PublicKey, mint: PublicKey) => TransactionInstruction;
|
|
54
|
+
export declare const setPaused: (admin: PublicKey, paused: boolean) => TransactionInstruction;
|
|
55
|
+
export declare const setAdmin: (admin: PublicKey, next: PublicKey) => TransactionInstruction;
|
|
56
|
+
export declare const acceptAdmin: (next: PublicKey) => TransactionInstruction;
|
|
57
|
+
export declare const proposeCaps: (admin: PublicKey, mint: PublicKey, perDeal: bigint, total: bigint) => TransactionInstruction;
|
|
58
|
+
export declare const observePrice: (preqMint: PublicKey, quoteVault: PublicKey) => TransactionInstruction;
|
|
59
|
+
export declare const activatePriceMode: (preqMint: PublicKey, quoteVault: PublicKey) => TransactionInstruction;
|
|
60
|
+
export interface Terms {
|
|
61
|
+
/** All amounts in the mint's smallest unit. */
|
|
62
|
+
price: bigint;
|
|
63
|
+
sellerBond: bigint;
|
|
64
|
+
buyerDeposit: bigint;
|
|
65
|
+
/** Unix seconds. */
|
|
66
|
+
deliverBy: bigint;
|
|
67
|
+
/** Seconds, 0..=7 days. */
|
|
68
|
+
lateWindow: bigint;
|
|
69
|
+
/** Seconds, 1..=30 days. */
|
|
70
|
+
payWindow: bigint;
|
|
71
|
+
}
|
|
72
|
+
export interface DealRef {
|
|
73
|
+
seller: PublicKey;
|
|
74
|
+
buyer: PublicKey;
|
|
75
|
+
mint: PublicKey;
|
|
76
|
+
/** SPL Token or Token-2022, matching the mint. */
|
|
77
|
+
tokenProgram: PublicKey;
|
|
78
|
+
nonce: bigint;
|
|
79
|
+
}
|
|
80
|
+
export declare const dealOf: (d: DealRef) => PublicKey;
|
|
81
|
+
/** Both seller and buyer must sign. Locks the bond and the deposit. */
|
|
82
|
+
export declare function open(d: DealRef, t: Terms, sellerToken?: PublicKey, buyerToken?: PublicKey): TransactionInstruction;
|
|
83
|
+
export declare const markDelivered: (d: DealRef) => TransactionInstruction;
|
|
84
|
+
export declare const confirmReceipt: (d: DealRef) => TransactionInstruction;
|
|
85
|
+
/** Pays price - deposit straight from buyer to seller. */
|
|
86
|
+
export declare const payBalance: (d: DealRef, buyerToken?: PublicKey, sellerToken?: PublicKey) => TransactionInstruction;
|
|
87
|
+
/** Both sign. The whole term must stay within 180 days of opening. */
|
|
88
|
+
export declare const extend: (d: DealRef, deliverBy: bigint, lateWindow: bigint, payWindow: bigint) => TransactionInstruction;
|
|
89
|
+
export declare const settle: (d: DealRef) => TransactionInstruction;
|
|
90
|
+
export declare const cancel: (d: DealRef) => TransactionInstruction;
|
|
91
|
+
/** Anyone. `to` must be a token account owned by the buyer. */
|
|
92
|
+
export declare const claimBuyer: (d: DealRef, to?: PublicKey) => TransactionInstruction;
|
|
93
|
+
/** Anyone. `to` must be a token account owned by the seller. Closes the deal once the vault is empty. */
|
|
94
|
+
export declare const claimSeller: (d: DealRef, to?: PublicKey) => TransactionInstruction;
|
|
95
|
+
/** The normal closing transaction: settle, buyer's claim, then seller's claim (order matters). */
|
|
96
|
+
export declare const settleAndClaim: (d: DealRef) => TransactionInstruction[];
|
|
97
|
+
/** Both sign: return each side's own money. */
|
|
98
|
+
export declare const cancelAndClaim: (d: DealRef) => TransactionInstruction[];
|
|
99
|
+
export declare const OUTCOMES: readonly ["open", "delivered", "buyer_default", "disputed", "seller_default", "cancelled"];
|
|
100
|
+
export interface DealState {
|
|
101
|
+
seller: PublicKey;
|
|
102
|
+
buyer: PublicKey;
|
|
103
|
+
mint: PublicKey;
|
|
104
|
+
nonce: bigint;
|
|
105
|
+
terms: Terms;
|
|
106
|
+
openedAt: bigint;
|
|
107
|
+
sellerMark: bigint | null;
|
|
108
|
+
buyerConfirm: bigint | null;
|
|
109
|
+
balancePaid: boolean;
|
|
110
|
+
outcome: (typeof OUTCOMES)[number];
|
|
111
|
+
owedSeller: bigint;
|
|
112
|
+
}
|
|
113
|
+
/** Decodes a Deal account (Anchor layout, 8-byte discriminator first). */
|
|
114
|
+
export declare function decodeDeal(raw: Uint8Array): DealState;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent402 performance bond: instruction builders (beta).
|
|
3
|
+
*
|
|
4
|
+
* The program never judges quality. Outcomes come only from signatures and
|
|
5
|
+
* the on-chain clock. Settlement records the outcome; each side then claims
|
|
6
|
+
* its own share. `settleAndClaim` puts settle + claim_buyer + claim_seller in
|
|
7
|
+
* one transaction, which is the normal path. If one side's token account is
|
|
8
|
+
* frozen, send `settle` and the other side's claim on their own.
|
|
9
|
+
*
|
|
10
|
+
* Never send tokens straight to a deal vault. Anything beyond the locked
|
|
11
|
+
* amount is returned to the buyer.
|
|
12
|
+
*/
|
|
13
|
+
import { PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
|
|
14
|
+
import { getAssociatedTokenAddressSync } from '@solana/spl-token';
|
|
15
|
+
/** Same program id on devnet and mainnet. */
|
|
16
|
+
export const BOND_PROGRAM_ID = new PublicKey('9hiCZZ6ddFG9oJBR1Su43Pj4JRFHeHC5cGiCgXtTuAaK');
|
|
17
|
+
export const MINTS = {
|
|
18
|
+
mainnet: {
|
|
19
|
+
USDC: new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
|
|
20
|
+
WSOL: new PublicKey('So11111111111111111111111111111111111111112'),
|
|
21
|
+
PREQ: new PublicKey('aTbGw3gU5JyJkAdXLj8rYZ3mCXkPbyHjtTAyCX3jQLX'),
|
|
22
|
+
},
|
|
23
|
+
devnet: {
|
|
24
|
+
USDC: new PublicKey('4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'),
|
|
25
|
+
WSOL: new PublicKey('So11111111111111111111111111111111111111112'),
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
export const PREQ_POOL = new PublicKey('51F34MKDFBLr5GwKiiJMWV9HsHv63dkkP3ZMMxPN71xc');
|
|
29
|
+
/** sha256("global:<name>")[..8], checked in test/discriminators.test.ts. */
|
|
30
|
+
export const DISC = {
|
|
31
|
+
init_counter: [247, 168, 146, 45, 125, 26, 142, 80],
|
|
32
|
+
init_config: [23, 235, 115, 232, 168, 96, 1, 231],
|
|
33
|
+
set_paused: [91, 60, 125, 192, 176, 225, 166, 218],
|
|
34
|
+
set_admin: [251, 163, 0, 52, 91, 194, 187, 92],
|
|
35
|
+
accept_admin: [112, 42, 45, 90, 116, 181, 13, 170],
|
|
36
|
+
propose_caps: [63, 24, 101, 104, 86, 149, 67, 243],
|
|
37
|
+
observe_price: [147, 178, 4, 21, 14, 213, 81, 159],
|
|
38
|
+
activate_price_mode: [181, 205, 243, 174, 176, 192, 111, 71],
|
|
39
|
+
open: [228, 220, 155, 71, 199, 189, 60, 45],
|
|
40
|
+
mark_delivered: [240, 118, 188, 142, 64, 85, 107, 18],
|
|
41
|
+
confirm_receipt: [203, 36, 80, 115, 249, 12, 141, 170],
|
|
42
|
+
pay_balance: [121, 220, 140, 105, 242, 163, 148, 24],
|
|
43
|
+
extend: [228, 127, 0, 1, 227, 154, 54, 168],
|
|
44
|
+
cancel: [232, 219, 223, 41, 219, 236, 220, 190],
|
|
45
|
+
settle: [175, 42, 185, 87, 144, 131, 102, 212],
|
|
46
|
+
claim_seller: [238, 218, 144, 171, 49, 203, 29, 50],
|
|
47
|
+
claim_buyer: [13, 61, 90, 109, 208, 223, 195, 211],
|
|
48
|
+
};
|
|
49
|
+
const enc = new TextEncoder();
|
|
50
|
+
const u64 = (v) => {
|
|
51
|
+
const b = new Uint8Array(8);
|
|
52
|
+
new DataView(b.buffer).setBigUint64(0, v, true);
|
|
53
|
+
return b;
|
|
54
|
+
};
|
|
55
|
+
const i64 = (v) => {
|
|
56
|
+
const b = new Uint8Array(8);
|
|
57
|
+
new DataView(b.buffer).setBigInt64(0, v, true);
|
|
58
|
+
return b;
|
|
59
|
+
};
|
|
60
|
+
const data = (d, ...parts) => Buffer.concat([Buffer.from(d), ...parts.map((p) => Buffer.from(p))]);
|
|
61
|
+
const w = (pubkey, isSigner = false) => ({ pubkey, isSigner, isWritable: true });
|
|
62
|
+
const r = (pubkey, isSigner = false) => ({ pubkey, isSigner, isWritable: false });
|
|
63
|
+
const ix = (keys, d, programId = BOND_PROGRAM_ID) => new TransactionInstruction({ programId, keys, data: d });
|
|
64
|
+
// ---------------- addresses ----------------
|
|
65
|
+
export const configPda = (programId = BOND_PROGRAM_ID) => PublicKey.findProgramAddressSync([enc.encode('config')], programId)[0];
|
|
66
|
+
export const counterPda = (mint, programId = BOND_PROGRAM_ID) => PublicKey.findProgramAddressSync([enc.encode('counter'), mint.toBytes()], programId)[0];
|
|
67
|
+
export const dealPda = (seller, buyer, nonce, programId = BOND_PROGRAM_ID) => PublicKey.findProgramAddressSync([enc.encode('deal'), seller.toBytes(), buyer.toBytes(), u64(nonce)], programId)[0];
|
|
68
|
+
export const vaultPda = (deal, programId = BOND_PROGRAM_ID) => PublicKey.findProgramAddressSync([enc.encode('vault'), deal.toBytes()], programId)[0];
|
|
69
|
+
// ---------------- setup (once per cluster) ----------------
|
|
70
|
+
export const initConfig = (payer) => ix([w(payer, true), w(configPda()), r(SystemProgram.programId)], data(DISC.init_config));
|
|
71
|
+
export const initCounter = (payer, mint) => ix([w(payer, true), r(mint), w(counterPda(mint)), r(SystemProgram.programId)], data(DISC.init_counter));
|
|
72
|
+
// ---------------- admin ----------------
|
|
73
|
+
export const setPaused = (admin, paused) => ix([r(admin, true), w(configPda())], data(DISC.set_paused, Uint8Array.of(paused ? 1 : 0)));
|
|
74
|
+
export const setAdmin = (admin, next) => ix([r(admin, true), w(configPda())], data(DISC.set_admin, next.toBytes()));
|
|
75
|
+
export const acceptAdmin = (next) => ix([r(next, true), w(configPda())], data(DISC.accept_admin));
|
|
76
|
+
export const proposeCaps = (admin, mint, perDeal, total) => ix([r(admin, true), r(configPda()), w(counterPda(mint))], data(DISC.propose_caps, u64(perDeal), u64(total)));
|
|
77
|
+
// ---------------- PREQ price record (anyone) ----------------
|
|
78
|
+
export const observePrice = (preqMint, quoteVault) => ix([w(counterPda(preqMint)), r(PREQ_POOL), r(quoteVault)], data(DISC.observe_price));
|
|
79
|
+
export const activatePriceMode = (preqMint, quoteVault) => ix([w(counterPda(preqMint)), r(PREQ_POOL), r(quoteVault)], data(DISC.activate_price_mode));
|
|
80
|
+
export const dealOf = (d) => dealPda(d.seller, d.buyer, d.nonce);
|
|
81
|
+
const ata = (owner, d) => getAssociatedTokenAddressSync(d.mint, owner, false, d.tokenProgram);
|
|
82
|
+
/** Both seller and buyer must sign. Locks the bond and the deposit. */
|
|
83
|
+
export function open(d, t, sellerToken = ata(d.seller, d), buyerToken = ata(d.buyer, d)) {
|
|
84
|
+
const deal = dealOf(d);
|
|
85
|
+
return ix([
|
|
86
|
+
w(d.seller, true),
|
|
87
|
+
r(d.buyer, true),
|
|
88
|
+
r(d.mint),
|
|
89
|
+
w(sellerToken),
|
|
90
|
+
w(buyerToken),
|
|
91
|
+
w(deal),
|
|
92
|
+
w(vaultPda(deal)),
|
|
93
|
+
w(counterPda(d.mint)),
|
|
94
|
+
r(configPda()),
|
|
95
|
+
r(d.tokenProgram),
|
|
96
|
+
r(SystemProgram.programId),
|
|
97
|
+
], data(DISC.open, u64(d.nonce), u64(t.price), u64(t.sellerBond), u64(t.buyerDeposit), i64(t.deliverBy), i64(t.lateWindow), i64(t.payWindow)));
|
|
98
|
+
}
|
|
99
|
+
export const markDelivered = (d) => ix([r(d.seller, true), w(dealOf(d))], data(DISC.mark_delivered));
|
|
100
|
+
export const confirmReceipt = (d) => ix([r(d.buyer, true), w(dealOf(d))], data(DISC.confirm_receipt));
|
|
101
|
+
/** Pays price - deposit straight from buyer to seller. */
|
|
102
|
+
export const payBalance = (d, buyerToken = ata(d.buyer, d), sellerToken = ata(d.seller, d)) => ix([r(d.buyer, true), w(dealOf(d)), r(d.mint), w(buyerToken), w(sellerToken), r(d.tokenProgram)], data(DISC.pay_balance));
|
|
103
|
+
/** Both sign. The whole term must stay within 180 days of opening. */
|
|
104
|
+
export const extend = (d, deliverBy, lateWindow, payWindow) => ix([r(d.seller, true), r(d.buyer, true), w(dealOf(d))], data(DISC.extend, i64(deliverBy), i64(lateWindow), i64(payWindow)));
|
|
105
|
+
const decideAccounts = (d) => {
|
|
106
|
+
const deal = dealOf(d);
|
|
107
|
+
return [w(deal), r(vaultPda(deal)), w(counterPda(d.mint))];
|
|
108
|
+
};
|
|
109
|
+
export const settle = (d) => ix(decideAccounts(d), data(DISC.settle));
|
|
110
|
+
export const cancel = (d) => ix([r(d.seller, true), r(d.buyer, true), ...decideAccounts(d)], data(DISC.cancel));
|
|
111
|
+
function claim(d, to, side) {
|
|
112
|
+
const deal = dealOf(d);
|
|
113
|
+
return ix([w(d.seller), w(deal), r(d.mint), w(vaultPda(deal)), w(to), r(d.tokenProgram)], data(side === 'seller' ? DISC.claim_seller : DISC.claim_buyer));
|
|
114
|
+
}
|
|
115
|
+
/** Anyone. `to` must be a token account owned by the buyer. */
|
|
116
|
+
export const claimBuyer = (d, to = ata(d.buyer, d)) => claim(d, to, 'buyer');
|
|
117
|
+
/** Anyone. `to` must be a token account owned by the seller. Closes the deal once the vault is empty. */
|
|
118
|
+
export const claimSeller = (d, to = ata(d.seller, d)) => claim(d, to, 'seller');
|
|
119
|
+
/** The normal closing transaction: settle, buyer's claim, then seller's claim (order matters). */
|
|
120
|
+
export const settleAndClaim = (d) => [settle(d), claimBuyer(d), claimSeller(d)];
|
|
121
|
+
/** Both sign: return each side's own money. */
|
|
122
|
+
export const cancelAndClaim = (d) => [cancel(d), claimBuyer(d), claimSeller(d)];
|
|
123
|
+
// ---------------- reading a deal ----------------
|
|
124
|
+
export const OUTCOMES = ['open', 'delivered', 'buyer_default', 'disputed', 'seller_default', 'cancelled'];
|
|
125
|
+
/** Decodes a Deal account (Anchor layout, 8-byte discriminator first). */
|
|
126
|
+
export function decodeDeal(raw) {
|
|
127
|
+
const v = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
128
|
+
let o = 8;
|
|
129
|
+
const key = () => new PublicKey(raw.slice(o, (o += 32)));
|
|
130
|
+
const n = () => { const x = v.getBigUint64(o, true); o += 8; return x; };
|
|
131
|
+
const s = () => { const x = v.getBigInt64(o, true); o += 8; return x; };
|
|
132
|
+
const seller = key(), buyer = key(), mint = key();
|
|
133
|
+
const nonce = n(), price = n(), sellerBond = n(), buyerDeposit = n();
|
|
134
|
+
const deliverBy = s(), lateWindow = s(), payWindow = s(), openedAt = s(), mark = s(), conf = s();
|
|
135
|
+
const balancePaid = raw[o++] === 1;
|
|
136
|
+
o += 2; // bump, vault_bump
|
|
137
|
+
const outcome = OUTCOMES[raw[o++]] ?? 'open';
|
|
138
|
+
const owedSeller = n();
|
|
139
|
+
return {
|
|
140
|
+
seller, buyer, mint, nonce,
|
|
141
|
+
terms: { price, sellerBond, buyerDeposit, deliverBy, lateWindow, payWindow },
|
|
142
|
+
openedAt,
|
|
143
|
+
sellerMark: mark === 0n ? null : mark,
|
|
144
|
+
buyerConfirm: conf === 0n ? null : conf,
|
|
145
|
+
balancePaid, outcome, owedSeller,
|
|
146
|
+
};
|
|
147
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent402-dev/bond-client",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"description": "Instruction builders for the Agent402 Bond Solana program (beta): seller bond and buyer deposit for one deal, outcomes by signatures and the Solana clock. Holds no keys and sends nothing.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"solana",
|
|
6
|
+
"escrow",
|
|
7
|
+
"bond",
|
|
8
|
+
"usdc",
|
|
9
|
+
"agent"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@solana/web3.js": "^1.98.0",
|
|
21
|
+
"@solana/spl-token": "^0.4.13"
|
|
22
|
+
},
|
|
23
|
+
"version": "0.1.0",
|
|
24
|
+
"author": "Agent402",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/agent402-dev/agent402-toolkit.git",
|
|
33
|
+
"directory": "packages/bond-client"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/agent402-dev/agent402-toolkit/tree/main/packages/bond-client#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/agent402-dev/agent402-toolkit/issues"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md",
|
|
45
|
+
"LICENSE",
|
|
46
|
+
"NOTICE"
|
|
47
|
+
]
|
|
48
|
+
}
|