@t2000/mpp-sui 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# @t2000/mpp-sui
|
|
2
|
+
|
|
3
|
+
Sui USDC payment method for the [Machine Payments Protocol (MPP)](https://mpp.dev). Accept and make payments on any API — the first MPP implementation on Sui.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@t2000/mpp-sui)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
**[Website](https://t2000.ai/mpp)** · **[GitHub](https://github.com/mission69b/t2000)** · **[SDK](https://www.npmjs.com/package/@t2000/sdk)** · **[CLI](https://www.npmjs.com/package/@t2000/cli)**
|
|
9
|
+
|
|
10
|
+
## What is MPP?
|
|
11
|
+
|
|
12
|
+
The [Machine Payments Protocol](https://mpp.dev) is an open standard by Stripe and Tempo Labs for agent-to-service payments. When a server returns HTTP `402 Payment Required`, the client pays automatically and retries — no API keys, no subscriptions, no human approval.
|
|
13
|
+
|
|
14
|
+
`@t2000/mpp-sui` adds **Sui USDC** as a payment method. It works with any MPP-compatible client or server via the `mppx` SDK.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @t2000/mpp-sui mppx
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Accept Payments (Server)
|
|
23
|
+
|
|
24
|
+
Add payments to any API in 5 lines:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { sui } from '@t2000/mpp-sui/server';
|
|
28
|
+
import { Mppx } from 'mppx';
|
|
29
|
+
|
|
30
|
+
const SUI_USDC = '0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC';
|
|
31
|
+
|
|
32
|
+
const mppx = Mppx.create({
|
|
33
|
+
methods: [sui({ currency: SUI_USDC, recipient: '0xYOUR_ADDRESS' })],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const GET = mppx.charge({ amount: '0.01' })(
|
|
37
|
+
() => Response.json({ data: 'paid content' })
|
|
38
|
+
);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
No webhooks. No Stripe dashboard. No KYC. USDC arrives directly in your wallet.
|
|
42
|
+
|
|
43
|
+
## Make Payments (Client)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { sui } from '@t2000/mpp-sui/client';
|
|
47
|
+
import { Mppx } from 'mppx/client';
|
|
48
|
+
import { SuiJsonRpcClient, getJsonRpcFullnodeUrl } from '@mysten/sui/jsonRpc';
|
|
49
|
+
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
50
|
+
|
|
51
|
+
const client = new SuiJsonRpcClient({
|
|
52
|
+
url: getJsonRpcFullnodeUrl('mainnet'),
|
|
53
|
+
network: 'mainnet',
|
|
54
|
+
});
|
|
55
|
+
const signer = Ed25519Keypair.deriveKeypair('your mnemonic');
|
|
56
|
+
|
|
57
|
+
const mppx = Mppx.create({
|
|
58
|
+
methods: [sui({ client, signer })],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const response = await mppx.fetch('https://api.example.com/resource');
|
|
62
|
+
// If the API returns 402, mppx pays automatically via Sui USDC.
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## With t2000 SDK
|
|
66
|
+
|
|
67
|
+
If you're using the [t2000 SDK](https://www.npmjs.com/package/@t2000/sdk), payments are even simpler:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { T2000 } from '@t2000/sdk';
|
|
71
|
+
|
|
72
|
+
const agent = await T2000.create({ pin: 'my-secret' });
|
|
73
|
+
|
|
74
|
+
const result = await agent.pay({
|
|
75
|
+
url: 'https://api.example.com/generate',
|
|
76
|
+
body: { prompt: 'a sunset' },
|
|
77
|
+
maxPrice: 0.05,
|
|
78
|
+
});
|
|
79
|
+
// Handles 402 → pay → retry automatically.
|
|
80
|
+
// Safeguards enforced (max per tx, daily limits).
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### CLI
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
t2000 pay https://api.example.com/data --max-price 0.10
|
|
87
|
+
|
|
88
|
+
t2000 pay https://api.example.com/analyze \
|
|
89
|
+
--method POST \
|
|
90
|
+
--data '{"text":"hello"}' \
|
|
91
|
+
--max-price 0.05
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## How It Works
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
Agent API Server
|
|
98
|
+
│ │
|
|
99
|
+
│── GET /resource ────────>│
|
|
100
|
+
│<── 402 Payment Required ─│
|
|
101
|
+
│ {amount, currency, │
|
|
102
|
+
│ recipient} │
|
|
103
|
+
│ │
|
|
104
|
+
│── USDC transfer on Sui ──│ (~400ms finality)
|
|
105
|
+
│ │
|
|
106
|
+
│── GET /resource ────────>│
|
|
107
|
+
│ + payment credential │── verify TX on-chain via RPC
|
|
108
|
+
│ (Sui tx digest) │
|
|
109
|
+
│<── 200 OK + data ────────│
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
No facilitator. No intermediary. The server verifies the Sui transaction directly via RPC.
|
|
113
|
+
|
|
114
|
+
## Server API
|
|
115
|
+
|
|
116
|
+
### `sui(options)`
|
|
117
|
+
|
|
118
|
+
Creates a Sui USDC payment method for the server.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { sui } from '@t2000/mpp-sui/server';
|
|
122
|
+
|
|
123
|
+
const method = sui({
|
|
124
|
+
currency: SUI_USDC, // Sui coin type for USDC
|
|
125
|
+
recipient: '0xYOUR_ADDR', // Where payments are sent
|
|
126
|
+
rpcUrl: '...', // Optional: custom RPC endpoint
|
|
127
|
+
network: 'mainnet', // Optional: 'mainnet' | 'testnet' | 'devnet'
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Verification checks:
|
|
132
|
+
- Transaction succeeded on-chain
|
|
133
|
+
- Payment sent to correct recipient (address-normalized comparison)
|
|
134
|
+
- Amount >= requested (BigInt precision, no floating-point)
|
|
135
|
+
|
|
136
|
+
## Client API
|
|
137
|
+
|
|
138
|
+
### `sui(options)`
|
|
139
|
+
|
|
140
|
+
Creates a Sui USDC payment method for the client.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { sui } from '@t2000/mpp-sui/client';
|
|
144
|
+
|
|
145
|
+
const method = sui({
|
|
146
|
+
client: suiJsonRpcClient, // SuiJsonRpcClient instance
|
|
147
|
+
signer: ed25519Keypair, // Ed25519Keypair for signing
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The client:
|
|
152
|
+
- Fetches all USDC coins (handles Sui pagination, max 50 per page)
|
|
153
|
+
- Checks balance before building the transaction
|
|
154
|
+
- Merges fragmented coins into a single payment
|
|
155
|
+
- Signs and broadcasts the transaction
|
|
156
|
+
- Returns the digest as the payment credential
|
|
157
|
+
|
|
158
|
+
## Utilities
|
|
159
|
+
|
|
160
|
+
### `SUI_USDC_TYPE`
|
|
161
|
+
|
|
162
|
+
The Sui coin type for Circle-issued USDC on mainnet.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { SUI_USDC_TYPE } from '@t2000/mpp-sui';
|
|
166
|
+
// '0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC'
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### `fetchCoins(client, owner, coinType)`
|
|
170
|
+
|
|
171
|
+
Fetches all coins of a given type, handling Sui's pagination (max 50 per page).
|
|
172
|
+
|
|
173
|
+
### `parseAmountToRaw(amount, decimals)`
|
|
174
|
+
|
|
175
|
+
Converts a string amount to BigInt raw units without floating-point math.
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
parseAmountToRaw('0.01', 6); // 10000n
|
|
179
|
+
parseAmountToRaw('1.50', 6); // 1500000n
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Why Sui?
|
|
183
|
+
|
|
184
|
+
MPP is chain-agnostic. We chose Sui because agent payments need:
|
|
185
|
+
|
|
186
|
+
| | Sui |
|
|
187
|
+
|---|---|
|
|
188
|
+
| **Finality** | ~400ms |
|
|
189
|
+
| **Gas** | <$0.001 per payment |
|
|
190
|
+
| **USDC** | Circle-issued, native |
|
|
191
|
+
| **Verification** | Direct RPC — no facilitator |
|
|
192
|
+
|
|
193
|
+
## Testing
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
pnpm --filter @t2000/mpp-sui test # 16 tests
|
|
197
|
+
pnpm --filter @t2000/mpp-sui typecheck
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT — see [LICENSE](https://github.com/mission69b/t2000/blob/main/LICENSE)
|