mppx 0.4.11 → 0.4.12

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/internal/env.d.ts +1 -1
  3. package/dist/internal/env.d.ts.map +1 -1
  4. package/dist/internal/env.js +2 -6
  5. package/dist/internal/env.js.map +1 -1
  6. package/dist/internal/types.d.ts +23 -0
  7. package/dist/internal/types.d.ts.map +1 -1
  8. package/dist/server/Mppx.d.ts +1 -1
  9. package/dist/server/Mppx.d.ts.map +1 -1
  10. package/dist/server/Mppx.js +49 -2
  11. package/dist/server/Mppx.js.map +1 -1
  12. package/dist/tempo/Methods.d.ts +15 -0
  13. package/dist/tempo/Methods.d.ts.map +1 -1
  14. package/dist/tempo/Methods.js +27 -3
  15. package/dist/tempo/Methods.js.map +1 -1
  16. package/dist/tempo/client/Charge.d.ts +21 -0
  17. package/dist/tempo/client/Charge.d.ts.map +1 -1
  18. package/dist/tempo/client/Charge.js +33 -7
  19. package/dist/tempo/client/Charge.js.map +1 -1
  20. package/dist/tempo/client/Methods.d.ts +15 -0
  21. package/dist/tempo/client/Methods.d.ts.map +1 -1
  22. package/dist/tempo/internal/account.d.ts +5 -11
  23. package/dist/tempo/internal/account.d.ts.map +1 -1
  24. package/dist/tempo/internal/charge.d.ts +20 -0
  25. package/dist/tempo/internal/charge.d.ts.map +1 -0
  26. package/dist/tempo/internal/charge.js +23 -0
  27. package/dist/tempo/internal/charge.js.map +1 -0
  28. package/dist/tempo/internal/fee-payer.d.ts.map +1 -1
  29. package/dist/tempo/internal/fee-payer.js +15 -3
  30. package/dist/tempo/internal/fee-payer.js.map +1 -1
  31. package/dist/tempo/server/Charge.d.ts +17 -2
  32. package/dist/tempo/server/Charge.d.ts.map +1 -1
  33. package/dist/tempo/server/Charge.js +148 -99
  34. package/dist/tempo/server/Charge.js.map +1 -1
  35. package/dist/tempo/server/Methods.d.ts +17 -2
  36. package/dist/tempo/server/Methods.d.ts.map +1 -1
  37. package/dist/tempo/server/Methods.js +4 -1
  38. package/dist/tempo/server/Methods.js.map +1 -1
  39. package/dist/tempo/server/Session.d.ts +9 -4
  40. package/dist/tempo/server/Session.d.ts.map +1 -1
  41. package/dist/tempo/server/Session.js +18 -3
  42. package/dist/tempo/server/Session.js.map +1 -1
  43. package/dist/tempo/session/Chain.d.ts +18 -2
  44. package/dist/tempo/session/Chain.d.ts.map +1 -1
  45. package/dist/tempo/session/Chain.js +18 -14
  46. package/dist/tempo/session/Chain.js.map +1 -1
  47. package/package.json +1 -1
  48. package/src/internal/env.test.ts +12 -12
  49. package/src/internal/env.ts +2 -6
  50. package/src/internal/types.ts +25 -0
  51. package/src/server/Mppx.test.ts +287 -0
  52. package/src/server/Mppx.ts +59 -5
  53. package/src/tempo/Methods.test.ts +79 -0
  54. package/src/tempo/Methods.ts +53 -17
  55. package/src/tempo/client/Charge.ts +41 -8
  56. package/src/tempo/internal/account.ts +7 -14
  57. package/src/tempo/internal/charge.ts +43 -0
  58. package/src/tempo/internal/fee-payer.test.ts +33 -14
  59. package/src/tempo/internal/fee-payer.ts +21 -6
  60. package/src/tempo/server/Charge.test.ts +231 -0
  61. package/src/tempo/server/Charge.ts +193 -124
  62. package/src/tempo/server/Methods.ts +4 -1
  63. package/src/tempo/server/Session.test.ts +28 -0
  64. package/src/tempo/server/Session.ts +26 -17
  65. package/src/tempo/session/Chain.test.ts +25 -5
  66. package/src/tempo/session/Chain.ts +30 -14
@@ -93,6 +93,11 @@ function assertUint128(amount: bigint): void {
93
93
  }
94
94
  }
95
95
 
96
+ /** Options for {@link settleOnChain}. */
97
+ export type SettleOptions =
98
+ | { feePayer: Account; account: Account }
99
+ | { feePayer?: undefined; account?: Account | undefined }
100
+
96
101
  /**
97
102
  * Submit a settle transaction on-chain.
98
103
  */
