@m2m-monster/core 0.0.0-stage → 1.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/README.md +6 -2
- package/dist/constants.d.ts +22 -0
- package/dist/constants.js +23 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +29 -0
- package/dist/fees.d.ts +24 -0
- package/dist/fees.js +40 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/networks.d.ts +21 -0
- package/dist/networks.js +32 -0
- package/dist/payment.d.ts +47 -0
- package/dist/payment.js +66 -0
- package/dist/public.d.ts +38 -0
- package/dist/public.js +46 -0
- package/dist/quote.d.ts +56 -0
- package/dist/quote.js +86 -0
- package/dist/receipt.d.ts +35 -0
- package/dist/receipt.js +62 -0
- package/dist/referrals.d.ts +14 -0
- package/dist/referrals.js +40 -0
- package/dist/refund.d.ts +12 -0
- package/dist/refund.js +46 -0
- package/dist/rpc.d.ts +40 -0
- package/dist/rpc.js +128 -0
- package/dist/split.d.ts +55 -0
- package/dist/split.js +172 -0
- package/dist/test-rpc.d.ts +45 -0
- package/dist/test-rpc.js +124 -0
- package/dist/x402.d.ts +66 -0
- package/dist/x402.js +80 -0
- package/package.json +47 -3
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/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @m2m-monster/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Core of [m2m.monster](https://m2m.monster/llms.txt), used by `@m2m-monster/sdk` and `@m2m-monster/mcp`: fee formulas, signed quotes and receipts, mint whitelists, and building and strict verification of the `m2m-split` transaction for x402 on Solana.
|
|
4
|
+
|
|
5
|
+
> Most agents want `@m2m-monster/sdk`.
|
|
6
|
+
|
|
7
|
+
License: Apache-2.0
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** USDC base units per dollar: fees are computed in units of 0.000001 USD (section F). */
|
|
2
|
+
export declare const USD_UNITS = 1000000n;
|
|
3
|
+
/** Agent-to-agent tariff: min(max(0.25% × amount, $0.001), $0.50) (section F). */
|
|
4
|
+
export declare const AGENT_FEE: {
|
|
5
|
+
readonly rateBps: 25n;
|
|
6
|
+
readonly min: 1000n;
|
|
7
|
+
readonly max: 500000n;
|
|
8
|
+
};
|
|
9
|
+
/** Minimum accepted payment: $0.001 (sections F, K). */
|
|
10
|
+
export declare const MIN_PAYMENT = 1000n;
|
|
11
|
+
/** Referral program (section C). */
|
|
12
|
+
export declare const REFERRAL: {
|
|
13
|
+
readonly referredDiscountPercent: 25n;
|
|
14
|
+
readonly referredDiscountDays: 90;
|
|
15
|
+
readonly creditPercent: 10n;
|
|
16
|
+
readonly creditMonths: 12;
|
|
17
|
+
readonly maxCombinedDiscountPercent: 50n;
|
|
18
|
+
readonly activationSettlements: 20;
|
|
19
|
+
readonly activationVolume: 10000000n;
|
|
20
|
+
};
|
|
21
|
+
/** Quote validity window in seconds (sections F, K). */
|
|
22
|
+
export declare const QUOTE_TTL_SECONDS = 30;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Values taken literally from the specification. Do not adjust them.
|
|
2
|
+
/** USDC base units per dollar: fees are computed in units of 0.000001 USD (section F). */
|
|
3
|
+
export const USD_UNITS = 1000000n;
|
|
4
|
+
/** Agent-to-agent tariff: min(max(0.25% × amount, $0.001), $0.50) (section F). */
|
|
5
|
+
export const AGENT_FEE = {
|
|
6
|
+
rateBps: 25n,
|
|
7
|
+
min: 1000n,
|
|
8
|
+
max: 500000n,
|
|
9
|
+
};
|
|
10
|
+
/** Minimum accepted payment: $0.001 (sections F, K). */
|
|
11
|
+
export const MIN_PAYMENT = 1000n;
|
|
12
|
+
/** Referral program (section C). */
|
|
13
|
+
export const REFERRAL = {
|
|
14
|
+
referredDiscountPercent: 25n,
|
|
15
|
+
referredDiscountDays: 90,
|
|
16
|
+
creditPercent: 10n,
|
|
17
|
+
creditMonths: 12,
|
|
18
|
+
maxCombinedDiscountPercent: 50n,
|
|
19
|
+
activationSettlements: 20,
|
|
20
|
+
activationVolume: 10000000n,
|
|
21
|
+
};
|
|
22
|
+
/** Quote validity window in seconds (sections F, K). */
|
|
23
|
+
export const QUOTE_TTL_SECONDS = 30;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const ERRORS: {
|
|
2
|
+
readonly INVALID_REQUEST: "The request is malformed or a field is invalid.";
|
|
3
|
+
readonly PAYMENT_BELOW_MINIMUM: "Payment amount is below the $0.001 minimum.";
|
|
4
|
+
readonly RECEIVER_TOKEN_ACCOUNT_MISSING: "The payee token account or the treasury USDC account does not exist. Accounts are never created inside a payment.";
|
|
5
|
+
readonly SIMULATION_FAILED: "Transaction simulation failed.";
|
|
6
|
+
readonly QUOTE_EXPIRED: "The quote has expired or was already used. Request a new quote.";
|
|
7
|
+
readonly QUOTE_INVALID: "The quote signature does not match the m2m.monster service key.";
|
|
8
|
+
readonly RATE_LIMITED: "Too many requests. Retry later.";
|
|
9
|
+
readonly MINT_NOT_ALLOWED: "The token mint is not in the whitelist for this network.";
|
|
10
|
+
readonly FEE_BALANCE_INSUFFICIENT_USDC: "The payer does not hold enough USDC to pay the fee. The fee is always paid in USDC.";
|
|
11
|
+
readonly PAYMENT_TRANSACTION_INVALID: "The transaction is not the m2m-split transaction described by the quote.";
|
|
12
|
+
readonly TRANSACTION_FAILED: "The transaction failed on-chain or was not confirmed in time.";
|
|
13
|
+
readonly RECEIPT_INVALID: "The receipt is not signed by the m2m.monster service key or does not match the transaction on-chain.";
|
|
14
|
+
readonly NOT_FOUND: "Not found.";
|
|
15
|
+
readonly UNAUTHORIZED: "Missing or invalid api_key.";
|
|
16
|
+
readonly CHALLENGE_INVALID: "The registration challenge is missing, expired, already used, or not signed by this public key.";
|
|
17
|
+
readonly IDEMPOTENCY_KEY_REUSED: "The idempotency key was already used for a different request.";
|
|
18
|
+
readonly INTERNAL_ERROR: "Internal error. Retry later.";
|
|
19
|
+
readonly PAYMENT_STATUS_UNKNOWN: "The status of a signed payment could not be determined. Do not pay again; retry later.";
|
|
20
|
+
};
|
|
21
|
+
export type ErrorCode = keyof typeof ERRORS;
|
|
22
|
+
export declare class M2mError extends Error {
|
|
23
|
+
readonly code: ErrorCode;
|
|
24
|
+
constructor(code: ErrorCode, message?: string);
|
|
25
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Stable error codes (section K). Never rename an existing code.
|
|
2
|
+
export const ERRORS = {
|
|
3
|
+
INVALID_REQUEST: "The request is malformed or a field is invalid.",
|
|
4
|
+
PAYMENT_BELOW_MINIMUM: "Payment amount is below the $0.001 minimum.",
|
|
5
|
+
RECEIVER_TOKEN_ACCOUNT_MISSING: "The payee token account or the treasury USDC account does not exist. Accounts are never created inside a payment.",
|
|
6
|
+
SIMULATION_FAILED: "Transaction simulation failed.",
|
|
7
|
+
QUOTE_EXPIRED: "The quote has expired or was already used. Request a new quote.",
|
|
8
|
+
QUOTE_INVALID: "The quote signature does not match the m2m.monster service key.",
|
|
9
|
+
RATE_LIMITED: "Too many requests. Retry later.",
|
|
10
|
+
MINT_NOT_ALLOWED: "The token mint is not in the whitelist for this network.",
|
|
11
|
+
FEE_BALANCE_INSUFFICIENT_USDC: "The payer does not hold enough USDC to pay the fee. The fee is always paid in USDC.",
|
|
12
|
+
PAYMENT_TRANSACTION_INVALID: "The transaction is not the m2m-split transaction described by the quote.",
|
|
13
|
+
TRANSACTION_FAILED: "The transaction failed on-chain or was not confirmed in time.",
|
|
14
|
+
RECEIPT_INVALID: "The receipt is not signed by the m2m.monster service key or does not match the transaction on-chain.",
|
|
15
|
+
NOT_FOUND: "Not found.",
|
|
16
|
+
UNAUTHORIZED: "Missing or invalid api_key.",
|
|
17
|
+
CHALLENGE_INVALID: "The registration challenge is missing, expired, already used, or not signed by this public key.",
|
|
18
|
+
IDEMPOTENCY_KEY_REUSED: "The idempotency key was already used for a different request.",
|
|
19
|
+
INTERNAL_ERROR: "Internal error. Retry later.",
|
|
20
|
+
PAYMENT_STATUS_UNKNOWN: "The status of a signed payment could not be determined. Do not pay again; retry later.",
|
|
21
|
+
};
|
|
22
|
+
export class M2mError extends Error {
|
|
23
|
+
code;
|
|
24
|
+
constructor(code, message = ERRORS[code]) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "M2mError";
|
|
27
|
+
this.code = code;
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/fees.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-to-agent fee in USDC base units for `amount` base units of a token
|
|
3
|
+
* worth 1 USD with `decimals` decimals (sections E, F). Rounded up.
|
|
4
|
+
*/
|
|
5
|
+
export declare function agentFee(amount: bigint, decimals: number): bigint;
|
|
6
|
+
export interface FeeBreakdown {
|
|
7
|
+
feeGross: bigint;
|
|
8
|
+
discount: bigint;
|
|
9
|
+
creditsUsed: bigint;
|
|
10
|
+
feeNet: bigint;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Referred discount first, then credits, never above 50% of the gross fee.
|
|
14
|
+
* Discounts round down (section C, decisions D3 and D6).
|
|
15
|
+
*/
|
|
16
|
+
export declare function applyDiscounts(input: {
|
|
17
|
+
feeGross: bigint;
|
|
18
|
+
referred: boolean;
|
|
19
|
+
credits: bigint;
|
|
20
|
+
}): FeeBreakdown;
|
|
21
|
+
/** Credit earned by the referrer from a fee paid by its referred agent (decision D4). */
|
|
22
|
+
export declare function referralCredit(feeNet: bigint): bigint;
|
|
23
|
+
/** A fee in USDC base units expressed in base units of a 1-USD token, rounded up. */
|
|
24
|
+
export declare function feeInTokenUnits(feeNet: bigint, decimals: number): bigint;
|
package/dist/fees.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { AGENT_FEE, MIN_PAYMENT, REFERRAL, USD_UNITS } from "./constants.js";
|
|
2
|
+
import { M2mError } from "./errors.js";
|
|
3
|
+
const ceilDiv = (a, b) => (a + b - 1n) / b;
|
|
4
|
+
const pow10 = (decimals) => 10n ** BigInt(decimals);
|
|
5
|
+
/**
|
|
6
|
+
* Agent-to-agent fee in USDC base units for `amount` base units of a token
|
|
7
|
+
* worth 1 USD with `decimals` decimals (sections E, F). Rounded up.
|
|
8
|
+
*/
|
|
9
|
+
export function agentFee(amount, decimals) {
|
|
10
|
+
const scale = pow10(decimals);
|
|
11
|
+
if (amount * USD_UNITS < MIN_PAYMENT * scale)
|
|
12
|
+
throw new M2mError("PAYMENT_BELOW_MINIMUM");
|
|
13
|
+
const fee = ceilDiv(amount * USD_UNITS * AGENT_FEE.rateBps, 10000n * scale);
|
|
14
|
+
if (fee < AGENT_FEE.min)
|
|
15
|
+
return AGENT_FEE.min;
|
|
16
|
+
if (fee > AGENT_FEE.max)
|
|
17
|
+
return AGENT_FEE.max;
|
|
18
|
+
return fee;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Referred discount first, then credits, never above 50% of the gross fee.
|
|
22
|
+
* Discounts round down (section C, decisions D3 and D6).
|
|
23
|
+
*/
|
|
24
|
+
export function applyDiscounts(input) {
|
|
25
|
+
const { feeGross, referred, credits } = input;
|
|
26
|
+
const cap = (feeGross * REFERRAL.maxCombinedDiscountPercent) / 100n;
|
|
27
|
+
const referredDiscount = referred ? (feeGross * REFERRAL.referredDiscountPercent) / 100n : 0n;
|
|
28
|
+
const room = cap - referredDiscount;
|
|
29
|
+
const creditsUsed = credits < room ? credits : room;
|
|
30
|
+
const discount = referredDiscount + creditsUsed;
|
|
31
|
+
return { feeGross, discount, creditsUsed, feeNet: feeGross - discount };
|
|
32
|
+
}
|
|
33
|
+
/** Credit earned by the referrer from a fee paid by its referred agent (decision D4). */
|
|
34
|
+
export function referralCredit(feeNet) {
|
|
35
|
+
return (feeNet * REFERRAL.creditPercent) / 100n;
|
|
36
|
+
}
|
|
37
|
+
/** A fee in USDC base units expressed in base units of a 1-USD token, rounded up. */
|
|
38
|
+
export function feeInTokenUnits(feeNet, decimals) {
|
|
39
|
+
return ceilDiv(feeNet * pow10(decimals), USD_UNITS);
|
|
40
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./constants.js";
|
|
2
|
+
export * from "./errors.js";
|
|
3
|
+
export * from "./fees.js";
|
|
4
|
+
export * from "./networks.js";
|
|
5
|
+
export * from "./payment.js";
|
|
6
|
+
export * from "./public.js";
|
|
7
|
+
export * from "./quote.js";
|
|
8
|
+
export * from "./receipt.js";
|
|
9
|
+
export * from "./referrals.js";
|
|
10
|
+
export * from "./refund.js";
|
|
11
|
+
export * from "./rpc.js";
|
|
12
|
+
export * from "./split.js";
|
|
13
|
+
export * from "./x402.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./constants.js";
|
|
2
|
+
export * from "./errors.js";
|
|
3
|
+
export * from "./fees.js";
|
|
4
|
+
export * from "./networks.js";
|
|
5
|
+
export * from "./payment.js";
|
|
6
|
+
export * from "./public.js";
|
|
7
|
+
export * from "./quote.js";
|
|
8
|
+
export * from "./receipt.js";
|
|
9
|
+
export * from "./referrals.js";
|
|
10
|
+
export * from "./refund.js";
|
|
11
|
+
export * from "./rpc.js";
|
|
12
|
+
export * from "./split.js";
|
|
13
|
+
export * from "./x402.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
2
|
+
export declare const DEVNET = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
|
|
3
|
+
export type Network = typeof MAINNET | typeof DEVNET;
|
|
4
|
+
export interface AllowedMint {
|
|
5
|
+
symbol: "USDC" | "USDT";
|
|
6
|
+
mint: string;
|
|
7
|
+
}
|
|
8
|
+
export interface NetworkConfig {
|
|
9
|
+
name: string;
|
|
10
|
+
/** USDC mint: the only token the fee is paid in (rule 5). */
|
|
11
|
+
usdc: string;
|
|
12
|
+
/**
|
|
13
|
+
* Treasury wallet: single-key, multisig planned as the project grows (rule 9). Only its public
|
|
14
|
+
* address is known here; no server holds its key. The API refuses to start without a valid one.
|
|
15
|
+
*/
|
|
16
|
+
treasury?: string;
|
|
17
|
+
mints: readonly AllowedMint[];
|
|
18
|
+
}
|
|
19
|
+
export declare const NETWORKS: Record<Network, NetworkConfig>;
|
|
20
|
+
export declare function isNetwork(value: string): value is Network;
|
|
21
|
+
export declare function allowedMint(network: string, mint: string): AllowedMint | undefined;
|
package/dist/networks.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Mint whitelists by exact address (section E). Mainnet and Devnet are never mixed.
|
|
2
|
+
// Decimals are read on-chain, never stored here.
|
|
3
|
+
export const MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
4
|
+
export const DEVNET = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
|
|
5
|
+
export const NETWORKS = {
|
|
6
|
+
[MAINNET]: {
|
|
7
|
+
name: "solana-mainnet",
|
|
8
|
+
// Provided by the user on 2026-10-15 (D58); its USDC account (classic SPL Token ATA) is
|
|
9
|
+
// EWQ5w4DPmkinV4iHn9H7pcwx5Q8fYQCnY46CzGD1ErTk.
|
|
10
|
+
treasury: "D68nkVrQD3pcs6qNcadXqWvoKqsNCKpLemy3zYVmGHym",
|
|
11
|
+
usdc: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
|
|
12
|
+
mints: [
|
|
13
|
+
{ symbol: "USDC", mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" },
|
|
14
|
+
{ symbol: "USDT", mint: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" },
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
// Decision D9: Devnet whitelist holds only Circle's Devnet USDC.
|
|
18
|
+
[DEVNET]: {
|
|
19
|
+
name: "solana-devnet",
|
|
20
|
+
treasury: "DeQ2ZniNv2BiK746guCUz8tcxJ1HvmLW1e7EjjTiMFMj",
|
|
21
|
+
usdc: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
|
|
22
|
+
mints: [{ symbol: "USDC", mint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU" }],
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
export function isNetwork(value) {
|
|
26
|
+
return Object.hasOwn(NETWORKS, value);
|
|
27
|
+
}
|
|
28
|
+
export function allowedMint(network, mint) {
|
|
29
|
+
if (!isNetwork(network))
|
|
30
|
+
return undefined;
|
|
31
|
+
return NETWORKS[network].mints.find((m) => m.mint === mint);
|
|
32
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Address, Blockhash } from "@solana/kit";
|
|
2
|
+
import { type SignedQuote } from "./quote.js";
|
|
3
|
+
import { type SolanaRpc } from "./rpc.js";
|
|
4
|
+
import { type VerifiedSplit } from "./split.js";
|
|
5
|
+
export interface PreparedTransaction {
|
|
6
|
+
/** Base64 wire transaction, unsigned. */
|
|
7
|
+
transaction: string;
|
|
8
|
+
computeUnitLimit: number;
|
|
9
|
+
computeUnitPrice: bigint;
|
|
10
|
+
lastValidBlockHeight: bigint;
|
|
11
|
+
}
|
|
12
|
+
export interface BuildParams {
|
|
13
|
+
computeUnitLimit: number;
|
|
14
|
+
computeUnitPrice: bigint;
|
|
15
|
+
lifetime: {
|
|
16
|
+
blockhash: Blockhash;
|
|
17
|
+
lastValidBlockHeight: bigint;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds with the maximum compute unit limit, simulates, sets the limit to 1.2 × units
|
|
22
|
+
* consumed and simulates the final transaction (section K; D16, D30).
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildSimulated(rpc: SolanaRpc, writableAccounts: Address[], maxComputeUnitPrice: bigint, build: (params: BuildParams) => Promise<string>): Promise<PreparedTransaction>;
|
|
25
|
+
/**
|
|
26
|
+
* Payer side before signing: checks the quote, that the payee and treasury token accounts
|
|
27
|
+
* exist and the USDC balance for the fee, then builds and simulates the unsigned m2m-split.
|
|
28
|
+
*/
|
|
29
|
+
export declare function preparePayment(options: {
|
|
30
|
+
rpc: SolanaRpc;
|
|
31
|
+
payer: Address;
|
|
32
|
+
quote: SignedQuote;
|
|
33
|
+
serviceAddress: string;
|
|
34
|
+
maxComputeUnitPrice?: bigint;
|
|
35
|
+
nowSeconds: number;
|
|
36
|
+
}): Promise<PreparedTransaction>;
|
|
37
|
+
/**
|
|
38
|
+
* Payee side before submitting: quote checks, strict layout with the payer signature and a
|
|
39
|
+
* simulation with signature checks (section G, step 3).
|
|
40
|
+
*/
|
|
41
|
+
export declare function verifySignedPayment(options: {
|
|
42
|
+
rpc: SolanaRpc;
|
|
43
|
+
quote: SignedQuote;
|
|
44
|
+
transaction: string;
|
|
45
|
+
serviceAddress: string;
|
|
46
|
+
nowSeconds: number;
|
|
47
|
+
}): Promise<VerifiedSplit>;
|
package/dist/payment.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { M2mError } from "./errors.js";
|
|
2
|
+
import { assertQuoteValid, splitExpectation } from "./quote.js";
|
|
3
|
+
import { associatedTokenAccount, estimateComputeUnitPrice, simulate, tokenBalance, } from "./rpc.js";
|
|
4
|
+
import { buildSplitTransaction, MAX_COMPUTE_UNIT_PRICE, verifySplitTransaction, } from "./split.js";
|
|
5
|
+
/** Simulation may use at most this many compute units. */
|
|
6
|
+
const PROVISIONAL_COMPUTE_UNIT_LIMIT = 1_400_000;
|
|
7
|
+
/**
|
|
8
|
+
* Builds with the maximum compute unit limit, simulates, sets the limit to 1.2 × units
|
|
9
|
+
* consumed and simulates the final transaction (section K; D16, D30).
|
|
10
|
+
*/
|
|
11
|
+
export async function buildSimulated(rpc, writableAccounts, maxComputeUnitPrice, build) {
|
|
12
|
+
const computeUnitPrice = await estimateComputeUnitPrice(rpc, writableAccounts, maxComputeUnitPrice);
|
|
13
|
+
const { value: lifetime } = await rpc.getLatestBlockhash({ commitment: "confirmed" }).send();
|
|
14
|
+
const at = (computeUnitLimit) => build({ computeUnitLimit, computeUnitPrice, lifetime });
|
|
15
|
+
const consumed = await simulate(rpc, await at(PROVISIONAL_COMPUTE_UNIT_LIMIT), {
|
|
16
|
+
sigVerify: false,
|
|
17
|
+
});
|
|
18
|
+
const computeUnitLimit = Math.ceil(Number(consumed) * 1.2);
|
|
19
|
+
const transaction = await at(computeUnitLimit);
|
|
20
|
+
await simulate(rpc, transaction, { sigVerify: false });
|
|
21
|
+
return {
|
|
22
|
+
transaction,
|
|
23
|
+
computeUnitLimit,
|
|
24
|
+
computeUnitPrice,
|
|
25
|
+
lastValidBlockHeight: lifetime.lastValidBlockHeight,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Payer side before signing: checks the quote, that the payee and treasury token accounts
|
|
30
|
+
* exist and the USDC balance for the fee, then builds and simulates the unsigned m2m-split.
|
|
31
|
+
*/
|
|
32
|
+
export async function preparePayment(options) {
|
|
33
|
+
const { rpc, payer, quote } = options;
|
|
34
|
+
await assertQuoteValid(quote, options.serviceAddress, options.nowSeconds);
|
|
35
|
+
const expected = splitExpectation(quote);
|
|
36
|
+
const usdc = quote.usdc_mint;
|
|
37
|
+
// Never create accounts inside a payment (section K).
|
|
38
|
+
const [payeeBalance, treasuryBalance, payerUsdc] = await Promise.all([
|
|
39
|
+
tokenBalance(rpc, expected.payee, expected.paymentMint),
|
|
40
|
+
tokenBalance(rpc, expected.treasury, usdc),
|
|
41
|
+
tokenBalance(rpc, payer, usdc),
|
|
42
|
+
]);
|
|
43
|
+
if (payeeBalance === undefined || treasuryBalance === undefined) {
|
|
44
|
+
throw new M2mError("RECEIVER_TOKEN_ACCOUNT_MISSING");
|
|
45
|
+
}
|
|
46
|
+
// The fee is always paid in USDC; never convert another token to cover it (rule 6).
|
|
47
|
+
if ((payerUsdc ?? 0n) < expected.feeNet)
|
|
48
|
+
throw new M2mError("FEE_BALANCE_INSUFFICIENT_USDC");
|
|
49
|
+
const writable = await Promise.all([
|
|
50
|
+
associatedTokenAccount(payer, expected.paymentMint),
|
|
51
|
+
associatedTokenAccount(expected.payee, expected.paymentMint),
|
|
52
|
+
associatedTokenAccount(payer, usdc),
|
|
53
|
+
associatedTokenAccount(expected.treasury, usdc),
|
|
54
|
+
]);
|
|
55
|
+
return buildSimulated(rpc, writable, options.maxComputeUnitPrice ?? MAX_COMPUTE_UNIT_PRICE, (params) => buildSplitTransaction({ ...expected, payer, ...params }));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Payee side before submitting: quote checks, strict layout with the payer signature and a
|
|
59
|
+
* simulation with signature checks (section G, step 3).
|
|
60
|
+
*/
|
|
61
|
+
export async function verifySignedPayment(options) {
|
|
62
|
+
await assertQuoteValid(options.quote, options.serviceAddress, options.nowSeconds);
|
|
63
|
+
const verified = await verifySplitTransaction(options.transaction, splitExpectation(options.quote), { signed: true });
|
|
64
|
+
await simulate(options.rpc, options.transaction, { sigVerify: true });
|
|
65
|
+
return verified;
|
|
66
|
+
}
|
package/dist/public.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Network } from "./networks.js";
|
|
2
|
+
/** USDC base units as a decimal dollar string without trailing zeros. */
|
|
3
|
+
export declare function formatUsd(units: bigint): string;
|
|
4
|
+
export declare function pricing(network: Network): {
|
|
5
|
+
version: number;
|
|
6
|
+
network: {
|
|
7
|
+
id: Network;
|
|
8
|
+
name: string;
|
|
9
|
+
tokens: readonly import("./networks.js").AllowedMint[];
|
|
10
|
+
};
|
|
11
|
+
quote_validity_seconds: number;
|
|
12
|
+
agent_to_agent: {
|
|
13
|
+
formula: string;
|
|
14
|
+
rate_percent: string;
|
|
15
|
+
min_fee_usd: string;
|
|
16
|
+
max_fee_usd: string;
|
|
17
|
+
min_payment_usd: string;
|
|
18
|
+
rounding: string;
|
|
19
|
+
borne_by: string;
|
|
20
|
+
fee_token: {
|
|
21
|
+
symbol: string;
|
|
22
|
+
mint: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
referral_program: {
|
|
26
|
+
referred_discount_percent: number;
|
|
27
|
+
referred_discount_days: 90;
|
|
28
|
+
referrer_credit_percent: number;
|
|
29
|
+
referrer_credit_months: 12;
|
|
30
|
+
credit_expiry_months: 12;
|
|
31
|
+
max_combined_discount_percent: number;
|
|
32
|
+
activation: {
|
|
33
|
+
settlements_as_payee: 20;
|
|
34
|
+
volume_usd: string;
|
|
35
|
+
};
|
|
36
|
+
credits: string;
|
|
37
|
+
};
|
|
38
|
+
};
|