@@ -100,16 +105,21 @@ export async function settleOnChain(
100
105
  client: Client,
101
106
  escrowContract: Address,
102
107
  voucher: SignedVoucher,
103
- feePayer?: Account | undefined,
108
+ options?: SettleOptions,
104
109
  ): Promise<Hex> {
105
110
  assertUint128(voucher.cumulativeAmount)
111
+ const resolved = options?.account ?? client.account
112
+ if (!resolved)
113
+ throw new Error(
114
+ 'Cannot settle channel: no account available. Pass an `account` to tempo.settle(), or provide a `getClient` that returns an account-bearing client.',
115
+ )
106
116
  const args = [voucher.channelId, voucher.cumulativeAmount, voucher.signature] as const
107
- if (feePayer) {
117
+ if (options?.feePayer) {
108
118
  const data = encodeFunctionData({ abi: escrowAbi, functionName: 'settle', args })
109
- return sendFeePayerTx(client, feePayer, escrowContract, data, 'settle')
119
+ return sendFeePayerTx(client, resolved, options.feePayer, escrowContract, data, 'settle')
110
120
  }
111
121
  return writeContract(client, {
112
- account: client.account!,
122
+ account: resolved,
113
123
  chain: client.chain,
114
124
  address: escrowContract,
115
125
  abi: escrowAbi,
@@ -118,6 +128,11 @@ export async function settleOnChain(
118
128
  })
119
129
  }
120
130
 
131
+ /** Options for {@link closeOnChain}. */
132
+ export type CloseOptions =
133
+ | { feePayer: Account; account: Account }
134
+ | { feePayer?: undefined; account?: Account | undefined }
135
+
121
136
  /**
122
137
  * Submit a close transaction on-chain.
123
138
  */
@@ -125,19 +140,18 @@ export async function closeOnChain(
125
140
  client: Client,
126
141
  escrowContract: Address,
127
142
  voucher: SignedVoucher,
128
- account?: Account,
129
- feePayer?: Account | undefined,
143
+ options?: CloseOptions,
130
144
  ): Promise<Hex> {
131
145
  assertUint128(voucher.cumulativeAmount)
132
- const resolved = account ?? client.account
146
+ const resolved = options?.account ?? client.account
133
147
  if (!resolved)
134
148
  throw new Error(
135
149
  'Cannot close channel: no account available. Pass an `account` (viem Account, e.g. privateKeyToAccount("0x...")) to tempo.session(), or provide a `getClient` that returns an account-bearing client.',
136
150
  )
137
151
  const args = [voucher.channelId, voucher.cumulativeAmount, voucher.signature] as const
138
- if (feePayer) {
152
+ if (options?.feePayer) {
139
153
  const data = encodeFunctionData({ abi: escrowAbi, functionName: 'close', args })
140
- return sendFeePayerTx(client, feePayer, escrowContract, data, 'close')
154
+ return sendFeePayerTx(client, resolved, options.feePayer, escrowContract, data, 'close')
141
155
  }
142
156
  return writeContract(client, {
143
157
  account: resolved,
@@ -155,9 +169,13 @@ export async function closeOnChain(
155
169
  * Follows the same signTransaction + sendRawTransactionSync pattern used
156
170
  * by broadcastOpenTransaction / broadcastTopUpTransaction, but originates
157
171
  * the transaction server-side (estimating gas and fees first).
172
+ *
173
+ * @param account - The logical sender / msg.sender (e.g. the payee).
174
+ * @param feePayer - The gas sponsor — only co-signs to cover fees.
158
175
  */
159
176
  async function sendFeePayerTx(
160
177
  client: Client,
178
+ account: Account,
161
179
  feePayer: Account,
162
180
  to: Address,
163
181
  data: Hex,
@@ -167,12 +185,10 @@ async function sendFeePayerTx(
167
185
  // token. `feePayer: true` tells the prepare hook to use expiring nonces but
168
186
  // does NOT set feeToken automatically, so we must provide it explicitly.
169
187
  const chainId = client.chain?.id
170
- const feeToken = chainId
171
- ? defaults.currency[chainId as keyof typeof defaults.currency]
172
- : undefined
188
+ const feeToken = chainId ? defaults.resolveCurrency({ chainId }) : undefined
173
189
 
174
190
  const prepared = await prepareTransactionRequest(client, {
175
- account: feePayer,
191
+ account,
176
192
  calls: [{ to, data }],
177
193
  feePayer: true,
178
194
  ...(feeToken ? { feeToken } : {}),
@@ -180,7 +196,7 @@ async function sendFeePayerTx(
180
196
 
181
197
  const serialized = (await signTransaction(client, {
182
198
  ...prepared,
183
- account: feePayer,
199
+ account,
184
200
  feePayer,
185
201
  } as never)) as Hex
186
202